forked from welthee/go-ethereum-aws-kms-tx-signer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signer_test.go
71 lines (56 loc) · 1.77 KB
/
signer_test.go
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
package ethawskmssigner_test
import (
"context"
"log"
"math/big"
"testing"
ethawskmssigner "github.com/0xSyndr/go-ethereum-aws-kms-tx-signer"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)
const keyId = "331c7988-c19b-4e30-8037-530389c92ac0"
const anotherEthAddr = "0xeB7eb6c156ac20a9c45beFDC95F1A13625B470b7"
const ethAddr = "wss://arb-sepolia.g.alchemy.com/v2/G_tXRCm6bn_Ii9WVfVmLyLk4BQ2eh73I"
func TestSigning(t *testing.T) {
ctx := context.Background()
awsCfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
log.Fatal(err)
}
kmsSvc := kms.NewFromConfig(awsCfg)
client, err := ethclient.Dial(ethAddr)
if err != nil {
log.Fatal(err)
}
clChainId, _ := client.ChainID(ctx)
transactOpts, err := ethawskmssigner.NewAwsKmsTransactorWithChainIDCtx(ctx, kmsSvc, keyId, clChainId)
if err != nil {
log.Fatalf("can not sign: %s", err)
}
nonce, err := client.PendingNonceAt(context.Background(), transactOpts.From)
if err != nil {
log.Fatal(err)
}
toAddress := common.HexToAddress(anotherEthAddr)
suggestedGasPrice, _ := client.SuggestGasPrice(ctx)
suggestedGasLimit, err := client.EstimateGas(ctx, ethereum.CallMsg{To: &toAddress, Data: nil})
if err != nil {
log.Fatal(err)
}
value := big.NewInt(10)
gasLimit := suggestedGasLimit
gasPrice := suggestedGasPrice
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, nil)
signedTx, err := transactOpts.Signer(transactOpts.From, tx)
if err != nil {
log.Fatal(err)
}
err = client.SendTransaction(ctx, signedTx)
if err != nil {
log.Fatalf("can not send tx %s", err)
}
}