-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathauthV2_json.go
130 lines (114 loc) · 3.17 KB
/
authV2_json.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
package circuits
import (
"encoding/hex"
"encoding/json"
"errors"
"math/big"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-crypto/babyjub"
"github.com/iden3/go-merkletree-sql/v2"
)
type jsonInt big.Int
func (j *jsonInt) UnmarshalJSON(bytes []byte) error {
var s string
if err := json.Unmarshal(bytes, &s); err != nil {
return err
}
var i = (*big.Int)(j)
_, ok := i.SetString(s, 10)
if !ok {
return errors.New("error parsing big.Int")
}
return nil
}
func (j *jsonInt) MarshalJSON() ([]byte, error) {
if j == nil {
return []byte("null"), nil
}
return json.Marshal((*big.Int)(j).String())
}
func (j *jsonInt) BigInt() *big.Int {
if j == nil {
return nil
}
return (*big.Int)(j)
}
type jsonSignature babyjub.Signature
func (s *jsonSignature) UnmarshalJSON(bytes []byte) error {
var sHex string
if err := json.Unmarshal(bytes, &sHex); err != nil {
return err
}
sigComp, err := hex.DecodeString(sHex)
if err != nil {
return err
}
var sigComp2 babyjub.SignatureComp
if len(sigComp2) != len(sigComp) {
return errors.New("incorrect signature length")
}
copy(sigComp2[:], sigComp)
sig, err := sigComp2.Decompress()
if err != nil {
return err
}
*((*babyjub.Signature)(s)) = *sig
return nil
}
func (s *jsonSignature) MarshalJSON() ([]byte, error) {
if s == nil {
return []byte("null"), nil
}
bs := babyjub.Signature(*s)
sigComp := bs.Compress()
return json.Marshal(hex.EncodeToString(sigComp[:]))
}
type jsonInputs struct {
GenesisID *core.ID `json:"genesisID"`
ProfileNonce *jsonInt `json:"profileNonce"`
AuthClaim *core.Claim `json:"authClaim"`
AuthClaimIncMtp *merkletree.Proof `json:"authClaimIncMtp"`
AuthClaimNonRevMtp *merkletree.Proof `json:"authClaimNonRevMtp"`
TreeState TreeState `json:"treeState"` // Identity state
GISTProof GISTProof `json:"gistProof"`
Signature *jsonSignature `json:"signature"`
Challenge *jsonInt `json:"challenge"`
}
func newJsonInputs(a AuthV2Inputs) jsonInputs {
var inputs jsonInputs
inputs.GenesisID = a.GenesisID
inputs.ProfileNonce = (*jsonInt)(a.ProfileNonce)
inputs.AuthClaim = a.AuthClaim
inputs.AuthClaimIncMtp = a.AuthClaimIncMtp
inputs.AuthClaimNonRevMtp = a.AuthClaimNonRevMtp
inputs.TreeState = a.TreeState
inputs.GISTProof = a.GISTProof
inputs.Signature = (*jsonSignature)(a.Signature)
inputs.Challenge = (*jsonInt)(a.Challenge)
return inputs
}
func (inputs jsonInputs) Unwrap() AuthV2Inputs {
var a AuthV2Inputs
a.GenesisID = inputs.GenesisID
a.ProfileNonce = (*big.Int)(inputs.ProfileNonce)
a.AuthClaim = inputs.AuthClaim
a.AuthClaimIncMtp = inputs.AuthClaimIncMtp
a.AuthClaimNonRevMtp = inputs.AuthClaimNonRevMtp
a.TreeState = inputs.TreeState
a.GISTProof = inputs.GISTProof
a.Signature = (*babyjub.Signature)(inputs.Signature)
a.Challenge = (*big.Int)(inputs.Challenge)
return a
}
func (a AuthV2Inputs) MarshalJSON() ([]byte, error) {
return json.Marshal(newJsonInputs(a))
}
func (a *AuthV2Inputs) UnmarshalJSON(in []byte) error {
var inputs jsonInputs
err := json.Unmarshal(in, &inputs)
if err != nil {
return err
}
*a = inputs.Unwrap()
return nil
}