Don't create a wallet when wallet.dat already exists
This commit is contained in:
parent
8d7f945251
commit
5a1e6f7e47
11
cli.go
11
cli.go
|
@ -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) {
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue