2023-09-12 19:14:08 +00:00
//go:build !gowaku_no_rln
// +build !gowaku_no_rln
2023-08-18 21:24:04 +00:00
package rlngenerate
import (
"context"
"errors"
"fmt"
"math/big"
cli "github.com/urfave/cli/v2"
"github.com/waku-org/go-waku/logging"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/group_manager/dynamic"
"github.com/waku-org/go-waku/waku/v2/protocol/rln/keystore"
2023-08-31 15:49:38 +00:00
"github.com/waku-org/go-waku/waku/v2/protocol/rln/web3"
2023-08-18 21:24:04 +00:00
"github.com/waku-org/go-waku/waku/v2/utils"
"github.com/waku-org/go-zerokit-rln/rln"
"go.uber.org/zap"
)
var options Options
var logger = utils . Logger ( ) . Named ( "rln-credentials" )
// Command generates a key file used to generate the node's peerID, encrypted with an optional password
var Command = cli . Command {
Name : "generate-rln-credentials" ,
Usage : "Generate credentials for usage with RLN" ,
Action : func ( cCtx * cli . Context ) error {
2023-08-23 20:50:25 +00:00
if options . ETHPrivateKey == nil {
err := errors . New ( "a private key must be specified" )
2023-08-18 21:24:04 +00:00
logger . Error ( "validating option flags" , zap . Error ( err ) )
return cli . Exit ( err , 1 )
}
2023-08-23 20:50:25 +00:00
err := execute ( context . Background ( ) )
2023-08-18 21:24:04 +00:00
if err != nil {
logger . Error ( "registering RLN credentials" , zap . Error ( err ) )
return cli . Exit ( err , 1 )
}
return nil
} ,
Flags : flags ,
}
func execute ( ctx context . Context ) error {
rlnInstance , err := rln . NewRLN ( )
if err != nil {
return err
}
2023-08-31 15:49:38 +00:00
web3Config , err := web3 . BuildConfig ( ctx , options . ETHClientAddress , options . MembershipContractAddress )
2023-08-18 21:24:04 +00:00
if err != nil {
return err
}
// prepare rln membership key pair
logger . Info ( "generating rln credential" )
identityCredential , err := rlnInstance . MembershipKeyGen ( )
if err != nil {
return err
}
// register the rln-relay peer to the membership contract
2023-08-31 15:49:38 +00:00
membershipIndex , err := register ( ctx , web3Config , identityCredential . IDCommitment )
2023-08-18 21:24:04 +00:00
if err != nil {
return err
}
// TODO: clean private key from memory
2023-08-31 15:49:38 +00:00
err = persistCredentials ( identityCredential , membershipIndex , web3Config . ChainID )
2023-08-18 21:24:04 +00:00
if err != nil {
return err
}
if logger . Level ( ) == zap . DebugLevel {
logger . Info ( "registered credentials into the membership contract" ,
2023-11-09 11:40:19 +00:00
logging . HexBytes ( "IDCommitment" , identityCredential . IDCommitment [ : ] ) ,
logging . HexBytes ( "IDNullifier" , identityCredential . IDNullifier [ : ] ) ,
logging . HexBytes ( "IDSecretHash" , identityCredential . IDSecretHash [ : ] ) ,
logging . HexBytes ( "IDTrapDoor" , identityCredential . IDTrapdoor [ : ] ) ,
2023-08-18 21:24:04 +00:00
zap . Uint ( "index" , membershipIndex ) ,
)
} else {
2023-11-09 11:40:19 +00:00
logger . Info ( "registered credentials into the membership contract" , logging . HexBytes ( "idCommitment" , identityCredential . IDCommitment [ : ] ) , zap . Uint ( "index" , membershipIndex ) )
2023-08-18 21:24:04 +00:00
}
2023-08-31 15:49:38 +00:00
web3Config . ETHClient . Close ( )
2023-08-18 21:24:04 +00:00
return nil
}
2023-08-24 18:42:50 +00:00
func persistCredentials ( identityCredential * rln . IdentityCredential , treeIndex rln . MembershipIndex , chainID * big . Int ) error {
2023-08-23 20:50:25 +00:00
appKeystore , err := keystore . New ( options . CredentialsPath , dynamic . RLNAppInfo , logger )
if err != nil {
return err
}
2023-08-24 18:42:50 +00:00
membershipCredential := keystore . MembershipCredentials {
IdentityCredential : identityCredential ,
TreeIndex : treeIndex ,
MembershipContractInfo : keystore . NewMembershipContractInfo ( chainID , options . MembershipContractAddress ) ,
2023-08-18 21:24:04 +00:00
}
2023-08-24 18:42:50 +00:00
err = appKeystore . AddMembershipCredentials ( membershipCredential , options . CredentialsPassword )
2023-08-18 21:24:04 +00:00
if err != nil {
return fmt . Errorf ( "failed to persist credentials: %w" , err )
}
2023-08-24 18:42:50 +00:00
logger . Info ( "persisted credentials succesfully" )
2023-08-18 21:24:04 +00:00
return nil
}