polkadot-js / common

Utilities and base libraries for use across polkadot-js for Polkadot and Substrate. Includes base libraries, crypto helpers and cross-environment helpers.
Apache License 2.0
252 stars 142 forks source link

Unable to generate keypair from random seed. #827

Closed kingsley-einstein closed 3 years ago

kingsley-einstein commented 3 years ago

I have tried to create a pair from URI using a random seed but I keep getting this error:

Expected a valid key to convert, with length 1, 2, 4, 8, 32, 33
try {
   const keyring = new Keyring({ type: "sr25519", ss58Format });
   const miniSecret = randomAsHex(32);
   const pair = keyring.addFromUri(miniSecret, { name });
   return Promise.resolve({
    statusCode: 200,
    payload: {
     address: pair.address,
     privateKey: miniSecret
    }
   });
  } catch (error) {
   console.log(error);
   return Promise.reject(new Error(error.message));
  }

When I try to generate a mini secret from a randomly generated mnemonic, I get the error:

RuntimeError: unreachable

This is very similar to the example in the documentation: https://polkadot.js.org/docs/keyring/start/suri

kingsley-einstein commented 3 years ago

@gavofyork @AndreasGassmann

jacogr commented 3 years ago

Here is a working sample, from your code as per https://polkadot.js.org/docs/util-crypto/FAQ#i-am-having-trouble-initializing-the-wasm-interface -

import { Keyring } from '@polkadot/keyring';
import { cryptoWaitReady, randomAsHex } from '@polkadot/util-crypto';

async function main () {
  await cryptoWaitReady();

  const keyring = new Keyring({ type: 'sr25519', ss58Format: 2 });
  const seed = randomAsHex(32);
  const pair = keyring.addFromUri(seed, { name: 'testing' });

  return {
    address: pair.address,
    seed
  };
}

main()
  .then((result) => {
    console.log(result);
    process.exit(0);
  })
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Output -

{
  address: 'FoAWwpGsvXpn4X1vt3Ea63o1orfr1o1qGnoEjcteZr43FKi',
  seed: '0xc2064b6b6d8e21026d3d046be8d45214de88c0de0a688cdf1fada2ab0337c048'
}

Runnable sample - https://github.com/jacogr/sample-create-random-key

kingsley-einstein commented 3 years ago

Here is a working sample, from your code as per https://polkadot.js.org/docs/util-crypto/FAQ#i-am-having-trouble-initializing-the-

Same problem

jacogr commented 3 years ago

Did you look at the linked repo - it is a runnable sample

kingsley-einstein commented 3 years ago

Did you look at the linked repo - it is a runnable sample

Yes.

jacogr commented 3 years ago

Cloned, installed and ran it?

$ git clone git@github.com:jacogr/sample-create-random-key.git try
Cloning into 'try'...
remote: Enumerating objects: 11, done.
remote: Counting objects: 100% (11/11), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 11 (delta 2), reused 10 (delta 1), pack-reused 0
Receiving objects: 100% (11/11), 6.84 KiB | 6.84 MiB/s, done.
Resolving deltas: 100% (2/2), done.
$ cd try 
$ yarn
yarn install v1.22.10
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
✨  Done in 0.58s.
$ yarn start
yarn run v1.22.10
$ node --experimental-json-modules ./index.mjs
(node:33095) ExperimentalWarning: Importing JSON modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
{
  address: 'G4jumS1WNJfZfMQUjD4Yph286KduYubNiQv2vQoQSzdan6R',
  seed: '0x5e0131c39a816668b282267340da5173bda033c4b63403fd6e9ce909677cff61'
}
✨  Done in 0.55s.
kingsley-einstein commented 3 years ago

Cloned, installed and ran it?

not at all. I just did something similar. I'm using Typescript and it's the back-end for a multi-blockchain app. It was working before. It suddenly stopped working.

kingsley-einstein commented 3 years ago

Cloned, installed and ran it?

I was initially using "mnemonicToMiniSecret" which was working but suddenly started throwing a Runtime error with the message "unreachable".

jacogr commented 3 years ago

Adjusting the above code in the same repo -

import { Keyring } from '@polkadot/keyring';
import { u8aToHex } from '@polkadot/util';
import { cryptoWaitReady, randomAsHex, mnemonicGenerate, mnemonicToMiniSecret } from '@polkadot/util-crypto';

async function main () {
  await cryptoWaitReady();

  const keyring = new Keyring({ type: 'sr25519', ss58Format: 2 });

  console.log('      via seed', keyring.addFromUri(randomAsHex(32), { name: 'testing' }).address);
  console.log('  via mnemonic', keyring.addFromUri(mnemonicGenerate(), { name: 'testing' }).address);

  const miniSecret = u8aToHex(mnemonicToMiniSecret(mnemonicGenerate()));

  console.log('via minisecret', keyring.addFromUri(miniSecret, { name: 'testing' }).address);
}

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

Output -

      via seed ELw6v9JYz9nVUFJ92gHebPxzjDia2ueiF9MD8v5AZ3BiVaB
  via mnemonic FP1F463pA7JgC3nUaqUks8ukiRE9Kv2DpEK9uiMxqTBsf95
via minisecret H2Nijz3bYh9FAXi7TnqhBMrppBg2Mqko7gjk6GwwDJcFLpR
jacogr commented 3 years ago

So, it is a local environment issue on your side. I would suggest -

kingsley-einstein commented 3 years ago

So, it is a local environment issue on your side. I would suggest -

  • check that your util/util-crypto/wasm is on the latest versions
  • same with API
  • ensure that there are not multiple of these installed

I presume the latest @polkadot/util/util-crypto/wasm version is 5.2.3

kingsley-einstein commented 3 years ago

So, it is a local environment issue on your side. I would suggest -

  • check that your util/util-crypto/wasm is on the latest versions
  • same with API
  • ensure that there are not multiple of these installed

Still getting same issue

kingsley-einstein commented 3 years ago

@jacogr This is getting frustrating. It was initially working and now, it is suddenly giving problems.

jacogr commented 3 years ago

It is certainly environmental based on the setup when it "suddenly stops working". (Code-wise, it seems to align with the linked working sample).

"initially working" - "suddenly stopped" - what changes did you make to change the outcome?

kingsley-einstein commented 3 years ago

It is certainly environmental based on the setup when it "suddenly stops working". (Code-wise, it seems to align with the linked working sample).

"initially working" - "suddenly stopped" - what changes did you make to change the outcome?

I made no change at all. Code has been the same since it was working. I was implementing a new coin when all of a sudden it stopped working.

jacogr commented 3 years ago

We've determined that the code you have is fine. Code doesn't "just break", so we know it is a change in environment caused by a change made. Do the following -

cat yarn.lock| grep @polkadot/wasm-crypto to ensure you don't have multiples. (yarn dedupe will adjust if there are)

kingsley-einstein commented 3 years ago

Thanks for your aid @jacogr . Issue has been resolved. It was the result of a conflicting library.

polkadot-js-bot commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.