mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2024-11-19 18:00:17 +01:00
REF: old hd wallets compatibility with new bitcoinjs
This commit is contained in:
parent
f9667c3722
commit
5d56857496
@ -29,11 +29,23 @@ export class SegwitBech32Wallet extends LegacyWallet {
|
||||
}).address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts script pub key to bech32 address if it can. Returns FALSE if it cant.
|
||||
*
|
||||
* @param scriptPubKey
|
||||
* @returns {boolean|string} Either bech32 address or false
|
||||
*/
|
||||
static scriptPubKeyToAddress(scriptPubKey) {
|
||||
const scriptPubKey2 = Buffer.from(scriptPubKey, 'hex');
|
||||
return bitcoin.payments.p2wpkh({
|
||||
let ret;
|
||||
try {
|
||||
ret = bitcoin.payments.p2wpkh({
|
||||
output: scriptPubKey2,
|
||||
network: bitcoin.networks.bitcoin,
|
||||
}).address;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -31,6 +31,26 @@ export class SegwitP2SHWallet extends LegacyWallet {
|
||||
return pubkeyToP2shSegwitAddress(pubKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts script pub key to p2sh address if it can. Returns FALSE if it cant.
|
||||
*
|
||||
* @param scriptPubKey
|
||||
* @returns {boolean|string} Either p2sh address or false
|
||||
*/
|
||||
static scriptPubKeyToAddress(scriptPubKey) {
|
||||
const scriptPubKey2 = Buffer.from(scriptPubKey, 'hex');
|
||||
let ret;
|
||||
try {
|
||||
ret = bitcoin.payments.p2sh({
|
||||
output: scriptPubKey2,
|
||||
network: bitcoin.networks.bitcoin,
|
||||
}).address;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
getAddress() {
|
||||
if (this._address) return this._address;
|
||||
let address;
|
||||
|
@ -29,6 +29,9 @@ it('can convert witness to address', () => {
|
||||
let address = SegwitP2SHWallet.witnessToAddress('035c618df829af694cb99e664ce1b34f80ad2c3b49bcd0d9c0b1836c66b2d25fd8');
|
||||
assert.strictEqual(address, '34ZVGb3gT8xMLT6fpqC6dNVqJtJmvdjbD7');
|
||||
|
||||
address = SegwitP2SHWallet.scriptPubKeyToAddress('a914e286d58e53f9247a4710e51232cce0686f16873c87');
|
||||
assert.strictEqual(address, '3NLnALo49CFEF4tCRhCvz45ySSfz3UktZC');
|
||||
|
||||
address = SegwitBech32Wallet.witnessToAddress('035c618df829af694cb99e664ce1b34f80ad2c3b49bcd0d9c0b1836c66b2d25fd8');
|
||||
assert.strictEqual(address, 'bc1quhnve8q4tk3unhmjts7ymxv8cd6w9xv8wy29uv');
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/* global it, describe, jasmine, afterAll, beforeAll */
|
||||
import { HDSegwitBech32Wallet, HDSegwitBech32Transaction, SegwitBech32Wallet } from '../../class';
|
||||
import { HDSegwitBech32Wallet, SegwitP2SHWallet, HDSegwitBech32Transaction, SegwitBech32Wallet } from '../../class';
|
||||
const bitcoin = require('bitcoinjs-lib');
|
||||
global.crypto = require('crypto'); // shall be used by tests under nodejs CLI, but not in RN environment
|
||||
let assert = require('assert');
|
||||
@ -148,7 +148,7 @@ describe('HDSegwitBech32Transaction', () => {
|
||||
let createdTx = bitcoin.Transaction.fromHex(tx.toHex());
|
||||
assert.strictEqual(createdTx.ins.length, 2);
|
||||
assert.strictEqual(createdTx.outs.length, 2);
|
||||
let addr0 = SegwitBech32Wallet.scriptPubKeyToAddress(createdTx.outs[0].script);
|
||||
let addr0 = SegwitP2SHWallet.scriptPubKeyToAddress(createdTx.outs[0].script);
|
||||
assert.ok(!hd.weOwnAddress(addr0));
|
||||
assert.strictEqual(addr0, '3NLnALo49CFEF4tCRhCvz45ySSfz3UktZC'); // dest address
|
||||
let addr1 = SegwitBech32Wallet.scriptPubKeyToAddress(createdTx.outs[1].script);
|
||||
|
Loading…
Reference in New Issue
Block a user