/
utils.go
116 lines (94 loc) · 2.26 KB
/
utils.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
package simulation
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"strconv"
"strings"
sdksim "github.com/cosmos/cosmos-sdk/types/simulation"
merkle "github.com/wealdtech/go-merkletree"
"github.com/wealdtech/go-merkletree/sha3"
)
const (
fileData = "jackal maxi"
)
// Generate random IPv4 url e.g http://1.1.1.1
// It may not be unique
func RandIPv4Url(r *rand.Rand) string {
b := make([]string, 4)
for i := 0; i < len(b); i++ {
b[i] = strconv.Itoa(sdksim.RandIntBetween(r, 0, 255))
}
return "http://" + strings.Join(b, ".")
}
// Generate merkle proof with similar operation as jackal provider daemon
// but the file used to generate is much simpler
// Returns json encoding of merkle proof
func GetMerkleProof() (item, jProof string) {
f := []byte(fileData)
var data [][]byte
h := sha256.New()
_, err := io.WriteString(h, fmt.Sprintf("%d%x", 0, f))
if err != nil {
panic(err)
}
hashName := h.Sum(nil)
data = append(data, hashName)
tree, err := merkle.NewUsing(data, sha3.New512(), false)
if err != nil {
panic(err)
}
h = sha256.New()
_, err = io.WriteString(h, fmt.Sprintf("%d%x", 0, f))
if err != nil {
panic(err)
}
hashedItem := h.Sum(nil)
proof, err := tree.GenerateProof(hashedItem, 0)
if err != nil {
panic(err)
}
jproof, err := json.Marshal(*proof)
if err != nil {
panic(err)
}
err = json.Unmarshal(jproof, &proof)
if err != nil {
panic(err)
}
sHex := hex.EncodeToString(tree.Root())
hex, err := hex.DecodeString(sHex)
if err != nil {
panic(err)
}
validProof, err := merkle.VerifyProofUsing(hashedItem, false, proof, [][]byte{hex}, sha3.New512())
if err != nil {
panic(err)
}
if !validProof {
panic(errors.New("GetMerkleProof() failed to verify proof"))
}
return fmt.Sprintf("%x", f), string(jproof)
}
// Generate merkle root with similar operation as jackal provider daemon
func GetMerkleRoot() string {
// The index of the file is zero
f := []byte(fileData)
var data [][]byte
h := sha256.New()
_, err := io.WriteString(h, fmt.Sprintf("%d%x", 0, f))
if err != nil {
panic(err)
}
hashName := h.Sum(nil)
data = append(data, hashName)
tree, err := merkle.NewUsing(data, sha3.New512(), false)
if err != nil {
panic(err)
}
return hex.EncodeToString(tree.Root())
}