Don't create a wallet when wallet.dat already exists

This commit is contained in:
Ivan Kuznetsov 2017-09-07 16:46:55 +07:00
parent 8d7f945251
commit 5a1e6f7e47
2 changed files with 15 additions and 5 deletions

11
cli.go
View File

@ -18,9 +18,14 @@ func (cli *CLI) createBlockchain(address string) {
}
func (cli *CLI) createWallet() {
wallet := NewWallet()
fmt.Printf("Your address: %s\n", wallet.GetAddress())
wallet.SaveToFile()
wallet, err := NewWallet()
if err == nil {
fmt.Printf("Your address: %s\n", wallet.GetAddress())
wallet.SaveToFile()
} else {
fmt.Println(err.Error())
os.Exit(1)
}
}
func (cli *CLI) getBalance(address string) {

View File

@ -6,8 +6,10 @@ import (
"crypto/rand"
"crypto/sha256"
"encoding/gob"
"errors"
"io/ioutil"
"log"
"os"
"golang.org/x/crypto/ripemd160"
)
@ -58,11 +60,14 @@ func (w Wallet) SaveToFile() {
}
// NewWallet creates and returns a Wallet
func NewWallet() *Wallet {
func NewWallet() (*Wallet, error) {
if _, err := os.Stat(walletFile); !os.IsNotExist(err) {
return nil, errors.New("Wallet already exists")
}
private, public := newKeyPair()
wallet := Wallet{private, public}
return &wallet
return &wallet, nil
}
func newKeyPair() ([]byte, []byte) {