Skip to content

Commit f693e0e

Browse files
committed
Removed the need for native code for seed generation, unlocking and creating wallet
1 parent b59a0d6 commit f693e0e

File tree

10 files changed

+169
-235
lines changed

10 files changed

+169
-235
lines changed

android/src/main/java/com/reactnativelightning/ReactNativeLightningModule.java

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -184,73 +184,6 @@ public void run() {
184184
new Thread(startLnd).start();
185185
}
186186

187-
@ReactMethod
188-
public void createWallet(String msg, final Promise promise) {
189-
Log.i("LndNativeModule", "Initializing wallet...");
190-
191-
class InitializeWalletCallback implements Callback {
192-
@Override
193-
public void onError(Exception e) {
194-
Log.i(TAG, "init err: " + e.getMessage());
195-
Log.d(TAG, "init err: " + e.getMessage());
196-
promise.reject(e);
197-
}
198-
@Override
199-
public void onResponse(byte[] bytes) {
200-
Log.i(TAG, "Wallet successfully initialized");
201-
Log.d(TAG, "Wallet successfully initialized");
202-
promise.resolve("initialized");
203-
}
204-
}
205-
206-
class InitWalletTask implements Runnable {
207-
byte[] request;
208-
InitWalletTask(byte[] r) { request = r;}
209-
public void run() {
210-
Lndmobile.initWallet(request, new InitializeWalletCallback());
211-
}
212-
}
213-
Thread t = new Thread(new InitWalletTask(Base64.decode(msg, Base64.NO_WRAP)));
214-
t.start();
215-
}
216-
217-
@ReactMethod
218-
public void unlockWallet(String msg, final Promise promise) {
219-
Log.i("LndNativeModule", "Unlocking wallet...");
220-
221-
class UnlockWalletCallback implements Callback {
222-
@Override
223-
public void onError(Exception e) {
224-
Log.i(TAG, "Unlock err: " + e.getMessage());
225-
Log.d(TAG, "Unlock err: " + e.getMessage());
226-
e.printStackTrace();
227-
228-
//TODO wallet still unlocked but sometimes returns an error. Error needs some investigation.
229-
if (e.getMessage().contains("transport is closing")) {
230-
promise.resolve("unlocked");
231-
} else {
232-
promise.reject(e);
233-
}
234-
}
235-
@Override
236-
public void onResponse(byte[] bytes) {
237-
Log.i(TAG, "Wallet successfully unlocked");
238-
Log.d(TAG, "Wallet successfully unlocked");
239-
promise.resolve("unlocked");
240-
}
241-
}
242-
243-
class UnlockWalletTask implements Runnable {
244-
byte[] request;
245-
UnlockWalletTask(byte[] r) { request = r;}
246-
public void run() {
247-
Lndmobile.unlockWallet(request, new UnlockWalletCallback());
248-
}
249-
}
250-
Thread t = new Thread(new UnlockWalletTask(Base64.decode(msg, Base64.NO_WRAP)));
251-
t.start();
252-
}
253-
254187
@ReactMethod
255188
public void walletExists(String network, final Promise promise) {
256189
File directory = new File(getReactApplicationContext().getFilesDir().toString() + "/lnd/data/chain/bitcoin/" + network + "/wallet.db");

example/App.tsx

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import lnd, {
2323
LndConf,
2424
ss_lnrpc,
2525
lnrpc,
26-
stateService,
2726
} from '@synonymdev/react-native-lightning';
2827

2928
import lndCache from '@synonymdev/react-native-lightning/dist/utils/neutrino-cache';
@@ -50,7 +49,7 @@ const App = () => {
5049
const [seed, setSeed] = useState<string[]>([]);
5150

5251
const startStateSubscription = () => {
53-
stateService.subscribeToStateChanges(
52+
lnd.stateService.subscribeToStateChanges(
5453
(res: Result<ss_lnrpc.WalletState>) => {
5554
if (res.isOk()) {
5655
setLndState(res.value);
@@ -62,7 +61,7 @@ const App = () => {
6261

6362
useEffect(() => {
6463
(async () => {
65-
const res = await stateService.getState();
64+
const res = await lnd.stateService.getState();
6665
if (res.isOk()) {
6766
setLndState(res.value);
6867
if (res.value !== ss_lnrpc.WalletState.WAITING_TO_START) {
@@ -73,7 +72,7 @@ const App = () => {
7372
}, []);
7473

7574
useEffect(() => {
76-
if (lndState.walletUnlocked) {
75+
if (lndState === ss_lnrpc.WalletState.RPC_ACTIVE) {
7776
lnd.subscribeToOnChainTransactions(
7877
(res: Result<lnrpc.Transaction>) => {
7978
if (res.isErr()) {
@@ -134,7 +133,7 @@ const App = () => {
134133
contentInsetAdjustmentBehavior="automatic"
135134
style={styles.scrollView}>
136135
<Text style={styles.state}>
137-
State: {stateService.readableState(lndState)}
136+
State: {lnd.stateService.readableState(lndState)}
138137
</Text>
139138

140139
<Text style={styles.message}>{message}</Text>
@@ -184,7 +183,7 @@ const App = () => {
184183
<Button
185184
title={'Generate seed'}
186185
onPress={async () => {
187-
const res = await lnd.genSeed();
186+
const res = await lnd.walletUnlocker.genSeed();
188187

189188
if (res.isErr()) {
190189
console.error(res.error);
@@ -204,30 +203,35 @@ const App = () => {
204203
<Button
205204
title={'Unlock wallet'}
206205
onPress={async () => {
207-
const res = await lnd.unlockWallet(dummyPassword);
206+
const res = await lnd.walletUnlocker.unlockWallet(
207+
dummyPassword,
208+
);
208209

209210
if (res.isErr()) {
210211
console.error(res.error);
211212
return;
212213
}
213214

214-
setMessage(JSON.stringify(res.value));
215+
setMessage('Unlocked.');
215216
}}
216217
/>
217218
) : null}
218219

219220
{showCreateButton ? (
220221
<Button
221-
title={'Create wallet'}
222+
title={'Init wallet'}
222223
onPress={async () => {
223-
const res = await lnd.createWallet(dummyPassword, seed);
224+
const res = await lnd.walletUnlocker.initWallet(
225+
dummyPassword,
226+
seed,
227+
);
224228

225229
if (res.isErr()) {
226230
console.error(res.error);
227231
return;
228232
}
229233

230-
setMessage(JSON.stringify(res.value));
234+
setMessage('Wallet initialised');
231235
}}
232236
/>
233237
) : null}

ios/ReactNativeLightning.m

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@ @interface RCT_EXTERN_MODULE(ReactNativeLightning, NSObject)
2525
rejecter:(RCTPromiseRejectBlock)reject
2626
)
2727

28-
RCT_EXTERN_METHOD(
29-
createWallet: (NSString *)body
30-
resolve: (RCTPromiseResolveBlock)resolve
31-
rejecter:(RCTPromiseRejectBlock)reject
32-
)
33-
34-
RCT_EXTERN_METHOD(
35-
unlockWallet: (NSString *)password
36-
resolve: (RCTPromiseResolveBlock)resolve
37-
rejecter:(RCTPromiseRejectBlock)reject
38-
)
39-
4028
RCT_EXTERN_METHOD(
4129
sendCommand: (NSString *)method
4230
body: (NSString *)body

ios/ReactNativeLightning.swift

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class ReactNativeLightning: NSObject {
115115
"BakeMacaroon": { (req: Data?, cb: BlindLndCallback) in LndmobileBakeMacaroon(req, cb) },
116116
"GenSeed": { (req: Data?, cb: BlindLndCallback) in LndmobileGenSeed(req, cb) },
117117
"GetState": { (req: Data?, cb: BlindLndCallback) in LndmobileGetState(req, cb) },
118+
"InitWallet": { (req: Data?, cb: BlindLndCallback) in LndmobileInitWallet(req, cb) },
119+
"UnlockWallet": { (req: Data?, cb: BlindLndCallback) in LndmobileUnlockWallet(req, cb) },
118120
]
119121
}()
120122

@@ -202,34 +204,6 @@ class ReactNativeLightning: NSObject {
202204
)
203205
}
204206

205-
@objc
206-
func createWallet(_ body: NSString, resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
207-
LndmobileInitWallet(
208-
Data(base64Encoded: String(body)),
209-
LndEmptyResponseCallback { (error) in
210-
if let e = error {
211-
return reject("error", e.localizedDescription, e)
212-
}
213-
214-
resolve(LightningCallbackResponses.walletCreated.rawValue)
215-
}
216-
)
217-
}
218-
219-
@objc
220-
func unlockWallet(_ body: NSString, resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
221-
LndmobileUnlockWallet(
222-
Data(base64Encoded: String(body)),
223-
LndEmptyResponseCallback { (error) in
224-
if let e = error {
225-
return reject("error", e.localizedDescription, e)
226-
}
227-
228-
resolve(LightningCallbackResponses.walletUnlocked.rawValue)
229-
}
230-
)
231-
}
232-
233207
@objc
234208
func sendCommand(_ method: NSString, body: NSString, resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
235209
let request = Data(base64Encoded: String(body))

src/grpc.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
TNativeStreamResponse
88
} from './utils/types';
99
import { err, ok, Result } from './utils/result';
10-
import { ss_lnrpc, stateService } from './index';
10+
import { ss_lnrpc } from './index';
1111

1212
class GrpcAction {
1313
private readonly lnd: NativeModulesStatic;
@@ -29,15 +29,34 @@ class GrpcAction {
2929
* @returns {Promise<void>}
3030
*/
3131
async checkGrpcReady(): Promise<void> {
32-
const res = await stateService.getState();
32+
try {
33+
const state = await this.getStateCommand();
3334

34-
if (res.isErr()) {
35+
if (state === ss_lnrpc.WalletState.WAITING_TO_START) {
36+
throw new Error('LND not started');
37+
}
38+
} catch (e) {
3539
throw new Error('Unable to determine LND state');
3640
}
41+
}
3742

38-
if (res.value === ss_lnrpc.WalletState.WAITING_TO_START) {
39-
throw new Error('LND not started');
43+
/**
44+
* Wrapper function for querying the current state of LND
45+
* @returns {Promise<ss_lnrpc.WalletState>}
46+
*/
47+
async getStateCommand(): Promise<ss_lnrpc.WalletState> {
48+
const serialisedReq = base64.fromByteArray(
49+
ss_lnrpc.GetStateRequest.encode(ss_lnrpc.GetStateRequest.create()).finish()
50+
);
51+
const { data: serializedResponse } = await this.lnd.sendCommand(
52+
EGrpcSyncMethods.GetState,
53+
serialisedReq
54+
);
55+
if (serializedResponse === undefined) {
56+
throw new Error('Missing response');
4057
}
58+
59+
return ss_lnrpc.GetStateResponse.decode(base64.toByteArray(serializedResponse)).state;
4160
}
4261

4362
/**

src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { lnrpc } from './protos/rpc';
55
import { wu_lnrpc } from './protos/walletunlocker';
66
import { ss_lnrpc } from './protos/stateservice';
77

8-
import stateService from './stateservice';
9-
10-
export { LndConf, lnrpc, stateService, wu_lnrpc, ss_lnrpc };
8+
export { LndConf, lnrpc, wu_lnrpc, ss_lnrpc };
119
export * from './utils/types';
1210
export default lnd;

0 commit comments

Comments
 (0)