// Copyright 2019-2026 @polkadot/extension-ui authors & contributors // SPDX-License-Identifier: Apache-2.0 /// import type { Chain } from '@polkadot/extension-chains/types'; import { HEISENBERG_SPEC, heisenbergMetadataDef } from '@polkadot/extension-base/test/metadata'; import { metadataExpand } from '@polkadot/extension-chains'; import { bnToBn, u8aToHex } from '@polkadot/util'; import { decodeMethod } from './decodeMethod.js'; describe('decoding a call for the approval screen', (): void => { const chain: Chain = metadataExpand(heisenbergMetadataDef(), false); const runtime = chain.runtime; if (!runtime) { throw new Error('the fixture metadata did not produce a runtime'); } // crystal_bob on Heisenberg, as an account id and as the address it renders to. const BOB_ID = '0x300bb607ba60e89461d2f9005668231ceb30237b33db53a614164b8590965519'; const BOB = 'qzkYEQv8tQsmniZYdame3Cku18RL5g9bGK9Pdydq5TMPdpE3y'; const transfer = u8aToHex(runtime.encodeCall('Balances', 'transfer_keep_alive', { dest: { Id: BOB_ID }, value: '1000000000' })); // What the screen exists to do. Before this it went through // registry.createType('Call', …), which cannot work on a chain @polkadot/types // cannot describe — so every transaction rendered as raw hex, which is the // failure mode where somebody approves bytes nobody read to them. it('names the call and its arguments as the runtime does', (): void => { const { args, name } = decodeMethod(transfer, chain, bnToBn(HEISENBERG_SPEC)); expect(name).toEqual('Balances.transfer_keep_alive'); expect(args?.['value']).toEqual('1000000000'); // An address, not 32 bytes of hex — somebody approving a transfer has to be // able to check the recipient against what they meant to send to. expect(args?.['dest']).toEqual({ Id: BOB }); }); // Metadata from a different runtime decodes a call into something plausible and // wrong. Showing hex is worse than showing a decoded call; showing the *wrong* // decoded call is worse than both. it('refuses to decode against a different spec version', (): void => { expect(decodeMethod(transfer, chain, bnToBn(HEISENBERG_SPEC + 1))).toEqual({ args: null, name: null }); }); // A chain the extension holds no metadata for. It will not sign one of these // either — see RequestExtrinsicSign — but the screen still has to render, and // rendering hex is the honest thing to show. it('returns nothing readable when there is no runtime', (): void => { expect(decodeMethod(transfer, { ...chain, runtime: null }, bnToBn(HEISENBERG_SPEC))).toEqual({ args: null, name: null }); }); // Bytes that are not a call at all. The screen must not throw: an exception // here lands between somebody and their funds. it('returns nothing readable for bytes that are not a call', (): void => { expect(decodeMethod('0xdeadbeef', chain, bnToBn(HEISENBERG_SPEC))).toEqual({ args: null, name: null }); }); });