Skip to content

Commit 85f4e99

Browse files
committed
feat: ini sapient
1 parent c5e520a commit 85f4e99

File tree

2 files changed

+98
-39
lines changed

2 files changed

+98
-39
lines changed

cmd/sequence/config.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ func (s *SignerElement) ToTree() v3.WalletConfigTree {
3232
}
3333
}
3434

35+
type SapientSignerElement struct {
36+
Address common.Address
37+
Weight uint8
38+
ImageHash core.ImageHash
39+
}
40+
41+
func (s *SapientSignerElement) ToTree() v3.WalletConfigTree {
42+
return &v3.WalletConfigTreeSapientSignerLeaf{
43+
Weight: s.Weight,
44+
Address: s.Address,
45+
ImageHash_: s.ImageHash,
46+
}
47+
}
48+
3549
type SubdigestElement struct {
3650
Digest core.Subdigest
3751
}
@@ -77,11 +91,12 @@ type JSONOutput struct {
7791
}
7892

7993
type JSONLeaf struct {
80-
Type string `json:"type"`
81-
Address string `json:"address,omitempty"`
82-
Weight string `json:"weight,omitempty"`
83-
Hash string `json:"hash,omitempty"`
84-
Digest string `json:"digest,omitempty"`
94+
Type string `json:"type"`
95+
Address string `json:"address,omitempty"`
96+
Weight string `json:"weight,omitempty"`
97+
Hash string `json:"hash,omitempty"`
98+
Digest string `json:"digest,omitempty"`
99+
ImageHash string `json:"imageHash,omitempty"`
85100
}
86101

