From 1e5dae56af27a047bc35a165aba813abb69563a0 Mon Sep 17 00:00:00 2001 From: Rosco Kalis Date: Fri, 14 Aug 2020 12:15:26 +0200 Subject: [PATCH] Enable P2SH addresses to be used with the ElectrumX endpoints --- src/routes/v3/electrumx.js | 4 +++- test/v3/a01-electrumx.js | 13 ++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/routes/v3/electrumx.js b/src/routes/v3/electrumx.js index 025766c..c5444cd 100644 --- a/src/routes/v3/electrumx.js +++ b/src/routes/v3/electrumx.js @@ -911,7 +911,9 @@ class Electrum { const address = _this.bitcore.Address.fromString(addrStr) // console.log(`address: ${address}`) - const script = _this.bitcore.Script.buildPublicKeyHashOut(address) + const script = address.isPayToPublicKeyHash() + ? _this.bitcore.Script.buildPublicKeyHashOut(address) + : _this.bitcore.Script.buildScriptHashOut(address) // console.log(`script: ${script}`) const scripthash = _this.bitcore.crypto.Hash.sha256(script.toBuffer()) diff --git a/test/v3/a01-electrumx.js b/test/v3/a01-electrumx.js index c93efc8..32515d5 100644 --- a/test/v3/a01-electrumx.js +++ b/test/v3/a01-electrumx.js @@ -94,7 +94,7 @@ describe('#ElectrumX Router', () => { }) describe('#addressToScripthash', () => { - it('should accurately return a scripthash', () => { + it('should accurately return a scripthash for a P2PKH address', () => { const addr = 'bitcoincash:qpr270a5sxphltdmggtj07v4nskn9gmg9yx4m5h7s4' const scripthash = electrumxRoute.addressToScripthash(addr) @@ -104,6 +104,17 @@ describe('#ElectrumX Router', () => { assert.equal(scripthash, expectedOutput) }) + + it('should accurately return a scripthash for a P2SH address', () => { + const addr = 'bitcoincash:pz0z7u9p96h2p6hfychxdrmwgdlzpk5luc5yks2wxq' + + const scripthash = electrumxRoute.addressToScripthash(addr) + + const expectedOutput = + '8bc2235c8e7d5634d9ec429fc0171f2c58e728d4f1e2fb7e440e313133cfa4f0' + + assert.equal(scripthash, expectedOutput) + }) }) describe('#_utxosFromElectrumx', () => {