Skip to content

Commit

Permalink
rpcserver: add parity with bitcoind for validateaddress
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
onyb committed Aug 24, 2020
1 parent 1db1b6f commit 5342807
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
12 changes: 10 additions & 2 deletions btcjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions rpcserverhelp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit 5342807

Please sign in to comment.