2022-09-06 16:40:19 -04:00
import * as rln from "@waku/rln" ;
2023-05-14 12:18:21 -04:00
rln . create ( ) . then ( async ( rlnInstance ) => {
const credentials = rlnInstance . generateIdentityCredentials ( ) ;
//peer's index in the Merkle Tree
const index = 5 ;
// Create a Merkle tree with random members
for ( let i = 0 ; i < 10 ; i ++ ) {
if ( i == index ) {
// insert the current peer's pk
rlnInstance . insertMember ( credentials . IDCommitment ) ;
} else {
// create a new key pair
const credentials = rlnInstance . generateIdentityCredentials ( ) ; // TODO: handle error
rlnInstance . insertMember ( credentials . IDCommitment ) ;
2022-09-06 16:40:19 -04:00
}
2023-05-14 12:18:21 -04:00
}
2022-09-06 16:40:19 -04:00
2023-05-14 12:18:21 -04:00
// prepare the message
const uint8Msg = Uint8Array . from (
"Hello World" . split ( "" ) . map ( ( x ) => x . charCodeAt ( ) )
) ;
2022-09-06 16:40:19 -04:00
2023-05-14 12:18:21 -04:00
// setting up the epoch
const epoch = new Date ( ) ;
2022-09-06 16:40:19 -04:00
2023-05-14 12:18:21 -04:00
console . log ( "Generating proof..." ) ;
console . time ( "proof_gen_timer" ) ;
let proof = await rlnInstance . generateRLNProof (
uint8Msg ,
index ,
epoch ,
credentials . IDSecretHash
) ;
console . timeEnd ( "proof_gen_timer" ) ;
console . log ( "Proof" , proof ) ;
2022-09-06 16:40:19 -04:00
2023-05-14 12:18:21 -04:00
try {
// verify the proof
let verifResult = rlnInstance . verifyRLNProof ( proof , uint8Msg ) ;
console . log ( "Is proof verified?" , verifResult ? "yes" : "no" ) ;
} catch ( err ) {
console . log ( "Invalid proof" ) ;
}
2023-01-26 18:58:18 +01:00
2023-05-14 12:18:21 -04:00
const provider = new ethers . providers . Web3Provider ( window . ethereum , "any" ) ;
2023-01-26 18:58:18 +01:00
2023-05-14 12:18:21 -04:00
const DEFAULT _SIGNATURE _MESSAGE =
"The signature of this message will be used to generate your RLN credentials. Anyone accessing it may send messages on your behalf, please only share with the RLN dApp" ;
2023-01-26 18:58:18 +01:00
2023-05-14 12:18:21 -04:00
const signer = provider . getSigner ( ) ;
const signature = await signer . signMessage ( DEFAULT _SIGNATURE _MESSAGE ) ;
console . log ( ` Got signature: ${ signature } ` ) ;
2023-01-26 18:58:18 +01:00
2023-05-14 12:18:21 -04:00
const contract = await rln . RLNContract . init ( rlnInstance , {
address : rln . SEPOLIA _CONTRACT . address ,
provider : signer ,
} ) ;
2023-01-26 18:58:18 +01:00
2023-05-14 12:18:21 -04:00
const event = await contract . registerMember ( rlnInstance , signature ) ;
console . log ( ` Registered as member with ${ event } ` ) ;
2023-01-26 18:58:18 +01:00
} ) ;