polkadot 账户操作

/* eslint-disable @typescript-eslint/restrict-template-expressions */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/**
 * @ignore Don't show this file in documentation.
 */ /** */

import { Keyring } from '@polkadot/api';
import { cryptoWaitReady, mnemonicGenerate, mnemonicToMiniSecret, randomAsHex, naclKeypairFromSeed, signatureVerify} from '@polkadot/util-crypto';

import {
  createSignedTx,
  createSigningPayload,
  decode,
  deriveAddress,
  getRegistry,
  getTxHash,
  methods,
  POLKADOT_SS58_FORMAT,
} from '../src';
import { rpcToNode, signWith } from './util';
import { stringToU8a, u8aToHex } from '@polkadot/util';

/**
 * Entry point of the script. This script assumes a Polkadot node is running
 * locally on `http://localhost:9933`.
 */
async function main(): Promise {
  // Wait for the promise to resolve async WASM
  await cryptoWaitReady();
  // Create a new keyring, and add an sr25519 type account
  const keyring = new Keyring({ ss58Format: 42, type: 'sr25519' });

  // Aries add TEST Start
  const mnemonic = mnemonicGenerate();
  console.log("memonic: ", mnemonic);
  //these will be equivalent
  console.log( "memonic addr: " , keyring.createFromUri(mnemonic).address);
  const mnemonicMini = mnemonicToMiniSecret(mnemonic);
  console.log("mnemonicMini addr: " , keyring.createFromUri(u8aToHex(mnemonicMini)).address);
  const { publicKey, secretKey } = naclKeypairFromSeed(mnemonicMini);
  console.log("publicKey: ", u8aToHex(publicKey), " secretKey: ", u8aToHex(secretKey));
  
  // for ( var i=0; i <5; i++) {
  //   const randomMini = randomAsHex(32);
  //   console.log("randomMini: ", randomMini);
  //   const spsr =  keyring.createFromUri(`${randomMini}`, {}, 'sr25519' );
  //   const sped = keyring.createFromUri(`${randomMini}`);
  //   console.log("  : " ,i , " sr25519 addr: " , deriveAddress(spsr.publicKey, POLKADOT_SS58_FORMAT), " default ed25519: ", deriveAddress(sped.publicKey, 42));
  // }

  // create an sr25519 pair from the mnemonic (keyring defaults)
  // keyring.setSS58Format(POLKADOT_SS58_FORMAT);
  const sp = keyring.createFromUri(mnemonic);
  console.log(" name: ", sp.meta.name, " address: ", sp.address);

  //转POLKADOT_SS58_FORMAT格式
  const ss58Addr = deriveAddress(sp.publicKey, POLKADOT_SS58_FORMAT);
  console.log(" ss58 addr: ", ss58Addr);

  // Aries code end
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

你可能感兴趣的:(polkadot 账户操作)