forked from alrevuelta/rln-proof-witness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
130 lines (110 loc) · 3.83 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
package main
import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math/big"
"net/http"
"strconv"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/waku-org/go-zerokit-rln/rln"
)
// See this branch with endpoint to retrieve merkle proof:
// https://github.com/waku-org/go-waku/compare/master...merkle-proof-provider
// Deployed in sandbox machine as proof of concept.
const MerkleProofProdiver = "http://65.21.94.244:30312"
func main() {
rlnInstance, err := rln.NewRLN()
if err != nil {
log.Fatal(err)
}
// Proof generation for this membership:
// https://sepolia.etherscan.io/tx/0x039ded260a587ad14262a1690e604adaf4a7326cbeb028095dc081f772c0cb44
idCommitment, err := rln.ToBytes32LE("df5b05a4d6c3f5a3aee2ea4664a1ccd529933058a2d82931153b6d0c0a0df32e")
if err != nil {
log.Fatal(err)
}
idCommitmentBig := new(big.Int).SetBytes(idCommitment[:])
// Only the owner will know this secret
idSecretHash, err := rln.ToBytes32LE("bad944102c2798ce77e8feb37d264f64b033e847f2e9ac29acb527543ea8f008")
if err != nil {
log.Fatal(err)
}
merkleProof, err := GetMerkleProof(idCommitmentBig)
if err != nil {
log.Fatal(err)
}
log.Info("idCommitmentBig: ", idCommitmentBig, " index: ", merkleProof.LeafIndex, " merkle root: ", merkleProof.MerkleRoot)
// RLN proof generation
someMessage := []byte("some message") // Your message
epoch := rln.ToEpoch(0) // Epoch timestsamp in seconds
// We create a witness with the merkle proof and our secret
witness := rln.CreateWitness(idSecretHash, someMessage, epoch, rln.MerkleProof{
PathIndexes: ConvertStringToUint8(merkleProof.MerkePathIndexes),
PathElements: ConvertStringToBytes(merkleProof.MerkePathElements),
})
// We can generate a RLN proof without having the whole tree
rlnProof, err := rlnInstance.GenerateRLNProofWithWitness(witness)
if err != nil {
log.Fatal(err)
}
fmt.Println("rln proof is: ", rlnProof)
}
// TODO: Duplicated from go code
// See: https://github.com/waku-org/go-waku/compare/master...merkle-proof-provider
type MerkleProofResponse struct {
MerkleRoot string `json:"root"`
MerkePathElements []string `json:"pathElements"`
MerkePathIndexes []string `json:"pathIndexes"`
LeafIndex uint64 `json:"leafIndex"`
CommitmentId string `json:"commitmentId"`
}
func GetMerkleProof(publicCommitment *big.Int) (*MerkleProofResponse, error) {
url := fmt.Sprintf("%s/debug/v1/merkleProof/%s", MerkleProofProdiver, publicCommitment.String())
resp, err := http.Get(url)
if err != nil {
return nil, errors.Wrap(err, "failed to get merkle proof")
}
defer resp.Body.Close()
merkleProof := &MerkleProofResponse{}
if resp.StatusCode == http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "failed to read response body")
}
if err = json.Unmarshal(bodyBytes, merkleProof); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal response body")
}
}
return merkleProof, err
}
func ConvertStringToUint8(pathIndexesStr []string) []uint8 {
// Convert merkle proof from string to uint8
pathIndexes := make([]uint8, 0)
for _, pathIndex := range pathIndexesStr {
pathIndexUint8, err := strconv.ParseUint(pathIndex, 10, 8)
if err != nil {
log.Fatal(err)
}
pathIndexes = append(pathIndexes, uint8(pathIndexUint8))
}
return pathIndexes
}
func ConvertStringToBytes(pathElementsStr []string) [][32]byte {
// TODO: check commitment id match
// TODO: check merkle proof is valid against root.
// Convert merkle proof from string to [32]byte
pathElements := make([][32]byte, 0)
for _, pathElement := range pathElementsStr {
pathElementBytes, err := hex.DecodeString(pathElement)
if err != nil {
log.Fatal(err)
}
var pathElement32 [32]byte
copy(pathElement32[:], pathElementBytes)
pathElements = append(pathElements, pathElement32)
}
return pathElements
}