From 082005828d9647ac6ecd2684a340e55f7b91f692 Mon Sep 17 00:00:00 2001 From: Miguel Mota Date: Sun, 23 Sep 2018 22:39:16 -0700 Subject: [PATCH] raw tx create/send --- code/transaction_raw_create.go | 5 ++-- code/transaction_raw_send.go | 4 +-- en/GLOSSARY.md | 4 +++ en/transaction-raw-create/README.md | 41 +++++++++++++++++++++++++++-- en/transaction-raw-send/README.md | 34 ++++++++++++++++++++++-- 5 files changed, 80 insertions(+), 8 deletions(-) diff --git a/code/transaction_raw_create.go b/code/transaction_raw_create.go index 8e5def9c..24f5f40e 100644 --- a/code/transaction_raw_create.go +++ b/code/transaction_raw_create.go @@ -59,7 +59,8 @@ func main() { } ts := types.Transactions{signedTx} - rawTx := hex.EncodeToString(ts.GetRlp(0)) + rawTxBytes := ts.GetRlp(0) + rawTxHex := hex.EncodeToString(rawTxBytes) - fmt.Printf(rawTx) // f86...772 + fmt.Printf(rawTxHex) // f86...772 } diff --git a/code/transaction_raw_send.go b/code/transaction_raw_send.go index 87bbac38..ae66727a 100644 --- a/code/transaction_raw_send.go +++ b/code/transaction_raw_send.go @@ -19,9 +19,9 @@ func main() { rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772" - var tx *types.Transaction - rawTxBytes, err := hex.DecodeString(rawTx) + + tx := new(types.Transaction) rlp.DecodeBytes(rawTxBytes, &tx) err = client.SendTransaction(context.Background(), tx) diff --git a/en/GLOSSARY.md b/en/GLOSSARY.md index 4f3926df..e28a0b9a 100644 --- a/en/GLOSSARY.md +++ b/en/GLOSSARY.md @@ -304,6 +304,10 @@ Sets of formal rules describing how to transmit or exchange data, especially acr A proof-of-authority testnet on the Ethereum blockchain. Supported by Geth only. +## RLP + +[Recursive Length Prefix](https://github.com/ethereum/wiki/wiki/RLP) (RLP) is a standard to encode arbitrarily nested arrays of binary data. RLP is the main encoding method used to serialize objects in Ethereum. + ## Ropsten A proof-of-work testnet on the Ethereum blockchain which best simulates production environment. Supported by Geth and Parity. diff --git a/en/transaction-raw-create/README.md b/en/transaction-raw-create/README.md index d373a9d6..316a7ba6 100644 --- a/en/transaction-raw-create/README.md +++ b/en/transaction-raw-create/README.md @@ -4,6 +4,42 @@ description: Tutorial on how to create a raw Ethereum transaction with Go. # Create Raw Transaction +If you've read the [previous sections](../transfer-eth), then you know how to load your private key to sign transactions. We'll assume you know how to do that by now and now you want to get the raw transaction data to be able to broadcast it at a later time. + +First construct the transaction object and sign it, for example: + +```go +tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data) + +signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey) +if err != nil { + log.Fatal(err) +} +``` + +Now before we can get the transaction in raw bytes format we'll need to initialize a `types.Transactions` type with the signed transaction as the first value. + +```go +ts := types.Transactions{signedTx} +``` + +The reason for doing this is because the `Transactions` type provides a `GetRlp` method for returning the transaction in RLP encoded format. RLP is a special encoding method Ethereum uses for serializing objects. The result of this is raw bytes. + +```go +rawTxBytes := ts.GetRlp(0) +``` + +Finally we can very easily turn the raw bytes into a hex string. + +```go +rawTxHex := hex.EncodeToString(rawTxBytes) + +fmt.Printf(rawTxHex) +// f86d8202b38477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ba0699ff162205967ccbabae13e07cdd4284258d46ec1051a70a51be51ec2bc69f3a04e6944d508244ea54a62ebf9a72683eeadacb73ad7c373ee542f1998147b220e +``` + +And now you have the raw transaction data which you can use to broadcast at a future date. In the [next section](../transaction-raw-send) we'll learn how to do broadcast a raw transaction. + --- ### Full code @@ -72,8 +108,9 @@ func main() { } ts := types.Transactions{signedTx} - rawTx := hex.EncodeToString(ts.GetRlp(0)) + rawTxBytes := ts.GetRlp(0) + rawTxHex := hex.EncodeToString(rawTxBytes) - fmt.Printf(rawTx) // f86...772 + fmt.Printf(rawTxHex) // f86...772 } ``` diff --git a/en/transaction-raw-send/README.md b/en/transaction-raw-send/README.md index 4c7e7f25..4e210751 100644 --- a/en/transaction-raw-send/README.md +++ b/en/transaction-raw-send/README.md @@ -4,6 +4,36 @@ description: Tutorial on how to send a raw Ethereum transaction with Go. # Send Raw Transaction +In the [previous section](../transaction-raw-create) we learned how to do create a raw transaction. Now we'll learn how to broadcast it to the Ethereum network in order for it to get processed and mined. + +First decode the raw transaction hex to bytes format. + +```go +rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772" + +rawTxBytes, err := hex.DecodeString(rawTx) +``` + +Now initialize a new `types.Transaction` pointer and call `DecodeBytes` from the go-ethereum `rlp` package passing it the raw transaction bytes and the pointer to the ethereum transaction type. RLP is an encoding method used by Ethereum to serialized and derialized data. + +```go +tx := new(types.Transaction) +rlp.DecodeBytes(rawTxBytes, &tx) +``` + +Now we can easily broadcast the transaction with our ethereum client. + +```go +err := client.SendTransaction(context.Background(), tx) +if err != nil { + log.Fatal(err) +} + +fmt.Printf("tx sent: %s", tx.Hash().Hex()) // tx sent: 0xc429e5f128387d224ba8bed6885e86525e14bfdc2eb24b5e9c3351a1176fd81f +``` + +You can see the transaction on etherscan: [https://rinkeby.etherscan.io/tx/0xc429e5f128387d224ba8bed6885e86525e14bfdc2eb24b5e9c3351a1176fd81f](https://rinkeby.etherscan.io/tx/0xc429e5f128387d224ba8bed6885e86525e14bfdc2eb24b5e9c3351a1176fd81f) + --- ### Full code @@ -32,9 +62,9 @@ func main() { rawTx := "f86d8202b28477359400825208944592d8f8d7b001e72cb26a73e4fa1806a51ac79d880de0b6b3a7640000802ca05924bde7ef10aa88db9c66dd4f5fb16b46dff2319b9968be983118b57bb50562a001b24b31010004f13d9a26b320845257a6cfc2bf819a3d55e3fc86263c5f0772" - var tx *types.Transaction - rawTxBytes, err := hex.DecodeString(rawTx) + + tx := new(types.Transaction) rlp.DecodeBytes(rawTxBytes, &tx) err = client.SendTransaction(context.Background(), tx)