How to use the @polkadot/util-crypto.naclVerify function in @polkadot/util-crypto

To help you get started, we’ve selected a few @polkadot/util-crypto examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / common / docs / examples / util-crypto / 02_sign_verify_message_nacl / index.js View on Github external
const { secretKey, publicKey } = naclKeypairFromSeed(stringToU8a(seedAlice));

  // Encrypt message. Create Uint8Array's filled with random bytes of specified length
  const secret = randomAsU8a(32);
  const messagePreEncryption = stringToU8a('please send me DOTs');
  const noncePreEncryption = randomAsU8a(24);

  const { encrypted } = naclEncrypt(messagePreEncryption, secret, noncePreEncryption);

  // Sign the message with a valid signature
  const messageSignature = naclSign(encrypted, secretKey);

  console.log(`Message signature: ${u8aToHex(messageSignature)}`);

  // Validate that the message was correctly signed
  const isValidSignature = naclVerify(encrypted, messageSignature, publicKey);

  console.log(`Was the message correctly signed? ${isValidSignature}`);
}
github polkadot-js / apps / packages / app-toolbox / src / Verify.tsx View on Github external
useEffect((): void => {
    let cryptoType: CryptoTypes = 'unknown';
    let isValid = isValidPk && isValidSignature;

    // We cannot just use the keyring verify since it may be an address. So here we first check
    // for ed25519, if not valid, we try against sr25519 - if neither are valid, well, we have
    // not been able to validate the signature
    if (isValid && publicKey) {
      let isValidSr = false;
      let isValidEd = false;

      try {
        isValidEd = naclVerify(data, signature, publicKey);
      } catch (error) {
        // do nothing, already set to false
      }

      if (isValidEd) {
        cryptoType = 'ed25519';
      } else {
        try {
          isValidSr = schnorrkelVerify(data, signature, publicKey);
        } catch (error) {
          // do nothing, already set to false
        }

        if (isValidSr) {
          cryptoType = 'sr25519';
        } else {
github polkadot-js / client / packages / client-runtime / src / crypto / index.ts View on Github external
instrument('ed25519_verify', (): number => {
        const m = heap.get(msgPtr, msgLen);
        const s = heap.get(sigPtr, 64);
        const p = heap.get(pubkeyPtr, 32);

        l.debug((): any[] => ['ed25519_verify', [msgPtr, msgLen, sigPtr, pubkeyPtr], '<-', { m, p, s }]);

        try {
          return naclVerify(m, s, p) ? 0 : 5;
        } catch (error) {
          return 5;
        }
      }),
    // eslint-disable-next-line @typescript-eslint/camelcase
github polkadot-js / common / packages / keyring / src / pair / index.ts View on Github external
const verify = (type: KeypairType, message: Uint8Array, signature: Uint8Array, publicKey: Uint8Array): boolean =>
  isSr25519(type)
    ? schnorrkelVerify(message, signature, publicKey)
    : naclVerify(message, signature, publicKey);