forked from 0xPolygonID/c-polygonid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
194 lines (164 loc) · 3.82 KB
/
types.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
package c_polygonid
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"log"
"math/big"
"strconv"
"strings"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-iden3-crypto/utils"
)
// wrapper type to unmarshal signature from json
type hexSigJson babyjub.Signature
func (s *hexSigJson) UnmarshalJSON(bytes []byte) error {
const compSigLn = len(babyjub.SignatureComp{})
if compSigLn*2+2 != len(bytes) {
log.Print(len(bytes), compSigLn)
return errors.New("invalid signature length")
}
if bytes[0] != '"' || bytes[len(bytes)-1] != '"' {
return errors.New("invalid signature format")
}
var compSigBytes [compSigLn]byte
ln, err := hex.Decode(compSigBytes[:], bytes[1:len(bytes)-1])
if err != nil {
return err
}
if ln != compSigLn {
return errors.New("incorrect signature length decoded")
}
_, err = (*babyjub.Signature)(s).Decompress(compSigBytes)
return err
}
type jsonByte byte
func (b *jsonByte) Byte() byte {
return byte(*b)
}
func (b *jsonByte) UnmarshalJSON(in []byte) error {
if len(in) == 0 {
return errors.New("invalid byte format")
}
num := string(in)
radix := 10
if in[0] == '"' {
if len(in) < 3 || in[len(in)-1] != '"' {
return errors.New("invalid byte format")
}
num = string(in[1 : len(in)-1])
prefix := ""
if len(num) > 2 {
prefix = num[:2]
}
switch prefix {
case "0x", "0X":
radix = 16
num = num[2:]
case "0b", "0B":
radix = 2
num = num[2:]
}
}
i, err := strconv.ParseUint(num, radix, 8)
if err != nil {
return err
}
*b = jsonByte(i)
return nil
}
type jsonNumber big.Int
// UnmarshalJSON implements json.Unmarshaler interface. It unmarshals any
// number-like JSON value into a big.Int.
//
// Examples:
//
// "0x123" -> 291
// "0X123" -> 291
// "123" -> 123
// 123 -> 123
func (j *jsonNumber) UnmarshalJSON(in []byte) error {
if len(in) == 0 {
return errors.New("empty input")
}
if j == nil {
return errors.New("jsonNumber is nil")
}
var s string
var radix = 10
if in[0] >= '0' && in[0] <= '9' {
s = string(in)
} else if in[0] == '"' && in[len(in)-1] == '"' {
s = string(in[1 : len(in)-1])
if strings.HasPrefix(s, "0x") || strings.HasPrefix(s, "0X") {
radix = 16
s = s[2:]
}
}
_, ok := ((*big.Int)(j)).SetString(s, radix)
if !ok {
return errors.New("invalid integer number format")
}
return nil
}
// function to fail a compilation if underlined type is not int32
func assertUnderlineTypeInt32[T ~int32](_ T) {}
func chainIDToInt(chainID core.ChainID) int {
// assertion is required to correctly handle the underlined type during
// int conversion
assertUnderlineTypeInt32(chainID)
return int(chainID)
}
func chainIDToBytes(chainID core.ChainID) []byte {
// assertion is required to correctly handle the underlined type during
// int to bytes serialization
assertUnderlineTypeInt32(chainID)
var chainIDBytes [4]byte
binary.LittleEndian.PutUint32(chainIDBytes[:], uint32(chainID))
return chainIDBytes[:]
}
type JsonFieldIntStr big.Int
func (i *JsonFieldIntStr) UnmarshalJSON(bytes []byte) error {
var s *string
err := json.Unmarshal(bytes, &s)
if err != nil {
return err
}
if s == nil {
(*big.Int)(i).SetInt64(0)
return nil
}
_, ok := (*big.Int)(i).SetString(*s, 10)
if !ok {
return fmt.Errorf("invalid Int string")
}
if !utils.CheckBigIntInField((*big.Int)(i)) {
return fmt.Errorf("int is not in field")
}
return nil
}
func (i *JsonFieldIntStr) Int() *big.Int {
return (*big.Int)(i)
}
type coreDID w3c.DID
func (d *coreDID) UnmarshalJSON(bytes []byte) error {
var s *string
err := json.Unmarshal(bytes, &s)
if err != nil {
return err
}
if s == nil {
*d = coreDID(w3c.DID{})
return nil
}
did, err := w3c.ParseDID(*s)
if err != nil {
return err
}
*d = coreDID(*did)
return nil
}