Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wallet stop method #86

Merged
merged 1 commit into from
Nov 29, 2024
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beignet",
"version": "0.0.48",
"version": "0.0.49",
"description": "A self-custodial, JS Bitcoin wallet management library.",
"main": "dist/index.js",
"scripts": {
Expand Down
22 changes: 22 additions & 0 deletions src/wallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,28 @@ export class Wallet {
}
}

/**
* Stops the wallet. Use this method to prepare the wallet to be de
* @returns {Promise<Result<string>>}
*/
public async stop(): Promise<Result<string>> {
try {
// if we are refreshing, we need to wait for it to finish
if (this.isRefreshing) {
await this.refreshWallet();
}
pwltr marked this conversation as resolved.
Show resolved Hide resolved
// disable onMessage callback
this.disableMessages = true;
// disable saving to storage
this._setData = undefined;
// disconnect from Electrum
await this.electrum.disconnect();
return ok('Wallet stopped.');
} catch (e) {
return err(e);
}
}

public async switchNetwork(
network: EAvailableNetworks,
servers?: TServer | TServer[]
Expand Down
2 changes: 1 addition & 1 deletion tests/boost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
EProtocol,
generateMnemonic,
Wallet
} from '../src';
} from '../';
import {
bitcoinURL,
electrumHost,
Expand Down
2 changes: 1 addition & 1 deletion tests/receive.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
generateMnemonic,
sleep,
Wallet
} from '../src';
} from '../';
import {
bitcoinURL,
electrumHost,
Expand Down
27 changes: 23 additions & 4 deletions tests/storage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { validateMnemonic } from 'bip39';
import { expect } from 'chai';
import net from 'net';
import tls from 'tls';
import sinon from 'sinon';

import { Wallet } from '../';
import { deleteDirectory, getData, servers, setData } from '../example/helpers';
Expand All @@ -20,6 +21,10 @@ const testTimeout = 60000;
let wallet: Wallet;
const WALLET_NAME = 'storagetestwallet0';

const storage = { getData, setData };
const storageSpy = sinon.spy(storage, 'setData');
const messageSpy = sinon.spy();

describe('Storage Test', async function (): Promise<void> {
this.timeout(testTimeout);

Expand All @@ -31,10 +36,8 @@ describe('Storage Test', async function (): Promise<void> {
network: EAvailableNetworks.testnet,
name: WALLET_NAME,
addressType: EAddressType.p2wpkh,
storage: {
getData,
setData
},
storage,
onMessage: messageSpy,
electrumOptions: {
servers: servers[EAvailableNetworks.testnet],
net,
Expand All @@ -43,6 +46,7 @@ describe('Storage Test', async function (): Promise<void> {
});
if (res.isErr()) throw res.error;
wallet = res.value;
await wallet.refreshWallet({});
});

after(async function () {
Expand Down Expand Up @@ -146,6 +150,21 @@ describe('Storage Test', async function (): Promise<void> {
);
});

it('Should successfully stop the wallet', async () => {
wallet.refreshWallet(); // start wallet refresh in the background
const r = await wallet.stop();
if (r.isErr()) throw r.error;
storageSpy.resetHistory();
messageSpy.resetHistory();
// try to force the wallet to update it's data
wallet.feeEstimates.timestamp = 0;
await wallet.updateFeeEstimates(true);
await wallet.refreshWallet();
// make sure that the wallet did not call setData or onMessage
expect(storageSpy.called).to.equal(false);
expect(messageSpy.called).to.equal(false);
});

// it('Attempts to create a new wallet using the same name of an existing wallet in storage.', async () => {
// const createRes = await Wallet.create({
// mnemonic: generateMnemonic(),
Expand Down