From 3a6ae93a4bb6c2a9257ec97dfe1c5cb3de33ef21 Mon Sep 17 00:00:00 2001 From: "Owain G. Ainsworth" Date: Thu, 13 Feb 2014 23:16:21 +0000 Subject: [PATCH] Implement validateaddress rpc command. Closes #60 --- rpcserver.go | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/rpcserver.go b/rpcserver.go index a0dc87e..f0b3a7d 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -55,6 +55,7 @@ var rpcHandlers = map[string]cmdHandler{ "sendtoaddress": SendToAddress, "settxfee": SetTxFee, "signmessage": SignMessage, + "validateaddress": ValidateAddress, "verifymessage": VerifyMessage, "walletlock": WalletLock, "walletpassphrase": WalletPassphrase, @@ -80,7 +81,6 @@ var rpcHandlers = map[string]cmdHandler{ "setaccount": Unimplemented, "signrawtransaction": Unimplemented, "stop": Unimplemented, - "validateaddress": Unimplemented, // Standard bitcoind methods which won't be implemented by btcwallet. "encryptwallet": Unsupported, @@ -1565,6 +1565,51 @@ func RecoverAddresses(icmd btcjson.Cmd) (interface{}, *btcjson.Error) { 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 // compact signature for the given address and message. func VerifyMessage(icmd btcjson.Cmd) (interface{}, *btcjson.Error) {