Skip to content

Commit

Permalink
wallet rpc: add getaddressinfo rpc command
Browse files Browse the repository at this point in the history
update the rpc command to better match bitcoind.
add the ismine and iswatchonly fields and
segwit related fields such as witness_version
and witness_program.
  • Loading branch information
Mark Tyneway committed Mar 26, 2019
1 parent 26a2000 commit a28ffa2
Show file tree
Hide file tree
Showing 3 changed files with 334 additions and 28 deletions.
35 changes: 35 additions & 0 deletions lib/wallet/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class RPC extends RPCBase {
this.add('dumpprivkey', this.dumpPrivKey);
this.add('dumpwallet', this.dumpWallet);
this.add('encryptwallet', this.encryptWallet);
this.add('getaddressinfo', this.getAddressInfo);
this.add('getaccountaddress', this.getAccountAddress);
this.add('getaccount', this.getAccount);
this.add('getaddressesbyaccount', this.getAddressesByAccount);
Expand Down Expand Up @@ -443,6 +444,40 @@ class RPC extends RPCBase {
return addrs;
}

async getAddressInfo(args, help) {
if (help || args.length !== 1)
throw new RPCError(errs.MISC_ERROR, 'getaddressinfo "address"');

const valid = new Validator(args);
const addr = valid.str(0, '');

const address = parseAddress(addr, this.network);
const script = Script.fromAddress(address);
const wallet = this.wallet.toJSON();

const path = await this.wallet.getPath(address);

const isScript = script.isScripthash() || script.isWitnessScripthash();
const isWitness = address.isProgram();

const result = {
address: address.toString(this.network),
scriptPubKey: script ? script.toJSON() : undefined,
ismine: path != null,
ischange: path ? path.branch === 1 : false,
iswatchonly: wallet.watchOnly,
isscript: isScript,
iswitness: isWitness
};

if (isWitness) {
result.witness_version = address.version;
result.witness_program = address.hash.toString('hex');
}

return result;
}

async getBalance(args, help) {
if (help || args.length > 3) {
throw new RPCError(errs.MISC_ERROR,
Expand Down
52 changes: 24 additions & 28 deletions test/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ let hash = null;
describe('HTTP', function() {
this.timeout(15000);

// m/44'/1'/0'/0/{0,1}
const pubkeys = [
Buffer.from('02a7451395735369f2ecdfc829c0f'
+ '774e88ef1303dfe5b2f04dbaab30a535dfdd6', 'hex'),
Buffer.from('03589ae7c835ce76e23cf8feb32f1a'
+ 'df4a7f2ba0ed2ad70801802b0bcd70e99c1c', 'hex')
];

it('should open node', async () => {
consensus.COINBASE_MATURITY = 0;
await node.open();
Expand Down Expand Up @@ -273,29 +281,32 @@ describe('HTTP', function() {
});

it('should not validate invalid address', async () => {
// send an address for the wrong network
// Valid Mainnet P2WPKH from
// https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki
const json = await nclient.execute('validateaddress', [
addr.toString('main')
'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'
]);

// Sending an address from the incorrect network
// should result in an invalid address
assert.deepStrictEqual(json, {
isvalid: false
});
});

it('should validate a p2wpkh address', async () => {
const info = await wallet.createAccount('foo3', {
witness: true
});
const address = 'bcrt1q8gk5z3dy7zv9ywe7synlrk58elz4hrnegvpv6m';
const addr = Address.fromString(address);
const script = Script.fromAddress(addr);

const json = await nclient.execute('validateaddress', [
info.receiveAddress
address
]);
const addr = Address.fromString(info.receiveAddress);
const script = Script.fromAddress(addr);

assert.deepStrictEqual(json, {
isvalid: true,
iswitness: true,
address: info.receiveAddress,
address: address,
isscript: addr.isScripthash(),
scriptPubKey: script.toJSON(),
witness_version: addr.version,
Expand All @@ -304,18 +315,11 @@ describe('HTTP', function() {
});

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
// Test the valid case - render the address to the
// correct network
{
const json = await nclient.execute('validateaddress', [
address.toString(node.network)
Expand All @@ -330,7 +334,8 @@ describe('HTTP', function() {
});
}

// test the invalid case
// Test the invalid case - render the address to the
// incorrect network, making it an invalid address
{
const json = await nclient.execute('validateaddress', [
address.toString('main')
Expand All @@ -343,15 +348,6 @@ describe('HTTP', function() {
});

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();
Expand Down
Loading

0 comments on commit a28ffa2

Please sign in to comment.