From 7145eef75b483775e6457aa628f98fe1ae05758a Mon Sep 17 00:00:00 2001 From: Anirudha Bose Date: Mon, 24 Aug 2020 20:47:32 +0200 Subject: [PATCH] rpcserver: add parity with bitcoind for validateaddress Updated the rpcserver handler for validateaddress JSON-RPC command to have parity with the bitcoind 0.20.0 interface. The new fields included are - isscript, iswitness, witness_version, and witness_program. The scriptPubKey field has been left out since it requires wallet access. This update has no impact on the rpcclient.ValidateAddress method, which uses the btcjson.ValidateAddressWalletResult type for modelling the response from bitcoind. --- btcjson/chainsvrresults.go | 12 ++++++++++-- rpcserver.go | 31 +++++++++++++++++++++++++++++++ rpcserverhelp.go | 8 ++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/btcjson/chainsvrresults.go b/btcjson/chainsvrresults.go index 482e3d72..63225802 100644 --- a/btcjson/chainsvrresults.go +++ b/btcjson/chainsvrresults.go @@ -671,9 +671,17 @@ type TxRawDecodeResult struct { // ValidateAddressChainResult models the data returned by the chain server // validateaddress command. +// +// Compared to the Bitcoin Core version, this struct lacks the scriptPubKey +// field since it requires wallet access, which is outside the scope of btcd. +// Ref: https://bitcoincore.org/en/doc/0.20.0/rpc/util/validateaddress/ type ValidateAddressChainResult struct { - IsValid bool `json:"isvalid"` - Address string `json:"address,omitempty"` + IsValid bool `json:"isvalid"` + Address string `json:"address,omitempty"` + IsScript *bool `json:"isscript,omitempty"` + IsWitness *bool `json:"iswitness,omitempty"` + WitnessVersion *int32 `json:"witness_version,omitempty"` + WitnessProgram *string `json:"witness_program,omitempty"` } // EstimateSmartFeeResult models the data returned buy the chain server diff --git a/rpcserver.go b/rpcserver.go index 6d684451..aa154dd0 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -3493,6 +3493,37 @@ func handleValidateAddress(s *rpcServer, cmd interface{}, closeChan <-chan struc return result, nil } + switch addr := addr.(type) { + case *btcutil.AddressPubKeyHash: + result.IsScript = btcjson.Bool(false) + result.IsWitness = btcjson.Bool(false) + + case *btcutil.AddressScriptHash: + result.IsScript = btcjson.Bool(true) + result.IsWitness = btcjson.Bool(false) + + case *btcutil.AddressPubKey: + result.IsScript = btcjson.Bool(false) + result.IsWitness = btcjson.Bool(false) + + case *btcutil.AddressWitnessPubKeyHash: + result.IsScript = btcjson.Bool(false) + result.IsWitness = btcjson.Bool(true) + result.WitnessVersion = btcjson.Int32(int32(addr.WitnessVersion())) + result.WitnessProgram = btcjson.String(hex.EncodeToString(addr.WitnessProgram())) + + case *btcutil.AddressWitnessScriptHash: + result.IsScript = btcjson.Bool(true) + result.IsWitness = btcjson.Bool(true) + result.WitnessVersion = btcjson.Int32(int32(addr.WitnessVersion())) + result.WitnessProgram = btcjson.String(hex.EncodeToString(addr.WitnessProgram())) + + default: + // Handle the case when a new Address is supported by btcutil, but none + // of the cases were matched in the switch block. The current behaviour + // is to do nothing, and only populate the Address and IsValid fields. + } + result.Address = addr.EncodeAddress() result.IsValid = true diff --git a/rpcserverhelp.go b/rpcserverhelp.go index cc3b33f3..9299f3e4 100644 --- a/rpcserverhelp.go +++ b/rpcserverhelp.go @@ -575,8 +575,12 @@ var helpDescsEnUS = map[string]string{ "submitblock--result1": "The reason the block was rejected", // ValidateAddressResult help. - "validateaddresschainresult-isvalid": "Whether or not the address is valid", - "validateaddresschainresult-address": "The bitcoin address (only when isvalid is true)", + "validateaddresschainresult-isvalid": "Whether or not the address is valid", + "validateaddresschainresult-address": "The bitcoin address (only when isvalid is true)", + "validateaddresschainresult-isscript": "If the key is a script", + "validateaddresschainresult-iswitness": "If the address is a witness address", + "validateaddresschainresult-witness_version": "The version number of the witness program", + "validateaddresschainresult-witness_program": "The hex value of the witness program", // ValidateAddressCmd help. "validateaddress--synopsis": "Verify an address is valid.",