-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added transaction creation at console; removed hotwallet
- Loading branch information
1 parent
7eee1b7
commit 4283bf5
Showing
12 changed files
with
145 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ run-console: cumulus | |
deps: | ||
glide install | ||
|
||
clean: | ||
clean: cumulus | ||
rm cumulus | ||
|
||
install-glide: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,61 @@ | ||
package app | ||
|
||
import "github.com/ubclaunchpad/cumulus/blockchain" | ||
import ( | ||
"github.com/ubclaunchpad/cumulus/blockchain" | ||
|
||
crand "crypto/rand" | ||
) | ||
|
||
// User holds basic user information. | ||
type User struct { | ||
HotWallet | ||
*blockchain.Wallet | ||
BlockSize uint32 | ||
} | ||
|
||
// HotWallet is a representation of the users wallet. | ||
type HotWallet struct { | ||
Name string | ||
blockchain.Wallet | ||
} | ||
|
||
// NewUser creates a new user | ||
func NewUser() *User { | ||
return &User{ | ||
HotWallet: HotWallet{ | ||
Wallet: blockchain.NewWallet(), | ||
Name: "default", | ||
}, | ||
Wallet: blockchain.NewWallet(), | ||
BlockSize: blockchain.DefaultBlockSize, | ||
} | ||
} | ||
|
||
// getCurrentUser gets the current user. | ||
// getCurrentUser gets the current user function only used for app initalization. | ||
func getCurrentUser() *User { | ||
// TODO: Check for local user information on disk, | ||
// If doesnt exist, create new user. | ||
return NewUser() | ||
} | ||
|
||
// Pay pays an amount of coin to an address `to`. | ||
func (a *App) Pay(to string, amount uint64) error { | ||
// Three atomic steps must occur. | ||
// 1. The amount must be debited from the wallet. | ||
err := a.CurrentUser.Wallet.Debit(amount) | ||
if err == nil { | ||
return err | ||
} | ||
|
||
// 2. A legitimate transaction must be built. | ||
tbody := blockchain.TxBody{ | ||
Sender: getCurrentUser().Wallet.Public(), | ||
Outputs: []blockchain.TxOutput{ | ||
blockchain.TxOutput{ | ||
Recipient: to, | ||
Amount: amount, | ||
}, | ||
}, | ||
} | ||
|
||
// 3. The transaction must be signed and added to the pool. | ||
if txn, err := tbody.Sign(*a.CurrentUser.Wallet, crand.Reader); err != nil { | ||
a.HandleTransaction(txn) | ||
} else { | ||
// Signature failed, credit the account, return an error. | ||
// Crediting cannot fail because debiting succeeded and process is single threaded. | ||
a.CurrentUser.Wallet.Credit(amount) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.