diff --git a/cli.go b/cli.go index 7441e3e..00e72d2 100644 --- a/cli.go +++ b/cli.go @@ -118,11 +118,11 @@ func (cli *CLI) Run() { } if createWalletCmd.Parsed() { - cli.createWallet() + cli.createWallet(nodeID) } if listAddressesCmd.Parsed() { - cli.listAddresses() + cli.listAddresses(nodeID) } if printChainCmd.Parsed() { diff --git a/cli_createwallet.go b/cli_createwallet.go index b42a142..57f74e3 100644 --- a/cli_createwallet.go +++ b/cli_createwallet.go @@ -2,8 +2,8 @@ package main import "fmt" -func (cli *CLI) createWallet() { - wallets, _ := NewWallets() +func (cli *CLI) createWallet(nodeID string) { + wallets, _ := NewWallets(nodeID) address := wallets.CreateWallet() wallets.SaveToFile() diff --git a/cli_listaddress.go b/cli_listaddress.go index 96fd282..0d30563 100644 --- a/cli_listaddress.go +++ b/cli_listaddress.go @@ -5,8 +5,8 @@ import ( "log" ) -func (cli *CLI) listAddresses() { - wallets, err := NewWallets() +func (cli *CLI) listAddresses(nodeID string) { + wallets, err := NewWallets(nodeID) if err != nil { log.Panic(err) } diff --git a/cli_send.go b/cli_send.go index a7c6f72..4e5a59a 100644 --- a/cli_send.go +++ b/cli_send.go @@ -17,7 +17,13 @@ func (cli *CLI) send(from, to string, amount int, nodeID string) { UTXOSet := UTXOSet{bc} 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, "") txs := []*Transaction{cbTx, tx} diff --git a/transaction.go b/transaction.go index 67ba940..0da2117 100644 --- a/transaction.go +++ b/transaction.go @@ -191,15 +191,10 @@ func NewCoinbaseTX(to, data string) *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 outputs []TXOutput - wallets, err := NewWallets() - if err != nil { - log.Panic(err) - } - wallet := wallets.GetWallet(from) pubKeyHash := HashPubKey(wallet.PublicKey) 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 + from := fmt.Sprintf("%s", wallet.GetAddress()) outputs = append(outputs, *NewTXOutput(amount, to)) if acc > amount { outputs = append(outputs, *NewTXOutput(acc-amount, from)) // a change diff --git a/wallet.go b/wallet.go index 31a2258..506b544 100644 --- a/wallet.go +++ b/wallet.go @@ -12,7 +12,6 @@ import ( ) const version = byte(0x00) -const walletFile = "wallet.dat" const addressChecksumLen = 4 // Wallet stores private and public keys diff --git a/wallets.go b/wallets.go index e5b2134..34d4799 100644 --- a/wallets.go +++ b/wallets.go @@ -10,13 +10,15 @@ import ( "os" ) +const walletFile = "wallet_%s.dat" + // 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 -func NewWallets() (*Wallets, error) { +func NewWallets(nodeID string) (*Wallets, error) { wallets := Wallets{} wallets.Wallets = make(map[string]*Wallet)