Implement (*Wallet).Lock() and reply with correct JSON
This commit is contained in:
parent
1aa324684d
commit
92a79baeff
2 changed files with 69 additions and 27 deletions
25
cmdmgr.go
25
cmdmgr.go
|
@ -20,8 +20,8 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/conformal/btcjson"
|
||||
"time"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Errors
|
||||
|
@ -194,6 +194,18 @@ func ReplyError(reply chan []byte, id interface{}, e *btcjson.Error) {
|
|||
}
|
||||
}
|
||||
|
||||
// ReplySuccess creates and marshalls a btcjson.Reply with the result r,
|
||||
// sending the reply to a reply channel.
|
||||
func ReplySuccess(reply chan []byte, id interface{}, result interface{}) {
|
||||
r := btcjson.Reply{
|
||||
Result: result,
|
||||
Id: &id,
|
||||
}
|
||||
if mr, err := json.Marshal(r); err != nil {
|
||||
reply <- mr
|
||||
}
|
||||
}
|
||||
|
||||
// GetAddressesByAccount Gets all addresses for an account.
|
||||
func GetAddressesByAccount(reply chan []byte, msg []byte) {
|
||||
var v map[string]interface{}
|
||||
|
@ -246,10 +258,17 @@ func GetNewAddress(reply chan []byte, msg []byte) {
|
|||
// TODO(jrick): figure out how multiple wallets/accounts will work
|
||||
// with this.
|
||||
func WalletLock(reply chan []byte, msg []byte) {
|
||||
// TODO(jrick)
|
||||
var v map[string]interface{}
|
||||
json.Unmarshal(msg, &v)
|
||||
if w := wallets[""]; w != nil {
|
||||
if err := w.Lock(); err != nil {
|
||||
ReplyError(reply, v["id"], &WalletWrongEncState)
|
||||
} else {
|
||||
ReplySuccess(reply, v["id"], nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// WalletPassphrase stores the decryption key for the default account,
|
||||
// unlocking the wallet.
|
||||
//
|
||||
|
|
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/conformal/btcutil"
|
||||
"github.com/conformal/btcwire"
|
||||
"io"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -228,6 +229,10 @@ type Wallet struct {
|
|||
appendedEntries varEntries
|
||||
|
||||
// These are not serialized
|
||||
key struct {
|
||||
sync.Mutex
|
||||
secret []byte
|
||||
}
|
||||
addrMap map[[ripemd160.Size]byte]*btcAddress
|
||||
addrCommentMap map[[ripemd160.Size]byte]*[]byte
|
||||
chainIdxMap map[int64]*[ripemd160.Size]byte
|
||||
|
@ -351,13 +356,31 @@ func (wallet *Wallet) Unlock(passphrase []byte) error {
|
|||
wallet.kdfParams.mem, wallet.kdfParams.nIter)
|
||||
|
||||
// Attempt unlocking root address
|
||||
return wallet.keyGenerator.unlock(key)
|
||||
if err := wallet.keyGenerator.unlock(key); err != nil {
|
||||
return err
|
||||
} else {
|
||||
wallet.key.Lock()
|
||||
wallet.key.secret = key
|
||||
wallet.key.Unlock()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Lock does a best effort to zero the keys.
|
||||
// Being go this might not succeed but try anway.
|
||||
// TODO(jrick)
|
||||
func (wallet *Wallet) Lock() {
|
||||
func (wallet *Wallet) Lock() (err error) {
|
||||
wallet.key.Lock()
|
||||
if wallet.key.secret != nil {
|
||||
for i, _ := range wallet.key.secret {
|
||||
wallet.key.secret[i] = 0
|
||||
}
|
||||
wallet.key.secret = nil
|
||||
} else {
|
||||
err = fmt.Errorf("Wallet already locked")
|
||||
}
|
||||
wallet.key.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
// Returns wallet version as string and int.
|
||||
|
|
Loading…
Reference in a new issue