Implement validateaddress rpc command.

Closes #60
This commit is contained in:
Owain G. Ainsworth 2014-02-13 23:16:21 +00:00
parent ed264697e7
commit 3a6ae93a4b

View file

@ -55,6 +55,7 @@ var rpcHandlers = map[string]cmdHandler{
"sendtoaddress": SendToAddress, "sendtoaddress": SendToAddress,
"settxfee": SetTxFee, "settxfee": SetTxFee,
"signmessage": SignMessage, "signmessage": SignMessage,
"validateaddress": ValidateAddress,
"verifymessage": VerifyMessage, "verifymessage": VerifyMessage,
"walletlock": WalletLock, "walletlock": WalletLock,
"walletpassphrase": WalletPassphrase, "walletpassphrase": WalletPassphrase,
@ -80,7 +81,6 @@ var rpcHandlers = map[string]cmdHandler{
"setaccount": Unimplemented, "setaccount": Unimplemented,
"signrawtransaction": Unimplemented, "signrawtransaction": Unimplemented,
"stop": Unimplemented, "stop": Unimplemented,
"validateaddress": Unimplemented,
// Standard bitcoind methods which won't be implemented by btcwallet. // Standard bitcoind methods which won't be implemented by btcwallet.
"encryptwallet": Unsupported, "encryptwallet": Unsupported,
@ -1565,6 +1565,51 @@ func RecoverAddresses(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
return nil, nil return nil, nil
} }
// ValidateAddress handles the validateaddress command.
func ValidateAddress(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {
cmd, ok := icmd.(*btcjson.ValidateAddressCmd)
if !ok {
return nil, &btcjson.ErrInternal
}
addr, err := btcutil.DecodeAddr(cmd.Address)
if err != nil {
return map[string]interface{}{
"isvalid": false,
}, nil
}
_, scriptHash := addr.(*btcutil.AddressScriptHash)
result := map[string]interface{}{
"address": addr.EncodeAddress(),
"isvalid": true,
"isscript": scriptHash,
}
account, err := LookupAccountByAddress(addr.EncodeAddress())
if err == nil {
// we ignore these errors because if this call passes this can't
// realistically fail.
a, _ := AcctMgr.Account(account)
ainfo, _ := a.AddressInfo(addr)
result["ismine"] = true
result["account"] = account
// TODO(oga) when we handle different types of addresses then
// we will need to check here and only provide the script,
// hexsript and list of addresses.
// if scripthash, the pubkey if pubkey/pubkeyhash, etc.
// for now we only support p2pkh so is irrelavent
result["compressed"] = ainfo.Compressed
result["pubkey"] = ainfo.Pubkey
} else {
result["ismine"] = false
}
return result, nil
}
// VerifyMessage handles the verifymessage command by verifying the provided // VerifyMessage handles the verifymessage command by verifying the provided
// compact signature for the given address and message. // compact signature for the given address and message.
func VerifyMessage(icmd btcjson.Cmd) (interface{}, *btcjson.Error) { func VerifyMessage(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {