Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Compact, createType, Metadata, TypeRegistry } from '@polkadot/types';
import { Balance } from '@polkadot/types/interfaces';
import { setSS58Format } from '@polkadot/util-crypto';
import { TxInfo, UnsignedTransaction } from './balanceTransfer';
import { BLOCKTIME, KUSAMA_SS58_FORMAT } from './util/constants';
/**
* Parse the transaction information from a signed transaction
* offline
*
* @param unsigned - The JSON representing the unsigned transaction
* @param metadataRpc - The SCALE-encoded metadata, as a hex string. Can be
* retrieved via the RPC call `state_getMetadata`
*/
export function decodeUnsignedTx(
unsigned: UnsignedTransaction,
metadataRpc: string
): TxInfo {
const registry = new TypeRegistry();
registry.setMetadata(new Metadata(registry, metadataRpc));
const method = createType(registry, 'Call', unsigned.method);
setSS58Format(KUSAMA_SS58_FORMAT);
return {
address: unsigned.address,
amount: (method.args[1] as Compact<Balance>).toNumber(),
blockHash: unsigned.blockHash,
blockNumber: createType(
registry,
'BlockNumber',
unsigned.blockNumber
).toNumber(),
genesisHash: unsigned.genesisHash,
keepAlive: method.methodName === 'transferKeepAlive',
metadataRpc,
nonce: createType(registry, 'Compact<Index>', unsigned.nonce).toNumber(),
specVersion: createType(registry, 'u32', unsigned.specVersion).toNumber(),
tip: createType(registry, 'Compact<Balance>', unsigned.tip).toNumber(),
to: method.args[0].toString(),
validityPeriod:
createType(registry, 'MortalEra', unsigned.era).period.toNumber() *
BLOCKTIME
};
}