0

When using Polkadot-JS App, a new account is saved in the polkadot-accounts directory with the following file format:

{"encoded":"DBSPyMN8LAWsoejHHMfqU2/m5YxxeGtD0HJpzQzyY44AgAAAAQAAAAgAAADug5JmpYwzc9oxUJXUY2zIWkZFQRtqoS3lgu/wdhRSLPwx5TKjaMRrIqrrSyO3uxRytoPmKT5wTDV/zGyh2S9xwozzqhQhBmJG7TJwA+oNqDKpQpj6cooWNSivRzKmpqPaMO+af0LPPOREvVGHESwBSf+xHMZ5fISIG97xbXhGXV89Oo4iEBSwpIhjz6+u7AiVtF2akpyxLkhqiIXC","encoding":{"content":["pkcs8","sr25519"],"type":["scrypt","xsalsa20-poly1305"],"version":"3"},"address":"14BrSo66a3zvZmTksQFBmtZYEHvyWGJEBQypVfDynPXbsz2T","meta":{"genesisHash":"0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3","isHardware":false,"name":"POLKY123TESTONLY","tags":[],"whenCreated":1665704506323}}

I would like to achieve the same result using the polkadot-js API.

When I add an account to the keyring it is not there when I run the script the next time. Is there some way to store all accounts in a file and then load them all when creating the keyring object?

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

It seems odd to me that there isn't an obvious way to do this. How does the App work? it loads all the accounts in the polkadot-accounts directory. So why is there no API call to achieve this? Or is there?

Also if I wanted to create an account and then easily load it into the Polkadot-JS APP, I would need it in the format above, so surely there must be a way to save an account in that format ?

Basically i'm looking for a method to 'create backup file' the same way the App does. It seems like there should be a simple function to do this?

wbinky
  • 160
  • 1
  • 11

1 Answers1

0

To generate a backup file similar to the one in Polkadot-JS App you need to use:

console.log(JSON.stringify(keyring.toJson(pair.address)));

Also when creating the key pair you need to add the meta data for genesisHash:

const pair = keyring.addFromUri(mnemonic, { name: 'POLKA_KEY_1', genesisHash:'0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3', isHardware:false}, 'sr25519');

wbinky
  • 160
  • 1
  • 11