-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Transaction Command #120
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8c6eb26
added transaction creation at console; removed hotwallet
chadlagore e8c8e72
add broadcast; add pending txns
chadlagore 90e8ccc
added pending functions
chadlagore 19ddb85
comments; handle transaction
chadlagore 284cd0f
use push method directly
chadlagore 33557a3
testing app.Pay, two cases handled
chadlagore 25856b1
re-enumerate steps
chadlagore f12ebbc
error language
chadlagore 7b02913
add test for getting balances
chadlagore f06601d
comments
chadlagore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,9 @@ import ( | |
crand "crypto/rand" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"errors" | ||
"io" | ||
"math" | ||
"math/big" | ||
|
||
c "github.com/ubclaunchpad/cumulus/common/constants" | ||
|
@@ -18,6 +20,8 @@ const ( | |
CoordLen = 32 | ||
// AddrLen is the length in bytes of addresses. | ||
AddrLen = 2 * CoordLen | ||
// ReprLen is the length in bytes of an address checksum. | ||
ReprLen = 40 | ||
// SigLen is the length in bytes of signatures. | ||
SigLen = AddrLen | ||
// AddressVersion is the version of the address shortening protocol. | ||
|
@@ -93,27 +97,32 @@ func (a Address) Key() *ecdsa.PublicKey { | |
} | ||
} | ||
|
||
// Wallet represents a wallet that we have the ability to sign for. | ||
type Wallet interface { | ||
// Account represents a wallet that we have the ability to sign for. | ||
type Account interface { | ||
Public() Address | ||
Sign(digest Hash, random io.Reader) (Signature, error) | ||
Debit(amount uint64) error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to above, I don't think we should modify a user account directly, it should be a function of the blockchain and should be recomputed each time we add a block to the chain. |
||
Credit(amount uint64) error | ||
} | ||
|
||
// Internal representation of a wallet. | ||
type wallet ecdsa.PrivateKey | ||
// Wallet is an account that can sign and hold a balance. | ||
type Wallet struct { | ||
*ecdsa.PrivateKey | ||
Balance uint64 | ||
} | ||
|
||
// Key retreives the underlying private key from a wallet. | ||
func (w *wallet) key() *ecdsa.PrivateKey { | ||
return (*ecdsa.PrivateKey)(w) | ||
func (w *Wallet) key() *ecdsa.PrivateKey { | ||
return w.PrivateKey | ||
} | ||
|
||
// Public returns the public key as byte array, or address, of the wallet. | ||
func (w *wallet) Public() Address { | ||
return Address{X: w.PublicKey.X, Y: w.PublicKey.Y} | ||
func (w *Wallet) Public() Address { | ||
return Address{X: w.PrivateKey.PublicKey.X, Y: w.PrivateKey.PublicKey.Y} | ||
} | ||
|
||
// Sign returns a signature of the digest. | ||
func (w *wallet) Sign(digest Hash, random io.Reader) (Signature, error) { | ||
func (w *Wallet) Sign(digest Hash, random io.Reader) (Signature, error) { | ||
r, s, err := ecdsa.Sign(random, w.key(), digest.Marshal()) | ||
return Signature{R: r, S: s}, err | ||
} | ||
|
@@ -147,9 +156,34 @@ func (s *Signature) Marshal() []byte { | |
return buf | ||
} | ||
|
||
// NewWallet produces a new Wallet that can sign transactionsand has a | ||
// NewWallet produces a new Wallet that can sign transactions and has a | ||
// public Address. | ||
func NewWallet() Wallet { | ||
func NewWallet() *Wallet { | ||
priv, _ := ecdsa.GenerateKey(curve, crand.Reader) | ||
return (*wallet)(priv) | ||
return &Wallet{ | ||
PrivateKey: priv, | ||
Balance: 0, | ||
} | ||
} | ||
|
||
// Debit attempts to spend some coin from the wallet. | ||
func (w *Wallet) Debit(amount uint64) error { | ||
if amount >= w.Balance { | ||
return errors.New("wallet does not have a high enough balance to satisfy the request") | ||
} else if amount < 0 { | ||
return errors.New("debit amounts may not be negative") | ||
} | ||
w.Balance -= amount | ||
return nil | ||
} | ||
|
||
// Credit attempts to add count to the wallet. | ||
func (w *Wallet) Credit(amount uint64) error { | ||
if amount >= math.MaxUint64 { | ||
return errors.New("wallet cannot hold this amount") | ||
} else if amount < 0 { | ||
return errors.New("credit amounts may not be negative") | ||
} | ||
w.Balance += amount | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to make some changes to this function.
We should:
Independently of this function, the balance of the current user's wallet should be computed each time a new block is added to our chain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, so the wallet balance is only updated when our transaction has landed in a block - makes total sense.