Skip to content

Commit 02f72d2

Browse files
committed
feat: ini signer configurations
1 parent bf9004f commit 02f72d2

File tree

3 files changed

+516
-55
lines changed

3 files changed

+516
-55
lines changed

core/v3/sapitent_signer.go

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package v3
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"math/big"
7+
8+
"github.com/0xsequence/ethkit/ethrpc"
9+
"github.com/0xsequence/ethkit/go-ethereum/common"
10+
"github.com/0xsequence/ethkit/go-ethereum/crypto"
11+
"github.com/0xsequence/go-sequence/core"
12+
"github.com/0xsequence/go-sequence/eip6492"
13+
)
14+
15+
type ValidationResult struct {
16+
IsValid bool
17+
ImageHash common.Hash
18+
EffectiveWeight uint8
19+
Error error
20+
}
21+
22+
func ValidateSapientSignature(
23+
ctx context.Context,
24+
provider *ethrpc.Provider,
25+
signer common.Address,
26+
subdigest core.Subdigest,
27+
signature []byte,
28+
weight uint8,
29+
) ValidationResult {
30+
if provider == nil {
31+
return ValidationResult{
32+
IsValid: false,
33+
ImageHash: common.Hash{},
34+
EffectiveWeight: 0,
35+
Error: fmt.Errorf("provider required for Sapient signature validation"),
36+
}
37+
}
38+
39+
isValid, err := eip6492.ValidateEIP6492Offchain(ctx, provider, signer, subdigest.Hash, signature, nil)
40+
if err != nil {
41+
return ValidationResult{
42+
IsValid: false,
43+
ImageHash: common.Hash{},
44+
EffectiveWeight: 0,
45+
Error: fmt.Errorf("failed to validate Sapient signature: %w", err),
46+
}
47+
}
48+
49+
if !isValid {
50+
return ValidationResult{
51+
IsValid: false,
52+
ImageHash: common.Hash{},
53+
EffectiveWeight: 0,
54+
Error: fmt.Errorf("invalid Sapient signature for %v", signer),
55+
}
56+
}
57+
58+
sigToValidate := signature
59+
if eip6492.IsEIP6492Signature(signature) {
60+
_, _, sigToValidate, err = eip6492.DecodeEIP6492Signature(signature)
61+
if err != nil {
62+
return ValidationResult{
63+
IsValid: false,
64+
ImageHash: common.Hash{},
65+
EffectiveWeight: 0,
66+
Error: fmt.Errorf("failed to decode EIP-6492 signature: %w", err),
67+
}
68+
}
69+
}
70+
71+
imageHash := crypto.Keccak256Hash(
72+
[]byte("Sequence sapient config:\n"),
73+
signer.Bytes(),
74+
new(big.Int).SetUint64(uint64(weight)).Bytes(),
75+
crypto.Keccak256(sigToValidate),
76+
)
77+
78+
return ValidationResult{
79+
IsValid: true,
80+
ImageHash: imageHash,
81+
EffectiveWeight: weight,
82+
Error: nil,
83+
}
84+
}
85+
86+
func ValidateSapientCompactSignature(
87+
ctx context.Context,
88+
provider *ethrpc.Provider,
89+
signer common.Address,
90+
subdigest core.Subdigest,
91+
signature []byte,
92+
weight uint8,
93+
) ValidationResult {
94+
if provider == nil {
95+
return ValidationResult{
96+
IsValid: false,
97+
ImageHash: common.Hash{},
98+
EffectiveWeight: 0,
99+
Error: fmt.Errorf("provider required for Sapient Compact signature validation"),
100+
}
101+
}
102+
103+
isValid, err := eip6492.ValidateEIP6492Offchain(ctx, provider, signer, subdigest.Hash, signature, nil)
104+
if err != nil {
105+
return ValidationResult{
106+
IsValid: false,
107+
ImageHash: common.Hash{},
108+
EffectiveWeight: 0,
109+
Error: fmt.Errorf("failed to validate Sapient Compact signature: %w", err),
110+
}
111+
}
112+
113+
if !isValid {
114+
return ValidationResult{
115+
IsValid: false,
116+
ImageHash: common.Hash{},
117+
EffectiveWeight: 0,
118+
Error: fmt.Errorf("invalid Sapient Compact signature for %v", signer),
119+
}
120+
}
121+
122+
sigToValidate := signature
123+
if eip6492.IsEIP6492Signature(signature) {
124+
_, _, sigToValidate, err = eip6492.DecodeEIP6492Signature(signature)
125+
if err != nil {
126+
return ValidationResult{
127+
IsValid: false,
128+
ImageHash: common.Hash{},
129+
EffectiveWeight: 0,
130+
Error: fmt.Errorf("failed to decode EIP-6492 signature: %w", err),
131+
}
132+
}
133+
}
134+
135+
imageHash := crypto.Keccak256Hash(
136+
[]byte("Sequence sapient config:\n"),
137+
signer.Bytes(),
138+
new(big.Int).SetUint64(uint64(weight)).Bytes(),
139+
crypto.Keccak256(sigToValidate),
140+
)
141+
142+
return ValidationResult{
143+
IsValid: true,
144+
ImageHash: imageHash,
145+
EffectiveWeight: weight,
146+
Error: nil,
147+
}
148+
}

0 commit comments

Comments
 (0)