From cc13f2eed56b08854e96b796f1ef2426dce388ef Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Wed, 21 Aug 2013 14:46:20 -0400 Subject: [PATCH] Implement JSON extension walletislocked --- cmdmgr.go | 29 +++++++++++++++++++++++++++++ wallet/wallet.go | 7 +++++++ 2 files changed, 36 insertions(+) diff --git a/cmdmgr.go b/cmdmgr.go index 4ccf7fb..fee963b 100644 --- a/cmdmgr.go +++ b/cmdmgr.go @@ -151,6 +151,7 @@ func ProcessFrontendMsg(reply chan []byte, msg []byte) { } switch cmd { + // Standard bitcoind methods case "getaddressesbyaccount": GetAddressesByAccount(reply, msg) case "getnewaddress": @@ -159,6 +160,11 @@ func ProcessFrontendMsg(reply chan []byte, msg []byte) { WalletLock(reply, msg) case "walletpassphrase": WalletPassphrase(reply, msg) + + // btcwallet extensions + case "walletislocked": + WalletIsLocked(reply, msg) + default: // btcwallet does not understand method. Pass to btcd. log.Info("Unknown btcwallet method", cmd) @@ -236,6 +242,29 @@ func GetNewAddress(reply chan []byte, msg []byte) { } } +// WalletIsLocked returns whether the wallet used by the specified +// account, or default account, is locked. +func WalletIsLocked(reply chan []byte, msg []byte) { + var v map[string]interface{} + json.Unmarshal(msg, &v) + params := v["params"].([]interface{}) + account := "" + if len(params) > 0 { + if acct, ok := params[0].(string); ok { + account = acct + } else { + ReplyError(reply, v["id"], &InvalidParams) + return + } + } + if w := wallets[account]; w != nil { + result := w.IsLocked() + ReplySuccess(reply, v["id"], result) + } else { + ReplyError(reply, v["id"], &WalletInvalidAccountName) + } +} + // WalletLock locks the wallet. // // TODO(jrick): figure out how multiple wallets/accounts will work diff --git a/wallet/wallet.go b/wallet/wallet.go index e598caa..7b80c15 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -383,6 +383,13 @@ func (wallet *Wallet) Lock() (err error) { return err } +func (wallet *Wallet) IsLocked() (locked bool) { + wallet.key.Lock() + locked = wallet.key.secret == nil + wallet.key.Unlock() + return locked +} + // Returns wallet version as string and int. // TODO(jrick) func (wallet *Wallet) Version() (string, int) {