1

When submitting a script through polkadot-js console and trying to sign using Talisman wallet, I got the error Cannot sign with a locked key pair:

image

The script is one of the examples, where SENDER is the address from my Talisman wallet:

const SENDER = '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY';
const RECEIVER = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';

// Get a random number between 1 and 100000
const randomAmount = Math.floor((Math.random() * 100000) + 1);

// Create a extrinsic, transferring randomAmount units to receiver.
const transfer = api.tx.balances.transferAllowDeath(RECEIVER, randomAmount);

// Sign and Send the transaction
await transfer.signAndSend(SENDER, ({ events = [], status }) => {
  if (status.isInBlock) {
    console.log('Successful transfer of ' + randomAmount + ' with hash ' + status.asInBlock.toHex());
  } else {
    console.log('Status of transfer: ' + status.type);
  }

  events.forEach(({ phase, event: { data, method, section } }) => {
    console.log(phase.toString() + ' : ' + section + '.' + method + ' ' + data.toString());
  });
});

Environment

  • Brave Browser - Version 1.65.114 Chromium: 124.0.6367.60 (Official Build) (64-bit)
  • Talisman Wallet - Version v1.25.0

1 Answer 1

1

The error is in the message - the keypair is locked. It's locked because you've only used the public key (the account) and not a private key when signing and sending. You need to either unlock it with a password pair.unlock or add the pair from the URI since you're working in dev mode:

// get the alice pair
const alicePair = keyring.addFromUri('//Alice');

// Sign and Send the transaction
await transfer.signAndSend(alicePair, ({ events = [], status }) => {

Source: Working with keypairs

EDIT: To unlock an account from Talisman, you need to take the outputted JSON and load as follows

const ss58Format = 42 // depends on the chain you're using
const json = JSON.parse(secret)
const {
    encoding: { content },
} = json
const keyring = new Keyring({ type: content[1], ss58Format })
            return keyring.addFromJson(json as KeyringPair$Json)

You can get the ss58 value from the ss58 registry.

Then when you use the keypair to send transactions you will need to unlock it with the password using

pair.unlock(password)
// do tx
2
  • sorry if I didn't explain myself well, I'm not working in dev mode. The SENDER account comes from the talisman wallet. Would there be any way to do the unlock in this case?
    – magecnion
    Commented Apr 25 at 7:54
  • hey, i've added an explanation for how to do it with a JSON file exported from Talisman / PolkadotJS Extension
    – forgetso
    Commented Apr 25 at 19:15

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.