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 350 forks source link

How to serialize and deserialize signed extrinsic? #4084

Closed imstar15 closed 2 years ago

imstar15 commented 2 years ago

I want to enable users to sign extrinsic on the web page, and then send the signed extrinsic to the server, and let the server submit it to the chain.

The signed extrinsic can be toHex or toU8a, but I currently can't deserialize signedExtrinsic.

Here is my code:

const api = await getKusamaWeb3Api();
const extrinsic = await api.tx.crowdloan.contribute(
  parachainIndex,
  contributionValue,
  null  // signature: Option<MultiSignature>, default value: null
);

// Only sign and not send
const signedExtrinsic = await extrinsic.signAsync(kusamaAddress, { signer: injector.signer });
// signedExtrinsic's type is Submittable
console.log('signedExtrinsic: ', signedExtrinsic);

// Serialization
const extrinsicU8a = signedExtrinsic.toU8a();
console.log('extrinsicU8a: ', extrinsicU8a);

// Deserialization
const extrinsicB = new Extrinsic(api.registry, extrinsicU8a);
console.log('extrinsicB: ', extrinsicB);

Here is the console log:

signedExtrinsic:  Submittable {registry: TypeRegistry, createdAtHash: undefined, _raw: Map(2), __private_43_ignoreStatusCb: false, __private_44_transformResult: ƒ, …}
index.js?707a:96 extrinsicU8a:  Uint8Array(116) [201, 1, 132, 0, 56, 87, 177, 110, 95, 164, 213, 196, 234, 70, 3, 236, 233, 10, 30, 46, 138, 62, 96, 63, 177, 0, 171, 115, 224, 105, 49, 9, 250, 231, 163, 19, 1, 18, 214, 152, 60, 29, 155, 246, 93, 168, 175, 54, 39, 45, 0, 1, 14, 161, 246, 50, 191, 97, 223, 129, 89, 100, 211, 79, 109, 10, 248, 109, 123, 134, 28, 205, 208, 58, 14, 180, 25, 135, 179, 201, 48, 93, 165, 128, 150, 253, 27, 189, 199, 44, 135, 170, 254, 142, 12, 55, 158, 217, 240, 24, …]
index.js?707a:141 error:  Error: createType(Call):: findMetaCall: Unable to find Call with index 0xc901/[201,1]
    at createTypeUnsafe (createType.js?abb9:57)
    at TypeRegistry.createTypeUnsafe (registry.js?6fc9:338)
    at TypeRegistry.createType (registry.js?6fc9:330)
    at Function.decodeExtrinsic (Extrinsic.js?25bd:37)
    at new GenericExtrinsicV4 (Extrinsic.js?25bd:20)
    at _callee$ (index.js?707a:97)
    at tryCatch (runtime.js?df26:63)
    at Generator.invoke [as _invoke] (runtime.js?df26:293)
    at Generator.eval [as next] (runtime.js?df26:118)
    at asyncGeneratorStep (asyncToGenerator.js?4564:3)
    at _next (asyncToGenerator.js?4564:25)

What is the correct way to do it?

thanks!

jacogr commented 2 years ago

api.tx(<signed value>) returns it to a submittable (hex input here is ok)

imstar15 commented 2 years ago

@jacogr Thanks

polkadot-js-bot commented 2 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.