mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-28 16:01:29 +00:00
Implement new JSON extension 'createencryptedwallet'.
This commit is contained in:
parent
9eae969230
commit
1c1ab52ef7
2 changed files with 77 additions and 13 deletions
11
cmd.go
11
cmd.go
|
@ -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) {
|
||||||
|
|
67
cmdmgr.go
67
cmdmgr.go
|
@ -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) == "" {
|
||||||
|
wname = ""
|
||||||
|
} else {
|
||||||
|
wname = "params[0].(string)"
|
||||||
|
}
|
||||||
|
|
||||||
wallets.RLock()
|
wallets.RLock()
|
||||||
w := wallets.m[""]
|
w := wallets.m[wname]
|
||||||
wallets.RUnlock()
|
wallets.RUnlock()
|
||||||
if w != nil {
|
if w != nil {
|
||||||
|
// TODO(jrick): generate new addresses if the address pool is empty.
|
||||||
addr := w.NextUnusedAddress()
|
addr := w.NextUnusedAddress()
|
||||||
ReplySuccess(reply, v["id"], addr)
|
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
|
||||||
|
|
Loading…
Add table
Reference in a new issue