Skip to content
This repository was archived by the owner on Oct 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,7 @@ private static boolean isStream(Method m) {
}

public void LndNativeModule(ReactApplicationContext reactContext) {
//Populate all available methods found in Lndmobile
Method[] methods = Lndmobile.class.getDeclaredMethods();
for (Method m : methods) {
String name = m.getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
if (isStream(m)) {
streamMethods.put(name, m);
} else {
syncMethods.put(name, m);
}
}
setAvailableMethods();
}

@Override
Expand All @@ -102,6 +92,21 @@ public Map<String, Object> getConstants() {
return constants;
}

//Populate all available methods found in Lndmobile
private void setAvailableMethods() {
Method[] methods = Lndmobile.class.getDeclaredMethods();

for (Method m : methods) {
String name = m.getName();
name = name.substring(0, 1).toUpperCase() + name.substring(1);
if (isStream(m)) {
streamMethods.put(name, m);
} else {
syncMethods.put(name, m);
}
}
}

@ReactMethod
public void start(String configContent, String network, final Promise promise) {
File appDir = getReactApplicationContext().getFilesDir();
Expand Down Expand Up @@ -437,14 +442,26 @@ public void onResponse(byte[] bytes) {

Method m = syncMethods.get(method);
if (m == null) {
promise.reject("LndNativeModule", "method not found");
return;
//Not all methods are exposed at time of init. Re-populate the available methods to make sure before rejecting.
setAvailableMethods();
m = syncMethods.get(method);
if (m == null) {
promise.reject("LndNativeModule", "'" + method + "' method not found");
return;
}
}

byte[] b = Base64.decode(msg, Base64.NO_WRAP);

try {
m.invoke(null, b, new NativeCallback(promise));

//If LND was stopped reset state
if (method.equals("StopDaemon")) {
state.setLndRunning(false, getReactApplicationContext());
state.setGrpcReady(false, getReactApplicationContext());
state.setWalletUnlocked(false, getReactApplicationContext());
}
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
promise.reject("LndNativeModule", e);
Expand Down
16 changes: 16 additions & 0 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ const App = () => {
const res = await lnd.start(lndConf);

if (res.isErr()) {
setMessage(res.error.message);
console.error(res.error);
return;
}

setMessage(JSON.stringify(res.value));
}}
/>

<Button
title={'Stop LND'}
onPress={async () => {
setMessage('Stopping LND...');
const res = await lnd.stop();
if (res.isErr()) {
setMessage(res.error.message);
console.error(res.error);
return;
}
Expand Down
9 changes: 8 additions & 1 deletion ios/ReactNativeLightning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,19 @@ class ReactNativeLightning: NSObject {
}

let completion = BlindLndCallback(onResponse)

guard let lndMethod = syncMethods[method as String] else {
return onResponse(nil, LightningError.unknownMethod)
}

lndMethod(request, completion)

//If LND was stopped reset state
if method == "StopDaemon" {
ReactNativeLightning.state.lndRunning = false
ReactNativeLightning.state.grpcReady = false
ReactNativeLightning.state.walletUnlocked = false
}
}

@objc
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-lightning",
"title": "React Native Lightning",
"version": "0.0.17",
"version": "0.0.18",
"description": "React Native wrapper for Lndmobile",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
18 changes: 18 additions & 0 deletions src/lnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,24 @@ class LND {
return err(e);
}
}

/**
* Stop the LND daemon
* @returns {Promise<Ok<lnrpc.StopResponse, Error> | Err<unknown, any>>}
*/
async stop(): Promise<Result<lnrpc.StopResponse, Error>> {
try {
const message = lnrpc.StopRequest.create();
const serializedResponse = await this.grpc.sendCommand(
EGrpcSyncMethods.StopDaemon,
lnrpc.StopRequest.encode(message).finish()
);

return ok(lnrpc.StopResponse.decode(serializedResponse));
} catch (e) {
return err(e);
}
}
}

export default new LND();