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

7
cli.go
View File

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

View File

@ -6,8 +6,10 @@ import (
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/gob" "encoding/gob"
"errors"
"io/ioutil" "io/ioutil"
"log" "log"
"os"
"golang.org/x/crypto/ripemd160" "golang.org/x/crypto/ripemd160"
) )
@ -58,11 +60,14 @@ func (w Wallet) SaveToFile() {
} }
// NewWallet creates and returns a Wallet // 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() private, public := newKeyPair()
wallet := Wallet{private, public} wallet := Wallet{private, public}
return &wallet return &wallet, nil
} }
func newKeyPair() ([]byte, []byte) { func newKeyPair() ([]byte, []byte) {