Skip to content
Snippets Groups Projects
Commit 1fb8a818 authored by Amaury Martiny's avatar Amaury Martiny
Browse files

Fix bugs

parent 7c43dea2
Branches
No related merge requests found
...@@ -7,12 +7,7 @@ import { TxInfo } from '../balanceTransfer'; ...@@ -7,12 +7,7 @@ import { TxInfo } from '../balanceTransfer';
import { KUSAMA_SS58_FORMAT } from '../util/constants'; import { KUSAMA_SS58_FORMAT } from '../util/constants';
/** /**
<<<<<<< HEAD:src/decode/decodeSignedTx.ts
* Parse the transaction information from a signed transaction offline * Parse the transaction information from a signed transaction offline
=======
* Parse the transaction information from a signed transaction
* offline
>>>>>>> master:src/decodeSignedTx.ts
* *
* @param unsigned - The JSON representing the unsigned transaction * @param unsigned - The JSON representing the unsigned transaction
* @param metadataRpc - The SCALE-encoded metadata, as a hex string. Can be * @param metadataRpc - The SCALE-encoded metadata, as a hex string. Can be
......
import { balanceTransfer } from './balanceTransfer';
import { createSignedTx } from './createSignedTx';
import { createSigningPayload } from './createSigningPayload';
import { decodeSignedTx } from './decodeSignedTx';
import { metadataRpc, signWithAlice, TEST_TX_INFO } from './util/testUtil';
describe('decodeSignedTx', () => {
it('should work', async done => {
const unsigned = balanceTransfer(TEST_TX_INFO);
const signingPayload = createSigningPayload(unsigned);
const signature = await signWithAlice(signingPayload);
const signedTx = createSignedTx(unsigned, signature);
const txInfo = decodeSignedTx(signedTx, metadataRpc);
([
'address',
'amount',
'keepAlive',
'nonce',
'tip',
'to'
] as const).forEach(key => expect(txInfo[key]).toBe(TEST_TX_INFO[key]));
done();
});
});
import { Compact, createType, Metadata, TypeRegistry } from '@polkadot/types';
import { Balance } from '@polkadot/types/interfaces';
import { hexToU8a } from '@polkadot/util';
import { setSS58Format } from '@polkadot/util-crypto';
import { TxInfo } from '../balanceTransfer';
import { KUSAMA_SS58_FORMAT } from '../util/constants';
/**
<<<<<<< HEAD:src/decode/decodeSignedTx.ts
* Parse the transaction information from a signed transaction offline
=======
* Parse the transaction information from a signed transaction
* offline
>>>>>>> master:src/decodeSignedTx.ts
*
* @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 decodeSignedTx(
signedTx: string,
metadataRpc: string
): Partial<TxInfo> {
const registry = new TypeRegistry();
registry.setMetadata(new Metadata(registry, metadataRpc));
const tx = createType(registry, 'Extrinsic', hexToU8a(signedTx), {
isSigned: true
});
setSS58Format(KUSAMA_SS58_FORMAT);
return {
address: tx.signer.toString(),
amount: (tx.method.args[1] as Compact<Balance>).toNumber(),
keepAlive: tx.method.methodName === 'transferKeepAlive',
metadataRpc,
nonce: tx.nonce.toNumber(),
tip: tx.tip.toNumber(),
to: tx.method.args[0].toString()
};
}
import { balanceTransfer } from './balanceTransfer';
import { decodeUnsignedTx } from './decodeUnsignedTx';
import { metadataRpc, TEST_TX_INFO } from './util/testUtil';
describe('decodeSignedTx', () => {
it('should work', () => {
const unsigned = balanceTransfer(TEST_TX_INFO);
const txInfo = decodeUnsignedTx(unsigned, metadataRpc);
([
'address',
'amount',
'blockHash',
'blockNumber',
'genesisHash',
'keepAlive',
'metadataRpc',
'nonce',
'specVersion',
'tip',
'to'
] as (keyof typeof TEST_TX_INFO)[]).forEach(key =>
expect(txInfo[key]).toBe(TEST_TX_INFO[key])
);
expect(txInfo.validityPeriod).toBeGreaterThanOrEqual(
TEST_TX_INFO.validityPeriod
);
});
});
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
};
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment