Wallet file name must depend on node ID

This commit is contained in:
Ivan Kuznetsov 2017-10-01 10:48:51 +07:00
parent 57f3680551
commit 0c91da0e52
7 changed files with 18 additions and 15 deletions

4
cli.go
View File

@ -118,11 +118,11 @@ func (cli *CLI) Run() {
} }
if createWalletCmd.Parsed() { if createWalletCmd.Parsed() {
cli.createWallet() cli.createWallet(nodeID)
} }
if listAddressesCmd.Parsed() { if listAddressesCmd.Parsed() {
cli.listAddresses() cli.listAddresses(nodeID)
} }
if printChainCmd.Parsed() { if printChainCmd.Parsed() {

View File

@ -2,8 +2,8 @@ package main
import "fmt" import "fmt"
func (cli *CLI) createWallet() { func (cli *CLI) createWallet(nodeID string) {
wallets, _ := NewWallets() wallets, _ := NewWallets(nodeID)
address := wallets.CreateWallet() address := wallets.CreateWallet()
wallets.SaveToFile() wallets.SaveToFile()

View File

@ -5,8 +5,8 @@ import (
"log" "log"
) )
func (cli *CLI) listAddresses() { func (cli *CLI) listAddresses(nodeID string) {
wallets, err := NewWallets() wallets, err := NewWallets(nodeID)
if err != nil { if err != nil {
log.Panic(err) log.Panic(err)
} }

View File

@ -17,7 +17,13 @@ func (cli *CLI) send(from, to string, amount int, nodeID string) {
UTXOSet := UTXOSet{bc} UTXOSet := UTXOSet{bc}
defer bc.db.Close() defer bc.db.Close()
tx := NewUTXOTransaction(from, to, amount, &UTXOSet) wallets, err := NewWallets(nodeID)
if err != nil {
log.Panic(err)
}
wallet := wallets.GetWallet(from)
tx := NewUTXOTransaction(&wallet, to, amount, &UTXOSet)
cbTx := NewCoinbaseTX(from, "") cbTx := NewCoinbaseTX(from, "")
txs := []*Transaction{cbTx, tx} txs := []*Transaction{cbTx, tx}

View File

@ -191,15 +191,10 @@ func NewCoinbaseTX(to, data string) *Transaction {
} }
// NewUTXOTransaction creates a new transaction // NewUTXOTransaction creates a new transaction
func NewUTXOTransaction(from, to string, amount int, UTXOSet *UTXOSet) *Transaction { func NewUTXOTransaction(wallet *Wallet, to string, amount int, UTXOSet *UTXOSet) *Transaction {
var inputs []TXInput var inputs []TXInput
var outputs []TXOutput var outputs []TXOutput
wallets, err := NewWallets()
if err != nil {
log.Panic(err)
}
wallet := wallets.GetWallet(from)
pubKeyHash := HashPubKey(wallet.PublicKey) pubKeyHash := HashPubKey(wallet.PublicKey)
acc, validOutputs := UTXOSet.FindSpendableOutputs(pubKeyHash, amount) acc, validOutputs := UTXOSet.FindSpendableOutputs(pubKeyHash, amount)
@ -221,6 +216,7 @@ func NewUTXOTransaction(from, to string, amount int, UTXOSet *UTXOSet) *Transact
} }
// Build a list of outputs // Build a list of outputs
from := fmt.Sprintf("%s", wallet.GetAddress())
outputs = append(outputs, *NewTXOutput(amount, to)) outputs = append(outputs, *NewTXOutput(amount, to))
if acc > amount { if acc > amount {
outputs = append(outputs, *NewTXOutput(acc-amount, from)) // a change outputs = append(outputs, *NewTXOutput(acc-amount, from)) // a change

View File

@ -12,7 +12,6 @@ import (
) )
const version = byte(0x00) const version = byte(0x00)
const walletFile = "wallet.dat"
const addressChecksumLen = 4 const addressChecksumLen = 4
// Wallet stores private and public keys // Wallet stores private and public keys

View File

@ -10,13 +10,15 @@ import (
"os" "os"
) )
const walletFile = "wallet_%s.dat"
// Wallets stores a collection of wallets // Wallets stores a collection of wallets
type Wallets struct { type Wallets struct {
Wallets map[string]*Wallet Wallets map[string]*Wallet
} }
// 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() (*Wallets, error) { func NewWallets(nodeID string) (*Wallets, error) {
wallets := Wallets{} wallets := Wallets{}
wallets.Wallets = make(map[string]*Wallet) wallets.Wallets = make(map[string]*Wallet)