Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(ExtrinsicPayload): decode assetId with and without option #6037

Merged
merged 1 commit into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/types/src/extrinsic/ExtrinsicPayload.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ describe('ExtrinsicPayload', (): void => {

reg.setSignedExtensions(fallbackExtensions.concat(['ChargeAssetTxPayment']));
const ext = new ExtrinsicPayload(reg, TEST_WITH_ASSET, { version: 4 });
// remove option byte
const ext2 = new ExtrinsicPayload(reg, { ...TEST_WITH_ASSET, assetId: `0x${TEST_WITH_ASSET.assetId.slice(4)}` }, { version: 4 });

expect(ext.assetId.toJSON()).toEqual({
interior: {
Expand All @@ -69,6 +71,7 @@ describe('ExtrinsicPayload', (): void => {
},
parents: 0
});
expect(ext.assetId.toJSON()).toEqual(ext2.assetId.toJSON());
});

it('handles toU8a(true) correctly', (): void => {
Expand Down
18 changes: 12 additions & 6 deletions packages/types/src/extrinsic/ExtrinsicPayload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ function decodeExtrinsicPayload (registry: Registry, value?: GenericExtrinsicPay
* ref: https://github.com/polkadot-js/api/pull/5967
*/
if (value && (value as ExtrinsicPayloadValue).assetId && isHex((value as ExtrinsicPayloadValue).assetId)) {
const adjustedPayload = {
...(value as ExtrinsicPayloadValue),
assetId: registry.createType('TAssetConversion', hexToU8a((value as ExtrinsicPayloadValue).assetId)).toJSON()
};

return registry.createTypeUnsafe(extVersion, [adjustedPayload, { version }]);
const assetId = registry.createType('TAssetConversion', hexToU8a((value as ExtrinsicPayloadValue).assetId));

// we only want to adjust the payload if the hex passed has the option
if ((value as ExtrinsicPayloadValue).assetId === '0x00' ||
(value as ExtrinsicPayloadValue).assetId === '0x01' + assetId.toHex().slice(2)) {
const adjustedPayload = {
...(value as ExtrinsicPayloadValue),
assetId: assetId.toJSON()
};

return registry.createTypeUnsafe(extVersion, [adjustedPayload, { version }]);
}
}

return registry.createTypeUnsafe(extVersion, [value, { version }]);
Expand Down