2017-09-09 22:54:58 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2017-09-30 22:48:51 -05:00
|
|
|
const walletFile = "wallet_%s.dat"
|
|
|
|
|
2017-09-09 22:54:58 -05:00
|
|
|
// Wallets stores a collection of wallets
|
|
|
|
type Wallets struct {
|
|
|
|
Wallets map[string]*Wallet
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewWallets creates Wallets and fills it from a file if it exists
|
2017-09-30 22:48:51 -05:00
|
|
|
func NewWallets(nodeID string) (*Wallets, error) {
|
2017-09-09 22:54:58 -05:00
|
|
|
wallets := Wallets{}
|
|
|
|
wallets.Wallets = make(map[string]*Wallet)
|
|
|
|
|
2017-09-30 22:53:26 -05:00
|
|
|
err := wallets.LoadFromFile(nodeID)
|
2017-09-09 22:54:58 -05:00
|
|
|
|
|
|
|
return &wallets, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateWallet adds a Wallet to Wallets
|
|
|
|
func (ws *Wallets) CreateWallet() string {
|
|
|
|
wallet := NewWallet()
|
|
|
|
address := fmt.Sprintf("%s", wallet.GetAddress())
|
|
|
|
|
|
|
|
ws.Wallets[address] = wallet
|
|
|
|
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAddresses returns an array of addresses stored in the wallet file
|
|
|
|
func (ws *Wallets) GetAddresses() []string {
|
|
|
|
var addresses []string
|
|
|
|
|
|
|
|
for address := range ws.Wallets {
|
|
|
|
addresses = append(addresses, address)
|
|
|
|
}
|
|
|
|
|
|
|
|
return addresses
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWallet returns a Wallet by its address
|
|
|
|
func (ws Wallets) GetWallet(address string) Wallet {
|
|
|
|
return *ws.Wallets[address]
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromFile loads wallets from the file
|
2017-09-30 22:53:26 -05:00
|
|
|
func (ws *Wallets) LoadFromFile(nodeID string) error {
|
|
|
|
walletFile := fmt.Sprintf(walletFile, nodeID)
|
2017-09-09 22:54:58 -05:00
|
|
|
if _, err := os.Stat(walletFile); os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fileContent, err := ioutil.ReadFile(walletFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wallets Wallets
|
|
|
|
gob.Register(elliptic.P256())
|
|
|
|
decoder := gob.NewDecoder(bytes.NewReader(fileContent))
|
|
|
|
err = decoder.Decode(&wallets)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.Wallets = wallets.Wallets
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveToFile saves wallets to a file
|
2017-09-30 22:53:26 -05:00
|
|
|
func (ws Wallets) SaveToFile(nodeID string) {
|
2017-09-09 22:54:58 -05:00
|
|
|
var content bytes.Buffer
|
2017-09-30 22:53:26 -05:00
|
|
|
walletFile := fmt.Sprintf(walletFile, nodeID)
|
2017-09-09 22:54:58 -05:00
|
|
|
|
|
|
|
gob.Register(elliptic.P256())
|
|
|
|
|
|
|
|
encoder := gob.NewEncoder(&content)
|
|
|
|
err := encoder.Encode(ws)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(walletFile, content.Bytes(), 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|