FIX: better input address parsing

This commit is contained in:
Overtorment 2018-06-24 22:27:34 +01:00
parent 11078d9bf7
commit 5cb8bf04da
3 changed files with 43 additions and 1 deletions

View File

@ -115,7 +115,9 @@ export class LegacyWallet extends AbstractWallet {
this.getAddress() +
'?limit=2000&after=' +
maxHeight +
((useBlockcypherTokens && '&token=' + this.getRandomBlockcypherToken()) || ''),
((useBlockcypherTokens &&
'&token=' + this.getRandomBlockcypherToken()) ||
''),
);
json = response.body;
if (
@ -190,6 +192,18 @@ export class LegacyWallet extends AbstractWallet {
// how much came out
value = 0;
for (let inp of tx.inputs) {
if (!inp.addresses) {
console.log('inp.addresses empty');
console.log('got witness', inp.witness); // TODO
inp.addresses = [];
if (inp.witness && inp.witness[1]) {
let address = SegwitBech32Wallet.witnessToAddress(inp.witness[1]);
inp.addresses.push(address);
} else {
inp.addresses.push('???');
}
}
if (inp.addresses.indexOf(this.getAddress()) !== -1) {
// found our address in outs of this TX
value -= inp.output_value;

View File

@ -28,4 +28,16 @@ export class SegwitBech32Wallet extends LegacyWallet {
return this._address;
}
static witnessToAddress(witness) {
const pubKey = Buffer.from(witness, 'hex');
const pubKeyHash = bitcoin.crypto.hash160(pubKey);
const scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(
pubKeyHash,
);
return bitcoin.address.fromOutputScript(
scriptPubKey,
bitcoin.networks.bitcoin,
);
}
}

View File

@ -13,6 +13,22 @@ export class SegwitP2SHWallet extends LegacyWallet {
return 'SegWit (P2SH)';
}
static witnessToAddress(witness) {
const pubKey = Buffer.from(witness, 'hex');
const pubKeyHash = bitcoin.crypto.hash160(pubKey);
const redeemScript = bitcoin.script.witnessPubKeyHash.output.encode(
pubKeyHash,
);
const redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
const scriptPubkey = bitcoin.script.scriptHash.output.encode(
redeemScriptHash,
);
return bitcoin.address.fromOutputScript(
scriptPubkey,
bitcoin.networks.bitcoin,
);
}
getAddress() {
if (this._address) return this._address;
let address;