BlueWallet/tests/integration/hd-legacy-breadwallet.test.ts

66 lines
2.1 KiB
TypeScript
Raw Normal View History

import assert from 'assert';
2020-11-27 16:42:12 +03:00
import * as bitcoin from 'bitcoinjs-lib';
import * as BlueElectrum from '../../blue_modules/BlueElectrum';
2024-05-20 10:54:13 +01:00
import { HDLegacyBreadwalletWallet } from '../../class';
2024-02-25 13:13:45 +00:00
import { AbstractHDElectrumWallet } from '../../class/wallets/abstract-hd-electrum-wallet';
2020-11-27 16:42:12 +03:00
jest.setTimeout(300 * 1000);
2024-02-25 13:13:45 +00:00
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
2020-11-27 16:42:12 +03:00
afterAll(async () => {
// after all tests we close socket so the test suite can actually terminate
BlueElectrum.forceDisconnect();
await sleep(20);
});
beforeAll(async () => {
// awaiting for Electrum to be connected. For RN Electrum would naturally connect
// while app starts up, but for tests we need to wait for it
2021-08-10 21:53:02 +02:00
await BlueElectrum.connectMain();
2020-11-27 16:42:12 +03:00
});
it('Legacy HD Breadwallet can fetch utxo, balance, and create transaction', async () => {
2020-11-27 16:42:12 +03:00
if (!process.env.HD_MNEMONIC_BREAD) {
console.error('process.env.HD_MNEMONIC_BREAD not set, skipped');
return;
}
const wallet = new HDLegacyBreadwalletWallet();
wallet.setSecret(process.env.HD_MNEMONIC_BREAD);
await wallet.fetchBalance();
// m/0'/0/1 1K9ofAnenRn1aR9TMMTreiin9ddjKWbS7z x 0.0001
// m/0'/0/2 bc1qh0vtrnjn7zs99j4n6xaadde95ctnnvegh9l2jn x 0.00032084
// m/0'/1/0 1A9Sc4opR6c7Ui6NazECiGmsmnUPh2WeHJ x 0.00016378 BTC
// m/0'/1/1 bc1qksn08tz44fvnnrpgrrexvs9526t6jg3xnj9tpc x 0.00012422
// 0.0001 + 0.00016378 + 0.00012422 + 0.00032084 = 0.00070884
assert.strictEqual(wallet.getBalance(), 70884);
// try to create a tx
await wallet.fetchUtxo();
for (const utxo of wallet.getUtxo()) {
assert.ok(utxo.txhex);
assert.ok(typeof utxo.vout !== 'undefined');
assert.ok(utxo.txid);
assert.ok(utxo.confirmations);
assert.ok(utxo.value);
}
2020-11-27 16:42:12 +03:00
const { tx } = wallet.createTransaction(
wallet.getUtxo(),
[{ address: 'bc1q47efz9aav8g4mnnz9r6ql4pf48phy3g509p7gx' }],
1,
'bc1qk9hvkxqsqmps6ex3qawr79rvtg8es4ecjfu5v0',
2024-02-25 13:13:45 +00:00
AbstractHDElectrumWallet.defaultRBFSequence,
false,
0,
2020-11-27 16:42:12 +03:00
);
2024-02-25 13:13:45 +00:00
assert.ok(tx);
2020-11-27 16:42:12 +03:00
const transaction = bitcoin.Transaction.fromHex(tx.toHex());
assert.ok(transaction.ins.length === 4);
assert.strictEqual(transaction.outs.length, 1);
});