Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const ethers = require("ethers");
  2.  
  3. const { bigInt } = require("snarkjs");
  4. const { eddsa, babyJub, mimc7 } = require("circomlib");
  5.  
  6. const f = async () => {
  7.   const privateKey =
  8.     "0x0123456789012345678901234567890123456789012345678901234567890123";
  9.  
  10.   const wallet = new ethers.Wallet(privateKey);
  11.  
  12.   // Message hash
  13.   const messageHash = ethers.utils.id("Hello world");
  14.   const messageHashBytes = ethers.utils.arrayify(messageHash);
  15.  
  16.   // Ethereum Signature
  17.   const flatSig = await wallet.signMessage(messageHashBytes);
  18.   const splittedSig = ethers.utils.splitSignature(flatSig);
  19.  
  20.   // Formatting signature
  21.   const r = bigInt(splittedSig.r).mod(babyJub.subOrder);
  22.   const s = bigInt(splittedSig.s);
  23.   const A = babyJub.mulPointEscalar(babyJub.Base8, s.shr(3));
  24.   const R8 = babyJub.mulPointEscalar(babyJub.Base8, r);
  25.   const hm = mimc7.multiHash([R8[0], R8[1], A[0], A[1], bigInt(messageHash)]);
  26.   const S = r.add(hm.mul(s)).mod(babyJub.subOrder);
  27.  
  28.   const signature = {
  29.     R8,
  30.     S
  31.   };
  32.  
  33.   const publicKey = babyJub.unpackPoint(
  34.     Buffer.from(wallet.signingKey.publicKey, "hex")
  35.   );
  36.  
  37.   console.log(
  38.     eddsa.verify(Buffer.from(messageHash, "hex"), signature, publicKey)
  39.   );
  40. };
  41.  
  42. f();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement