2020-03-09 19:51:34 +01:00
|
|
|
import { LegacyWallet, SegwitP2SHWallet, SegwitBech32Wallet } from '../../class';
|
2020-06-01 14:54:23 +02:00
|
|
|
const assert = require('assert');
|
2020-03-09 19:51:34 +01:00
|
|
|
global.net = require('net'); // needed by Electrum client. For RN it is proviced in shim.js
|
2020-03-26 17:37:11 +01:00
|
|
|
global.tls = require('tls'); // needed by Electrum client. For RN it is proviced in shim.js
|
2020-07-01 13:56:52 +02:00
|
|
|
const BlueElectrum = require('../../blue_modules/BlueElectrum'); // so it connects ASAP
|
2020-03-09 19:51:34 +01:00
|
|
|
|
|
|
|
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
// after all tests we close socket so the test suite can actually terminate
|
|
|
|
BlueElectrum.forceDisconnect();
|
|
|
|
});
|
|
|
|
|
|
|
|
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
|
|
|
|
await BlueElectrum.waitTillConnected();
|
|
|
|
});
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
describe('LegacyWallet', function () {
|
2020-03-09 19:51:34 +01:00
|
|
|
it('can serialize and unserialize correctly', () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const a = new LegacyWallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
a.setLabel('my1');
|
2020-06-01 14:54:23 +02:00
|
|
|
const key = JSON.stringify(a);
|
2020-03-09 19:51:34 +01:00
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
const b = LegacyWallet.fromJson(key);
|
2020-03-31 13:05:27 +02:00
|
|
|
assert.strictEqual(b.type, LegacyWallet.type);
|
2020-03-09 19:51:34 +01:00
|
|
|
assert.strictEqual(key, JSON.stringify(b));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can validate addresses', () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new LegacyWallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
assert.ok(w.isAddressValid('12eQ9m4sgAwTSQoNXkRABKhCXCsjm2jdVG'));
|
|
|
|
assert.ok(!w.isAddressValid('12eQ9m4sgAwTSQoNXkRABKhCXCsjm2j'));
|
|
|
|
assert.ok(w.isAddressValid('3BDsBDxDimYgNZzsqszNZobqQq3yeUoJf2'));
|
|
|
|
assert.ok(!w.isAddressValid('3BDsBDxDimYgNZzsqszNZobqQq3yeUo'));
|
|
|
|
assert.ok(!w.isAddressValid('12345'));
|
|
|
|
assert.ok(w.isAddressValid('bc1quuafy8htjjj263cvpj7md84magzmc8svmh8lrm'));
|
|
|
|
assert.ok(w.isAddressValid('BC1QH6TF004TY7Z7UN2V5NTU4MKF630545GVHS45U7'));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can fetch balance', async () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new LegacyWallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
w._address = '115fUy41sZkAG14CmdP1VbEKcNRZJWkUWG'; // hack internals
|
|
|
|
assert.ok(w.weOwnAddress('115fUy41sZkAG14CmdP1VbEKcNRZJWkUWG'));
|
|
|
|
assert.ok(!w.weOwnAddress('aaa'));
|
|
|
|
assert.ok(w.getBalance() === 0);
|
|
|
|
assert.ok(w.getUnconfirmedBalance() === 0);
|
|
|
|
assert.ok(w._lastBalanceFetch === 0);
|
|
|
|
await w.fetchBalance();
|
|
|
|
assert.ok(w.getBalance() === 18262000);
|
|
|
|
assert.ok(w.getUnconfirmedBalance() === 0);
|
|
|
|
assert.ok(w._lastBalanceFetch > 0);
|
|
|
|
});
|
|
|
|
|
2021-01-25 16:29:12 +01:00
|
|
|
it('can fetch TXs and derive UTXO from them', async () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new LegacyWallet();
|
2021-01-25 16:29:12 +01:00
|
|
|
w._address = '3GCvDBAktgQQtsbN6x5DYiQCMmgZ9Yk8BK';
|
2020-03-09 19:51:34 +01:00
|
|
|
await w.fetchTransactions();
|
2021-01-25 16:29:12 +01:00
|
|
|
assert.strictEqual(w.getTransactions().length, 1);
|
2020-03-09 19:51:34 +01:00
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
for (const tx of w.getTransactions()) {
|
2020-03-09 19:51:34 +01:00
|
|
|
assert.ok(tx.hash);
|
|
|
|
assert.ok(tx.value);
|
|
|
|
assert.ok(tx.received);
|
|
|
|
assert.ok(tx.confirmations > 1);
|
|
|
|
}
|
2020-05-20 20:44:05 +02:00
|
|
|
|
2021-01-25 16:29:12 +01:00
|
|
|
assert.ok(w.weOwnTransaction('b2ac59bc282083498d1e87805d89bef9d3f3bc216c1d2c4dfaa2e2911b547100'));
|
2020-08-10 16:17:50 +02:00
|
|
|
assert.ok(!w.weOwnTransaction('825c12f277d1f84911ac15ad1f41a3de28e9d906868a930b0a7bca61b17c8881'));
|
2021-01-25 16:29:12 +01:00
|
|
|
|
|
|
|
assert.strictEqual(w.getUtxo().length, 1);
|
|
|
|
|
|
|
|
for (const tx of w.getUtxo()) {
|
|
|
|
assert.strictEqual(tx.txid, 'b2ac59bc282083498d1e87805d89bef9d3f3bc216c1d2c4dfaa2e2911b547100');
|
|
|
|
assert.strictEqual(tx.vout, 0);
|
|
|
|
assert.strictEqual(tx.address, '3GCvDBAktgQQtsbN6x5DYiQCMmgZ9Yk8BK');
|
|
|
|
assert.strictEqual(tx.value, 51432);
|
|
|
|
assert.strictEqual(tx.value, tx.amount);
|
|
|
|
assert.ok(tx.confirmations > 0);
|
|
|
|
}
|
2020-05-18 21:20:50 +02:00
|
|
|
});
|
2020-03-09 19:51:34 +01:00
|
|
|
|
2020-05-19 22:45:54 +02:00
|
|
|
it.each([
|
2020-08-10 16:17:50 +02:00
|
|
|
// Transaction with missing address output https://www.blockchain.com/btc/tx/d45818ae11a584357f7b74da26012d2becf4ef064db015a45bdfcd9cb438929d
|
2020-05-19 22:45:54 +02:00
|
|
|
['addresses for vout missing', '1PVfrmbn1vSMoFZB2Ga7nDuXLFDyJZHrHK'],
|
2020-06-15 13:55:31 +02:00
|
|
|
// ['txdatas were coming back null from BlueElectrum because of high batchsize', '34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo'],
|
|
|
|
// skipped because its slow and flaky if being run in pack with other electrum tests. uncomment and run single
|
|
|
|
// if you need to debug huge electrum batches
|
2020-05-19 22:45:54 +02:00
|
|
|
])(
|
|
|
|
'can fetch TXs when %s',
|
|
|
|
async (useCase, address) => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new LegacyWallet();
|
2020-05-19 22:45:54 +02:00
|
|
|
w._address = address;
|
|
|
|
await w.fetchTransactions();
|
|
|
|
|
|
|
|
assert.ok(w.getTransactions().length > 0);
|
2020-06-01 14:54:23 +02:00
|
|
|
for (const tx of w.getTransactions()) {
|
2020-05-19 22:45:54 +02:00
|
|
|
assert.ok(tx.hash);
|
|
|
|
assert.ok(tx.value);
|
|
|
|
assert.ok(tx.received);
|
|
|
|
assert.ok(tx.confirmations > 1);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
240000,
|
|
|
|
);
|
2020-03-09 19:51:34 +01:00
|
|
|
|
|
|
|
it('can fetch UTXO', async () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new LegacyWallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
w._address = '12c6DSiU4Rq3P4ZxziKxzrL5LmMBrzjrJX';
|
|
|
|
await w.fetchUtxo();
|
|
|
|
assert.ok(w.utxo.length > 0, 'unexpected empty UTXO');
|
|
|
|
assert.ok(w.getUtxo().length > 0, 'unexpected empty UTXO');
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
assert.ok(w.getUtxo()[0].value);
|
|
|
|
assert.ok(w.getUtxo()[0].vout === 1, JSON.stringify(w.getUtxo()[0]));
|
|
|
|
assert.ok(w.getUtxo()[0].txid);
|
|
|
|
assert.ok(w.getUtxo()[0].confirmations);
|
2020-03-09 19:51:34 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
describe('SegwitP2SHWallet', function () {
|
2020-03-09 19:51:34 +01:00
|
|
|
it('can generate segwit P2SH address from WIF', async () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const l = new SegwitP2SHWallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
l.setSecret('Kxr9tQED9H44gCmp6HAdmemAzU3n84H3dGkuWTKvE23JgHMW8gct');
|
|
|
|
assert.ok(l.getAddress() === '34AgLJhwXrvmkZS1o5TrcdeevMt22Nar53', 'expected ' + l.getAddress());
|
|
|
|
assert.ok(l.getAddress() === (await l.getAddressAsync()));
|
|
|
|
assert.ok(l.weOwnAddress('34AgLJhwXrvmkZS1o5TrcdeevMt22Nar53'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
describe('SegwitBech32Wallet', function () {
|
2020-03-09 19:51:34 +01:00
|
|
|
it('can fetch balance', async () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new SegwitBech32Wallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
w._address = 'bc1qn887fmetaytw4vj68vsh529ft408q8j9x3dndc';
|
|
|
|
assert.ok(w.weOwnAddress('bc1qn887fmetaytw4vj68vsh529ft408q8j9x3dndc'));
|
|
|
|
await w.fetchBalance();
|
|
|
|
assert.strictEqual(w.getBalance(), 100000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can fetch UTXO', async () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new SegwitBech32Wallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
w._address = 'bc1qn887fmetaytw4vj68vsh529ft408q8j9x3dndc';
|
|
|
|
await w.fetchUtxo();
|
2020-03-31 18:46:31 +02:00
|
|
|
const l1 = w.getUtxo().length;
|
2020-03-09 19:51:34 +01:00
|
|
|
assert.ok(w.getUtxo().length > 0, 'unexpected empty UTXO');
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
assert.ok(w.getUtxo()[0].value);
|
|
|
|
assert.ok(w.getUtxo()[0].vout === 0);
|
|
|
|
assert.ok(w.getUtxo()[0].txid);
|
|
|
|
assert.ok(w.getUtxo()[0].confirmations, JSON.stringify(w.getUtxo()[0], null, 2));
|
2020-03-31 18:46:31 +02:00
|
|
|
// double fetch shouldnt duplicate UTXOs:
|
|
|
|
await w.fetchUtxo();
|
|
|
|
const l2 = w.getUtxo().length;
|
|
|
|
assert.strictEqual(l1, l2);
|
2020-03-09 19:51:34 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can fetch TXs', async () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new LegacyWallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
w._address = 'bc1quhnve8q4tk3unhmjts7ymxv8cd6w9xv8wy29uv';
|
|
|
|
await w.fetchTransactions();
|
|
|
|
assert.strictEqual(w.getTransactions().length, 2);
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
for (const tx of w.getTransactions()) {
|
2020-03-09 19:51:34 +01:00
|
|
|
assert.ok(tx.hash);
|
|
|
|
assert.ok(tx.value);
|
|
|
|
assert.ok(tx.received);
|
|
|
|
assert.ok(tx.confirmations > 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(w.getTransactions()[0].value, -892111);
|
|
|
|
assert.strictEqual(w.getTransactions()[1].value, 892111);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can fetch TXs', async () => {
|
2020-06-01 14:54:23 +02:00
|
|
|
const w = new LegacyWallet();
|
2020-03-09 19:51:34 +01:00
|
|
|
w._address = 'bc1qn887fmetaytw4vj68vsh529ft408q8j9x3dndc';
|
|
|
|
assert.ok(w.weOwnAddress('bc1qn887fmetaytw4vj68vsh529ft408q8j9x3dndc'));
|
|
|
|
await w.fetchTransactions();
|
|
|
|
assert.strictEqual(w.getTransactions().length, 1);
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
for (const tx of w.getTransactions()) {
|
2020-03-09 19:51:34 +01:00
|
|
|
assert.ok(tx.hash);
|
|
|
|
assert.strictEqual(tx.value, 100000);
|
|
|
|
assert.ok(tx.received);
|
|
|
|
assert.ok(tx.confirmations > 1);
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:54:23 +02:00
|
|
|
const tx0 = w.getTransactions()[0];
|
|
|
|
assert.ok(tx0.inputs);
|
|
|
|
assert.ok(tx0.inputs.length === 1);
|
|
|
|
assert.ok(tx0.outputs);
|
|
|
|
assert.ok(tx0.outputs.length === 3);
|
2020-08-10 16:17:50 +02:00
|
|
|
|
|
|
|
assert.ok(w.weOwnTransaction('49944e90fe917952e36b1967cdbc1139e60c89b4800b91258bf2345a77a8b888'));
|
|
|
|
assert.ok(!w.weOwnTransaction('825c12f277d1f84911ac15ad1f41a3de28e9d906868a930b0a7bca61b17c8881'));
|
2020-03-09 19:51:34 +01:00
|
|
|
});
|
|
|
|
});
|