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"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/conformal/btcjson"
|
"github.com/conformal/btcjson"
|
||||||
"time"
|
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Errors
|
// 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.
|
// GetAddressesByAccount Gets all addresses for an account.
|
||||||
func GetAddressesByAccount(reply chan []byte, msg []byte) {
|
func GetAddressesByAccount(reply chan []byte, msg []byte) {
|
||||||
var v map[string]interface{}
|
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
|
// TODO(jrick): figure out how multiple wallets/accounts will work
|
||||||
// with this.
|
// with this.
|
||||||
func WalletLock(reply chan []byte, msg []byte) {
|
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,
|
// WalletPassphrase stores the decryption key for the default account,
|
||||||
// unlocking the wallet.
|
// unlocking the wallet.
|
||||||
//
|
//
|
||||||
|
|
|
@ -30,6 +30,7 @@ import (
|
||||||
"github.com/conformal/btcutil"
|
"github.com/conformal/btcutil"
|
||||||
"github.com/conformal/btcwire"
|
"github.com/conformal/btcwire"
|
||||||
"io"
|
"io"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -228,6 +229,10 @@ type Wallet struct {
|
||||||
appendedEntries varEntries
|
appendedEntries varEntries
|
||||||
|
|
||||||
// These are not serialized
|
// These are not serialized
|
||||||
|
key struct {
|
||||||
|
sync.Mutex
|
||||||
|
secret []byte
|
||||||
|
}
|
||||||
addrMap map[[ripemd160.Size]byte]*btcAddress
|
addrMap map[[ripemd160.Size]byte]*btcAddress
|
||||||
addrCommentMap map[[ripemd160.Size]byte]*[]byte
|
addrCommentMap map[[ripemd160.Size]byte]*[]byte
|
||||||
chainIdxMap map[int64]*[ripemd160.Size]byte
|
chainIdxMap map[int64]*[ripemd160.Size]byte
|
||||||
|
@ -351,13 +356,31 @@ func (wallet *Wallet) Unlock(passphrase []byte) error {
|
||||||
wallet.kdfParams.mem, wallet.kdfParams.nIter)
|
wallet.kdfParams.mem, wallet.kdfParams.nIter)
|
||||||
|
|
||||||
// Attempt unlocking root address
|
// 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.
|
// Lock does a best effort to zero the keys.
|
||||||
// Being go this might not succeed but try anway.
|
// Being go this might not succeed but try anway.
|
||||||
// TODO(jrick)
|
// 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.
|
// Returns wallet version as string and int.
|
||||||
|
|
Loading…
Add table
Reference in a new issue