diff --git a/class/segwit-bech-wallet.js b/class/segwit-bech-wallet.js index 9a56ec725..a2db477bd 100644 --- a/class/segwit-bech-wallet.js +++ b/class/segwit-bech-wallet.js @@ -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({ - output: scriptPubKey2, - network: bitcoin.networks.bitcoin, - }).address; + let ret; + try { + ret = bitcoin.payments.p2wpkh({ + output: scriptPubKey2, + network: bitcoin.networks.bitcoin, + }).address; + } catch (_) { + return false; + } + return ret; } } diff --git a/class/segwit-p2sh-wallet.js b/class/segwit-p2sh-wallet.js index b61c81dd9..4469c6f48 100644 --- a/class/segwit-p2sh-wallet.js +++ b/class/segwit-p2sh-wallet.js @@ -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; diff --git a/tests/integration/HDWallet.test.js b/tests/integration/HDWallet.test.js index fee7d06d1..6de2c02a0 100644 --- a/tests/integration/HDWallet.test.js +++ b/tests/integration/HDWallet.test.js @@ -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'); diff --git a/tests/integration/hd-segwit-bech32-transaction.test.js b/tests/integration/hd-segwit-bech32-transaction.test.js index 440c8fa68..148c43588 100644 --- a/tests/integration/hd-segwit-bech32-transaction.test.js +++ b/tests/integration/hd-segwit-bech32-transaction.test.js @@ -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);