generate.ts 1017 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2017-2019 @polkadot/app-accounts authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import { GeneratorMatch, GeneratorOptions } from './types';
  5. import { encodeAddress, mnemonicGenerate, naclKeypairFromSeed, randomAsU8a, schnorrkelKeypairFromSeed, mnemonicToMiniSecret } from '@polkadot/util-crypto';
  6. import calculate from './calculate';
  7. export default function generator (test: string[][], options: GeneratorOptions): GeneratorMatch {
  8. const mnemonic = options.withHex
  9. ? undefined
  10. : mnemonicGenerate(12);
  11. const seed = mnemonic
  12. ? mnemonicToMiniSecret(mnemonic)
  13. : randomAsU8a();
  14. const pair = options.type === 'sr25519'
  15. ? schnorrkelKeypairFromSeed(seed)
  16. : naclKeypairFromSeed(seed);
  17. const address = encodeAddress(pair.publicKey);
  18. const { count, offset } = calculate(test, address, options);
  19. return {
  20. address,
  21. count,
  22. mnemonic,
  23. offset,
  24. seed
  25. };
  26. }