Skip to content
decodeSignedTx.ts 1.36 KiB
Newer Older
Amaury Martiny's avatar
Amaury Martiny committed
import { Compact, createType, Metadata, TypeRegistry } from '@polkadot/types';
import { Balance } from '@polkadot/types/interfaces';
import { hexToU8a } from '@polkadot/util';
Amaury Martiny's avatar
Amaury Martiny committed
import { setSS58Format } from '@polkadot/util-crypto';
Amaury Martiny's avatar
Amaury Martiny committed
import { TxInfo } from '../balanceTransfer';
import { KUSAMA_SS58_FORMAT } from '../util/constants';
Amaury Martiny's avatar
Amaury Martiny committed
<<<<<<< HEAD:src/decode/decodeSignedTx.ts
 * Parse the transaction information from a signed transaction offline
=======
 * Parse the transaction information from a signed transaction
Amaury Martiny's avatar
Amaury Martiny committed
 * offline
Amaury Martiny's avatar
Amaury Martiny committed
>>>>>>> master:src/decodeSignedTx.ts
 *
 * @param unsigned - The JSON representing the unsigned transaction
Amaury Martiny's avatar
Amaury Martiny committed
 * @param metadataRpc - The SCALE-encoded metadata, as a hex string. Can be
 * retrieved via the RPC call `state_getMetadata`
export function decodeSignedTx(
Amaury Martiny's avatar
Amaury Martiny committed
  signedTx: string,
  metadataRpc: string
): Partial<TxInfo> {
  const registry = new TypeRegistry();
Amaury Martiny's avatar
Amaury Martiny committed
  registry.setMetadata(new Metadata(registry, metadataRpc));

  const tx = createType(registry, 'Extrinsic', hexToU8a(signedTx), {
    isSigned: true
  });

Amaury Martiny's avatar
Amaury Martiny committed
  setSS58Format(KUSAMA_SS58_FORMAT);
Amaury Martiny's avatar
Amaury Martiny committed
    address: tx.signer.toString(),
    amount: (tx.method.args[1] as Compact<Balance>).toNumber(),
Amaury Martiny's avatar
Amaury Martiny committed
    keepAlive: tx.method.methodName === 'transferKeepAlive',
Amaury Martiny's avatar
Amaury Martiny committed
    metadataRpc,
    nonce: tx.nonce.toNumber(),
    tip: tx.tip.toNumber(),
    to: tx.method.args[0].toString()