Implement new JSON extension 'createencryptedwallet'.

This commit is contained in:
Josh Rickmar 2013-09-03 09:49:16 -04:00
parent 9eae969230
commit 1c1ab52ef7
2 changed files with 77 additions and 13 deletions

11
cmd.go
View file

@ -85,8 +85,9 @@ type BtcWallet struct {
tx.TxStore tx.TxStore
} }
func OpenOrCreateWallet(cfg *config, account string) (*BtcWallet, error) { // walletdir returns the directory path which holds the wallet, utxo,
// Open wallet file specified by account. // and tx files.
func walletdir(cfg *config, account string) string {
var wname string var wname string
if account == "" { if account == "" {
wname = "btcwallet" wname = "btcwallet"
@ -94,7 +95,11 @@ func OpenOrCreateWallet(cfg *config, account string) (*BtcWallet, error) {
wname = fmt.Sprintf("btcwallet-%s", account) wname = fmt.Sprintf("btcwallet-%s", account)
} }
wdir := filepath.Join(cfg.DataDir, wname) return filepath.Join(cfg.DataDir, wname)
}
func OpenOrCreateWallet(cfg *config, account string) (*BtcWallet, error) {
wdir := walletdir(cfg, account)
fi, err := os.Stat(wdir) fi, err := os.Stat(wdir)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {

View file

@ -20,6 +20,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/conformal/btcjson" "github.com/conformal/btcjson"
"github.com/conformal/btcwallet/wallet"
"sync" "sync"
"time" "time"
) )
@ -162,6 +163,8 @@ func ProcessFrontendMsg(reply chan []byte, msg []byte) {
WalletPassphrase(reply, msg) WalletPassphrase(reply, msg)
// btcwallet extensions // btcwallet extensions
case "createencryptedwallet":
CreateEncryptedWallet(reply, msg)
case "walletislocked": case "walletislocked":
WalletIsLocked(reply, msg) WalletIsLocked(reply, msg)
@ -230,22 +233,78 @@ func GetAddressesByAccount(reply chan []byte, msg []byte) {
ReplySuccess(reply, v["id"], result) ReplySuccess(reply, v["id"], result)
} }
// GetNewAddress gets or generates a new address for an account. // GetNewAddress gets or generates a new address for an account. If
// // the requested wallet does not exist, a JSON error will be returned to
// TODO(jrick): support non-default account wallets. // the client.
func GetNewAddress(reply chan []byte, msg []byte) { func GetNewAddress(reply chan []byte, msg []byte) {
var v map[string]interface{} var v map[string]interface{}
json.Unmarshal(msg, &v) json.Unmarshal(msg, &v)
params := v["params"].([]interface{}) params := v["params"].([]interface{})
var wname string
if len(params) == 0 || params[0].(string) == "" { if len(params) == 0 || params[0].(string) == "" {
wallets.RLock() wname = ""
w := wallets.m[""] } else {
wallets.RUnlock() wname = "params[0].(string)"
if w != nil {
addr := w.NextUnusedAddress()
ReplySuccess(reply, v["id"], addr)
}
} }
wallets.RLock()
w := wallets.m[wname]
wallets.RUnlock()
if w != nil {
// TODO(jrick): generate new addresses if the address pool is empty.
addr := w.NextUnusedAddress()
ReplySuccess(reply, v["id"], addr)
} else {
ReplyError(reply, v["id"], &WalletInvalidAccountName)
}
}
// CreateEncryptedWallet creates a new encrypted wallet. The form of the command is:
//
// createencryptedwallet [account] [description] [passphrase]
//
// All three parameters are required, and must be of type string. If
// the wallet specified by account already exists, an invalid account
// name error is returned to the client.
func CreateEncryptedWallet(reply chan []byte, msg []byte) {
var v map[string]interface{}
json.Unmarshal(msg, &v)
params := v["params"].([]interface{})
var wname string
if len(params) != 3 {
ReplyError(reply, v["id"], &InvalidParams)
return
}
wname, ok1 := params[0].(string)
desc, ok2 := params[1].(string)
pass, ok3 := params[2].(string)
if !ok1 || !ok2 || !ok3 {
ReplyError(reply, v["id"], &InvalidParams)
return
}
// Does this wallet already exist?
wallets.RLock()
if w := wallets.m[wname]; w != nil {
e := WalletInvalidAccountName
e.Message = "Wallet already exists."
ReplyError(reply, v["id"], &e)
return
}
wallets.RUnlock()
w, err := wallet.NewWallet(wname, desc, []byte(pass))
if err != nil {
log.Error("Error creating wallet: " + err.Error())
ReplyError(reply, v["id"], &InternalError)
return
}
wallets.Lock()
wallets.m[wname] = &BtcWallet{
Wallet: w,
}
wallets.Unlock()
} }
// WalletIsLocked returns whether the wallet used by the specified // WalletIsLocked returns whether the wallet used by the specified