@@ -7,35 +7,41 @@ package integration
77
88import (
99 "context"
10+ "crypto/ecdsa"
1011 "math/big"
1112 "os"
1213 "testing"
1314
15+ "github.com/cartesi/rollups-node/internal/config"
1416 "github.com/cartesi/rollups-node/internal/kms"
17+ "github.com/cartesi/rollups-node/pkg/ethutil"
1518
1619 "github.com/aws/aws-sdk-go-v2/aws"
17- "github.com/aws/aws-sdk-go-v2/config"
20+ awscfg "github.com/aws/aws-sdk-go-v2/config"
1821 awskms "github.com/aws/aws-sdk-go-v2/service/kms"
1922 kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types"
23+ "github.com/ethereum/go-ethereum/ethclient"
2024 "github.com/ethereum/go-ethereum/common"
2125 "github.com/ethereum/go-ethereum/core/types"
26+ "github.com/ethereum/go-ethereum/crypto"
2227 "github.com/stretchr/testify/require"
2328)
2429
25- func TestLocalStackAWSTransactOptsFactory (t * testing.T ) {
30+ func setupTesting (t * testing.T ) (* awskms.Client , * awskms.CreateKeyOutput ) {
31+ t .Helper ()
32+
2633 endpoint := os .Getenv ("LOCALSTACK_KMS_ENDPOINT" )
2734 if endpoint == "" {
2835 t .Skip ("LOCALSTACK_KMS_ENDPOINT is not set; skipping LocalStack KMS integration test" )
2936 }
30- ctx := context .Background ()
31- cfg , err := config .LoadDefaultConfig (ctx ,
32- config .WithRegion ("us-east-1" ),
33- config .WithBaseEndpoint (endpoint ),
37+ cfg , err := awscfg .LoadDefaultConfig (t .Context (),
38+ awscfg .WithRegion ("us-east-1" ),
39+ awscfg .WithBaseEndpoint (endpoint ),
3440 )
3541 require .NoError (t , err )
3642 client := awskms .NewFromConfig (cfg )
3743
38- created , err := client .CreateKey (ctx , & awskms.CreateKeyInput {
44+ created , err := client .CreateKey (t . Context () , & awskms.CreateKeyInput {
3945 KeyUsage : kmstypes .KeyUsageTypeSignVerify ,
4046 KeySpec : kmstypes .KeySpecEccSecgP256k1 ,
4147 })
@@ -49,13 +55,70 @@ func TestLocalStackAWSTransactOptsFactory(t *testing.T) {
4955 require .NotNil (t , created .KeyMetadata )
5056 require .NotNil (t , created .KeyMetadata .KeyId )
5157
58+ return client , created
59+ }
60+
61+ func sendFunds (
62+ t * testing.T ,
63+ value * big.Int ,
64+ SignTx kms.SignTxFn ,
65+ sender common.Address ,
66+ recipient common.Address ,
67+ ) {
68+ ethEndpoint , err := config .GetBlockchainHttpEndpoint ()
69+ require .NoError (t , err )
70+ client , err := ethclient .Dial (ethEndpoint .Raw ()) // anvil
71+ require .NoError (t , err )
72+
73+ nonce , err := client .PendingNonceAt (t .Context (), sender )
74+ require .NoError (t , err )
75+ gasLimit := uint64 (21000 )
76+ gasPrice , err := client .SuggestGasPrice (t .Context ())
77+ require .NoError (t , err )
78+ var data []byte
79+ tx := types .NewTransaction (nonce , recipient , value , gasLimit , gasPrice , data )
80+ chainID , err := client .NetworkID (t .Context ())
81+ require .NoError (t , err )
82+ signedTx , err := SignTx (t .Context (), tx , types .LatestSignerForChainID (chainID ))
83+ require .NoError (t , err )
84+ err = client .SendTransaction (t .Context (), signedTx )
85+ require .NoError (t , err )
86+ }
87+
88+ func TestLocalStackAWSSignTx (t * testing.T ) {
89+ client , created := setupTesting (t )
90+
91+ awsSignTx , _ , awsAddress , err := kms .CreateAWSSignTxFn (
92+ t .Context (), client , created .KeyMetadata .KeyId ,
93+ )
94+ require .NoError (t , err )
95+
96+ value20 := big .NewInt (2000000000000000000 ) // in wei (2 eth)
97+ value10 := big .NewInt (1000000000000000000 ) // in wei (1 eth)
98+ anvilPrivateKey , err := ethutil .MnemonicToPrivateKey (ethutil .FoundryMnemonic , 0 )
99+ if err != nil {
100+ panic (err )
101+ }
102+ anvilPublicKey := anvilPrivateKey .Public ().(* ecdsa.PublicKey )
103+ anvilAddress := crypto .PubkeyToAddress (* anvilPublicKey )
104+ anvilSignTx := func (_ context.Context , tx * types.Transaction , s types.Signer ) (* types.Transaction , error ) {
105+ return types .SignTx (tx , s , anvilPrivateKey )
106+ }
107+
108+ sendFunds (t , value20 , anvilSignTx , anvilAddress , awsAddress )
109+ sendFunds (t , value10 , awsSignTx , awsAddress , anvilAddress )
110+ }
111+
112+ func TestLocalStackAWSTransactionOptsFactory (t * testing.T ) {
113+ client , created := setupTesting (t )
114+
52115 chainID := big .NewInt (31337 )
53116 factory , err := kms .CreateAWSTransactOptsFactory (
54- ctx , client , created .KeyMetadata .KeyId , types .LatestSignerForChainID (chainID ),
117+ t . Context () , client , created .KeyMetadata .KeyId , types .LatestSignerForChainID (chainID ),
55118 )
56119 require .NoError (t , err )
57120 require .NotEqual (t , common.Address {}, factory .From ())
58- opts , err := factory .NewTransactOpts (ctx )
121+ opts , err := factory .NewTransactOpts (t . Context () )
59122 require .NoError (t , err )
60123
61124 to := common.Address {0x01 }
@@ -78,7 +141,7 @@ func TestLocalStackAWSTransactOptsFactory(t *testing.T) {
78141 })
79142 }
80143
81- _ , _ = client .ScheduleKeyDeletion (ctx , & awskms.ScheduleKeyDeletionInput {
144+ _ , _ = client .ScheduleKeyDeletion (t . Context () , & awskms.ScheduleKeyDeletionInput {
82145 KeyId : created .KeyMetadata .KeyId , PendingWindowInDays : aws .Int32 (7 ), //nolint:mnd
83146 })
84147}
0 commit comments