forked from getamis/alice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrawTransaction.go
More file actions
92 lines (77 loc) · 2.93 KB
/
rawTransaction.go
File metadata and controls
92 lines (77 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"crypto/ecdsa"
"encoding/hex"
"fmt"
"log"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rlp"
//"github.com/ethereum/go-ethereum/common/hexutil"
)
func main() {
client, err := ethclient.Dial("https://ropsten.infura.io/v3/375a84d45ba0456a8d39a32cce31471c")
if err != nil {
log.Fatal(err)
}
privateKey, err := crypto.HexToECDSA("9F0DC1063B5D8BAB2A985056A09A1A669C005CD638335C17CD99AAE9618CEB89")
if err != nil {
log.Fatal(err)
}
publicKey := privateKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
log.Fatal("error casting public key to ECDSA")
}
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
fmt.Printf("fromAddress: %d\n", fromAddress)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
log.Fatal(err)
}
//fmt.Printf("%d\n", nonce)
//var nonce hexutil.Uint644
//return uint64(nonce)
//r: 113435503106888623780845422972399380807025119313940195470264666760611920129301 s: 47105817020856271236461922630442430066243070903082139113143856007077248751442
value := big.NewInt(1000000000000000) // in wei (0.001 eth)
gasLimit := uint64(21000) // in units
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Printf("%d\n", gasPrice)
toAddress := common.HexToAddress("0x743376fd2a693723A60942D0b4B2F1765ea1Dbb0")
var data []byte
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, data)
chainID, err := client.NetworkID(context.Background())
if err != nil {
log.Fatal(err)
}
// s = types.NewEIP155Signer(chainID)
signedTx, err := types.SignTx(tx, types.NewEIP155Signer(chainID), privateKey)
if err != nil {
log.Fatal(err)
}
r := new(big.Int)
s := new(big.Int)
v := new(big.Int)
v, r, s = signedTx.RawSignatureValues()
fmt.Printf("r: %d s: %d v: %d\n", r,s,v)
ts := types.Transactions{signedTx}
rawTx := hex.EncodeToString(ts.GetRlp(0))
fmt.Printf(rawTx) // f86...772
fmt.Printf("\n")
//rawTx := "f86b808502540be40082520894743376fd2a693723a60942d0b4b2f1765ea1dbb087038d7ea4c680008029a0149d5ada14f2afbafc81e2b166f8eebd078bdd49c93fb240fbfc1e156e96cf52a00c8910089e4a97c33b3f35c35d86c7d0b2df9b736516740ea095d3d51282"
var txs *types.Transaction
rawTxBytes, err := hex.DecodeString(rawTx)
rlp.DecodeBytes(rawTxBytes, &txs)
err = client.SendTransaction(context.Background(), txs)
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s", txs.Hash().Hex()) // tx sent: 0xc429e5f128387d224ba8bed6885e86525e14bfdc2eb24b5e9c3351a1176fd81f
}