Skip to content

Commit

Permalink
add genesis id to transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
algomaxj committed Mar 7, 2019
1 parent 3ce7a5b commit c848079
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,14 @@ func main() {
}
fmt.Println("Made a kmd client")

// Create an algod client
algodClient, err := algod.MakeClient(algodAddress, algodToken)
if err != nil {
fmt.Printf("failed to make algod client: %s\n", err)
return
}
fmt.Println("Made an algod client")

// Get the list of wallets
listResponse, err := kmdClient.ListWallets()
if err != nil {
Expand Down Expand Up @@ -388,8 +396,16 @@ func main() {
fmt.Printf("Generated address 2 %s\n", gen2Response.Address)
toAddr := gen2Response.Address

// Get the suggested transaction parameters
txParams, err := algodClient.SuggestedParams()
if err != nil {
fmt.Printf("error getting suggested tx params: %s\n", err)
return
}

// Make transaction
tx, err := transaction.MakePaymentTxn(fromAddr, toAddr, 1, 100, 300, 400, nil)
genID := txParams.GenesisID
tx, err := transaction.MakePaymentTxn(fromAddr, toAddr, 1, 100, 300, 400, nil, genID)
if err != nil {
fmt.Printf("Error creating transaction: %s\n", err)
return
Expand All @@ -404,13 +420,6 @@ func main() {

fmt.Printf("kmd made signed transaction with bytes: %x\n", signResponse.SignedTransaction)

// Create an algod client
algodClient, err := algod.MakeClient(algodAddress, algodToken)
if err != nil {
fmt.Printf("failed to make algod client: %s\n", err)
return
}

// Broadcast the transaction to the network
// Note that this transaction will get rejected because the accounts do not have any tokens
sendResponse, err := algodClient.SendRawTransaction(signResponse.SignedTransaction)
Expand Down
26 changes: 26 additions & 0 deletions client/algod/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,32 @@ type TransactionFee struct {
Fee uint64 `json:"fee"`
}

// TransactionParams contains the parameters that help a client construct
// a new transaction.
// swagger:model TransactionParams
type TransactionParams struct {
// Fee is the suggested transaction fee
//
// required: true
Fee uint64 `json:"fee"`

// Genesis ID
//
// required: true
GenesisID string `json:"genesisID"`

// LastRound indicates the last round seen
//
// required: true
LastRound uint64 `json:"lastRound"`

// ConsensusVersion indicates the consensus protocol version
// as of LastRound.
//
// required: true
ConsensusVersion string `json:"consensusVersion"`
}

// TransactionID Description
// swagger:model TransactionID
type TransactionID struct {
Expand Down
6 changes: 6 additions & 0 deletions client/algod/wrappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func (client Client) SuggestedFee() (response models.TransactionFee, err error)
return
}

// SuggestedParams gets the suggested transaction parameters
func (client Client) SuggestedParams() (response models.TransactionParams, err error) {
err = client.get(&response, "/transactions/params", nil)
return
}

// SendRawTransaction gets the bytes of a SignedTxn and broadcasts it to the network
func (client Client) SendRawTransaction(stx []byte) (response models.TransactionID, err error) {
err = client.post(&response, "/transactions", stx)
Expand Down
12 changes: 10 additions & 2 deletions examples/gen-addresses/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,24 @@ func main() {
}

// Extract the private key of the first address
fmt.Printf("Extracting private key for %s", addresses[0])
fmt.Printf("Extracting private key for %s\n", addresses[0])
resp4, err := kmdClient.ExportKey(exampleWalletHandleToken, exampleWalletPassword, addresses[0])
if err != nil {
fmt.Printf("Error extracting secret key: %s\n", err)
return
}
privateKey := resp4.PrivateKey

// Get the suggested transaction parameters
txParams, err := algodClient.SuggestedParams()
if err != nil {
fmt.Printf("error getting suggested tx params: %s\n", err)
return
}

// Sign a sample transaction using this library, *not* kmd
tx, err := transaction.MakePaymentTxn(addresses[0], addresses[1], 1, 100, 300, 400, nil)
genID := txParams.GenesisID
tx, err := transaction.MakePaymentTxn(addresses[0], addresses[1], 1, 100, 300, 400, nil, genID)
if err != nil {
fmt.Printf("Error creating transaction: %s\n", err)
return
Expand Down
3 changes: 2 additions & 1 deletion transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

// MakePaymentTxn constructs a payment transaction using the passed parameters.
// `from` and `to` addresses should be checksummed, human-readable addresses
func MakePaymentTxn(from, to string, fee, amount, firstRound, lastRound uint64, note []byte) (tx types.Transaction, err error) {
func MakePaymentTxn(from, to string, fee, amount, firstRound, lastRound uint64, note []byte, genesisID string) (tx types.Transaction, err error) {
// Decode from address
fromAddr, err := types.DecodeAddress(from)
if err != nil {
Expand All @@ -28,6 +28,7 @@ func MakePaymentTxn(from, to string, fee, amount, firstRound, lastRound uint64,
FirstValid: types.Round(firstRound),
LastValid: types.Round(lastRound),
Note: note,
GenesisID: genesisID,
},
PaymentTxnFields: types.PaymentTxnFields{
Receiver: toAddr,
Expand Down
1 change: 1 addition & 0 deletions types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ type Header struct {
FirstValid Round `codec:"fv"`
LastValid Round `codec:"lv"`
Note []byte `codec:"note"`
GenesisID string `codec:"gen"`
}

0 comments on commit c848079

Please sign in to comment.