Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit a5bebfc

Browse files
committed
Integrate file into wallet action
1 parent 2622586 commit a5bebfc

File tree

2 files changed

+20
-32
lines changed

2 files changed

+20
-32
lines changed

src/action/wallet.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ import { when } from 'mobx';
1414
import * as log from './log';
1515

1616
class WalletAction {
17-
constructor(store, grpc, db, nav, notification, FS) {
17+
constructor(store, grpc, db, nav, notification, file) {
1818
this._store = store;
1919
this._grpc = grpc;
2020
this._db = db;
2121
this._nav = nav;
2222
this._notification = notification;
23-
this._FS = FS;
23+
this._file = file;
2424
}
2525

2626
//
@@ -266,7 +266,7 @@ class WalletAction {
266266
*/
267267
async initWallet({ walletPassword, seedMnemonic, recoveryWindow = 0 }) {
268268
try {
269-
await Promise.all([this.deleteDB('testnet'), this.deleteDB('mainnet')]);
269+
await this.deleteDB();
270270
await this._grpc.sendUnlockerCommand('InitWallet', {
271271
walletPassword: toBuffer(walletPassword),
272272
cipherSeedMnemonic: seedMnemonic,
@@ -293,20 +293,19 @@ class WalletAction {
293293
/**
294294
* Delete the wallet.db file. This allows the user to restore their wallet
295295
* (including channel state) from the seed if they've they've forgotten the
296-
* wallet pin/password.
296+
* wallet pin/password. We need to delete both mainnet and testnet wallet
297+
* files since we haven't set `store.network` at this point in the app life
298+
* cycle yet (which happens later when we query getInfo).
297299
* @return {Promise<undefined>}
298300
*/
299-
async deleteDB(network) {
300-
if (!this._FS) {
301+
async deleteDB() {
302+
if (!this._file) {
301303
return;
302304
}
303-
const lndDir = this._FS.DocumentDirectoryPath;
304-
const dbPath = `${lndDir}/data/chain/bitcoin/${network}/wallet.db`;
305-
try {
306-
await this._FS.unlink(dbPath);
307-
} catch (err) {
308-
log.info(`No ${network} wallet to delete.`);
309-
}
305+
await Promise.all([
306+
this._file.deleteWalletDB('testnet'),
307+
this._file.deleteWalletDB('mainnet'),
308+
]);
310309
}
311310

312311
/**

test/unit/action/wallet.spec.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import AppStorage from '../../../src/action/app-storage';
44
import WalletAction from '../../../src/action/wallet';
55
import NavAction from '../../../src/action/nav';
66
import NavActionMobile from '../../../src/action/nav-mobile';
7+
import FileAction from '../../../src/action/file-mobile';
78
import NotificationAction from '../../../src/action/notification';
89
import * as logger from '../../../src/action/log';
910
import nock from 'nock';
@@ -18,7 +19,7 @@ describe('Action Wallet Unit Tests', () => {
1819
let wallet;
1920
let nav;
2021
let notification;
21-
let RNFS;
22+
let file;
2223

2324
beforeEach(() => {
2425
sandbox = sinon.createSandbox({});
@@ -31,11 +32,8 @@ describe('Action Wallet Unit Tests', () => {
3132
db = sinon.createStubInstance(AppStorage);
3233
notification = sinon.createStubInstance(NotificationAction);
3334
nav = sinon.createStubInstance(NavAction);
34-
RNFS = {
35-
DocumentDirectoryPath: '/foo/bar',
36-
unlink: sinon.stub(),
37-
};
38-
wallet = new WalletAction(store, grpc, db, nav, notification, RNFS);
35+
file = sinon.createStubInstance(FileAction);
36+
wallet = new WalletAction(store, grpc, db, nav, notification, file);
3937
});
4038

4139
afterEach(() => {
@@ -308,18 +306,9 @@ describe('Action Wallet Unit Tests', () => {
308306
describe('initWallet()', () => {
309307
it('should init wallet', async () => {
310308
grpc.sendUnlockerCommand.withArgs('InitWallet').resolves();
311-
RNFS.unlink.resolves();
312309
await wallet.initWallet({ walletPassword: 'baz', seedMnemonic: ['foo'] });
313-
expect(
314-
RNFS.unlink,
315-
'was called with',
316-
'/foo/bar/data/chain/bitcoin/mainnet/wallet.db'
317-
);
318-
expect(
319-
RNFS.unlink,
320-
'was called with',
321-
'/foo/bar/data/chain/bitcoin/testnet/wallet.db'
322-
);
310+
expect(file.deleteWalletDB, 'was called with', 'mainnet');
311+
expect(file.deleteWalletDB, 'was called with', 'testnet');
323312
expect(store.walletUnlocked, 'to be', true);
324313
expect(grpc.sendUnlockerCommand, 'was called with', 'InitWallet', {
325314
walletPassword: Buffer.from('baz', 'utf8'),
@@ -330,9 +319,9 @@ describe('Action Wallet Unit Tests', () => {
330319

331320
it('should not delete wallet if RNFS not supported', async () => {
332321
grpc.sendUnlockerCommand.withArgs('InitWallet').resolves();
333-
delete wallet._FS;
322+
delete wallet._file;
334323
await wallet.initWallet({ walletPassword: 'baz', seedMnemonic: ['foo'] });
335-
expect(RNFS.unlink, 'was not called');
324+
expect(file.deleteWalletDB, 'was not called');
336325
expect(store.walletUnlocked, 'to be', true);
337326
expect(grpc.sendUnlockerCommand, 'was called with', 'InitWallet', {
338327
walletPassword: Buffer.from('baz', 'utf8'),

0 commit comments

Comments
 (0)