REF: bip84

This commit is contained in:
Overtorment 2019-06-08 13:33:35 +01:00
parent f9eabc10c1
commit 8bea87e145
3 changed files with 13 additions and 18 deletions

View File

@ -1,5 +1,4 @@
import AsyncStorage from '@react-native-community/async-storage';
import { SegwitBech32Wallet } from './class';
const ElectrumClient = require('electrum-client');
let bitcoin = require('bitcoinjs-lib');
let reverse = require('buffer-reverse');
@ -130,24 +129,14 @@ async function getTransactionsFullByAddress(address) {
let full = await mainClient.blockchainTransaction_get(tx.tx_hash, true);
full.address = address;
for (let input of full.vin) {
try {
if (!input.txinwitness) {
// no witness - p2pkh address
let chunksIn = bitcoin.script.decompile(Buffer.from(input.scriptSig.hex, 'hex'));
let hash = bitcoin.crypto.hash160(chunksIn[chunksIn.length - 1]);
input.address = bitcoin.address.toBase58Check(hash, bitcoin.networks.bitcoin.pubKeyHash);
} else {
input.address = SegwitBech32Wallet.witnessToAddress(input.txinwitness[1]);
}
} catch (Error) {
console.warn(Error);
input.addresses = 'unknown';
}
input.addresses = [input.address];
// now we need to fetch previous TX where this VIN became an output, so we can see its amount
let prevTxForVin = await mainClient.blockchainTransaction_get(input.txid, true);
if (prevTxForVin && prevTxForVin.vout && prevTxForVin.vout[input.vout]) {
input.value = prevTxForVin.vout[input.vout].value;
// also, we extract destination address from prev output:
if (prevTxForVin.vout[input.vout].scriptPubKey && prevTxForVin.vout[input.vout].scriptPubKey.addresses) {
input.addresses = prevTxForVin.vout[input.vout].scriptPubKey.addresses;
}
}
}

View File

@ -227,10 +227,15 @@ describe('Bech32 Segwit HD (BIP84)', () => {
for (let tx of hd.getTransactions()) {
if (tx.hash === 'e9ef58baf4cff3ad55913a360c2fa1fd124309c59dcd720cdb172ce46582097b') {
assert.strictEqual(tx.value, -129545);
assert.strictEqual(tx.inputs[0].addresses[0], 'bc1qffcl35r05wyf06meu3dalfevawx559n0ufrxcw');
assert.strictEqual(tx.inputs[1].addresses[0], 'bc1qtvh8mjcfdg9224nx4wu3sw7fmmtmy2k3jhdeul');
assert.strictEqual(tx.inputs[2].addresses[0], 'bc1qhe03zgvq4fmfw8l2qq2zu4dxyhgyukcz6k2a5w');
txFound++;
}
if (tx.hash === 'e112771fd43962abfe4e4623bf788d6d95ff1bd0f9b56a6a41fb9ed4dacc75f1') {
assert.strictEqual(tx.value, 1000000);
assert.strictEqual(tx.inputs[0].addresses[0], '3NLnALo49CFEF4tCRhCvz45ySSfz3UktZC');
assert.strictEqual(tx.inputs[1].addresses[0], '3NLnALo49CFEF4tCRhCvz45ySSfz3UktZC');
txFound++;
}
if (tx.hash === 'c94bdec21c72d3441245caa164b00315b131f6b72513369f4be1b00b9fb99cc5') {
@ -238,10 +243,11 @@ describe('Bech32 Segwit HD (BIP84)', () => {
txFound++;
}
if (tx.hash === '51fc225ddf24f7e124f034637f46442645ca7ea2c442b28124d4bcdd04e30195') {
// TODO: one of inputs should be segwit-p2sh
assert.strictEqual(tx.inputs[0].addresses[0], '3NLnALo49CFEF4tCRhCvz45ySSfz3UktZC');
txFound++;
}
}
assert.strictEqual(txFound, 3);
assert.strictEqual(txFound, 4);
await hd.fetchUtxo();
let changeAddress = await hd.getChangeAddressAsync();

View File

@ -253,7 +253,7 @@ export class HDSegwitBech32Wallet extends AbstractHDWallet {
for (let vin of tx.inputs) {
// if input (spending) goes from our address - we are loosing!
if (vin.address && this.weOwnAddress(vin.address)) {
if ((vin.address && this.weOwnAddress(vin.address)) || (vin.addresses && vin.addresses[0] && this.weOwnAddress(vin.addresses[0]))) {
tx.value -= new BigNumber(vin.value).multipliedBy(100000000).toNumber();
}
}