|
| 1 | +// (c) Cartesi and individual authors (see AUTHORS) |
| 2 | +// SPDX-License-Identifier: Apache-2.0 (see LICENSE) |
| 3 | + |
| 4 | +//go:build endtoendtests |
| 5 | + |
| 6 | +package integration |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "math/big" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/cartesi/rollups-node/internal/kms" |
| 15 | + |
| 16 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 17 | + "github.com/aws/aws-sdk-go-v2/config" |
| 18 | + awskms "github.com/aws/aws-sdk-go-v2/service/kms" |
| 19 | + kmstypes "github.com/aws/aws-sdk-go-v2/service/kms/types" |
| 20 | + "github.com/ethereum/go-ethereum/common" |
| 21 | + "github.com/ethereum/go-ethereum/core/types" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestLocalStackAWSTransactOptsFactory(t *testing.T) { |
| 26 | + endpoint := os.Getenv("LOCALSTACK_KMS_ENDPOINT") |
| 27 | + if endpoint == "" { |
| 28 | + t.Skip("LOCALSTACK_KMS_ENDPOINT is not set; skipping LocalStack KMS integration test") |
| 29 | + } |
| 30 | + ctx := context.Background() |
| 31 | + cfg, err := config.LoadDefaultConfig(ctx, |
| 32 | + config.WithRegion("us-east-1"), |
| 33 | + config.WithBaseEndpoint(endpoint), |
| 34 | + ) |
| 35 | + require.NoError(t, err) |
| 36 | + client := awskms.NewFromConfig(cfg) |
| 37 | + |
| 38 | + created, err := client.CreateKey(ctx, &awskms.CreateKeyInput{ |
| 39 | + KeyUsage: kmstypes.KeyUsageTypeSignVerify, |
| 40 | + KeySpec: kmstypes.KeySpecEccSecgP256k1, |
| 41 | + }) |
| 42 | + if err != nil { |
| 43 | + message := "unable to create key on LocalStack" |
| 44 | + if os.Getenv("LOCALSTACK_KMS_REQUIRED") == "true" { |
| 45 | + t.Fatalf("%s: %v", message, err) |
| 46 | + } |
| 47 | + t.Skipf("%s: %v", message, err) |
| 48 | + } |
| 49 | + require.NotNil(t, created.KeyMetadata) |
| 50 | + require.NotNil(t, created.KeyMetadata.KeyId) |
| 51 | + |
| 52 | + chainID := big.NewInt(31337) |
| 53 | + factory, err := kms.CreateAWSTransactOptsFactory( |
| 54 | + ctx, client, created.KeyMetadata.KeyId, types.LatestSignerForChainID(chainID), |
| 55 | + ) |
| 56 | + require.NoError(t, err) |
| 57 | + require.NotEqual(t, common.Address{}, factory.From()) |
| 58 | + opts, err := factory.NewTransactOpts(ctx) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + to := common.Address{0x01} |
| 62 | + tests := map[string]*types.Transaction{ |
| 63 | + "legacy": types.NewTx(&types.LegacyTx{ |
| 64 | + Nonce: 1, GasPrice: big.NewInt(2), Gas: 21000, To: &to, Value: big.NewInt(3), |
| 65 | + }), |
| 66 | + "dynamic fee": types.NewTx(&types.DynamicFeeTx{ |
| 67 | + ChainID: chainID, Nonce: 2, GasTipCap: big.NewInt(1), GasFeeCap: big.NewInt(2), |
| 68 | + Gas: 21000, To: &to, Value: big.NewInt(3), |
| 69 | + }), |
| 70 | + } |
| 71 | + for name, tx := range tests { |
| 72 | + t.Run(name, func(t *testing.T) { |
| 73 | + signed, err := opts.Signer(opts.From, tx) |
| 74 | + require.NoError(t, err) |
| 75 | + sender, err := types.Sender(types.LatestSignerForChainID(chainID), signed) |
| 76 | + require.NoError(t, err) |
| 77 | + require.Equal(t, factory.From(), sender) |
| 78 | + }) |
| 79 | + } |
| 80 | + |
| 81 | + _, _ = client.ScheduleKeyDeletion(ctx, &awskms.ScheduleKeyDeletionInput{ |
| 82 | + KeyId: created.KeyMetadata.KeyId, PendingWindowInDays: aws.Int32(7), //nolint:mnd |
| 83 | + }) |
| 84 | +} |
0 commit comments