go-waku/examples/chat2/rln-credentials.go

118 lines
2.9 KiB
Go
Raw Normal View History

2022-08-09 00:02:08 +00:00
package main
import (
2022-10-10 22:08:35 +00:00
"bytes"
2022-08-09 00:02:08 +00:00
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
2022-09-11 21:08:58 +00:00
"path/filepath"
2022-08-09 00:02:08 +00:00
"github.com/ethereum/go-ethereum/common"
2022-10-10 22:08:35 +00:00
"github.com/status-im/go-waku/waku/v2/node"
2022-10-04 23:15:39 +00:00
"github.com/status-im/go-zerokit-rln/rln"
2022-08-09 00:02:08 +00:00
)
2022-09-11 21:08:58 +00:00
const RLN_CREDENTIALS_FILENAME = "rlnCredentials.txt"
2022-08-09 00:02:08 +00:00
func fileExists(path string) bool {
if _, err := os.Stat(path); err == nil {
return false
} else if errors.Is(err, os.ErrNotExist) {
return false
} else {
return false
}
}
2022-10-10 22:08:35 +00:00
func writeRLNMembershipCredentialsToFile(path string, keyPair *rln.MembershipKeyPair, idx rln.MembershipIndex, contractAddress common.Address) error {
if path == "" {
return nil // No path to save file
}
2022-09-11 21:08:58 +00:00
2022-10-10 22:08:35 +00:00
path = filepath.Join(path, RLN_CREDENTIALS_FILENAME)
2022-08-09 00:02:08 +00:00
if fileExists(path) {
return nil
}
2022-10-10 22:08:35 +00:00
if keyPair == nil {
return nil // No credentials to write
}
credentialsJSON, err := json.Marshal(node.MembershipCredentials{
Keypair: &rln.MembershipKeyPair{
IDKey: keyPair.IDKey,
IDCommitment: keyPair.IDCommitment,
},
Index: idx,
Contract: contractAddress,
2022-08-09 00:02:08 +00:00
})
if err != nil {
return err
}
return ioutil.WriteFile(path, credentialsJSON, 0600)
}
2022-10-10 22:08:35 +00:00
func loadMembershipCredentialsFromFile(rlnCredentialsPath string) (node.MembershipCredentials, error) {
2022-09-11 21:08:58 +00:00
src, err := ioutil.ReadFile(rlnCredentialsPath)
2022-08-09 00:02:08 +00:00
if err != nil {
2022-10-10 22:08:35 +00:00
return node.MembershipCredentials{}, err
2022-08-09 00:02:08 +00:00
}
2022-10-10 22:08:35 +00:00
var credentials node.MembershipCredentials
2022-08-09 00:02:08 +00:00
err = json.Unmarshal(src, &credentials)
2022-10-10 22:08:35 +00:00
return credentials, err
2022-08-09 00:02:08 +00:00
}
2022-10-10 22:08:35 +00:00
func getMembershipCredentials(options RLNRelayOptions) (credentials node.MembershipCredentials, err error) {
2022-08-15 18:29:59 +00:00
valuesWereInput := false
2022-10-10 22:08:35 +00:00
if options.IDKey != "" || options.IDCommitment != "" {
2022-08-15 18:29:59 +00:00
valuesWereInput = true
}
2022-10-10 22:08:35 +00:00
path := options.CredentialsPath
if path == "" {
return node.MembershipCredentials{
Contract: options.MembershipContractAddress,
}, nil
}
2022-08-15 18:29:59 +00:00
var osErr error
if !valuesWereInput {
2022-09-11 21:08:58 +00:00
path = filepath.Join(path, RLN_CREDENTIALS_FILENAME)
2022-08-15 18:29:59 +00:00
if _, osErr = os.Stat(path); osErr == nil {
2022-10-10 22:08:35 +00:00
if credentials, err := loadMembershipCredentialsFromFile(path); err != nil {
return node.MembershipCredentials{}, fmt.Errorf("could not read membership credentials file: %w", err)
2022-08-15 18:29:59 +00:00
} else {
2022-10-10 22:08:35 +00:00
if (bytes.Equal(credentials.Contract.Bytes(), common.Address{}.Bytes())) {
credentials.Contract = options.MembershipContractAddress
}
return credentials, nil
2022-08-15 18:29:59 +00:00
}
2022-08-09 00:02:08 +00:00
}
}
2022-10-10 22:08:35 +00:00
var keypair *rln.MembershipKeyPair
2022-08-15 18:29:59 +00:00
if valuesWereInput || os.IsNotExist(osErr) {
2022-10-10 22:08:35 +00:00
if options.IDKey != "" && options.IDCommitment != "" {
keypair = new(rln.MembershipKeyPair)
copy((keypair.IDKey)[:], common.FromHex(options.IDKey))
copy((keypair.IDCommitment)[:], common.FromHex(options.IDCommitment))
2022-08-09 00:02:08 +00:00
}
2022-10-10 22:08:35 +00:00
return node.MembershipCredentials{
Keypair: keypair,
Index: uint(options.MembershipIndex),
Contract: options.MembershipContractAddress,
}, nil
2022-08-09 00:02:08 +00:00
}
2022-10-10 22:08:35 +00:00
return node.MembershipCredentials{}, fmt.Errorf("could not read membership credentials file: %w", err)
2022-08-09 00:02:08 +00:00
}