forked from welthee/go-ethereum-aws-kms-tx-signer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
typed_data_signer_test.go
108 lines (95 loc) · 3.14 KB
/
typed_data_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package ethawskmssigner_test
import (
"context"
"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/credentials"
"github.com/aws/aws-sdk-go-v2/service/kms"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"github.com/stretchr/testify/assert"
)
func TestSignTypedData(t *testing.T) {
ctx := context.Background()
awsCfg, err := config.LoadDefaultConfig(context.Background(),
config.WithRegion("ap-northeast-1"),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(
"<AWS_ACCESS_KEY_ID>",
"<AWS_SECRET_ACCESS_KEY>",
"",
)),
)
if err != nil {
t.Fatalf("Failed to load AWS config: %v", err)
}
kmsSvc := kms.NewFromConfig(awsCfg)
client, err := ethclient.Dial(ethAddr)
if err != nil {
t.Fatalf("Failed to connect to Ethereum client: %v", err)
}
chainID, err := client.ChainID(ctx)
if err != nil {
t.Fatalf("Failed to get chain ID: %v", err)
}
signer, err := ethawskmssigner.NewAwsKmsTypedDataSigner(kmsSvc, keyId, chainID)
if err != nil {
t.Fatalf("Failed to create AwsKmsTypedDataSigner: %v", err)
}
// EIP-712 typed data
typedData := apitypes.TypedData{
Types: apitypes.Types{
"EIP712Domain": {
{Name: "name", Type: "string"},
{Name: "version", Type: "string"},
{Name: "chainId", Type: "uint256"},
{Name: "verifyingContract", Type: "address"},
},
"Person": {
{Name: "name", Type: "string"},
{Name: "wallet", Type: "address"},
},
"Mail": {
{Name: "from", Type: "Person"},
{Name: "to", Type: "Person"},
{Name: "contents", Type: "string"},
},
},
PrimaryType: "Mail",
Domain: apitypes.TypedDataDomain{
Name: "Ether Mail",
Version: "1",
ChainId: (*math.HexOrDecimal256)(chainID),
VerifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
Message: apitypes.TypedDataMessage{
"from": map[string]interface{}{
"name": "Cow",
"wallet": "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
},
"to": map[string]interface{}{
"name": "Bob",
"wallet": "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
},
"contents": "Hello, Bob!",
},
}
signature, err := signer.SignTypedData(typedData)
if err != nil {
t.Fatalf("Failed to sign typed data: %v", err)
}
// Verify the signature length
assert.Equal(t, 65, len(signature), "Signature length should be 65 bytes")
// Print the signature for manual verification if needed
t.Logf("Signature: 0x%s", hexutil.Encode(signature))
hash, err := ethawskmssigner.EncodeForSigning(typedData)
if err != nil {
t.Fatalf("Failed to encode typed data for signing: %v", err)
}
recovered := ethawskmssigner.VerifySig(signer.Address().Hex(), hexutil.Encode(signature), hash.Bytes())
assert.True(t, recovered, "Signature should be verified")
recovered = ethawskmssigner.VerifySig(anotherEthAddr, hexutil.Encode(signature), hash.Bytes())
assert.False(t, recovered, "Signature should fail verification with wrong address")
}