Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ethers = require("ethers");
- const { bigInt } = require("snarkjs");
- const { eddsa, babyJub, mimc7 } = require("circomlib");
- const f = async () => {
- const privateKey =
- "0x0123456789012345678901234567890123456789012345678901234567890123";
- const wallet = new ethers.Wallet(privateKey);
- // Message hash
- const messageHash = ethers.utils.id("Hello world");
- const messageHashBytes = ethers.utils.arrayify(messageHash);
- // Ethereum Signature
- const flatSig = await wallet.signMessage(messageHashBytes);
- const splittedSig = ethers.utils.splitSignature(flatSig);
- // Formatting signature
- const r = bigInt(splittedSig.r).mod(babyJub.subOrder);
- const s = bigInt(splittedSig.s);
- const A = babyJub.mulPointEscalar(babyJub.Base8, s.shr(3));
- const R8 = babyJub.mulPointEscalar(babyJub.Base8, r);
- const hm = mimc7.multiHash([R8[0], R8[1], A[0], A[1], bigInt(messageHash)]);
- const S = r.add(hm.mul(s)).mod(babyJub.subOrder);
- const signature = {
- R8,
- S
- };
- const publicKey = babyJub.unpackPoint(
- Buffer.from(wallet.signingKey.publicKey, "hex")
- );
- console.log(
- eddsa.verify(Buffer.from(messageHash, "hex"), signature, publicKey)
- );
- };
- f();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement