How to use the @polkadot/util-crypto.naclKeypairFromSeed 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 / 03_mnemonic_generate_validate_bip39 / index.js View on Github external
// Create mnemonic string for Alice using BIP39
  const mnemonicAlice = mnemonicGenerate();

  console.log(`Generated mnemonic: ${mnemonicAlice}`);

  // Validate the mnemic string that was generated
  const isValidMnemonic = mnemonicValidate(mnemonicAlice);

  console.log(`isValidMnemonic: ${isValidMnemonic}`);

  // Create valid seed from mnemonic as u8a and convert it to a string
  // FIXME - Replace with mnemonicToSeed once exposed
  const seedAlice = mnemonicToSeed(mnemonicAlice);

  // Generate new public/secret keypair for Alice from the supplied seed
  const { secretKey, publicKey } = naclKeypairFromSeed(seedAlice);

  // Encrypt, Sign and Validate the message. See Example 'Sign & Verify Message'
}
github polkadot-js / common / docs / examples / util-crypto / 02_sign_verify_message_nacl / index.js View on Github external
async function main () {
  // Create account seed for Alice as fallback if generated mnemonic not valid
  const seedAlice = 'Alice'.padEnd(32, ' ');

  // Generate new public/secret keypair for Alice from the supplied seed
  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);
github polkadot-js / common / packages / keyring / src / keyring.ts View on Github external
if ([12, 15, 18, 21, 24].includes(parts.length)) {
        // FIXME This keeps compat with older versions, but breaks compat with subkey
        // seed = type === 'sr25519'
        //   ? mnemonicToMiniSecret(phrase, password)
        //   : mnemonicToSeed(phrase, password);
        seed = mnemonicToMiniSecret(phrase, password);
      } else {
        assert(str.length <= 32, 'specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes');

        seed = stringToU8a(str.padEnd(32));
      }
    }

    const keypair = type === 'sr25519'
      ? schnorrkelFromSeed(seed)
      : naclFromSeed(seed);
    const derived = keyFromPath(keypair, path, type);

    return createPair(type, derived, meta, null);
  }
github polkadot-js / common / packages / keyring / src / keyring.ts View on Github external
public addFromSeed (seed: Uint8Array, meta: KeyringPair$Meta = {}, type: KeypairType = this.type): KeyringPair {
    const keypair = type === 'sr25519'
      ? schnorrkelFromSeed(seed)
      : naclFromSeed(seed);

    return this.addPair(createPair(type, keypair, meta, null));
  }
github polkadot-js / apps / packages / app-accounts / src / bipWorker.ts View on Github external
ctx.onmessage = async ({ data: { pairType } }): Promise => {
  await cryptoWaitReady();

  const seed = mnemonicGenerate();
  const miniSecret = mnemonicToMiniSecret(seed);
  const { publicKey } = pairType === 'sr25519'
    ? schnorrkelKeypairFromSeed(miniSecret)
    : naclKeypairFromSeed(miniSecret);

  ctx.postMessage({
    publicKey,
    seed
  });
};
github polkadot-js / common / packages / keyring / src / pair / index.ts View on Github external
const fromSeed = (type: KeypairType, seed: Uint8Array): Keypair =>
  isSr25519(type)
    ? schnorrkelFromSeed(seed)
    : naclFromSeed(seed);