-
Notifications
You must be signed in to change notification settings - Fork 2
/
kate.go
150 lines (117 loc) · 3.29 KB
/
kate.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
package rpc
import (
"encoding/json"
"fmt"
"math/big"
"strings"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
)
const MaxCells = 10000
type Cell struct {
Row uint64 `json:"row"`
Col uint64 `json:"col"`
}
func NewCell(row, col uint64) Cell {
return Cell{Row: row, Col: col}
}
type GDataProof struct {
RawScalar big.Int `json:"RawScalar"` // For U256
Proof GProof `json:"Proof"`
}
type GProof struct {
Data [48]byte `json:"Data"`
}
func (g *GDataProof) UnmarshalJSON(data []byte) error {
var tupleData [2]json.RawMessage
if err := json.Unmarshal(data, &tupleData); err != nil {
return err
}
// Unmarshal RawScalar and remove the '0x' prefix
var rawScalarString string
if err := json.Unmarshal(tupleData[0], &rawScalarString); err != nil {
return err
}
fmt.Println("RawScalarString:", rawScalarString)
// Strip '0x' prefix and convert to big.Int
trimmedScalarString := strings.TrimPrefix(rawScalarString, "0x")
rawScalar, ok := new(big.Int).SetString(trimmedScalarString, 16)
if !ok {
fmt.Printf("Failed to convert RawScalar to big.Int, string was: %s\n", trimmedScalarString)
return fmt.Errorf("invalid RawScalar format")
}
g.RawScalar = *rawScalar
// Unmarshal Proof
var proof [48]byte
if err := json.Unmarshal(tupleData[1], &proof); err != nil {
return err
}
g.Proof = GProof{proof}
return nil
}
func formatBigIntWithCommas(b *big.Int) string {
if b == nil {
return ""
}
numStr := b.String()
// Starting index for inserting commas
startOffset := 0
if numStr[0] == '-' {
startOffset = 1 // Keep the negative sign intact
}
// Slice to hold the parts of the number
var parts []string
// Iterate over the string in reverse, collecting slices of 3 digits
for i := len(numStr); i > startOffset; i -= 3 {
end := i
start := i - 3
if start < startOffset {
start = startOffset
}
parts = append([]string{numStr[start:end]}, parts...)
}
// Join the parts with commas
return strings.Join(parts, ",")
}
func (g *GDataProof) MarshalJSON() ([]byte, error) {
rawScalarStr := formatBigIntWithCommas(&g.RawScalar)
proofHex := fmt.Sprintf("0x%x", g.Proof.Data)
return json.Marshal([]interface{}{rawScalarStr, proofHex})
}
type ProofResponse struct {
DataProof DataProof
Message Message // Interface to capture different message types
}
type TxDataRoot struct {
DataRoot types.Hash
BlobRoot types.Hash
BridgeRoot types.Hash
}
// DataProof struct represents the data proof response
type DataProof struct {
Roots TxDataRoot
Proof []types.Hash
NumberOfLeaves uint32 // Change to uint32 to match Rust u32
LeafIndex uint32 // Change to uint32 to match Rust u32
Leaf types.Hash
}
// Message interface represents the enum variants
type Message interface {
isMessage()
}
type BoundedData struct {
Data []byte
}
// BoundedDataMaxLen is the maximum length for the bounded data
const BoundedDataMaxLen = 32 // Adjust the maximum length as needed
// ArbitraryMessage struct represents the ArbitraryMessage variant
type ArbitraryMessage struct {
BoundedData
}
func (a *ArbitraryMessage) isMessage() {}
// FungibleToken struct represents the FungibleToken variant
type FungibleToken struct {
AssetID types.Hash
Amount uint128 // Define uint128 type as needed
}
func (f *FungibleToken) isMessage() {}
type uint128 uint64