Other common methods
Some other common methods you will use often are:
const hash = Poseidon.hash([x]); // takes array of Fields, returns Fieldconst privKey = PrivateKey.random(); // create a private keyconst pubKey = PublicKey.fromPrivateKey(privKey); // derive public keyconst msg = [hash];const sig = Signature.create(privKey, msg); // sign a messageconst isVerified = sig.verify(pubKey, msg); // Bool(true)Try it out
Define a variable x for a Field with a default value set to 1:
const x = Field(1);Declare a variable named hash and use the Poseidon hash function, which accepts an array of Fields as input and produces a single Field as its output:
const hash = Poseidon.hash([x]); // takes array of Fields, returns FieldDeclare a variable privKey and generate a private key using the random method on the PrivateKey class:
const privKey = PrivateKey.random(); // create a private keyDeclare a variable pubKey by deriving a public key from the private key using the fromPrivateKey method on the PublicKey class:
const pubKey = PublicKey.fromPrivateKey(privKey); // derive public keyDeclare a variable named msg and set it to our calculated hash.
const msg = [hash];Declare a variable named sig and generate a signature by signing the message with the private key.
const sig = Signature.create(privKey, msg); // sign a messageDefine a variable named isVerified to validate the signature using the public key and the message.
const isVerified = sig.verify(pubKey, msg); // Bool(true)We can log the value of the isVerified variable to check that we have successfully signed and verified the message:
Provable.log('is signature verified:', isVerified) // should be trueNow build and run the script in the terminal:
npm run build && node build/src/index.js- Installing dependencies