How to use the @polkadot/util-crypto.cryptoWaitReady 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 / extension / packages / extension / src / background / index.ts View on Github external
// setup the notification (same a FF default background, white text)
extension.browserAction.setBadgeBackgroundColor({ color: '#d90000' });

// listen to all messages and handle appropriately
extension.runtime.onConnect.addListener((port): void => {
  // shouldn't happen, however... only listen to what we know about
  assert([PORT_CONTENT, PORT_EXTENSION].includes(port.name), `Unknown connection from ${port.name}`);

  // message and disconnect handlers
  port.onMessage.addListener((data): void => handlers(data, port));
  port.onDisconnect.addListener((): void => console.log(`Disconnected from ${port.name}`));
});

// initial setup
cryptoWaitReady()
  .then((): void => {
    console.log('crypto initialized');

    // load all the keyring data
    keyring.loadAll({ store: new ExtensionStore(), type: 'sr25519' });

    console.log('initialization completed');
  })
  .catch((error): void => {
    console.error('initialization failed', error);
  });
github polkadot-js / apps / packages / app-accounts / src / bipWorker.ts View on Github external
// Copyright 2017-2019 @polkadot/app-accounts authors & contributors
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.

import { cryptoWaitReady, mnemonicGenerate, mnemonicToMiniSecret, naclKeypairFromSeed, schnorrkelKeypairFromSeed } from '@polkadot/util-crypto';

const ctx: Worker = self as unknown as Worker;

cryptoWaitReady().catch((): void => {
  // ignore
});

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 / tools / packages / signer-cli / src / cmdSign.ts View on Github external
export default async function cmdSign (account: string, seed: string, type: Curves, [payload]: string[]): Promise {
  await cryptoWaitReady();

  const keyring = new Keyring({ type });
  const pair = keyring.createFromUri(seed);
  const signature = pair.sign(hexToU8a(payload));

  const prefix = new Uint8Array(curvePrefixes[type]);

  console.log(`Signature: ${u8aToHex(u8aConcat(prefix, signature))}`);

  process.exit(0);
}
github polkadot-js / apps / packages / app-accounts / src / vanitygen / cli.ts View on Github external
indicator++;

  if (indicator === INDICATORS.length) {
    indicator = 0;
  }

  process.stdout.write(`\r[${INDICATORS[indicator]}] ${(total.toString().match(NUMBER_REGEX) || []).join(',')} keys in ${(elapsed).toFixed(2)}s (${(total / elapsed).toFixed(0)} keys/s)`);
}

function showBest (): void {
  const { address, count, mnemonic, offset, seed } = best;

  console.log(`\r::: ${address.slice(0, offset)}${chalk.cyan(address.slice(offset, count + offset))}${address.slice(count + offset)} <= ${u8aToHex(seed)} (count=${count}, offset=${offset})${mnemonic ? '\n                                                        ' + mnemonic : ''}`);
}

cryptoWaitReady()
  .then((): void => {
    while (true) {
      const nextBest = generator(options).found.reduce((best, match): Best => {
        if ((match.count > best.count) || ((match.count === best.count) && (match.offset <= best.offset))) {
          return match;
        }

        return best;
      }, best);

      total += options.runs;

      if (nextBest.address !== best.address) {
        best = nextBest;
        showBest();
        showProgress();
github polkadot-js / ui / packages / example-react / src / index.tsx View on Github external
<section>
        <label>ss58 format</label>
        <select value="{ss58Format}">
          {settings.availablePrefixes
            .filter((_, index): boolean =&gt; index !== 0)
            .map(({ text, value }): React.ReactNode =&gt; (
              <option value="{value}">{text}</option>
            ))
          }
        </select>
      </section>
    
  );
}

cryptoWaitReady().then((): void =&gt; {
  keyring.loadAll({ ss58Format: 42, type: 'sr25519' });
  ReactDOM.render(, rootElement);
});
github polkadot-js / client / packages / client / src / index.ts View on Github external
public async start (config: ConfigPartial): Promise {
    await cryptoWaitReady();

    const verStatus = await clientId.getNpmStatus();
    const status = verStatus
      ? `(${verStatus})`
      : '';

    this.sync = config.sync;

    l.log(`Running version ${clientId.version} ${status}`);
    l.log(`Initializing for ${this.sync} sync on chain ${config.chain}`);

    this.chain = new Chain(config as Config);

    if (config.p2p &amp;&amp; config.p2p.active &amp;&amp; config.p2p.port) {
      this.p2p = new P2p(config as Config, this.chain);
    }