How to use the @polkadot/keyring.default function in @polkadot/keyring

To help you get started, we’ve selected a few @polkadot/keyring 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 / api / docs / examples / deprecated-rpc / 04_generate_account / index.js View on Github external
const crypto = require('crypto');
const Keyring = require('@polkadot/keyring').default;
const u8aToHex = require('@polkadot/util/u8a/toHex').default;
const Encoder = new TextEncoder(); // always utf-8

// A fixed seed from an array
const SEED1 = new Uint8Array([
  1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  22, 23, 24, 25, 26, 27, 28, 29, 30, 0, 0
]);

// A fixed seed from a 32 chars string
const SEED2 = Encoder.encode('correct horse battery and staple');

const keyring = new Keyring();
const pair1 = keyring.addFromSeed(SEED1);
const pair2 = keyring.addFromSeed(SEED2);

console.log('Deterministic addresses (same for each run):');
console.log(`Address 1\t ${pair1.address()}\t Seed: ${u8aToHex(SEED1)}`);
console.log(`Address 2\t ${pair2.address()}\t Seed: ${u8aToHex(SEED2)}`);

// A random seed
const SEED = new Uint8Array(32);

console.log('Random Addresses (random for each run):');

for (var i = 3; i < 13; i++) {
  Buffer.from(crypto.randomFillSync(SEED));
  const pair3 = keyring.addFromSeed(SEED);
  console.log(`Address ${i}\t ${pair3.address()}\t Seed: ${u8aToHex(SEED)}`);
github polkadot-js / common / docs / examples / keyring / 02_load_accounts / index.js View on Github external
async function main () {
  // Create account seed for Alice and Bob
  const ALICE_SEED = 'Alice'.padEnd(32, ' ');
  const BOB_SEED = 'Bob'.padEnd(32, ' ');

  // Create an instance of the Keyring
  const keyring = new Keyring();

  // Create their pairs with account seeds. Add to keyring pair dictionary
  keyring.addFromSeed(stringToU8a(ALICE_SEED));
  keyring.addFromSeed(stringToU8a(BOB_SEED));

  // Note that we did not use `addFromAddress` since it does not add the
  // secret key to memory so we cannot later retrieve it with `getPairs`

  // Retrieve all keyring pairs from the keyring pair dictionary
  keyring
    .getPairs()
    .forEach((pair, index) => {
      const { address } = pair;

      console.log(`\nAccount with index #${index} has json: `, JSON.stringify(keyring.toJson(address), null, 2));
    });
github polkadot-js / api / docs / examples / deprecated-rpc / 06_craft_extrinsic / index.js View on Github external
const BN = require('bn.js');
const Rpc = require('@polkadot/rpc-core').default;
const WsProvider = require('@polkadot/rpc-provider/ws').default;
const extrinsics = require('@polkadot/extrinsics').default;
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode').default;
const encodeLength = require('@polkadot/extrinsics/codec/encode/length').default;
const Keyring = require('@polkadot/keyring').default;
const storage = require('@polkadot/storage').default;

const Encoder = new TextEncoder();
const keyring = new Keyring();
const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Rpc(provider);

async function getAccountIndex (address) {
  return api.state.getStorage([storage.system.public.accountIndexOf, address]);
}

async function transfer (keyRingFrom, addressTo, amount) {
  const accountIndex = await getAccountIndex(keyRingFrom.address());

  console.log(`Current accountIndex: ${accountIndex}`);

  // encode the call for signing
  const message = encodeExtrinsic(
    keyRingFrom.publicKey(),
    accountIndex,
github polkadot-js / common / docs / examples / keyring / 01_create_account / index.js View on Github external
async function main () {
  // Create account seed for Alice
  const ALICE_SEED = 'Alice'.padEnd(32, ' ');

  // Create an instance of the Keyring
  const keyring = new Keyring();

  // Create pair and add Alice to keyring pair dictionary (with account seed)
  const pairAlice = keyring.addFromSeed(stringToU8a(ALICE_SEED));

  console.log(`Created keyring pair for Alice with address: ${keyring.getPair(pairAlice.address).address}`);
}
github polkadot-js / api / docs / examples / deprecated-rpc / 07_transfer_dots / index.js View on Github external
const BN = require('bn.js');
const Rpc = require('@polkadot/rpc-core').default;
const WsProvider = require('@polkadot/rpc-provider/ws').default;
const extrinsics = require('@polkadot/extrinsics').default;
const encodeExtrinsic = require('@polkadot/extrinsics/codec/encode/uncheckedLength').default;
const Keyring = require('@polkadot/keyring').default;
const storage = require('@polkadot/storage').default;

const Encoder = new TextEncoder();
const keyring = new Keyring();
const provider = new WsProvider('ws://127.0.0.1:9944');
const api = new Rpc(provider);

async function getAccountIndex (address) {
  return api.state.getStorage([storage.system.public.accountIndexOf, address]);
}

async function transfer (keyRingFrom, addressTo, amount) {
  const nextAccountIndex = await getAccountIndex(keyRingFrom.address());

  // encode the call for signing
  const encoded = encodeExtrinsic(
    keyRingFrom,
    nextAccountIndex,
    extrinsics.staking.public.transfer,
    [addressTo, amount]
github w3f / polkadot-deployer / lib / network / crypto.js View on Github external
create: async (nodes, environment=false) => {
    if (environment) {
      return environmentKeys(nodes);
    }
    const output = {};
    const keyTypes = module.exports.keyTypes();
    keyTypes.forEach((type) => {
      output[type] = [];
    });
    const keyringEd = new Keyring({ type: 'ed25519' });
    const keyringSr = new Keyring({ type: 'sr25519' });

    await waitReady();

    for (let counter = 0; counter < nodes; counter++) {
      keyTypes.forEach((type) => {
        const { seedU8a, seed, mnemonic } = generateSeed();

        let keyring;
        if (type === 'session_grandpa') {
          keyring = keyringEd;
        } else {
          keyring = keyringSr;
        }

        const pair = keyring.addFromSeed(seedU8a);
github w3f / polkadot-deployer / lib / network / crypto.js View on Github external
create: async (nodes, environment=false) => {
    if (environment) {
      return environmentKeys(nodes);
    }
    const output = {};
    const keyTypes = module.exports.keyTypes();
    keyTypes.forEach((type) => {
      output[type] = [];
    });
    const keyringEd = new Keyring({ type: 'ed25519' });
    const keyringSr = new Keyring({ type: 'sr25519' });

    await waitReady();

    for (let counter = 0; counter < nodes; counter++) {
      keyTypes.forEach((type) => {
        const { seedU8a, seed, mnemonic } = generateSeed();

        let keyring;
        if (type === 'session_grandpa') {
          keyring = keyringEd;
        } else {
          keyring = keyringSr;
        }

        const pair = keyring.addFromSeed(seedU8a);
        const address = pair.address;

@polkadot/keyring

Keyring management

Apache-2.0
Latest version published 8 days ago

Package Health Score

84 / 100
Full package analysis

Similar packages