87102
type JSONNode struct {
@@ -137,6 +152,13 @@ func treeToMap(tree v3.WalletConfigTree) interface{} {
137152
Address: t.Address.Hex(),
138153
Weight: fmt.Sprintf("%d", t.Weight),
139154
}
155+
case *v3.WalletConfigTreeSapientSignerLeaf:
156+
return JSONLeaf{
157+
Type: "sapient-signer",
158+
Address: t.Address.Hex(),
159+
Weight: fmt.Sprintf("%d", t.Weight),
160+
ImageHash: t.ImageHash_.Hex(),
161+
}
140162
case *v3.WalletConfigTreeSubdigestLeaf:
141163
return JSONLeaf{
142164
Type: "subdigest",
@@ -281,6 +303,20 @@ func parseElement(element string) (ConfigElement, error) {
281303
Weight: uint8(weight.Uint64()),
282304
}, nil
283305

306+
case "sapient":
307+
if len(parts) != 4 {
308+
return nil, fmt.Errorf("invalid sapient-signer format: %s", element)
309+
}
310+
weight := new(big.Int)
311+
if _, ok := weight.SetString(parts[3], 10); !ok {
312+
return nil, fmt.Errorf("invalid weight: %s", parts[3])
313+
}
314+
return &SapientSignerElement{
315+
Address: common.HexToAddress(parts[2]),
316+
ImageHash: core.ImageHash{Hash: common.HexToHash(parts[1])},
317+
Weight: uint8(weight.Uint64()),
318+
}, nil
319+
284320
case "subdigest":
285321
if len(parts) != 2 {
286322
return nil, fmt.Errorf("invalid subdigest format: %s", element)

core/v3/v3.go

Lines changed: 57 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,35 +1663,41 @@ func (l *signatureTreeSapientLeaf) reduceImageHash() (core.ImageHash, error) {
16631663
func (l *signatureTreeSapientLeaf) write(writer io.Writer) error {
16641664
sigLen := uint64(len(l.Signature))
16651665
sizeSize := minBytesFor(sigLen)
1666+
if sizeSize == 0 {
1667+
// Ensure at least 1 byte for the length, even if 0
1668+
sizeSize = 1
1669+
}
16661670
if sizeSize > 3 {
16671671
return fmt.Errorf("signature length %d requires %d bytes, exceeds maximum of 3", sigLen, sizeSize)
16681672
}
16691673

1670-
var firstByte byte
1671-
if l.Weight <= 3 && l.Weight > 0 {
1672-
firstByte = FLAG_SIGNATURE_SAPIENT<<4 | (byte(sizeSize) << 2) | l.Weight
1673-
_, err := writer.Write([]byte{firstByte})
1674-
if err != nil {
1675-
return fmt.Errorf("unable to write Sapient leaf type: %w", err)
1676-
}
1674+
var firstByte byte = FLAG_SIGNATURE_SAPIENT<<4 | (byte(sizeSize) << 2)
1675+
var weightBytes []byte
1676+
1677+
if l.Weight >= 1 && l.Weight <= 3 {
1678+
firstByte |= l.Weight
16771679
} else {
1678-
firstByte = FLAG_SIGNATURE_SAPIENT<<4 | (byte(sizeSize) << 2)
1679-
_, err := writer.Write([]byte{firstByte})
1680-
if err != nil {
1681-
return fmt.Errorf("unable to write Sapient leaf type: %w", err)
1682-
}
1683-
_, err = writer.Write([]byte{l.Weight})
1680+
weightBytes = []byte{l.Weight}
1681+
}
1682+
1683+
_, err := writer.Write([]byte{firstByte})
1684+
if err != nil {
1685+
return fmt.Errorf("unable to write Sapient leaf type: %w", err)
1686+
}
1687+
1688+
if len(weightBytes) > 0 {
1689+
_, err = writer.Write(weightBytes)
16841690
if err != nil {
16851691
return fmt.Errorf("unable to write dynamic weight: %w", err)
16861692
}
16871693
}
16881694

1689-
_, err := writer.Write(l.Address.Bytes())
1695+
_, err = writer.Write(l.Address.Bytes())
16901696
if err != nil {
16911697
return fmt.Errorf("unable to write address: %w", err)
16921698
}
16931699

1694-
err = writeUintX(writer, uint64(len(l.Signature)), sizeSize)
1700+
err = writeUintX(writer, sigLen, sizeSize)
16951701
if err != nil {
16961702
return fmt.Errorf("unable to write signature length: %w", err)
16971703
}
@@ -1794,35 +1800,41 @@ func (l *signatureTreeSapientCompactLeaf) reduceImageHash() (core.ImageHash, err
17941800
func (l *signatureTreeSapientCompactLeaf) write(writer io.Writer) error {
17951801
sigLen := uint64(len(l.Signature))
17961802
sizeSize := minBytesFor(sigLen)
1803+
if sizeSize == 0 {
1804+
// Ensure at least 1 byte for the length, even if 0
1805+
sizeSize = 1
1806+
}
17971807
if sizeSize > 3 {
17981808
return fmt.Errorf("signature length %d requires %d bytes, exceeds maximum of 3", sigLen, sizeSize)
17991809
}
18001810

1801-
var firstByte byte
1802-
if l.Weight <= 3 && l.Weight > 0 {
1803-
firstByte = FLAG_SIGNATURE_SAPIENT<<4 | (byte(sizeSize) << 2) | l.Weight
1804-
_, err := writer.Write([]byte{firstByte})
1805-
if err != nil {
1806-
return fmt.Errorf("unable to write Sapient leaf type: %w", err)
1807-
}
1811+
var firstByte byte = FLAG_SIGNATURE_SAPIENT_COMPACT<<4 | (byte(sizeSize) << 2)
1812+
var weightBytes []byte
1813+
1814+
if l.Weight >= 1 && l.Weight <= 3 {
1815+
firstByte |= l.Weight
18081816
} else {
1809-
firstByte = FLAG_SIGNATURE_SAPIENT<<4 | (byte(sizeSize) << 2)
1810-
_, err := writer.Write([]byte{firstByte})
1811-
if err != nil {
1812-
return fmt.Errorf("unable to write Sapient leaf type: %w", err)
1813-
}
1814-
_, err = writer.Write([]byte{l.Weight})
1817+
weightBytes = []byte{l.Weight}
1818+
}
1819+
1820+
_, err := writer.Write([]byte{firstByte})
1821+
if err != nil {
1822+
return fmt.Errorf("unable to write SapientCompact leaf type: %w", err)
1823+
}
1824+
1825+
if len(weightBytes) > 0 {
1826+
_, err = writer.Write(weightBytes)
18151827
if err != nil {
18161828
return fmt.Errorf("unable to write dynamic weight: %w", err)
18171829
}
18181830
}
18191831

1820-
_, err := writer.Write(l.Address.Bytes())
1832+
_, err = writer.Write(l.Address.Bytes())
18211833
if err != nil {
18221834
return fmt.Errorf("unable to write address: %w", err)
18231835
}
18241836

1825-
err = writeUintX(writer, uint64(len(l.Signature)), sizeSize)
1837+
err = writeUintX(writer, sigLen, sizeSize)
18261838
if err != nil {
18271839
return fmt.Errorf("unable to write signature length: %w", err)
18281840
}
@@ -2528,10 +2540,21 @@ func (l *WalletConfigTreeSapientSignerLeaf) unverifiedWeight(signers map[common.
25282540

25292541
func (l *WalletConfigTreeSapientSignerLeaf) buildSignatureTree(signerSignatures map[common.Address]signerSignature) signatureTree {
25302542
if signature, ok := signerSignatures[l.Address]; ok {
2531-
return &signatureTreeSapientLeaf{
2532-
Weight: l.Weight,
2533-
Address: l.Address,
2534-
Signature: signature.signature,
2543+
switch signature.type_ {
2544+
case core.SignerSignatureTypeSapient:
2545+
return &signatureTreeSapientLeaf{
2546+
Weight: l.Weight,
2547+
Address: l.Address,
2548+
Signature: signature.signature,
2549+
}
2550+
case core.SignerSignatureTypeSapientCompact:
2551+
return &signatureTreeSapientCompactLeaf{
2552+
Weight: l.Weight,
2553+
Address: l.Address,
2554+
Signature: signature.signature,
2555+
}
2556+
default:
2557+
panic(fmt.Sprintf("unexpected signature type for sapient signer: %v", signature.type_))
25352558
}
25362559
}
25372560
hashedImage := l.ImageHash()

0 commit comments

Comments
 (0)