2

I am using Polkadot-js api and wondered if there is an API to generate hierarchical deterministic wallet addresses from a given seed?

I see that there is some mention of soft and hard derivation paths, in Substrate's subkey documentation but do not know if this has been ported to Polkadot-js or how it could be invoked.

T9b
  • 3,312
  • 5
  • 31
  • 50

1 Answers1

3

SURI are supported by Polkadot-js using createFromUri or addFromUri to create or add a new account to your keyring. You can import keyring from '@polkadot/ui-keyring' and then use it:

keyring.createFromUri(`${phrase.trim()}${derivePath}`, {}, pairType)

Here is the definition of the function

edit: The derive path could be any combination of /[soft], //[hard], that can be repeated and don't have to be in this order, on top of this, you can have an additional ///[password].

So you can pass as argument to the createFromUri function, something like:[mnemonic phrase]//Kusama//DAO/1 or [mnemonic phrase]//MyMainFunds/0///ThisIsMyPassword.

Tbaut
  • 306
  • 1
  • 7
  • Could you edit your reply to be more explicit on what the derive path should look like. For example BIP44 derivation path can be written as `m/44'/0'/0'/0/0` but that will "probably" not work like that. What would be the correct syntax? – T9b Sep 10 '19 at 21:17
  • Adding further info. The apostrophe denotes a hard path. Therefore in substrate the BIP44 equivalent is `m//44//0//0/0/0`. Furthermore in substrate using a soft derivation path you are able to share this derivation path with a root public address too allow third parties to generate valid payment addresses on your behalf without requesting from you. However you should not share the root address unless the seed has been password protected (by appending `///yourpassword` to the seed. This way you obtain a root address that cannot be retrieved from the seed alone, but also requires the password. – T9b Nov 06 '19 at 14:02