Skip to content

Commit

Permalink
Embedded LND: delete + create multiple wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
kaloudis committed Jan 19, 2025
1 parent c162985 commit e91db23
Show file tree
Hide file tree
Showing 22 changed files with 344 additions and 230 deletions.
4 changes: 2 additions & 2 deletions android/app/src/main/java/com/zeus/LndMobile.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public void checkStatus(Promise promise) {
}

@ReactMethod
public void startLnd(String args, Boolean isTorEnabled, Boolean isTestnet, Promise promise) {
public void startLnd(String args, String lndDir, Boolean isTorEnabled, Boolean isTestnet, Promise promise) {
// TODO args is only used on iOS right now
int req = new Random().nextInt();
requests.put(req, promise);
Expand All @@ -346,7 +346,7 @@ public void startLnd(String args, Boolean isTorEnabled, Boolean isTestnet, Promi

Bundle bundle = new Bundle();

String params = "--lnddir=" + getReactApplicationContext().getFilesDir().getPath();
String params = "--lnddir=" + getReactApplicationContext().getFilesDir().getPath() + "/" + lndDir;
if (isTorEnabled) {
// int listenPort = ZeusTorUtils.getListenPort(isTestnet);
// String controlSocket = "unix://" + getReactApplicationContext().getDir(TorService.class.getSimpleName(), Context.MODE_PRIVATE).getAbsolutePath() + "/data/ControlSocket";
Expand Down
8 changes: 4 additions & 4 deletions android/app/src/main/java/com/zeus/LndMobileTools.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public String getName() {
}

@ReactMethod
void writeConfig(String config, Promise promise) {
String filename = getReactApplicationContext().getFilesDir().toString() + "/lnd.conf";
void writeConfig(String config, String lndDir, Promise promise) {
String filename = getReactApplicationContext().getFilesDir().toString() + "/" + lndDir + "/lnd.conf";

try {
new File(filename).getParentFile().mkdirs();
Expand Down Expand Up @@ -431,8 +431,8 @@ public void DEBUG_deleteSpeedloaderDgraphDirectory(Promise promise) {
}

@ReactMethod
public void deleteLndDirectory(Promise promise) {
String filename = getReactApplicationContext().getCacheDir().toString() + "/lnd";
public void deleteLndDirectory(String lndDir, Promise promise) {
String filename = getReactApplicationContext().getCacheDir().toString() + "/" + lndDir;
File file = new File(filename);
deleteRecursive(file);
promise.resolve(null);
Expand Down
4 changes: 2 additions & 2 deletions backends/CLNRest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ export default class CLNRest {
.join('&')}`;
}

const headers: any = this.getHeaders(rune);
const headers: any = this.getHeaders(rune || '');
headers['Content-Type'] = 'application/json';

const url = this.getURL(host, port, route);
const url = this.getURL(host || '', port || '', route);

return this.restReq(
headers,
Expand Down
2 changes: 1 addition & 1 deletion backends/Eclair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class Eclair {
return calls.get(id);
}

url = url.slice(-1) === '/' ? url : url + '/';
url = url?.slice(-1) === '/' ? url : url + '/';
const headers = {
Authorization: 'Basic ' + Base64Utils.utf8ToBase64(':' + password),
'Content-Type': 'application/x-www-form-urlencoded'
Expand Down
27 changes: 21 additions & 6 deletions backends/LND.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,12 @@ export default class LND {
const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
const methodRoute = `${route}?method=${method}`;
const url = this.getURL(host || lndhubUrl, port, methodRoute, true);
const url = this.getURL(
host || lndhubUrl || '',
port || '',
methodRoute,
true
);

return new Promise(function (resolve, reject) {
const ws: any = new WebSocket(url, null, {
Expand Down Expand Up @@ -216,7 +221,7 @@ export default class LND {
const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth);
headers['Content-Type'] = 'application/json';
const url = this.getURL(host || lndhubUrl, port, route);
const url = this.getURL(host || lndhubUrl || '', port || '', route);
return this.restReq(
headers,
url,
Expand Down Expand Up @@ -274,7 +279,12 @@ export default class LND {
const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
const methodRoute = `${route}?method=${method}`;
const url = this.getURL(host || lndhubUrl, port, methodRoute, true);
const url = this.getURL(
host || lndhubUrl || '',
port || '',
methodRoute,
true
);

const ws: any = new WebSocket(url, null, {
headers
Expand Down Expand Up @@ -420,7 +430,12 @@ export default class LND {
const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
const methodRoute = '/v1/channels/stream?method=POST';
const url = this.getURL(host || lndhubUrl, port, methodRoute, true);
const url = this.getURL(
host || lndhubUrl || '',
port || '',
methodRoute,
true
);

return new Promise(function (resolve, reject) {
const ws: any = new WebSocket(url, null, {
Expand Down Expand Up @@ -619,8 +634,8 @@ export default class LND {
const auth = macaroonHex || accessToken;
const headers: any = this.getHeaders(auth, true);
const url = this.getURL(
host || lndhubUrl,
port,
host || lndhubUrl || '',
port || '',
'/v1/channels/acceptor?method=POST',
true
);
Expand Down
2 changes: 1 addition & 1 deletion backends/Spark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class Spark {
return calls.get(id);
}

url = url.slice(-4) === '/rpc' ? url : url + '/rpc';
url = url?.slice(-4) === '/rpc' ? url : url + '/rpc';

const headers: any = { 'X-Access': accessKey };
if (range) {
Expand Down
4 changes: 2 additions & 2 deletions ios/LndMobile/Lnd.swift
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ open class Lnd {
return flags
}

func startLnd(_ args: String, isTorEnabled: Bool, isTestnet: Bool, lndStartedCallback: @escaping Callback) -> Void {
func startLnd(_ args: String, lndDir: String, isTorEnabled: Bool, isTestnet: Bool, lndStartedCallback: @escaping Callback) -> Void {
let applicationSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
let lndPath = applicationSupport.appendingPathComponent("lnd", isDirectory: true)
let lndPath = applicationSupport.appendingPathComponent(lndDir, isDirectory: true)

var lndArgs = "--nolisten --lnddir=\"\(lndPath.path)\" " + args
if (isTorEnabled) {
Expand Down
1 change: 1 addition & 0 deletions ios/LndMobile/LndMobile.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ @interface RCT_EXTERN_MODULE(LndMobile, RCTEventEmitter)

RCT_EXTERN_METHOD(
startLnd: (NSString *)args
lndDir: (NSString *)lndDir
isTorEnabled: (BOOL)isTorEnabled
isTestnet: (BOOL)isTestnet
resolver: (RCTPromiseResolveBlock)resolve
Expand Down
6 changes: 3 additions & 3 deletions ios/LndMobile/LndMobile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ class LndMobile: RCTEventEmitter {
resolve(Lnd.shared.checkStatus())
}

@objc(startLnd:isTorEnabled:isTestnet:resolver:rejecter:)
func startLnd(_ args: String, isTorEnabled: Bool, isTestnet: Bool, resolve: @escaping RCTPromiseResolveBlock, rejecter reject:@escaping RCTPromiseRejectBlock) {
Lnd.shared.startLnd(args, isTorEnabled: isTorEnabled, isTestnet: isTestnet) { (data, error) in
@objc(startLnd:lndDir:isTorEnabled:isTestnet:resolver:rejecter:)
func startLnd(_ args: String, lndDir: String, isTorEnabled: Bool, isTestnet: Bool, resolve: @escaping RCTPromiseResolveBlock, rejecter reject:@escaping RCTPromiseRejectBlock) {
Lnd.shared.startLnd(args, lndDir: lndDir, isTorEnabled: isTorEnabled, isTestnet: isTestnet) { (data, error) in
if let e = error {
reject("error", e.localizedDescription, e)
return
Expand Down
10 changes: 7 additions & 3 deletions ios/LndMobile/LndMobileTools.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ @interface RCT_EXTERN_MODULE(LndMobileTools, RCTEventEmitter)

RCT_EXTERN_METHOD(
writeConfig: (NSString *)config
lndDir: (NSString *)lndDir
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)
Expand Down Expand Up @@ -59,7 +60,8 @@ @interface RCT_EXTERN_MODULE(LndMobileTools, RCTEventEmitter)
)

RCT_EXTERN_METHOD(
deleteLndDirectory: (RCTPromiseResolveBlock)resolve
deleteLndDirectory: (NSString)lndDir
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)

Expand All @@ -80,12 +82,14 @@ @interface RCT_EXTERN_MODULE(LndMobileTools, RCTEventEmitter)
)

RCT_EXTERN_METHOD(
createIOSApplicationSupportAndLndDirectories: (RCTPromiseResolveBlock)resolve
createIOSApplicationSupportAndLndDirectories: (NSString)lndDir
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)

RCT_EXTERN_METHOD(
excludeLndICloudBackup: (RCTPromiseResolveBlock)resolve
excludeLndICloudBackup: (NSString)lndDir
resolver: (RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)

Expand Down
33 changes: 17 additions & 16 deletions ios/LndMobile/LndMobileTools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ class LndMobileTools: RCTEventEmitter {
return false
}

@objc(writeConfig:resolver:rejecter:)
func writeConfig(config: String, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
@objc(writeConfig:lndDir:resolver:rejecter:)
func writeConfig(config: String, lndDir: String, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
do {
let paths = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)
let url = paths[0].appendingPathComponent("lnd", isDirectory: true).appendingPathComponent("lnd.conf", isDirectory: false)
let url = paths[0].appendingPathComponent(lndDir, isDirectory: true).appendingPathComponent("lnd.conf", isDirectory: false)
NSLog(url.relativeString)

try config.write(to: url, atomically: true, encoding: .utf8)
Expand Down Expand Up @@ -227,19 +227,20 @@ class LndMobileTools: RCTEventEmitter {
resolve(nil)
}

@objc(deleteLndDirectory:rejecter:)
func deleteLndDirectory(resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
let cachePath = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)[0]
let lndPath = cachePath.appendingPathComponent("lnd", isDirectory: true)

@objc(deleteLndDirectory:resolver:rejecter:)
func deleteLndDirectory(lndDir: String, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
let fileManager = FileManager.default
let applicationSupportUrl = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
let lndUrl = applicationSupportUrl.appendingPathComponent(lndDir)

do {
try FileManager.default.removeItem(at: lndPath)
try FileManager.default.removeItem(at: lndUrl)
} catch {
reject("error deleting lnd dir", error.localizedDescription, error)
return
}

resolve(nil)
resolve(true)
}

@objc(DEBUG_deleteNeutrinoFiles:resolver:rejecter:)
Expand Down Expand Up @@ -280,11 +281,11 @@ class LndMobileTools: RCTEventEmitter {
resolve(FileManager.default.fileExists(atPath: lndFolder.path))
}

@objc(createIOSApplicationSupportAndLndDirectories:rejecter:)
func createIOSApplicationSupportAndLndDirectories(resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
@objc(createIOSApplicationSupportAndLndDirectories:resolver:rejecter:)
func createIOSApplicationSupportAndLndDirectories(lndDir: String, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
do {
let applicationSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
let lndFolder = applicationSupport.appendingPathComponent("lnd", isDirectory: true)
let lndFolder = applicationSupport.appendingPathComponent(lndDir, isDirectory: true)
// This will create the lnd folder as well as "Application Support"
try FileManager.default.createDirectory(at: lndFolder, withIntermediateDirectories: true)

Expand All @@ -294,10 +295,10 @@ class LndMobileTools: RCTEventEmitter {
}
}

@objc(excludeLndICloudBackup:rejecter:)
func excludeLndICloudBackup(resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
@objc(excludeLndICloudBackup:resolver:rejecter:)
func excludeLndICloudBackup(lndDir: String, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) {
let applicationSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
var lndFolder = applicationSupport.appendingPathComponent("lnd", isDirectory: true)
var lndFolder = applicationSupport.appendingPathComponent(lndDir, isDirectory: true)

do {
if FileManager.default.fileExists(atPath: lndFolder.path) {
Expand Down
11 changes: 7 additions & 4 deletions lndmobile/LndMobile.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ILndMobile {
initialize(): Promise<{ data: string }>;
startLnd(
args: string,
lndDir: string,
isTorEnabled?: boolean,
isTestnet?: boolean
): Promise<{ data: string }>;
Expand Down Expand Up @@ -50,7 +51,7 @@ export interface ILndMobile {
}

export interface ILndMobileTools {
writeConfig(data: string): Promise<string>;
writeConfig(lndDir: string, config: string): Promise<string>;
killLnd(): Promise<boolean>;
copyLndLog(network: string): Promise<boolean>;
tailLog(numberOfLines: number, network: string): Promise<string>;
Expand All @@ -60,7 +61,7 @@ export interface ILndMobileTools {
DEBUG_getWalletPasswordFromKeychain(): Promise<string>;
DEBUG_deleteSpeedloaderLastrunFile(): Promise<boolean>;
DEBUG_deleteSpeedloaderDgraphDirectory(): Promise<null>;
deleteLndDirectory(): Promise<null>;
deleteLndDirectory(lndDir: string): Promise<null>;
DEBUG_deleteNeutrinoFiles(network: string): Promise<boolean>;

// Android-specific
Expand All @@ -77,8 +78,10 @@ export interface ILndMobileTools {
checkICloudEnabled(): Promise<boolean>;
checkApplicationSupportExists(): Promise<boolean>;
checkLndFolderExists(): Promise<boolean>;
createIOSApplicationSupportAndLndDirectories(): Promise<boolean>;
excludeLndICloudBackup(): Promise<boolean>;
createIOSApplicationSupportAndLndDirectories(
lndDir: string
): Promise<boolean>;
excludeLndICloudBackup(lndDir: string): Promise<boolean>;
TEMP_moveLndToApplicationSupport(): Promise<boolean>;

// macOS-specific
Expand Down
30 changes: 22 additions & 8 deletions lndmobile/LndMobileInjection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,38 @@ import { OutPoint } from '../models/TransactionRequest';
export interface ILndMobileInjections {
index: {
initialize: () => Promise<{ data: string } | number>;
writeConfig: (config: string) => Promise<string>;
writeConfig: ({
lndDir,
config
}: {
lndDir: string;
config: string;
}) => Promise<string>;
subscribeState: () => Promise<string>;
decodeState: (data: string) => lnrpc.SubscribeStateResponse;
checkStatus: () => Promise<number>;
startLnd: (
args: string,
isTorEnabled?: boolean,
isTestnet?: boolean
) => Promise<string>;
startLnd: ({
args,
lndDir,
isTorEnabled,
isTestnet
}: {
args: string;
lndDir: string;
isTorEnabled?: boolean;
isTestnet?: boolean;
}) => Promise<string>;
stopLnd: () => Promise<string>;
gossipSync: (serviceUrl: string) => Promise<{ data: string }>;
cancelGossipSync: () => void;
checkICloudEnabled: () => Promise<boolean>;
checkApplicationSupportExists: () => Promise<boolean>;
checkLndFolderExists: () => Promise<boolean>;
createIOSApplicationSupportAndLndDirectories: () => Promise<boolean>;
createIOSApplicationSupportAndLndDirectories: (
lndDir: string
) => Promise<boolean>;
TEMP_moveLndToApplicationSupport: () => Promise<boolean>;
excludeLndICloudBackup: () => Promise<boolean>;
excludeLndICloudBackup: (lndDir: string) => Promise<boolean>;

addInvoice: ({
amount,
Expand Down
Loading

0 comments on commit e91db23

Please sign in to comment.