Save wallet to a file

This commit is contained in:
Ivan Kuznetsov 2017-09-07 16:42:38 +07:00
parent 24b19381d2
commit 8d7f945251
3 changed files with 17 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.db
wallet.dat

1
cli.go
View File

@ -20,6 +20,7 @@ func (cli *CLI) createBlockchain(address string) {
func (cli *CLI) createWallet() {
wallet := NewWallet()
fmt.Printf("Your address: %s\n", wallet.GetAddress())
wallet.SaveToFile()
}
func (cli *CLI) getBalance(address string) {

View File

@ -1,15 +1,19 @@
package main
import (
"bytes"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/gob"
"io/ioutil"
"log"
"golang.org/x/crypto/ripemd160"
)
const version = byte(0x00)
const walletFile = "wallet.dat"
// Wallet ...
type Wallet struct {
@ -39,7 +43,18 @@ func (w Wallet) GetAddress() []byte {
// SaveToFile saves the wallet to a file
func (w Wallet) SaveToFile() {
var content bytes.Buffer
encoder := gob.NewEncoder(&content)
err := encoder.Encode(w)
if err != nil {
log.Panic(err)
}
err = ioutil.WriteFile(walletFile, content.Bytes(), 0644)
if err != nil {
log.Panic(err)
}
}
// NewWallet creates and returns a Wallet