diff --git a/package.json b/package.json index efe8e8f..f799248 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "A self-custodial, JS Bitcoin wallet management library.", "main": "dist/index.js", "scripts": { - "test": "yarn build && env mocha --exit -r ts-node/register 'tests/**/*.ts'", + "test": "yarn build && env mocha --exit -r ts-node/register 'tests/**/*.test.ts'", "test:boost": "yarn build && env mocha --exit -r ts-node/register 'tests/boost.test.ts'", "test:wallet": "yarn build && env mocha --exit -r ts-node/register 'tests/wallet.test.ts'", "test:receive": "yarn build && env mocha --exit -r ts-node/register 'tests/receive.test.ts'", diff --git a/src/wallet/index.ts b/src/wallet/index.ts index 0c261b7..e39ae8b 100644 --- a/src/wallet/index.ts +++ b/src/wallet/index.ts @@ -3,6 +3,8 @@ import * as bitcoin from 'bitcoinjs-lib'; import { Network, networks } from 'bitcoinjs-lib'; import BIP32Factory, { BIP32Interface } from 'bip32'; import * as ecc from '@bitcoinerlab/secp256k1'; +import cloneDeep from 'lodash.clonedeep'; + import { EAddressType, EAvailableNetworks, @@ -108,7 +110,6 @@ import { import { Electrum } from '../electrum'; import { Transaction } from '../transaction'; import { GAP_LIMIT, GAP_LIMIT_CHANGE, TRANSACTION_DEFAULTS } from './constants'; -import cloneDeep from 'lodash.clonedeep'; import { btcToSats } from '../utils/conversion'; const bip32 = BIP32Factory(ecc); diff --git a/tests/boost.test.ts b/tests/boost.test.ts index 9c0a2df..32e4486 100644 --- a/tests/boost.test.ts +++ b/tests/boost.test.ts @@ -25,68 +25,66 @@ let waitForElectrum: TWaitForElectrum; const rpc = new BitcoinJsonRpc(bitcoinURL); const failure = { canBoost: false, rbf: false, cpfp: false }; -beforeEach(async function () { +describe('Boost', async function () { this.timeout(testTimeout); - let balance = await rpc.getBalance(); - const address = await rpc.getNewAddress(); - await rpc.generateToAddress(1, address); + beforeEach(async function () { + this.timeout(testTimeout); + + let balance = await rpc.getBalance(); + const address = await rpc.getNewAddress(); + await rpc.generateToAddress(1, address); - while (balance < 10) { - await rpc.generateToAddress(10, address); - balance = await rpc.getBalance(); - } + while (balance < 10) { + await rpc.generateToAddress(10, address); + balance = await rpc.getBalance(); + } - waitForElectrum = await initWaitForElectrumToSync( - { host: electrumHost, port: electrumPort }, - bitcoinURL - ); + waitForElectrum = await initWaitForElectrumToSync( + { host: electrumHost, port: electrumPort }, + bitcoinURL + ); - await waitForElectrum(); + await waitForElectrum(); - const mnemonic = generateMnemonic(); + const mnemonic = generateMnemonic(); - const res = await Wallet.create({ - rbf: true, - mnemonic, - // mnemonic: 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', - network: EAvailableNetworks.regtest, - addressType: EAddressType.p2wpkh, - electrumOptions: { - servers: [ - { - host: '127.0.0.1', - ssl: 60002, - tcp: 60001, - protocol: EProtocol.tcp - } - ], - net, - tls - }, - // reduce gap limit to speed up tests - gapLimitOptions: { - lookAhead: 2, - lookBehind: 2, - lookAheadChange: 2, - lookBehindChange: 2 + const res = await Wallet.create({ + rbf: true, + mnemonic, + // mnemonic: 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about', + network: EAvailableNetworks.regtest, + addressType: EAddressType.p2wpkh, + electrumOptions: { + servers: [ + { + host: '127.0.0.1', + ssl: 60002, + tcp: 60001, + protocol: EProtocol.tcp + } + ], + net, + tls + }, + // reduce gap limit to speed up tests + gapLimitOptions: { + lookAhead: 2, + lookBehind: 2, + lookAheadChange: 2, + lookBehindChange: 2 + } + }); + if (res.isErr()) { + throw res.error; } + wallet = res.value; + await wallet.refreshWallet({}); }); - if (res.isErr()) { - throw res.error; - } - wallet = res.value; - await wallet.refreshWallet({}); -}); - -afterEach(async function () { - if (wallet?.electrum) { - await wallet.electrum.disconnect(); - } -}); -describe('Boost', async function () { - this.timeout(testTimeout); + afterEach(async function () { + await wallet?.electrum?.disconnect(); + }); it('Should fail in some cases.', async () => { // tx not found diff --git a/tests/derivation.test.ts b/tests/derivation.test.ts index d44915e..3983975 100644 --- a/tests/derivation.test.ts +++ b/tests/derivation.test.ts @@ -1,4 +1,4 @@ -import * as chai from 'chai'; +import { expect } from 'chai'; import { EAddressType, EAvailableNetworks, @@ -8,8 +8,6 @@ import { getKeyDerivationPathString } from '../src'; -const expect = chai.expect; - describe('Derivation Methods', () => { it('Should return default derivation path object for p2wpkh', () => { const pathRes = getKeyDerivationPath({ diff --git a/tests/electrum.test.ts b/tests/electrum.test.ts index 0e82680..16a6886 100644 --- a/tests/electrum.test.ts +++ b/tests/electrum.test.ts @@ -1,39 +1,46 @@ -import * as chai from 'chai'; +import { expect } from 'chai'; import net from 'net'; import tls from 'tls'; import { Wallet } from '../'; -import { EAvailableNetworks, EAddressType, IGetUtxosResponse } from '../src'; -import { TEST_MNEMONIC } from './constants'; -import { Result } from '../src'; import { servers } from '../example/helpers'; +import { + EAddressType, + EAvailableNetworks, + IGetUtxosResponse, + Result +} from '../src'; +import { TEST_MNEMONIC } from './constants'; import { EXPECTED_SHARED_RESULTS } from './expected-results'; -const expect = chai.expect; - const testTimeout = 60000; let wallet: Wallet; -before(async function () { +describe('Electrum Methods', async function (): Promise { this.timeout(testTimeout); - const res = await Wallet.create({ - mnemonic: TEST_MNEMONIC, - network: EAvailableNetworks.testnet, - addressType: EAddressType.p2wpkh, - electrumOptions: { - servers: servers[EAvailableNetworks.testnet], - net, - tls - } + + before(async function () { + this.timeout(testTimeout); + const res = await Wallet.create({ + mnemonic: TEST_MNEMONIC, + network: EAvailableNetworks.testnet, + addressType: EAddressType.p2wpkh, + electrumOptions: { + servers: servers[EAvailableNetworks.testnet], + net, + tls + } + }); + if (res.isErr()) throw res.error; + wallet = res.value; + await wallet.refreshWallet({}); + }); + + after(async function () { + await wallet?.electrum?.disconnect(); }); - if (res.isErr()) throw res.error; - wallet = res.value; - await wallet.refreshWallet({}); -}); -describe('Electrum Methods', async function (): Promise { - this.timeout(testTimeout); it('connectToElectrum: Should connect to a random Electrum server', async () => { const connectResponse = await wallet.connectToElectrum(); if (connectResponse.isErr()) throw connectResponse.error; diff --git a/tests/receive.test.ts b/tests/receive.test.ts index ed94520..bc3d460 100644 --- a/tests/receive.test.ts +++ b/tests/receive.test.ts @@ -26,68 +26,66 @@ let waitForElectrum: TWaitForElectrum; const rpc = new BitcoinJsonRpc(bitcoinURL); const ml = new MessageListener(); -beforeEach(async function () { +describe('Receive', async function () { this.timeout(testTimeout); - ml.clear(); - - // Ensure sufficient balance in regtest - let balance = await rpc.getBalance(); - const address = await rpc.getNewAddress(); - await rpc.generateToAddress(1, address); - - while (balance < 10) { - await rpc.generateToAddress(10, address); - balance = await rpc.getBalance(); - } - - waitForElectrum = await initWaitForElectrumToSync( - { host: electrumHost, port: electrumPort }, - bitcoinURL - ); - - await waitForElectrum(); - - const mnemonic = generateMnemonic(); - - const res = await Wallet.create({ - mnemonic, - network: EAvailableNetworks.regtest, - addressType: EAddressType.p2wpkh, - electrumOptions: { - servers: [ - { - host: '127.0.0.1', - ssl: 60002, - tcp: 60001, - protocol: EProtocol.tcp - } - ], - net, - tls - }, - // reduce gap limit to speed up tests - gapLimitOptions: { - lookAhead: 2, - lookBehind: 2, - lookAheadChange: 2, - lookBehindChange: 2 - }, - addressTypesToMonitor: [EAddressType.p2wpkh], - onMessage: ml.onMessage - }); - if (res.isErr()) throw res.error; - wallet = res.value; - await wallet.refreshWallet({}); -}); -afterEach(async function () { - if (wallet?.electrum) { - await wallet.electrum.disconnect(); - } -}); + beforeEach(async function () { + this.timeout(testTimeout); + ml.clear(); -describe('Receive', async function () { - this.timeout(testTimeout); + // Ensure sufficient balance in regtest + let balance = await rpc.getBalance(); + const address = await rpc.getNewAddress(); + await rpc.generateToAddress(1, address); + + while (balance < 10) { + await rpc.generateToAddress(10, address); + balance = await rpc.getBalance(); + } + + waitForElectrum = await initWaitForElectrumToSync( + { host: electrumHost, port: electrumPort }, + bitcoinURL + ); + + await waitForElectrum(); + + const mnemonic = generateMnemonic(); + + const res = await Wallet.create({ + mnemonic, + network: EAvailableNetworks.regtest, + addressType: EAddressType.p2wpkh, + electrumOptions: { + servers: [ + { + host: '127.0.0.1', + ssl: 60002, + tcp: 60001, + protocol: EProtocol.tcp + } + ], + net, + tls + }, + // reduce gap limit to speed up tests + gapLimitOptions: { + lookAhead: 2, + lookBehind: 2, + lookAheadChange: 2, + lookBehindChange: 2 + }, + addressTypesToMonitor: [EAddressType.p2wpkh], + onMessage: ml.onMessage + }); + if (res.isErr()) throw res.error; + wallet = res.value; + await wallet.refreshWallet({}); + }); + + afterEach(async function () { + await wallet?.electrum?.disconnect(); + }); it('Should generate new receiving address', async () => { const r = await wallet.getNextAvailableAddress(); diff --git a/tests/storage.test.ts b/tests/storage.test.ts index 10b15da..9025167 100644 --- a/tests/storage.test.ts +++ b/tests/storage.test.ts @@ -1,51 +1,54 @@ -import * as chai from 'chai'; import { validateMnemonic } from 'bip39'; +import { expect } from 'chai'; import net from 'net'; import tls from 'tls'; import { Wallet } from '../'; +import { deleteDirectory, getData, servers, setData } from '../example/helpers'; import { EAddressType, EAvailableNetworks, + generateMnemonic, IGetUtxosResponse, - Result, - generateMnemonic + Result } from '../src'; import { TEST_MNEMONIC } from './constants'; -import { deleteDirectory, getData, servers, setData } from '../example/helpers'; import { EXPECTED_SHARED_RESULTS } from './expected-results'; -const expect = chai.expect; - const testTimeout = 60000; let wallet: Wallet; const WALLET_NAME = 'storagetestwallet0'; -before(async function () { +describe('Storage Test', async function (): Promise { this.timeout(testTimeout); - await deleteDirectory('example/walletData'); // Start test with clean slate. - const res = await Wallet.create({ - mnemonic: TEST_MNEMONIC, - network: EAvailableNetworks.testnet, - name: WALLET_NAME, - addressType: EAddressType.p2wpkh, - storage: { - getData, - setData - }, - electrumOptions: { - servers: servers[EAvailableNetworks.testnet], - net, - tls - } + + before(async function () { + this.timeout(testTimeout); + await deleteDirectory('example/walletData'); // Start test with clean slate. + const res = await Wallet.create({ + mnemonic: TEST_MNEMONIC, + network: EAvailableNetworks.testnet, + name: WALLET_NAME, + addressType: EAddressType.p2wpkh, + storage: { + getData, + setData + }, + electrumOptions: { + servers: servers[EAvailableNetworks.testnet], + net, + tls + } + }); + if (res.isErr()) throw res.error; + wallet = res.value; + }); + + after(async function () { + await wallet?.electrum?.disconnect(); }); - if (res.isErr()) throw res.error; - wallet = res.value; -}); -describe('Storage Test', async function (): Promise { - this.timeout(testTimeout); it('Should successfully create a wallet.', () => { expect(wallet).not.to.be.null; }); diff --git a/tests/transaction.test.ts b/tests/transaction.test.ts index 892d65e..af44cbf 100644 --- a/tests/transaction.test.ts +++ b/tests/transaction.test.ts @@ -1,4 +1,4 @@ -import * as chai from 'chai'; +import { expect } from 'chai'; import net from 'net'; import tls from 'tls'; @@ -8,35 +8,37 @@ import { ISweepPrivateKeyRes, Wallet } from '../'; +import { servers } from '../example/helpers'; import { EAvailableNetworks, Result } from '../src'; import { TRANSACTION_TEST_MNEMONIC } from './constants'; -import { servers } from '../example/helpers'; import { EXPECTED_TRANSACTION_RESULTS } from './expected-results'; -const expect = chai.expect; - const testTimeout = 60000; let wallet: Wallet; -before(async function () { +describe('Transaction Test', async function (): Promise { this.timeout(testTimeout); - const res = await Wallet.create({ - mnemonic: TRANSACTION_TEST_MNEMONIC, - network: EAvailableNetworks.testnet, - electrumOptions: { - servers: servers[EAvailableNetworks.testnet], - net, - tls - } + + before(async function () { + this.timeout(testTimeout); + const res = await Wallet.create({ + mnemonic: TRANSACTION_TEST_MNEMONIC, + network: EAvailableNetworks.testnet, + electrumOptions: { + servers: servers[EAvailableNetworks.testnet], + net, + tls + } + }); + if (res.isErr()) throw res.error; + wallet = res.value; + await wallet.refreshWallet({}); }); - if (res.isErr()) throw res.error; - wallet = res.value; - await wallet.refreshWallet({}); -}); -describe('Transaction Test', async function (): Promise { - this.timeout(testTimeout); + after(async function () { + await wallet?.electrum?.disconnect(); + }); it('Should successfully create a wallet.', (): void => { expect(wallet).not.to.be.null; diff --git a/tests/wallet.test.ts b/tests/wallet.test.ts index b44aa33..17e7926 100644 --- a/tests/wallet.test.ts +++ b/tests/wallet.test.ts @@ -1,16 +1,17 @@ -import * as chai from 'chai'; import { validateMnemonic } from 'bip39'; +import { expect } from 'chai'; import net from 'net'; -import tls from 'tls'; import sinon from 'sinon'; +import tls from 'tls'; import { - filterAddressesObjForGapLimit, filterAddressesObjForAddressesList, + filterAddressesObjForGapLimit, IAddress, IAddresses, Wallet } from '../'; +import { servers } from '../example/helpers'; import { EAddressType, EAvailableNetworks, @@ -22,37 +23,38 @@ import { Result } from '../src'; import { TEST_MNEMONIC } from './constants'; -import { servers } from '../example/helpers'; import { EXPECTED_SHARED_RESULTS, EXPECTED_WALLET_RESULTS } from './expected-results'; -const expect = chai.expect; - const testTimeout = 60000; let wallet: Wallet; -before(async function () { +describe('Wallet Library', async function () { this.timeout(testTimeout); - const res = await Wallet.create({ - mnemonic: TEST_MNEMONIC, - network: EAvailableNetworks.testnet, - addressType: EAddressType.p2wpkh, - electrumOptions: { - servers: servers[EAvailableNetworks.testnet], - net, - tls - } + + before(async function () { + this.timeout(testTimeout); + const res = await Wallet.create({ + mnemonic: TEST_MNEMONIC, + network: EAvailableNetworks.testnet, + addressType: EAddressType.p2wpkh, + electrumOptions: { + servers: servers[EAvailableNetworks.testnet], + net, + tls + } + }); + if (res.isErr()) throw res.error; + wallet = res.value; + await wallet.refreshWallet({}); }); - if (res.isErr()) throw res.error; - wallet = res.value; - await wallet.refreshWallet({}); -}); -describe('Wallet Library', async function () { - this.timeout(testTimeout); + after(async function () { + await wallet?.electrum?.disconnect(); + }); it('Should successfully create a wallet.', () => { expect(wallet).not.to.be.null;