@@ -53,8 +53,6 @@ public class ReactNativeLightningModule extends ReactContextBaseJavaModule {
5353
5454 private FileObserver logObserver ;
5555
56- private static LndState state = new LndState ();
57-
5856 public ReactNativeLightningModule (ReactApplicationContext reactContext ) {
5957 super (reactContext );
6058 }
@@ -173,142 +171,19 @@ public void onError(Exception e) {
173171 public void onResponse (byte [] bytes ) {
174172 Log .i (TAG , "Wallet ready to be unlocked" );
175173 Log .d (TAG , "Wallet ready to be unlocked" );
176- state .setLndRunning (true , getReactApplicationContext ());
177174 promise .resolve ("LND started" );
178175 }
179176 }
180- class RPCCallback implements Callback {
181- @ Override
182- public void onError (Exception e ) {
183- Log .i (TAG , "RPC start err: " + e .getMessage ());
184- Log .d (TAG , "RPC start err: " + e .getMessage ());
185- }
186- @ Override
187- public void onResponse (byte [] bytes ) {
188- Log .i (TAG , "RPC ready for requests" );
189- Log .d (TAG , "RPC ready for requests" );
190- state .setGrpcReady (true , getReactApplicationContext ());
191- }
192- }
193177
194178 Runnable startLnd = new Runnable () {
195179 @ Override
196180 public void run () {
197- Lndmobile .start (args , new StartedCallback (), new RPCCallback () );
181+ Lndmobile .start (args , new StartedCallback ());
198182 }
199183 };
200184 new Thread (startLnd ).start ();
201185 }
202186
203- @ ReactMethod
204- public void genSeed (final Promise promise ) {
205- Log .i ("LndNativeModule" , "Generating seed..." );
206-
207- class SeedCallback implements Callback {
208- @ Override
209- public void onError (Exception e ) {
210- Log .i (TAG , "Seed err: " + e .getMessage ());
211- Log .d (TAG , "Seed err: " + e .getMessage ());
212- promise .reject (e );
213- }
214- @ Override
215- public void onResponse (byte [] bytes ) {
216- Log .i (TAG , "Seed generated successfully" );
217- Log .d (TAG , "Seed generated successfully" );
218-
219- String b64 = "" ;
220- if (bytes != null && bytes .length > 0 ) {
221- b64 = Base64 .encodeToString (bytes , Base64 .NO_WRAP );
222- }
223-
224- WritableMap params = Arguments .createMap ();
225- params .putString (respB64DataKey , b64 );
226-
227- promise .resolve (params );
228- }
229- }
230-
231- class GenSeedTask implements Runnable {
232- byte [] request ;
233- GenSeedTask (byte [] r ) { request = r ;}
234- public void run () {
235- Lndmobile .genSeed (request , new SeedCallback ());
236- }
237- }
238-
239- Thread t = new Thread (new GenSeedTask (new byte [0 ]));
240- t .start ();
241- }
242-
243- @ ReactMethod
244- public void createWallet (String msg , final Promise promise ) {
245- Log .i ("LndNativeModule" , "Initializing wallet..." );
246-
247- class InitializeWalletCallback implements Callback {
248- @ Override
249- public void onError (Exception e ) {
250- Log .i (TAG , "init err: " + e .getMessage ());
251- Log .d (TAG , "init err: " + e .getMessage ());
252- promise .reject (e );
253- }
254- @ Override
255- public void onResponse (byte [] bytes ) {
256- Log .i (TAG , "Wallet successfully initialized" );
257- Log .d (TAG , "Wallet successfully initialized" );
258- state .setWalletUnlocked (true , getReactApplicationContext ());
259- promise .resolve ("initialized" );
260- }
261- }
262-
263- class InitWalletTask implements Runnable {
264- byte [] request ;
265- InitWalletTask (byte [] r ) { request = r ;}
266- public void run () {
267- Lndmobile .initWallet (request , new InitializeWalletCallback ());
268- }
269- }
270- Thread t = new Thread (new InitWalletTask (Base64 .decode (msg , Base64 .NO_WRAP )));
271- t .start ();
272- }
273-
274- @ ReactMethod
275- public void unlockWallet (String msg , final Promise promise ) {
276- Log .i ("LndNativeModule" , "Unlocking wallet..." );
277-
278- class UnlockWalletCallback implements Callback {
279- @ Override
280- public void onError (Exception e ) {
281- Log .i (TAG , "Unlock err: " + e .getMessage ());
282- Log .d (TAG , "Unlock err: " + e .getMessage ());
283- e .printStackTrace ();
284-
285- //TODO wallet still unlocked but sometimes returns an error. Error needs some investigation.
286- if (e .getMessage ().contains ("transport is closing" )) {
287- promise .resolve ("unlocked" );
288- } else {
289- promise .reject (e );
290- }
291- }
292- @ Override
293- public void onResponse (byte [] bytes ) {
294- Log .i (TAG , "Wallet successfully unlocked" );
295- Log .d (TAG , "Wallet successfully unlocked" );
296- state .setWalletUnlocked (true , getReactApplicationContext ());
297- promise .resolve ("unlocked" );
298- }
299- }
300-
301- class UnlockWalletTask implements Runnable {
302- byte [] request ;
303- UnlockWalletTask (byte [] r ) { request = r ;}
304- public void run () {
305- Lndmobile .unlockWallet (request , new UnlockWalletCallback ());
306- }
307- }
308- Thread t = new Thread (new UnlockWalletTask (Base64 .decode (msg , Base64 .NO_WRAP )));
309- t .start ();
310- }
311-
312187 @ ReactMethod
313188 public void walletExists (String network , final Promise promise ) {
314189 File directory = new File (getReactApplicationContext ().getFilesDir ().toString () + "/lnd/data/chain/bitcoin/" + network + "/wallet.db" );
@@ -317,11 +192,6 @@ public void walletExists(String network, final Promise promise) {
317192 promise .resolve (exists );
318193 }
319194
320- @ ReactMethod
321- public void currentState (final Promise promise ) {
322- promise .resolve (state .formatted ());
323- }
324-
325195 @ ReactMethod
326196 public void logFileContent (String network , Integer limit , final Promise promise ) {
327197 File appDir = getReactApplicationContext ().getFilesDir ();
@@ -433,13 +303,6 @@ public void onResponse(byte[] bytes) {
433303
434304 try {
435305 m .invoke (null , Base64 .decode (msg , Base64 .NO_WRAP ), new NativeCallback (promise ));
436-
437- //If LND was stopped reset state
438- if (method .equals ("StopDaemon" )) {
439- state .setLndRunning (false , getReactApplicationContext ());
440- state .setGrpcReady (false , getReactApplicationContext ());
441- state .setWalletUnlocked (false , getReactApplicationContext ());
442- }
443306 } catch (IllegalAccessException | InvocationTargetException e ) {
444307 e .printStackTrace ();
445308 promise .reject ("LndNativeModule" , e );
@@ -519,36 +382,3 @@ public void sendStreamWrite(String streamId, String msg) {
519382 }
520383 }
521384}
522-
523- class LndState {
524- private Boolean lndRunning = false ;
525- private Boolean walletUnlocked = false ;
526- private Boolean grpcReady = false ;
527-
528- public void setLndRunning (Boolean lndRunning , ReactApplicationContext context ) {
529- this .lndRunning = lndRunning ;
530- updateStateStream (context );
531- }
532-
533- public void setWalletUnlocked (Boolean walletUnlocked , ReactApplicationContext context ) {
534- this .walletUnlocked = walletUnlocked ;
535- updateStateStream (context );
536- }
537-
538- public void setGrpcReady (Boolean grpcReady , ReactApplicationContext context ) {
539- this .grpcReady = grpcReady ;
540- updateStateStream (context );
541- }
542-
543- WritableMap formatted () {
544- WritableMap params = Arguments .createMap ();
545- params .putBoolean ("lndRunning" , lndRunning );
546- params .putBoolean ("walletUnlocked" , walletUnlocked );
547- params .putBoolean ("grpcReady" , grpcReady );
548- return params ;
549- }
550-
551- private void updateStateStream (ReactApplicationContext context ) {
552- context .getJSModule (DeviceEventManagerModule .RCTDeviceEventEmitter .class ).emit ("lndStateUpdate" , formatted ());
553- }
554- }
0 commit comments