Skip to content

Instantly share code, notes, and snippets.

@jaanek
Created August 9, 2021 13:55
Show Gist options
  • Save jaanek/0f941d2c6fd2e07fe513a4691dda602e to your computer and use it in GitHub Desktop.
Save jaanek/0f941d2c6fd2e07fe513a4691dda602e to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"fmt"
"math/big"
"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/rlp"
)
func main() {
b, err := buildSignedRawTx()
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("%x\n", b)
// decode tx
tx, err := decodeRlpTx(b)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("%+v\n", tx)
}
func buildSignedRawTx() ([]byte, error) {
var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
dynTx := types.DynamicFeeTransaction{
ChainID: uint256.NewInt(1),
CommonTx: types.CommonTx{
Nonce: 1,
To: &testAddr,
Value: uint256.NewInt(10),
Gas: 25000,
Data: common.FromHex("5544"),
},
Tip: uint256.NewInt(1),
FeeCap: uint256.NewInt(1),
}
signer := types.LatestSignerForChainID(big.NewInt(1))
tx, err := types.SignTx(&dynTx, *signer, testKey)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
err = tx.MarshalBinary(buf)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
// _, err = api.SendRawTransaction(ctx, buf.Bytes())
}
func decodeRlpTx(b []byte) (types.Transaction, error) {
rlpData := hexutil.Bytes(b[:])
tx, err := types.DecodeTransaction(rlp.NewStream(bytes.NewReader(rlpData), 0))
if err != nil {
return nil, err
}
signer := types.LatestSignerForChainID(big.NewInt(1))
sender, err := tx.Sender(*signer)
if err != nil {
return nil, err
}
tx.SetSender(sender)
return tx, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment