forked from FantasyJony/openzeppelin-merkle-tree-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (123 loc) · 3.59 KB
/
main.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
package main
import (
"fmt"
smt "github.com/FantasyJony/openzeppelin-merkle-tree-go/standard_merkle_tree"
"github.com/ethereum/go-ethereum/common/hexutil"
)
func main() {
values := [][]interface{}{
{
smt.SolAddress("0x1111111111111111111111111111111111111111"),
smt.SolNumber("5000000000000000000"),
},
{
smt.SolAddress("0x2222222222222222222222222222222222222222"),
smt.SolNumber("2500000000000000000"),
},
{
smt.SolAddress("0x3333333333333333333333333333333333333333"),
smt.SolNumber("1500000000000000000"),
},
{
smt.SolAddress("0x4444444444444444444444444444444444444444"),
smt.SolNumber("1000000000000000000"),
},
}
leafEncodings := []string{
smt.SOL_ADDRESS,
smt.SOL_UINT256,
}
fmt.Println("=== (1) Of BEGIN ===")
// (1) make tree
t1, err := smt.Of(values, leafEncodings)
handleError(err)
fmt.Println("Root: ", hexutil.Encode(t1.GetRoot()))
fmt.Println("=== (1) Of END ===")
fmt.Println()
fmt.Println("=== (2) CreateTree BEGIN ===")
// (2.1) create tree
t2, err := smt.CreateTree(leafEncodings)
// (2.2) add leaf
fmt.Println("=== (2.2) AddLeaf BEGIN ===")
for k, v := range values {
leafHash0, err := t2.AddLeaf(v)
handleError(err)
fmt.Println(k, " AddLeaf: ", hexutil.Encode(leafHash0))
}
fmt.Println("=== (2.2) AddLeaf END ===")
// (2.3) make tree
fmt.Println("=== (2.3) MakeTree BEGIN ===")
r3, err := t2.MakeTree()
fmt.Println("MakeTree Root: ", hexutil.Encode(r3))
fmt.Println("=== (2.3) MakeTree END ===")
fmt.Println()
// (3) dump
fmt.Println("=== (3) Dump BEGIN ===")
dump, err := t1.TreeMarshal()
handleError(err)
fmt.Println(string(dump))
fmt.Println("=== (3) Dump END ===")
fmt.Println()
// (4) load
fmt.Println("=== (4) Load BEGIN ===")
t3, err := smt.Load(dump)
handleError(err)
entries := t3.Entries()
for k, v := range entries {
if v.Value[0] == smt.SolAddress("0x1111111111111111111111111111111111111111") {
proof, err := t3.GetProofWithIndex(k)
handleError(err)
fmt.Println(fmt.Sprintf("ValueIndex: %v , TreeIndex: %v , Hash: %v , Value: %v", v.ValueIndex, v.TreeIndex, hexutil.Encode(v.Hash), v.Value))
for pk, pv := range proof {
fmt.Println(fmt.Sprintf("[%v] = %v", pk, hexutil.Encode(pv)))
}
}
}
fmt.Println("=== (4) Load END ===")
fmt.Println()
// (5) dump leaf proof
fmt.Println("=== (5) DumpLeafProof BEGIN ===")
leafProofDump, err := t3.TreeProofMarshal()
handleError(err)
fmt.Println(string(leafProofDump))
fmt.Println("=== (5) DumpLeafProof END ===")
fmt.Println()
// (6) verify proof
fmt.Println("=== (6) Verify BEGIN ===")
firstProof, err := t3.GetProofWithIndex(0)
handleError(err)
firstValue := entries[0].Value
verified, err := t3.Verify(firstProof, firstValue)
handleError(err)
fmt.Println("Verify:", verified)
fmt.Println("=== (6) Verify END ===")
fmt.Println()
// (7)
fmt.Println("=== (7) VerifyMultiProof BEGIN ===")
mulitProof, err := t3.GetMultiProof([][]interface{}{values[0], values[1]})
//t3.VerifyMultiProof()
fmt.Println("Proof:")
for k, v := range mulitProof.Proof {
fmt.Println(fmt.Sprintf("[%v] = %v", k, hexutil.Encode(v)))
}
//fmt.Println("=== (7.1) Proof END ===")
//fmt.Println()
fmt.Println("ProofFlags:")
//fmt.Println("=== (7.2) ProofFlags BEGIN ===")
fmt.Println(mulitProof.ProofFlags)
//fmt.Println("=== (7.2) ProofFlags END ===")
//fmt.Println()
fmt.Println("Values:")
for _, v := range mulitProof.Values {
fmt.Println(v)
}
verified, err = t3.VerifyMultiProof(mulitProof)
handleError(err)
fmt.Println("Verify:", verified)
fmt.Println("=== (7) VerifyMultiProof END ===")
}
func handleError(err error) {
if err != nil {
panic(err)
}
}