3

When I try to derive the Alice account with PolkadotJS I somehow end up with a different address than in the docs.

Here is the code:

const keyring = new (require('@polkadot/keyring').Keyring)();

let pair = keyring.createFromUri('//Alice');
console.log(`${pair.address}`);
// prints 5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu

Prefixing the it with the dev menmonic bottom drive obey lake curtain smoke basket hold race lonely fit walk does not make a difference.

The unofficial python bindings from polkascan and subkey both yield the correct address:

from substrateinterface import Keypair

keypair = Keypair.create_from_uri('//Alice')
print(keypair.ss58_address)
# prints 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY

Subkey:

docker run -it --rm --pull=always docker.io/parity/subkey:latest inspect "//Alice" | grep "SS58 Address"
# prints 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY

Am I somehow using it wrong or why is PolkadotJS printing 5FA9nQDVg267DEd8m1ZypXLBnvN7SFxYwV7ndqSYGiN9TTpu?

1 Answer 1

6

Make sure you specify your keyring to use sr25519 versus ed25519 which I believe is default:

// Create a keyring instance
const keyring = new Keyring({ type: 'sr25519' });

See this complete example:

// Import the API, Keyring and some utility functions
const { ApiPromise } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');

async function main () {
  // Instantiate the API
  const api = await ApiPromise.create();

  // Constuct the keyring after the API (crypto has an async init)
  const keyring = new Keyring({ type: 'sr25519' });

  // Add Alice to our keyring with a hard-deived path (empty phrase, so uses dev)
  const alice = keyring.addFromUri('//Alice');

  console.log(`${alice.address}`);
  // prints 5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY

}

main().catch(console.error).finally(() => process.exit());
2
  • Yea this works. Somehow weird that sr25519 is not the default. Mar 17 at 21:25
  • That is a good follow up question that I don't know the answer to :)
    – Shawn Tabrizi
    Mar 17 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.