polkadot-js / api

Promise and RxJS APIs around Polkadot and Substrate based chains via RPC calls. It is dynamically generated based on what the Substrate runtime provides in terms of metadata.
Apache License 2.0
1.07k stars 354 forks source link

submitExtrinsic(extrinsic: Extrinsic): Hash:: 1010: Invalid Transaction: Transaction has a bad signature #3733

Closed polariseye closed 3 years ago

polariseye commented 3 years ago

hi,could you please help me fix the error.

import { deriveAddress,methods } from '@substrate/txwrapper-polkadot';
import { ApiPromise,WsProvider } from "@polkadot/api";
import { Keyring } from "@polkadot/keyring";
import { cryptoWaitReady } from '@polkadot/util-crypto';

async function main(){
    await cryptoWaitReady();

    const wsProvider = new WsProvider('wss://westend-rpc.polkadot.io');
    const api = await ApiPromise.create({ provider: wsProvider });

    let keyring= new Keyring({
        type:"sr25519",
    });
    const Alice = keyring.addFromUri("//Alice");
    let from = Alice.address;
    let to = "5H2Dq1m8Cg7qsMaADCxnPDf3mk3CePDiVdVF6nyNeRbKWjc3";
    let amount = BigInt(12345);

    const tx = api.tx.balances.transfer(to, amount);
    const accountInfo = await api.query.system.account(from) ;
    console.log(`nonce:${JSON.stringify( accountInfo)}`)

    let signedBlock= await api.rpc.chain.getBlock();
    // create the payload
    const signer = api.createType('SignerPayload', {
        blockHash:  signedBlock.hash.toString(),
        genesisHash: api.genesisHash.toString(),
        nonce:accountInfo.nonce,
        runtimeVersion: api.runtimeVersion,
        address:from,
        blockNumber:signedBlock.block.header.number.toString(),
        method: tx,
        version: api.extrinsicVersion,
    });

  try {
    const { signature } = api.createType('ExtrinsicPayload', signer.toPayload(), { version: api.extrinsicVersion }).sign(Alice);
    console.log(`sign:${signature}`)

    tx.addSignature(from, signature, signer.toPayload());

      console.log(await tx.send());
  } catch (error) {
      console.error(error);
  }  
}
main().catch(console.log)
polariseye commented 3 years ago

https://github.com/polkadot-js/api/issues/3733 the is the info i refer to

jacogr commented 3 years ago

You never want to create the signer payload directly - it is based on the metadata. Just use

const tx = api.tx.balances.transfer(...);

await tx.signAsync(...);
polariseye commented 3 years ago

You never want to create the signer payload directly - it is based on the metadata. Just use

const tx = api.tx.balances.transfer(...);

await tx.signAsync(...);

i have used api.tx.balances.transfer. and tx.addSignature. and sign by const { signature } = api.createType('ExtrinsicPayload', signer.toPayload(), { version: api.extrinsicVersion }).sign(Alice);

polariseye commented 3 years ago

i have fix it.and i finish my example: https://github.com/polariseye/polka-signer

polkadot-js-bot commented 3 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue if you think you have a related problem or query.