From 26a2000b0177fd2668b7d82e5aa52829cf2bfdf6 Mon Sep 17 00:00:00 2001 From: Mark Tyneway Date: Fri, 15 Mar 2019 22:39:05 -0700 Subject: [PATCH] node rpc: update validateaddress updates validateaddress to match the changes in bitcoind https://github.com/bitcoin/bitcoin/pull/10583. removes the fields ismine and iswatchonly, those are things that a wallet would know, not a node. adds segwit related fields, witness_version and witness_program. test changes to the node rpc method validateaddress with p2pkh, p2sh, p2wpkh and p2wsh addresses --- lib/node/rpc.js | 15 +++++-- test/http-test.js | 104 +++++++++++++++++++++++++++++++++++++++++++++- test/node-test.js | 4 +- 3 files changed, 116 insertions(+), 7 deletions(-) diff --git a/lib/node/rpc.js b/lib/node/rpc.js index d86fb21d..d9c08827 100644 --- a/lib/node/rpc.js +++ b/lib/node/rpc.js @@ -2073,14 +2073,23 @@ class RPC extends RPCBase { } const script = Script.fromAddress(addr); + const isWitness = addr.isProgram(); + const isScript = script.isScripthash() || script.isWitnessScripthash(); - return { + const result = { isvalid: true, address: addr.toString(this.network), scriptPubKey: script.toJSON(), - ismine: false, - iswatchonly: false + isscript: isScript, + iswitness: isWitness }; + + if (isWitness) { + result.witness_version = addr.version; + result.witness_program = addr.hash.toString('hex'); + } + + return result; } async verifyMessage(args, help) { diff --git a/test/http-test.js b/test/http-test.js index a7a8214d..f464950a 100644 --- a/test/http-test.js +++ b/test/http-test.js @@ -267,8 +267,108 @@ describe('HTTP', function() { isvalid: true, address: addr.toString(node.network), scriptPubKey: Script.fromAddress(addr).toRaw().toString('hex'), - ismine: false, - iswatchonly: false + iswitness: false, + isscript: false + }); + }); + + it('should not validate invalid address', async () => { + // send an address for the wrong network + const json = await nclient.execute('validateaddress', [ + addr.toString('main') + ]); + assert.deepStrictEqual(json, { + isvalid: false + }); + }); + + it('should validate a p2wpkh address', async () => { + const info = await wallet.createAccount('foo3', { + witness: true + }); + const json = await nclient.execute('validateaddress', [ + info.receiveAddress + ]); + const addr = Address.fromString(info.receiveAddress); + const script = Script.fromAddress(addr); + + assert.deepStrictEqual(json, { + isvalid: true, + iswitness: true, + address: info.receiveAddress, + isscript: addr.isScripthash(), + scriptPubKey: script.toJSON(), + witness_version: addr.version, + witness_program: addr.hash.toString('hex') + }); + }); + + it('should validate a p2sh address', async () => { + await wallet.createAccount('foo4'); + + const pubkeys = []; + for (let i = 0; i < 2; i++) { + const result = await wallet.createAddress('foo4', 'default'); + pubkeys.push(Buffer.from(result.publicKey, 'hex')); + } + + const script = Script.fromMultisig(2, 2, pubkeys); + const address = Address.fromScript(script); + + // test the valid case + { + const json = await nclient.execute('validateaddress', [ + address.toString(node.network) + ]); + + assert.deepEqual(json, { + isvalid: true, + address: address.toString(node.network), + scriptPubKey: Script.fromAddress(address).toJSON(), + isscript: true, + iswitness: false + }); + } + + // test the invalid case + { + const json = await nclient.execute('validateaddress', [ + address.toString('main') + ]); + + assert.deepEqual(json, { + isvalid: false + }); + } + }); + + it('should validate a p2wsh address', async () => { + await wallet.createAccount('foo5', { + witness: true + }); + + const pubkeys = []; + for (let i = 0; i < 2; i++) { + const result = await wallet.createAddress('foo5', 'default'); + pubkeys.push(Buffer.from(result.publicKey, 'hex')); + } + const script = Script.fromMultisig(2, 2, pubkeys); + const scriptPubKey = script.forWitness(); + const program = script.sha256(); + const address = Address.fromProgram(0, program); + + const json = await nclient.execute('validateaddress', [ + address.toString(node.network) + ]); + + assert.deepEqual(json, { + isvalid: true, + address: address.toString(node.network), + scriptPubKey: scriptPubKey.toJSON(), + isscript: true, + iswitness: true, + witness_version: 0, + witness_program: program.toString('hex') }); }); diff --git a/test/node-test.js b/test/node-test.js index c46a13fa..1d7a21bd 100644 --- a/test/node-test.js +++ b/test/node-test.js @@ -591,8 +591,8 @@ describe('Node', function() { isvalid: true, address: addr.toString(node.network), scriptPubKey: Script.fromAddress(addr, node.network).toJSON(), - ismine: false, - iswatchonly: false + isscript: false, + iswitness: false }); });