Merge cb538eade9
into ff408351a2
This commit is contained in:
commit
2fb3918959
52
wallets.go
52
wallets.go
|
@ -2,21 +2,29 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/ecdsa"
|
||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const walletFile = "wallet_%s.dat"
|
const walletFile = "wallet_%s.dat"
|
||||||
|
|
||||||
// Wallets stores a collection of wallets
|
|
||||||
type Wallets struct {
|
type Wallets struct {
|
||||||
Wallets map[string]*Wallet
|
Wallets map[string]*Wallet
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SerializableWallet Solve gob: type elliptic.p256Curve has no exported fields
|
||||||
|
type SerializableWallet struct {
|
||||||
|
D *big.Int
|
||||||
|
X, Y *big.Int
|
||||||
|
PublicKey []byte
|
||||||
|
}
|
||||||
|
|
||||||
// NewWallets creates Wallets and fills it from a file if it exists
|
// NewWallets creates Wallets and fills it from a file if it exists
|
||||||
func NewWallets(nodeID string) (*Wallets, error) {
|
func NewWallets(nodeID string) (*Wallets, error) {
|
||||||
wallets := Wallets{}
|
wallets := Wallets{}
|
||||||
|
@ -55,7 +63,9 @@ func (ws Wallets) GetWallet(address string) Wallet {
|
||||||
|
|
||||||
// LoadFromFile loads wallets from the file
|
// LoadFromFile loads wallets from the file
|
||||||
func (ws *Wallets) LoadFromFile(nodeID string) error {
|
func (ws *Wallets) LoadFromFile(nodeID string) error {
|
||||||
|
|
||||||
walletFile := fmt.Sprintf(walletFile, nodeID)
|
walletFile := fmt.Sprintf(walletFile, nodeID)
|
||||||
|
//fmt.Println(walletFile)
|
||||||
if _, err := os.Stat(walletFile); os.IsNotExist(err) {
|
if _, err := os.Stat(walletFile); os.IsNotExist(err) {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -65,15 +75,29 @@ func (ws *Wallets) LoadFromFile(nodeID string) error {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var wallets Wallets
|
var wallets map[string]SerializableWallet
|
||||||
gob.Register(elliptic.P256())
|
//gob.Register(elliptic.P256())
|
||||||
|
gob.Register(SerializableWallet{})
|
||||||
decoder := gob.NewDecoder(bytes.NewReader(fileContent))
|
decoder := gob.NewDecoder(bytes.NewReader(fileContent))
|
||||||
err = decoder.Decode(&wallets)
|
err = decoder.Decode(&wallets)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
ws.Wallets = make(map[string]*Wallet)
|
||||||
ws.Wallets = wallets.Wallets
|
//ws.Wallets = wallets.Wallets
|
||||||
|
for k, v := range wallets {
|
||||||
|
ws.Wallets[k] = &Wallet{
|
||||||
|
PrivateKey: ecdsa.PrivateKey{
|
||||||
|
PublicKey: ecdsa.PublicKey{
|
||||||
|
Curve: elliptic.P256(),
|
||||||
|
X: v.X,
|
||||||
|
Y: v.Y,
|
||||||
|
},
|
||||||
|
D: v.D,
|
||||||
|
},
|
||||||
|
PublicKey: v.PublicKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -82,17 +106,29 @@ func (ws *Wallets) LoadFromFile(nodeID string) error {
|
||||||
func (ws Wallets) SaveToFile(nodeID string) {
|
func (ws Wallets) SaveToFile(nodeID string) {
|
||||||
var content bytes.Buffer
|
var content bytes.Buffer
|
||||||
walletFile := fmt.Sprintf(walletFile, nodeID)
|
walletFile := fmt.Sprintf(walletFile, nodeID)
|
||||||
|
//fmt.Println(walletFile)
|
||||||
|
|
||||||
gob.Register(elliptic.P256())
|
gob.Register(SerializableWallet{})
|
||||||
|
|
||||||
|
wallets := make(map[string]SerializableWallet)
|
||||||
|
for k, v := range ws.Wallets {
|
||||||
|
wallets[k] = SerializableWallet{
|
||||||
|
D: v.PrivateKey.D,
|
||||||
|
X: v.PrivateKey.PublicKey.X,
|
||||||
|
Y: v.PrivateKey.PublicKey.Y,
|
||||||
|
PublicKey: v.PublicKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
encoder := gob.NewEncoder(&content)
|
encoder := gob.NewEncoder(&content)
|
||||||
err := encoder.Encode(ws)
|
err := encoder.Encode(wallets)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ioutil.WriteFile(walletFile, content.Bytes(), 0644)
|
err = os.WriteFile(walletFile, content.Bytes(), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue