From 1e5dae56af27a047bc35a165aba813abb69563a0 Mon Sep 17 00:00:00 2001 From: Rosco Kalis Date: Fri, 14 Aug 2020 12:15:26 +0200 Subject: [PATCH 1/2] 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', () => { From 443fb233aec543b63ee4faee1361e407d5891179 Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Mon, 17 Aug 2020 08:08:49 -0700 Subject: [PATCH 2/2] fix(util): Fixed failing integration test --- package.json | 2 +- test/v3/util.js | 89 +++++++++++++++++-------------------------------- 2 files changed, 32 insertions(+), 59 deletions(-) diff --git a/package.json b/package.json index ccdb00d..cc19cff 100644 --- a/package.json +++ b/package.json @@ -17,7 +17,7 @@ "test": "npm run lint && npm run test-v3", "lint": "standard --env mocha --fix", "test-v3": "export NETWORK=mainnet && nyc --reporter=text mocha --timeout 60000 test/v3/", - "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/a01-electrumx.js", + "test:temp": "export NETWORK=mainnet && mocha --timeout 25000 test/v3/util.js", "test:integration": "mocha test/v3/integration", "test:integration:slpdb": "mocha --timeout 25000 test/v3/integration/slp*.js", "coverage": "nyc report --reporter=text-lcov | coveralls", diff --git a/test/v3/util.js b/test/v3/util.js index 4701e0d..76dbbe7 100644 --- a/test/v3/util.js +++ b/test/v3/util.js @@ -135,11 +135,12 @@ describe('#Util', () => { // Mock the RPC call for unit tests. if (process.env.TEST === 'unit') { nock(`${process.env.RPC_BASEURL}`) - .post(uri => uri.includes('/')) + .post((uri) => uri.includes('/')) .reply(200, { result: mockData.mockAddress }) } - req.params.address = 'bitcoincash:qpujxqra3jmdlzzapwmmt7uspr7q0c9ff5hzljcrnd' + req.params.address = + 'bitcoincash:qpujxqra3jmdlzzapwmmt7uspr7q0c9ff5hzljcrnd' const result = await validateAddress(req, res) // console.log(`result: ${util.inspect(result)}`) @@ -255,7 +256,7 @@ describe('#Util', () => { // Mock the RPC call for unit tests. if (process.env.TEST === 'unit') { nock(`${process.env.RPC_BASEURL}`) - .post(uri => uri.includes('/')) + .post((uri) => uri.includes('/')) .reply(200, { result: mockData.mockAddress }) } @@ -281,7 +282,7 @@ describe('#Util', () => { // Mock the RPC call for unit tests. if (process.env.TEST === 'unit') { nock(`${process.env.RPC_BASEURL}`) - .post(uri => uri.includes('/')) + .post((uri) => uri.includes('/')) .times(2) .reply(200, { result: mockData.mockAddress }) } @@ -356,17 +357,11 @@ describe('#Util', () => { // Mock the RPC call for unit tests. sandbox - .stub( - utilRouteInst.blockbook, - 'balanceFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'balanceFromBlockbook') .resolves(mockData.mockBalance) sandbox - .stub( - utilRouteInst.blockbook, - 'utxosFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'utxosFromBlockbook') .resolves(mockData.mockUtxos) sandbox @@ -389,51 +384,41 @@ describe('#Util', () => { assert.equal(result, 'test-txid') }) - } - it('should return balance if balance-only is true', async () => { - // Mock the RPC call for unit tests. - if (process.env.TEST === 'unit') { + it('should return balance if balance-only is true', async () => { + // Mock the RPC call for unit tests. + sandbox - .stub( - utilRouteInst.blockbook, - 'balanceFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'balanceFromBlockbook') .resolves(mockData.mockBalance) - } - // Mock sendRawTransaction() so that the hex does not actually get broadcast - // to the network. - sandbox - .stub(utilRouteInst.bchjs.RawTransactions, 'sendRawTransaction') - .resolves('test-txid') + // Mock sendRawTransaction() so that the hex does not actually get broadcast + // to the network. + sandbox + .stub(utilRouteInst.bchjs.RawTransactions, 'sendRawTransaction') + .resolves('test-txid') - req.body = { - wif: 'L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt', - balanceOnly: true - } + req.body = { + wif: 'L5GEFg1tETLWBugmhSo9Zc4ms968qVmfmTroDxsJ982AiudAQGyt', + balanceOnly: true + } - const result = await utilRouteInst.sweepWif(req, res) - // console.log(`result: ${JSON.stringify(result, null, 2)}`) + const result = await utilRouteInst.sweepWif(req, res) + // console.log(`result: ${JSON.stringify(result, null, 2)}`) - assert.isNumber(result) - }) + assert.isNumber(result) + }) + } // Unit tests only if (process.env.TEST === 'unit') { it('should generate transaction for valid BCH-only sweep', async () => { sandbox - .stub( - utilRouteInst.blockbook, - 'balanceFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'balanceFromBlockbook') .resolves(mockData.mockBalance) sandbox - .stub( - utilRouteInst.blockbook, - 'utxosFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'utxosFromBlockbook') .resolves(mockData.mockUtxos) // Force token utxo to appear as regular BCH utxo. @@ -460,17 +445,11 @@ describe('#Util', () => { it('should throw 422 error if no non-token UTXOs', async () => { sandbox - .stub( - utilRouteInst.blockbook, - 'balanceFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'balanceFromBlockbook') .resolves(mockData.mockBalance) sandbox - .stub( - utilRouteInst.blockbook, - 'utxosFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'utxosFromBlockbook') .resolves(mockData.mockUtxos) // Force token utxo to appear as regular BCH utxo. @@ -502,17 +481,11 @@ describe('#Util', () => { it('should detect and throw error for multiple token classes', async () => { sandbox - .stub( - utilRouteInst.blockbook, - 'balanceFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'balanceFromBlockbook') .resolves(mockData.mockBalance) sandbox - .stub( - utilRouteInst.blockbook, - 'utxosFromBlockbook' - ) + .stub(utilRouteInst.blockbook, 'utxosFromBlockbook') .resolves(mockData.mockThreeUtxos) // Force token utxo to appear as regular BCH utxo.