From 1918bd369813458774973f0cd6a68cce4d734a77 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Tue, 3 Sep 2013 10:21:14 -0400 Subject: [PATCH] Do not fail if default wallet does not exist. We must instead wait for the user to explicitly generate their own wallet for an account name. This is because all btcwallet wallets must be encrypted, and the passphrase must be known at time of wallet generation. --- cmd.go | 21 ++++++++++++++------- cmdmgr.go | 4 +++- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/cmd.go b/cmd.go index 2e03e78..56e7320 100644 --- a/cmd.go +++ b/cmd.go @@ -17,6 +17,7 @@ package main import ( + "errors" "fmt" "github.com/conformal/btcwallet/tx" "github.com/conformal/btcwallet/wallet" @@ -27,6 +28,10 @@ import ( "time" ) +var ( + ErrNoWallet = errors.New("Wallet file does not exist.") +) + var ( log seelog.LoggerInterface = seelog.Default cfg *config @@ -59,11 +64,14 @@ func main() { */ // Open wallet - btcw, err := OpenOrCreateWallet(cfg, "") + btcw, err := OpenWallet(cfg, "") if err != nil { - panic(err) + log.Info(err.Error()) + } else { + wallets.Lock() + wallets.m[""] = btcw + wallets.Unlock() } - _ = btcw // Start HTTP server to listen and send messages to frontend and btcd // backend. Try reconnection if connection failed. @@ -98,7 +106,7 @@ func walletdir(cfg *config, account string) string { return filepath.Join(cfg.DataDir, wname) } -func OpenOrCreateWallet(cfg *config, account string) (*BtcWallet, error) { +func OpenWallet(cfg *config, account string) (*BtcWallet, error) { wdir := walletdir(cfg, account) fi, err := os.Stat(wdir) if err != nil { @@ -122,9 +130,8 @@ func OpenOrCreateWallet(cfg *config, account string) (*BtcWallet, error) { var wfile, txfile, utxofile *os.File if wfile, err = os.Open(wfilepath); err != nil { if os.IsNotExist(err) { - if wfile, err = os.Create(wfilepath); err != nil { - return nil, err - } + // Must create and save wallet first. + return nil, ErrNoWallet } else { return nil, err } diff --git a/cmdmgr.go b/cmdmgr.go index 79e5d5e..5d30513 100644 --- a/cmdmgr.go +++ b/cmdmgr.go @@ -255,7 +255,9 @@ func GetNewAddress(reply chan []byte, msg []byte) { addr := w.NextUnusedAddress() ReplySuccess(reply, v["id"], addr) } else { - ReplyError(reply, v["id"], &WalletInvalidAccountName) + e := WalletInvalidAccountName + e.Message = fmt.Sprintf("Wallet for account '%s' does not exist.", wname) + ReplyError(reply, v["id"], &e) } }