1

All examples that I found are using //Alice. However, these accounts are usually funded with test tokens and don't require a password to sign transactions. So, I would like to know how to sign and authorize a transaction using my dev wallet, that has a password, using PolkadotJs library.

I've tried so far some functions like sign(...) and getPair(...). But neither of them need a password as a parameter.

unlocking the account with password

1 Answer 1

1

How did you create an account? You only need the password if you are getting the keys from the JSON backup file.

Your JSON backup file is named like this: your_account_address.json. Unlike your mnemonic phrase, your JSON backup file is password-protected. It has all the information to restore your account, but you cannot just read it because it is encrypted for safety. To restore your account with the JSON backup file, you need to have both the file and the password to decrypt

And this is why Polkadot UI is asking it because is not storing the keys or the mnemonic.

In PolkadotJS you can recover the keys from the JSON file you have created with Polkadot UI and sign a message/transaction like this:

import keyring from '@polkadot/ui-keyring';
import { cryptoWaitReady } from '@polkadot/util-crypto';

async function keys() {
  // before sr25519 can be used, the WASM libraries needs to be loaded and initialized
  await cryptoWaitReady();
  // load all available addresses and accounts
  keyring.loadAll({ ss58Format: 42, type: 'sr25519' });

  const pairFromJSON = keyring.createFromJson(your_json);
  //Check your address
  console.log(pairFromJSON.address);
  // decrypt it
  const keys = keyring.addPair(pairFromJSON, 'myStr0ngP@ssworD');

  //Do a normal signature (You can sign an extrinsic too)
  const signature = keys.pair.sign("message");
  console.log(signature);
 
}

If you want to create the JSON file with PolkadotJS too, you can get your keys from a Mnemonic phrase:

 // With that you get the json file and the pair to sign messages/extrinsics
 const { pair, json } = keyring.addUri(MNEMONIC_PHRASE, 'myStr0ngP@ssworD', { name: 'mnemonic acc' });

 // sign with the pair generated above
 const signature = pair.sign("message");

Or you can also generate the keys itself using PolkadotJS:

import { randomAsHex } from '@polkadot/util-crypto';

// generate a random hex raw seed, this needs to be 32 bytes in length
const seed = randomAsHex(32);

// add the account, encrypting it, override to ed25519
const { pair, json } = keyring.addUri(seed, 'myStr0ngP@ssworD', { name: 'hex seed acc' }, 'ed25519');

// sign with the pair generated above
 const signature = pair.sign("message");
4
  • In this example, I'm using a wallet created by Polkadot UI. This account is already in my browser. Every time I try to make a transaction using the Balance pallet this modal appears asking for my password. I'm trying to simulate this same process using PolkadotJs API.
    – rirotho
    Commented Jan 20, 2023 at 13:55
  • I have updated my answer, with more information about how to decrypt this json using polkadotJS or even how to generate your own keys
    – Alex Bean
    Commented Jan 20, 2023 at 16:20
  • Thanks, for your answer Alex. So, every time I want to make a new transaction do I have to call keyring.addUri(seed, password, options) in order to sign a transaction? Isn't there an unlock function that only needs the password? Because there is a boolean "unlock for 15 mim" when I enabled this bool I don't have to type my password during this time
    – rirotho
    Commented Jan 20, 2023 at 16:51
  • The boolean is only for the PolkadotUI, is on you to decide if for security you want to ask it every time or not ask at all and keep the pair in memory (which I don't recommend)
    – Alex Bean
    Commented Jan 20, 2023 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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