-
Notifications
You must be signed in to change notification settings - Fork 2
/
proof.go
82 lines (70 loc) · 2.03 KB
/
proof.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
package c_polygonid
import (
"encoding/json"
"math/big"
"github.com/iden3/go-merkletree-sql/v2"
)
func ProofFromSmartContract(
scProof SmartContractProof) (*merkletree.Proof, *merkletree.Hash, error) {
var nodeAux *merkletree.NodeAux
var err error
if scProof.AuxExistence {
nodeAux = &merkletree.NodeAux{}
nodeAux.Key, err = merkletree.NewHashFromBigInt(scProof.AuxIndex)
if err != nil {
return &merkletree.Proof{}, &merkletree.Hash{}, err
}
nodeAux.Value, err = merkletree.NewHashFromBigInt(scProof.AuxValue)
if err != nil {
return &merkletree.Proof{}, &merkletree.Hash{}, err
}
}
allSiblings := make([]*merkletree.Hash, len(scProof.Siblings))
for i, s := range scProof.Siblings {
allSiblings[i], err = merkletree.NewHashFromBigInt(s)
if err != nil {
return &merkletree.Proof{}, &merkletree.Hash{}, err
}
}
proof, err := merkletree.NewProofFromData(scProof.Existence, allSiblings,
nodeAux)
if err != nil {
return &merkletree.Proof{}, &merkletree.Hash{}, err
}
root, err := merkletree.NewHashFromBigInt(scProof.Root)
if err != nil {
return &merkletree.Proof{}, &merkletree.Hash{}, err
}
return proof, root, nil
}
type SmartContractProof struct {
Root *big.Int
Existence bool
Siblings []*big.Int
AuxExistence bool
AuxIndex *big.Int
AuxValue *big.Int
}
func (s *SmartContractProof) UnmarshalJSON(bytes []byte) error {
var j struct {
Root JsonBigInt `json:"root"`
Existence bool `json:"existence"`
Siblings [32]JsonBigInt `json:"siblings"`
AuxExistence bool `json:"auxExistence"`
AuxIndex JsonBigInt `json:"auxIndex"`
AuxValue JsonBigInt `json:"auxValue"`
}
if err := json.Unmarshal(bytes, &j); err != nil {
return err
}
s.Root = j.Root.BigInt()
s.Existence = j.Existence
s.Siblings = make([]*big.Int, len(j.Siblings))
for i := range j.Siblings {
s.Siblings[i] = j.Siblings[i].BigInt()
}
s.AuxExistence = j.AuxExistence
s.AuxIndex = j.AuxIndex.BigInt()
s.AuxValue = j.AuxValue.BigInt()
return nil
}