-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
185 lines (164 loc) · 4.15 KB
/
utils.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package gelato
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var (
forwardRequestTypehash = crypto.Keccak256Hash([]byte("ForwardRequest(uint256 chainId,address target,bytes data,address feeToken,uint256 paymentType,uint256 maxFee,uint256 gas,address sponsor,uint256 sponsorChainId,uint256 nonce,bool enforceSponsorNonce,bool enforceSponsorNonceOrdering)"))
)
func padBytesLeft(in []byte, n int) []byte {
if len(in) > n {
return in // error probably
}
out := make([]byte, n-len(in))
return append(out, in...)
}
// GetFunctionSignature returns the 4-byte function signature of a Solidity function.
func GetFunctionSignature(fn string) []byte {
hash := crypto.Keccak256Hash([]byte(fn))
return hash[:4]
}
func getEIP712DomainSeparator(name, version []byte, chainID *big.Int, address ethcommon.Address) ([32]byte, error) {
bytes32Ty, err := abi.NewType("bytes32", "", nil)
if err != nil {
return [32]byte{}, fmt.Errorf("failed to create bytes32 type: %w", err)
}
addressTy, err := abi.NewType("address", "", nil)
if err != nil {
return [32]byte{}, fmt.Errorf("failed to create address type: %w", err)
}
eip712DomainHash := crypto.Keccak256Hash([]byte("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"))
var chainIDArr [32]byte
copy(chainIDArr[:], padBytesLeft(chainID.Bytes(), 32))
args := &abi.Arguments{
{
Type: bytes32Ty,
},
{
Type: bytes32Ty,
},
{
Type: bytes32Ty,
},
{
Type: bytes32Ty,
},
{
Type: addressTy,
},
}
domainSeparatorPreimage, err := args.Pack(
eip712DomainHash,
crypto.Keccak256Hash(name),
crypto.Keccak256Hash(version),
chainIDArr,
address,
)
if err != nil {
return [32]byte{}, fmt.Errorf("failed to create domainSeparatorPreimage: %w", err)
}
return crypto.Keccak256Hash(domainSeparatorPreimage), nil
}
// GetForwardRequestDigestToSign returns a 32-byte digest for signing
func GetForwardRequestDigestToSign(req *ForwardRequest) ([32]byte, error) {
relayAddress := getRelayForwarderAddress(req.ChainID)
domainSeparator, err := getEIP712DomainSeparator(
[]byte("GelatoRelayForwarder"),
[]byte("V1"),
req.ChainID,
relayAddress,
)
if err != nil {
return [32]byte{}, err
}
uint256Ty, err := abi.NewType("uint256", "", nil)
if err != nil {
return [32]byte{}, fmt.Errorf("failed to create uint256 type: %w", err)
}
bytes32Ty, err := abi.NewType("bytes32", "", nil)
if err != nil {
return [32]byte{}, fmt.Errorf("failed to create bytes32 type: %w", err)
}
addressTy, err := abi.NewType("address", "", nil)
if err != nil {
return [32]byte{}, fmt.Errorf("failed to create address type: %w", err)
}
boolTy, err := abi.NewType("bool", "", nil)
if err != nil {
return [32]byte{}, fmt.Errorf("failed to create bool type: %w", err)
}
// https://github.com/gelatodigital/relay-sdk/blob/8a9b9b2d0ef92ea9a3d6d64a230d9467a4b4da6d/src/constants/index.ts#L14
hashedData := crypto.Keccak256Hash(req.Data)
args := &abi.Arguments{
{
Type: bytes32Ty,
},
{
Type: uint256Ty,
},
{
Type: addressTy,
},
{
Type: bytes32Ty,
},
{
Type: addressTy,
},
{
Type: uint256Ty,
},
{
Type: uint256Ty,
},
{
Type: uint256Ty,
},
{
Type: addressTy,
},
{
Type: uint256Ty,
},
{
Type: uint256Ty,
},
{
Type: boolTy,
},
{
Type: boolTy,
},
}
hashPreimage, err := args.Pack(
forwardRequestTypehash,
req.ChainID,
req.Target,
hashedData,
ethcommon.HexToAddress(req.FeeToken),
req.PaymentType,
req.MaxFee,
req.Gas,
req.Sponsor,
req.SponsorChainID,
req.Nonce,
req.EnforceSponsorNonce,
req.EnforceSponsorNonceOrdering,
)
if err != nil {
return [32]byte{}, err
}
hash := crypto.Keccak256Hash(hashPreimage)
// what is this from?
// https://github.com/gelatodigital/relay-sdk/blob/162f6392fb23bf52ef32f4c96168bc0433a5b0c0/src/lib/index.ts#L337
prefix, err := hex.DecodeString("1901")
if err != nil {
return [32]byte{}, err
}
digestPreimage := append(append(prefix, domainSeparator[:]...), hash[:]...)
return crypto.Keccak256Hash(digestPreimage), nil
}