Save wallet to a file
This commit is contained in:
parent
24b19381d2
commit
8d7f945251
|
@ -1 +1,2 @@
|
|||
*.db
|
||||
wallet.dat
|
||||
|
|
1
cli.go
1
cli.go
|
@ -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) {
|
||||
|
|
15
wallet.go
15
wallet.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue