-
Notifications
You must be signed in to change notification settings - Fork 9
/
signature.go
352 lines (285 loc) · 12.8 KB
/
signature.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package sequence
import (
"bytes"
"context"
"fmt"
"math/big"
"github.com/0xsequence/ethkit/ethcoder"
"github.com/0xsequence/ethkit/ethrpc"
"github.com/0xsequence/ethkit/ethwallet"
"github.com/0xsequence/ethkit/go-ethereum/accounts"
"github.com/0xsequence/ethkit/go-ethereum/accounts/abi/bind"
"github.com/0xsequence/ethkit/go-ethereum/common"
"github.com/0xsequence/ethkit/go-ethereum/common/hexutil"
"github.com/0xsequence/ethkit/go-ethereum/crypto"
"github.com/0xsequence/go-sequence/contracts"
"github.com/0xsequence/go-sequence/contracts/gen/ierc1271"
"github.com/0xsequence/go-sequence/core"
v1 "github.com/0xsequence/go-sequence/core/v1"
v2 "github.com/0xsequence/go-sequence/core/v2"
"github.com/0xsequence/go-sequence/eip6492"
"github.com/goware/logger"
)
func Sign[C core.WalletConfig](wallet *Wallet[C], input common.Hash) ([]byte, error) {
return wallet.SignDigest(context.Background(), input)
}
// GenericDecodeSignature sequence into individual parts
func GenericDecodeSignature[C core.WalletConfig](sequenceSignature []byte) (core.Signature[C], error) {
sig := sequenceSignature
s := len(sig)
if s < 2 {
return nil, fmt.Errorf("sequence: invalid signature, out-of-bounds threshold")
}
c, err := core.GetCoreForWalletConfig[C]()
if err != nil {
return nil, fmt.Errorf("sequence: failed to get core implementation, %v", err)
}
decodedSignature, err := c.DecodeSignature(sig)
if err != nil {
return nil, fmt.Errorf("sequence: invalid signature, %v", err)
}
return decodedSignature, nil
}
func V1DecodeSignature(sequenceSignature []byte) (core.Signature[*v1.WalletConfig], error) {
return GenericDecodeSignature[*v1.WalletConfig](sequenceSignature)
}
func V2DecodeSignature(sequenceSignature []byte) (core.Signature[*v2.WalletConfig], error) {
return GenericDecodeSignature[*v2.WalletConfig](sequenceSignature)
}
func DecodeSignature(sequenceSignature []byte) (core.Signature[*v2.WalletConfig], error) {
return V2DecodeSignature(sequenceSignature)
}
func GenericRecoverWalletConfigFromDigest[C core.WalletConfig](digest, seqSig []byte, walletAddress common.Address, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (C, *big.Int, error) {
var (
wc C
weight *big.Int
err error
)
decoded, err := GenericDecodeSignature[C](seqSig)
if err != nil {
return wc, weight, err
}
wc, weight, err = decoded.Recover(context.Background(), core.Digest{Hash: common.BytesToHash(digest)}, walletAddress, chainID, provider)
if err != nil {
return wc, weight, err
}
return wc, weight, nil
}
func V1RecoverWalletConfigFromDigest(digest, seqSig []byte, walletAddress common.Address, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (*v1.WalletConfig, *big.Int, error) {
return GenericRecoverWalletConfigFromDigest[*v1.WalletConfig](digest, seqSig, walletAddress, walletContext, chainID, provider)
}
func V2RecoverWalletConfigFromDigest(digest, seqSig []byte, walletAddress common.Address, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (*v2.WalletConfig, *big.Int, error) {
return GenericRecoverWalletConfigFromDigest[*v2.WalletConfig](digest, seqSig, walletAddress, walletContext, chainID, provider)
}
func RecoverWalletConfigFromDigest(digest, seqSig []byte, walletAddress common.Address, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (*v2.WalletConfig, *big.Int, error) {
return V2RecoverWalletConfigFromDigest(digest, seqSig, walletAddress, walletContext, chainID, provider)
}
func GenericIsValidSignature[C core.WalletConfig](walletAddress common.Address, digest common.Hash, seqSig []byte, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
// Try to do it first with ethereum sign signature format
ok, err := ethwallet.IsValid191Signature(walletAddress, digest[:], seqSig)
if err == nil {
return ok, nil
}
if provider == nil {
return false, fmt.Errorf("failed to validate: provider is required")
}
code, err := provider.CodeAt(context.TODO(), walletAddress, nil)
if err != nil {
return false, fmt.Errorf("unable to get code at %v: %w", walletAddress, err)
}
if len(code) == 0 {
res, err := GenericIsValidUndeployedSignature[C](walletAddress, digest, seqSig, walletContext, chainID, provider)
if err == nil && res {
return true, nil
}
return false, fmt.Errorf("failed to validate: %v", err)
} else {
// Smart wallet is deployed, query erc1271 method to check signature
erc1271, err := ierc1271.NewIERC1271(walletAddress, provider)
if err != nil {
return false, err
}
// NOTE: we expect digest to be ready for ERC1271 call, so digest must be fully encoded as expected
res, err := erc1271.IsValidSignature(&bind.CallOpts{From: common.Address{0x1}}, digest, seqSig)
if err != nil {
return false, err
}
if ierc1271.IsValidSignatureBytes32_MagicReturnValue != hexutil.Encode(res[:]) {
return false, fmt.Errorf("failed to validate")
}
}
return true, nil
}
func V1IsValidSignature(walletAddress common.Address, digest common.Hash, seqSig []byte, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
return GenericIsValidSignature[*v1.WalletConfig](walletAddress, digest, seqSig, walletContext, chainID, provider)
}
func V2IsValidSignature(walletAddress common.Address, digest common.Hash, seqSig []byte, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
return GenericIsValidSignature[*v2.WalletConfig](walletAddress, digest, seqSig, walletContext, chainID, provider)
}
func GeneralIsValidSignature(walletAddress common.Address, digest common.Hash, seqSig []byte, walletContexts WalletContexts, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
isValid, err := V2IsValidSignature(walletAddress, digest, seqSig, walletContexts[2], chainID, provider)
if err == nil {
return isValid, nil
}
isValid, err2 := V1IsValidSignature(walletAddress, digest, seqSig, walletContexts[1], chainID, provider)
if err2 != nil {
return false, fmt.Errorf("failed to validate: %v, %v", err, err2)
}
return isValid, nil
}
func IsValidMessageSignature(address common.Address, message []byte, signature []byte, chainID *big.Int, provider *ethrpc.Provider, optLogger *logger.Logger) (bool, error) {
log := logger.Nop()
if optLogger != nil {
log = *optLogger
}
isValid, err := ethwallet.IsValid191Signature(address, message, signature)
if err == nil && isValid {
return true, nil
}
return IsValidSignature(log, address, common.BytesToHash(accounts.TextHash(message)), signature, SequenceContexts(), chainID, provider)
}
func IsValidTypedDataSignature(address common.Address, encodedTypedData []byte, signature []byte, chainID *big.Int, provider *ethrpc.Provider, optLogger *logger.Logger) (bool, error) {
log := logger.Nop()
if optLogger != nil {
log = *optLogger
}
isValid, err := ethwallet.IsValid191Signature(address, encodedTypedData, signature)
if err == nil && isValid {
return true, nil
}
typedDataDigest := common.BytesToHash(ethcoder.Keccak256(encodedTypedData))
return IsValidSignature(log, address, typedDataDigest, signature, SequenceContexts(), chainID, provider)
}
func IsValidSignature(log logger.Logger, walletAddress common.Address, digest common.Hash, seqSig []byte, walletContexts WalletContexts, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
eip6492isValid, _ := eip6492.ValidateEIP6492Offchain(context.Background(), provider, walletAddress, digest, seqSig, nil)
if eip6492isValid {
return true, nil
}
// NOTICE: This is legacy code, we only need it while we deploy EIP-6492
// on prod, otherwise we need to coordinate both.
// as soon as we have EIP-6492 running on prod we can remove this.
var err error
seqSig, err = UnwrapEIP6492Signature(seqSig)
if err != nil {
return false, err
}
generalIsValid, err := GeneralIsValidSignature(walletAddress, digest, seqSig, walletContexts, chainID, provider)
if err != nil {
return false, err
}
if generalIsValid {
log.Errorf("WARNING: Legacy signature validation used, please upgrade to EIP-6492: %s, %s, %s", walletAddress, digest, common.Bytes2Hex(seqSig))
}
return generalIsValid, nil
}
func GenericIsValidUndeployedSignature[C core.WalletConfig](walletAddress common.Address, digest common.Hash, seqSig []byte, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
c, err := core.GetCoreForWalletConfig[C]()
if err != nil {
return false, fmt.Errorf("sequence: failed to get core implementation, %v", err)
}
decoded, err := c.DecodeSignature(seqSig)
if err != nil {
return false, err
}
recovered, weight, err := decoded.Recover(context.Background(), core.Digest{Hash: digest}, walletAddress, chainID, provider)
if err != nil {
return false, err
}
imageHash := recovered.ImageHash()
address, err := AddressFromImageHash(imageHash.Hex(), walletContext)
if err != nil {
return false, err
}
if address != walletAddress || weight.Uint64() < uint64(recovered.Threshold()) {
return false, fmt.Errorf("failed to validate")
}
return true, nil
}
func V1IsValidUndeployedSignature(walletAddress common.Address, digest common.Hash, seqSig []byte, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
return GenericIsValidUndeployedSignature[*v1.WalletConfig](walletAddress, digest, seqSig, walletContext, chainID, provider)
}
func V2IsValidUndeployedSignature(walletAddress common.Address, digest common.Hash, seqSig []byte, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
return GenericIsValidUndeployedSignature[*v2.WalletConfig](walletAddress, digest, seqSig, walletContext, chainID, provider)
}
func IsValidUndeployedSignature(walletAddress common.Address, digest common.Hash, seqSig []byte, walletContext WalletContext, chainID *big.Int, provider *ethrpc.Provider) (bool, error) {
return V2IsValidUndeployedSignature(walletAddress, digest, seqSig, walletContext, chainID, provider)
}
func MessageDigest(message []byte) common.Hash {
return common.BytesToHash(ethcoder.Keccak256(message))
}
func MustEncodeSig(str string) common.Hash {
return crypto.Keccak256Hash([]byte(str))
}
func EIP6492Signature(signature []byte, config core.WalletConfig) ([]byte, error) {
context := SequenceContextForWalletConfig(config)
factory := context.FactoryAddress
if factory == (common.Address{}) {
return nil, fmt.Errorf("unknown factory address for wallet config type %T", config)
}
mainModule := context.MainModuleAddress
imageHash := config.ImageHash().Hash
deploy, err := contracts.V2.WalletFactory.ABI.Pack("deploy", mainModule, imageHash)
if err != nil {
return nil, fmt.Errorf("unable to encode deploy call: %w", err)
}
signature, err = ethcoder.AbiCoder([]string{"address", "bytes", "bytes"}, []interface{}{factory, deploy, signature})
if err != nil {
return nil, fmt.Errorf("unable to encode eip-6492 signature: %w", err)
}
signature = append(signature, eip6492.EIP6492MagicBytes...)
return signature, nil
}
func EIP6492SignatureWithMultipleDeployments(signature []byte, configs []core.WalletConfig) ([]byte, error) {
if len(configs) == 1 {
return EIP6492Signature(signature, configs[0])
}
var txns Transactions
for _, config := range configs {
context := SequenceContextForWalletConfig(config)
factory := context.FactoryAddress
if factory == (common.Address{}) {
return nil, fmt.Errorf("unknown factory address for wallet config type %T", config)
}
mainModule := context.MainModuleAddress
imageHash := config.ImageHash().Hash
deploy, err := contracts.V2.WalletFactory.ABI.Pack("deploy", mainModule, imageHash)
if err != nil {
return nil, fmt.Errorf("unable to encode deploy call: %w", err)
}
txns = append(txns, &Transaction{
RevertOnError: true,
To: factory,
Data: deploy,
})
}
encodedTxns, err := txns.EncodedTransactions()
if err != nil {
return nil, err
}
execdata, err := contracts.WalletMainModule.Encode("execute", encodedTxns, big.NewInt(0), []byte{})
if err != nil {
return nil, err
}
signature, err = ethcoder.AbiCoder([]string{"address", "bytes", "bytes"}, []interface{}{sequenceContextV2.GuestModuleAddress, execdata, signature})
if err != nil {
return nil, fmt.Errorf("unable to encode eip-6492 signature: %w", err)
}
signature = append(signature, eip6492.EIP6492MagicBytes...)
return signature, nil
}
func UnwrapEIP6492Signature(signature []byte) ([]byte, error) {
if !bytes.HasSuffix(signature, eip6492.EIP6492MagicBytes) {
return signature, nil
}
signature = bytes.TrimSuffix(signature, eip6492.EIP6492MagicBytes)
var (
to common.Address
data []byte
signature_ []byte
)
err := ethcoder.AbiDecoder([]string{"address", "bytes", "bytes"}, signature, []any{&to, &data, &signature_})
if err != nil {
return nil, fmt.Errorf("unable to decode eip-6492 signature: %w", err)
}
return signature_, nil
}