2022-08-09 00:02:08 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/status-im/go-rln/rln"
|
|
|
|
)
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
type membershipKeyPair struct {
|
|
|
|
IDKey rln.IDKey `json:"idKey"`
|
|
|
|
IDCommitment rln.IDCommitment `json:"idCommitment"`
|
|
|
|
}
|
|
|
|
|
2022-08-09 00:02:08 +00:00
|
|
|
type membershipCredentials struct {
|
2022-08-15 18:29:59 +00:00
|
|
|
Keypair membershipKeyPair `json:"membershipKeyPair"`
|
|
|
|
Index rln.MembershipIndex `json:"rlnIndex"`
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeRLNMembershipCredentialsToFile(path string, keyPair rln.MembershipKeyPair, idx rln.MembershipIndex) error {
|
|
|
|
if fileExists(path) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
credentialsJSON, err := json.Marshal(membershipCredentials{
|
2022-08-15 18:29:59 +00:00
|
|
|
Keypair: membershipKeyPair(keyPair),
|
2022-08-09 00:02:08 +00:00
|
|
|
Index: idx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(path, credentialsJSON, 0600)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadMembershipCredentialsFromFile(path string) (rln.MembershipKeyPair, rln.MembershipIndex, error) {
|
|
|
|
src, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
|
|
|
return rln.MembershipKeyPair{}, rln.MembershipIndex(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
var credentials membershipCredentials
|
|
|
|
err = json.Unmarshal(src, &credentials)
|
|
|
|
if err != nil {
|
|
|
|
return rln.MembershipKeyPair{}, rln.MembershipIndex(0), err
|
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
return rln.MembershipKeyPair(credentials.Keypair), credentials.Index, err
|
2022-08-09 00:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getMembershipCredentials(path string, rlnIDKey string, rlnIDCommitment string, rlnMembershipIndex int) (idKey *rln.IDKey, idCommitment *rln.IDCommitment, index rln.MembershipIndex, err error) {
|
2022-08-15 18:29:59 +00:00
|
|
|
valuesWereInput := false
|
|
|
|
if rlnIDKey != "" || rlnIDCommitment != "" {
|
|
|
|
valuesWereInput = true
|
|
|
|
}
|
|
|
|
|
|
|
|
var osErr error
|
|
|
|
if !valuesWereInput {
|
|
|
|
if _, osErr = os.Stat(path); osErr == nil {
|
|
|
|
if keyPair, index, err := loadMembershipCredentialsFromFile(path); err != nil {
|
|
|
|
return nil, nil, rln.MembershipIndex(0), fmt.Errorf("could not read membership credentials file: %w", err)
|
|
|
|
} else {
|
|
|
|
return &keyPair.IDKey, &keyPair.IDCommitment, index, nil
|
|
|
|
}
|
2022-08-09 00:02:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
if valuesWereInput || os.IsNotExist(osErr) {
|
2022-08-09 00:02:08 +00:00
|
|
|
if rlnIDKey != "" {
|
|
|
|
idKey = new(rln.IDKey)
|
|
|
|
copy((*idKey)[:], common.FromHex(rlnIDKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rlnIDCommitment != "" {
|
|
|
|
idCommitment = new(rln.IDCommitment)
|
|
|
|
copy((*idCommitment)[:], common.FromHex(rlnIDCommitment))
|
|
|
|
}
|
|
|
|
|
|
|
|
return idKey, idCommitment, rln.MembershipIndex(rlnMembershipIndex), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil, rln.MembershipIndex(0), fmt.Errorf("could not read membership credentials file: %w", err)
|
|
|
|
}
|