Save wallet to a file
This commit is contained in:
parent
24b19381d2
commit
8d7f945251
|
@ -1 +1,2 @@
|
||||||
*.db
|
*.db
|
||||||
|
wallet.dat
|
||||||
|
|
1
cli.go
1
cli.go
|
@ -20,6 +20,7 @@ func (cli *CLI) createBlockchain(address string) {
|
||||||
func (cli *CLI) createWallet() {
|
func (cli *CLI) createWallet() {
|
||||||
wallet := NewWallet()
|
wallet := NewWallet()
|
||||||
fmt.Printf("Your address: %s\n", wallet.GetAddress())
|
fmt.Printf("Your address: %s\n", wallet.GetAddress())
|
||||||
|
wallet.SaveToFile()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *CLI) getBalance(address string) {
|
func (cli *CLI) getBalance(address string) {
|
||||||
|
|
15
wallet.go
15
wallet.go
|
@ -1,15 +1,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
|
"encoding/gob"
|
||||||
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"golang.org/x/crypto/ripemd160"
|
"golang.org/x/crypto/ripemd160"
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = byte(0x00)
|
const version = byte(0x00)
|
||||||
|
const walletFile = "wallet.dat"
|
||||||
|
|
||||||
// Wallet ...
|
// Wallet ...
|
||||||
type Wallet struct {
|
type Wallet struct {
|
||||||
|
@ -39,7 +43,18 @@ func (w Wallet) GetAddress() []byte {
|
||||||
|
|
||||||
// SaveToFile saves the wallet to a file
|
// SaveToFile saves the wallet to a file
|
||||||
func (w Wallet) SaveToFile() {
|
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
|
// NewWallet creates and returns a Wallet
|
||||||
|
|
Loading…
Reference in New Issue