Skip to content

Commit

Permalink
fix: add support for CheckMetadataHash signedExtension (#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
bee344 authored Jun 11, 2024
1 parent 5414e33 commit 6632dd0
Show file tree
Hide file tree
Showing 31 changed files with 322 additions and 215 deletions.
2 changes: 1 addition & 1 deletion packages/txwrapper-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"build": "yarn build:workspace"
},
"dependencies": {
"@polkadot/api": "^11.0.2",
"@polkadot/api": "^11.2.1",
"@polkadot/keyring": "^12.6.2",
"memoizee": "0.4.15"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import {
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
signWithAlice,
signWithAliceAstar,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import {
balancesTransfer,
balancesTransferKeepAlive,
} from '../../test-helpers';
import { createSignedTx } from './createSignedTx';
import { createSigningPayload } from './createSigningPayload';

describe('createSignedTx', () => {
it('should work', async () => {
const unsigned = balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
const unsigned = balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
);
Expand All @@ -24,7 +28,7 @@ describe('createSignedTx', () => {

const tx = createSignedTx(unsigned, signature, KUSAMA_TEST_OPTIONS);
expect(tx).toBe(
'0x2d028400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d005b4cf1d210c93eb6e6428a88c1b298e280d761fe50fe02d6c3253075ed23f239bcbc9a4f9bf0494a869f797355daf55ed6de373572328a1f6f4519f48f8f280feb58080004070096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30',
'0x31028400d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d006ac2635aa767cad3c73c2ba7e508b7428dc6d2aafba03e9d5602cb2715d4eedc1726d08350fef3b9cbd99193fd2849c79272bf1ba2e526caac5c99638bb3d60deb5808000004030096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30',
);
});

Expand All @@ -35,7 +39,7 @@ describe('createSignedTx', () => {
ASTAR_TEST_OPTIONS,
);
const signingPayload = createSigningPayload(unsigned, ASTAR_TEST_OPTIONS);
const signature = await signWithAlice(signingPayload);
const signature = await signWithAliceAstar(signingPayload);

const tx = createSignedTx(unsigned, signature, ASTAR_TEST_OPTIONS);
expect(tx).toBe(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ import {
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import {
balancesTransfer,
balancesTransferKeepAlive,
} from '../../test-helpers';
import { createSigningPayload } from './createSigningPayload';

describe('createSigningPayload', () => {
it('should work', () => {
const signingPayload = createSigningPayload(
balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
),
KUSAMA_TEST_OPTIONS,
);

expect(signingPayload).toEqual(
'0x9004070096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30eb580800d624000006000000e3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f6361fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582',
'0x9004030096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30eb58080000758e010006000000e3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f6361fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b58200',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ import {
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import {
balancesTransfer,
balancesTransferKeepAlive,
} from '../../test-helpers';
import { createSigningPayloadToU8a } from './createSigningPayloadToU8a';

describe('createSigningPayloadToU8a', () => {
it('should work', () => {
const signingPayload = createSigningPayloadToU8a(
balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
),
KUSAMA_TEST_OPTIONS,
);

expect(u8aToHex(signingPayload)).toEqual(
'0x04070096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30eb580800d624000006000000e3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f6361fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b582',
'0x04030096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30eb58080000758e010006000000e3777fa922cafbff200cadeaea1a76bd7898ad5b89f7848999058b50e715f6361fc7493f3c1e9ac758a183839906475f8363aafb1b1d3e910fe16fab4ae1b58200',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,24 @@ import {
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import {
balancesTransfer,
balancesTransferKeepAlive,
} from '../../test-helpers';
import { encodeUnsignedTransaction } from './encodeUnsignedTx';

describe('encodeUnsignedTransaction', () => {
it('should return the correct output', () => {
const unsigned = balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
const unsigned = balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
);

const encoded = encodeUnsignedTransaction(unsigned, KUSAMA_TEST_OPTIONS);

expect(encoded).toBe(
'0x940404070096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30',
'0x940404030096074594cccf1cd185fa8a72ceaeefd86648f8d45514f3ce33c31bdd07e4655d30',
);
});

Expand Down
14 changes: 9 additions & 5 deletions packages/txwrapper-core/src/core/construct/getTxHash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@ import {
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
signWithAlice,
signWithAliceAstar,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import {
balancesTransfer,
balancesTransferKeepAlive,
} from '../../test-helpers';
import { construct } from '..';
import { getTxHash } from './getTxHash';

describe('getTxHash', () => {
it('should work', async () => {
const unsigned = balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
const unsigned = balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
);
Expand All @@ -32,7 +36,7 @@ describe('getTxHash', () => {

const txHash = getTxHash(signedTx);
expect(txHash).toBe(
'0x50cbe175485736a5284a5758e29608d4e3f95c329ca42b108f7445f9f2ed06bb',
'0xd1b90c86cefbce9639cf70b6b03a00947fff6a0daa53038a940a70b489fe77e6',
);
});

Expand All @@ -46,7 +50,7 @@ describe('getTxHash', () => {
unsigned,
ASTAR_TEST_OPTIONS,
);
const signature = await signWithAlice(signingPayload);
const signature = await signWithAliceAstar(signingPayload);
const signedTx = construct.signedTx(
unsigned,
signature,
Expand Down
20 changes: 10 additions & 10 deletions packages/txwrapper-core/src/core/decode/decode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import { itDecodesBalancesTransferCommon } from '../../test-helpers';
import { balancesTransferKeepAlive } from '../../test-helpers';
import { itDecodesBalancesTransferKeepAlive } from '../../test-helpers';
import { DecodedSigningPayload } from '../../types';
import { construct } from '..';
import { decode } from './decode';
Expand All @@ -16,7 +16,7 @@ import { itDecodesUnsignedBalanceTransferTx } from './decodeUnsignedTx.spec';

describe('decode', () => {
it('should decode signedTx', async () => {
const unsigned = balancesTransfer(
const unsigned = balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transfer,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
Expand All @@ -36,25 +36,25 @@ describe('decode', () => {

itDecodesSignedBalancesTransferTx(decoded);

itDecodesBalancesTransferCommon(decoded);
itDecodesBalancesTransferKeepAlive(decoded);
});

it('decodes an unsigned tx', () => {
const unsigned = balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
const unsigned = balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
);
const decoded = decode(unsigned, KUSAMA_TEST_OPTIONS);

itDecodesUnsignedBalanceTransferTx(decoded);

itDecodesBalancesTransferCommon(decoded);
itDecodesBalancesTransferKeepAlive(decoded);
});

it('should decode signing payload', () => {
const unsigned = balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
const unsigned = balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
);
Expand All @@ -70,6 +70,6 @@ describe('decode', () => {

itDecodesSigningPayloadBalancesTransfer(decoded);

itDecodesBalancesTransferCommon(decoded);
itDecodesBalancesTransferKeepAlive(decoded);
});
});
12 changes: 7 additions & 5 deletions packages/txwrapper-core/src/core/decode/decodeSignedTx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import {
ASTAR_TEST_OPTIONS,
KUSAMA_TEST_OPTIONS,
signWithAlice,
signWithAliceAstar,
TEST_BASE_TX_INFO,
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import {
balancesTransfer,
balancesTransferKeepAlive,
itDecodesBalancesTransferAstar,
} from '../../test-helpers';
import { itDecodesBalancesTransferCommon } from '../../test-helpers';
import { itDecodesBalancesTransferKeepAlive } from '../../test-helpers';
import { DecodedSignedTx } from '../../types';
import { construct } from '..';
import { decodeSignedTx } from './decodeSignedTx';
Expand All @@ -35,8 +37,8 @@ export function itDecodesSignedBalancesTransferTxAstar(

describe('decodeSignedTx', () => {
it('should decode balances::transfer', async () => {
const unsigned = balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
const unsigned = balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
);
Expand All @@ -55,7 +57,7 @@ describe('decodeSignedTx', () => {

itDecodesSignedBalancesTransferTx(decoded);

itDecodesBalancesTransferCommon(decoded);
itDecodesBalancesTransferKeepAlive(decoded);
});

it('should decode balances::transfer for Astar', async () => {
Expand All @@ -68,7 +70,7 @@ describe('decodeSignedTx', () => {
unsigned,
ASTAR_TEST_OPTIONS,
);
const signature = await signWithAlice(signingPayload);
const signature = await signWithAliceAstar(signingPayload);
const signedTx = construct.signedTx(
unsigned,
signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import {
itDecodesBalancesTransferAstar,
itDecodesBalancesTransferCommon,
balancesTransfer,
balancesTransferKeepAlive,
itDecodesBalancesTransferKeepAlive,
} from '../../test-helpers';
import { itDecodesBalancesTransferAstar } from '../../test-helpers';
import { DecodedSigningPayload } from '../../types';
import { construct } from '..';
import { decodeSigningPayload } from './decodeSigningPayload';
Expand Down Expand Up @@ -47,10 +48,10 @@ export function itDecodesSigningPayloadBalancesTransferAstar(
}

describe('decodeSigningPayload', () => {
it('should decode balances::transfer', () => {
it('should decode balances::transferKeepAlive', () => {
const signingPayload = construct.signingPayload(
balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
),
Expand All @@ -61,13 +62,13 @@ describe('decodeSigningPayload', () => {

itDecodesSigningPayloadBalancesTransfer(decoded);

itDecodesBalancesTransferCommon(decoded);
itDecodesBalancesTransferKeepAlive(decoded);
});

it('should decode balances::transfer with an immortal era', () => {
const signingPayload = construct.signingPayload(
balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
Object.assign({}, KUSAMA_TEST_OPTIONS, { isImmortalEra: true }),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@ export function decodeSigningPayload(
} catch (_error) {
tip = payload.tip.toString();
}
let metadataHash: string | undefined;
if (payload.inner.metadataHash) {
metadataHash = payload.inner.metadataHash.isSome
? payload.inner.metadataHash.toString()
: undefined;
}

return {
assetId: assetId,
assetId,
blockHash: payload.blockHash.toHex(),
eraPeriod,
genesisHash: payload.genesisHash.toHex(),
metadataHash,
metadataRpc,
method,
nonce: payload.nonce.toNumber(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {
TEST_METHOD_ARGS,
} from '@substrate/txwrapper-dev';

import { balancesTransfer } from '../../test-helpers';
import { balancesTransferKeepAlive } from '../../test-helpers';
import { encodeUnsignedTransaction } from '../construct/encodeUnsignedTx';
import { decodeUnsignedHexTx } from './decodeUnsignedHexTx';

describe('decodeUnsignedHexTx', () => {
it('should return the correct output', () => {
const unsigned = balancesTransfer(
TEST_METHOD_ARGS.balances.transfer,
const unsigned = balancesTransferKeepAlive(
TEST_METHOD_ARGS.balances.transferKeepAlive,
TEST_BASE_TX_INFO,
KUSAMA_TEST_OPTIONS,
);
Expand All @@ -22,7 +22,7 @@ describe('decodeUnsignedHexTx', () => {
dest: { id: 'Fy2rsYCoowQBtuFXqLE65ehAY9T6KWcGiNCQAyPDCkfpm4s' },
value: 12,
},
callIndex: '0x0407',
callIndex: '0x0403',
},
version: 4,
};
Expand Down
Loading

0 comments on commit 6632dd0

Please sign in to comment.