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.",