mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-22 06:52:41 +01:00
WIP: fetch utxo for HD wallet
This commit is contained in:
parent
a63a9fc7a7
commit
ccdfe31139
2 changed files with 63 additions and 0 deletions
|
@ -69,6 +69,21 @@ it('can generate Segwit HD (BIP49)', async () => {
|
|||
assert.ok(hd2.validateMnemonic());
|
||||
});
|
||||
|
||||
it('Segwit HD (BIP49) can fetch UTXO', async function() {
|
||||
let hd = new HDSegwitP2SHWallet();
|
||||
hd.usedAddresses = ['1Ez69SnzzmePmZX3WpEzMKTrcBF2gpNQ55', '1BiTCHeYzJNMxBLFCMkwYXNdFEdPJP53ZV']; // hacking internals
|
||||
await hd.fetchUtxo();
|
||||
assert.equal(hd.utxo.length, 8);
|
||||
assert.ok(hd.utxo[0].confirmations);
|
||||
assert.ok(hd.utxo[0].txid);
|
||||
assert.ok(hd.utxo[0].vout);
|
||||
assert.ok(hd.utxo[0].amount);
|
||||
assert.ok(
|
||||
hd.utxo[0].address &&
|
||||
(hd.utxo[0].address === '1Ez69SnzzmePmZX3WpEzMKTrcBF2gpNQ55' || hd.utxo[0].address === '1BiTCHeYzJNMxBLFCMkwYXNdFEdPJP53ZV'),
|
||||
);
|
||||
});
|
||||
|
||||
it('can work with malformed mnemonic', () => {
|
||||
let mnemonic =
|
||||
'honey risk juice trip orient galaxy win situate shoot anchor bounce remind horse traffic exotic since escape mimic ramp skin judge owner topple erode';
|
||||
|
|
|
@ -268,6 +268,54 @@ export class HDSegwitP2SHWallet extends AbstractHDWallet {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
async fetchUtxo() {
|
||||
const api = new Frisbee({
|
||||
baseURI: 'https://blockchain.info',
|
||||
});
|
||||
|
||||
// unspent?active=$address
|
||||
|
||||
if (this.usedAddresses.length === 0) {
|
||||
// just for any case, refresh balance (it refreshes internal `this.usedAddresses`)
|
||||
await this.fetchBalance();
|
||||
}
|
||||
|
||||
let addresses = this.usedAddresses.join('|');
|
||||
addresses += '|' + this._getExternalAddressByIndex(this.next_free_address_index);
|
||||
addresses += '|' + this._getInternalAddressByIndex(this.next_free_change_address_index);
|
||||
|
||||
let utxos = [];
|
||||
|
||||
let response;
|
||||
try {
|
||||
response = await api.get('/unspent?active=' + addresses + '&limit=1000');
|
||||
// this endpoint does not support offset of some kind o_O
|
||||
// so doing only one call
|
||||
let json = response.body;
|
||||
if (typeof json === 'undefined' || typeof json.unspent_outputs === 'undefined') {
|
||||
throw new Error('Could not fetch UTXO from API' + response.err);
|
||||
}
|
||||
|
||||
for (let unspent of json.unspent_outputs) {
|
||||
// a lil transform for signer module
|
||||
unspent.txid = unspent.tx_hash;
|
||||
unspent.vout = unspent.tx_output_n;
|
||||
unspent.amount = unspent.value;
|
||||
|
||||
let chunksIn = bitcoin.script.decompile(Buffer.from(unspent.script, 'hex'));
|
||||
unspent.address = bitcoin.address.fromOutputScript(chunksIn);
|
||||
utxos.push(unspent);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn(err);
|
||||
}
|
||||
|
||||
this.utxo = utxos;
|
||||
}
|
||||
|
||||
weOwnAddress(addr) {
|
||||
let hashmap = {};
|
||||
for (let a of this.usedAddresses) {
|
||||
|
|
Loading…
Add table
Reference in a new issue