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.
This commit is contained in:
Josh Rickmar 2013-09-03 10:21:14 -04:00
parent 1c1ab52ef7
commit 1918bd3698
2 changed files with 17 additions and 8 deletions

21
cmd.go
View file

@ -17,6 +17,7 @@
package main package main
import ( import (
"errors"
"fmt" "fmt"
"github.com/conformal/btcwallet/tx" "github.com/conformal/btcwallet/tx"
"github.com/conformal/btcwallet/wallet" "github.com/conformal/btcwallet/wallet"
@ -27,6 +28,10 @@ import (
"time" "time"
) )
var (
ErrNoWallet = errors.New("Wallet file does not exist.")
)
var ( var (
log seelog.LoggerInterface = seelog.Default log seelog.LoggerInterface = seelog.Default
cfg *config cfg *config
@ -59,11 +64,14 @@ func main() {
*/ */
// Open wallet // Open wallet
btcw, err := OpenOrCreateWallet(cfg, "") btcw, err := OpenWallet(cfg, "")
if err != nil { 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 // Start HTTP server to listen and send messages to frontend and btcd
// backend. Try reconnection if connection failed. // backend. Try reconnection if connection failed.
@ -98,7 +106,7 @@ func walletdir(cfg *config, account string) string {
return filepath.Join(cfg.DataDir, wname) 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) wdir := walletdir(cfg, account)
fi, err := os.Stat(wdir) fi, err := os.Stat(wdir)
if err != nil { if err != nil {
@ -122,9 +130,8 @@ func OpenOrCreateWallet(cfg *config, account string) (*BtcWallet, error) {
var wfile, txfile, utxofile *os.File var wfile, txfile, utxofile *os.File
if wfile, err = os.Open(wfilepath); err != nil { if wfile, err = os.Open(wfilepath); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
if wfile, err = os.Create(wfilepath); err != nil { // Must create and save wallet first.
return nil, err return nil, ErrNoWallet
}
} else { } else {
return nil, err return nil, err
} }

View file

@ -255,7 +255,9 @@ func GetNewAddress(reply chan []byte, msg []byte) {
addr := w.NextUnusedAddress() addr := w.NextUnusedAddress()
ReplySuccess(reply, v["id"], addr) ReplySuccess(reply, v["id"], addr)
} else { } 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)
} }
} }