2022-08-12 12:44:13 +00:00
|
|
|
//go:build gowaku_rln
|
|
|
|
// +build gowaku_rln
|
|
|
|
|
2022-08-09 00:02:08 +00:00
|
|
|
package waku
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"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/accounts/keystore"
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/status-im/go-rln/rln"
|
2022-09-11 21:08:58 +00:00
|
|
|
"go.uber.org/zap"
|
2022-08-09 00:02:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type membershipCredentials struct {
|
|
|
|
Keypair rln.MembershipKeyPair `json:"keypair"`
|
|
|
|
Index rln.MembershipIndex `json:"index"`
|
|
|
|
}
|
|
|
|
|
2022-09-11 21:08:58 +00:00
|
|
|
const RLN_CREDENTIALS_FILENAME = "rlnCredentials.txt"
|
|
|
|
|
2022-08-09 00:02:08 +00:00
|
|
|
func writeRLNMembershipCredentialsToFile(keyPair rln.MembershipKeyPair, idx rln.MembershipIndex, path string, passwd []byte, overwrite bool) error {
|
2022-09-11 21:08:58 +00:00
|
|
|
path = filepath.Join(path, RLN_CREDENTIALS_FILENAME)
|
|
|
|
|
2022-08-09 00:02:08 +00:00
|
|
|
if err := checkForFileExistence(path, overwrite); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
credentialsJSON, err := json.Marshal(membershipCredentials{
|
|
|
|
Keypair: keyPair,
|
|
|
|
Index: idx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedCredentials, err := keystore.EncryptDataV3(credentialsJSON, passwd, keystore.StandardScryptN, keystore.StandardScryptP)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := json.Marshal(encryptedCredentials)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(path, output, 0600)
|
|
|
|
}
|
|
|
|
|
2022-09-11 21:08:58 +00:00
|
|
|
func loadMembershipCredentialsFromFile(credentialsFilePath string, passwd string) (rln.MembershipKeyPair, rln.MembershipIndex, error) {
|
|
|
|
src, err := ioutil.ReadFile(credentialsFilePath)
|
2022-08-09 00:02:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return rln.MembershipKeyPair{}, rln.MembershipIndex(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
var encryptedK keystore.CryptoJSON
|
|
|
|
err = json.Unmarshal(src, &encryptedK)
|
|
|
|
if err != nil {
|
|
|
|
return rln.MembershipKeyPair{}, rln.MembershipIndex(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
credentialsBytes, err := keystore.DecryptDataV3(encryptedK, passwd)
|
|
|
|
if err != nil {
|
|
|
|
return rln.MembershipKeyPair{}, rln.MembershipIndex(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
var credentials membershipCredentials
|
|
|
|
err = json.Unmarshal(credentialsBytes, &credentials)
|
|
|
|
if err != nil {
|
|
|
|
return rln.MembershipKeyPair{}, rln.MembershipIndex(0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
return credentials.Keypair, credentials.Index, err
|
|
|
|
}
|
|
|
|
|
2022-09-11 21:08:58 +00:00
|
|
|
func getMembershipCredentials(logger *zap.Logger, options Options) (fromFile bool, idKey *rln.IDKey, idCommitment *rln.IDCommitment, index rln.MembershipIndex, err error) {
|
|
|
|
credentialsFilePath := filepath.Join(options.RLNRelay.CredentialsPath, RLN_CREDENTIALS_FILENAME)
|
|
|
|
if _, err = os.Stat(credentialsFilePath); err == nil {
|
|
|
|
if keyPair, index, err := loadMembershipCredentialsFromFile(credentialsFilePath, options.KeyPasswd); err != nil {
|
2022-08-09 00:02:08 +00:00
|
|
|
return false, nil, nil, rln.MembershipIndex(0), fmt.Errorf("could not read membership credentials file: %w", err)
|
|
|
|
} else {
|
2022-09-11 21:08:58 +00:00
|
|
|
logger.Info("loaded rln credentials", zap.String("filepath", credentialsFilePath))
|
2022-08-09 00:02:08 +00:00
|
|
|
return true, &keyPair.IDKey, &keyPair.IDCommitment, index, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
if options.RLNRelay.IDKey != "" {
|
|
|
|
idKey = new(rln.IDKey)
|
|
|
|
copy((*idKey)[:], common.FromHex(options.RLNRelay.IDKey))
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.RLNRelay.IDCommitment != "" {
|
|
|
|
idCommitment = new(rln.IDCommitment)
|
|
|
|
copy((*idCommitment)[:], common.FromHex(options.RLNRelay.IDCommitment))
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, idKey, idCommitment, rln.MembershipIndex(options.RLNRelay.MembershipIndex), nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil, nil, rln.MembershipIndex(0), fmt.Errorf("could not read membership credentials file: %w", err)
|
|
|
|
}
|