Skip to content

Commit

Permalink
chore(deps): Update PJS (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
bee344 authored Sep 18, 2024
1 parent dc527ec commit f46f9c5
Show file tree
Hide file tree
Showing 25 changed files with 465 additions and 629 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"docs": "typedoc --plugin typedoc-plugin-missing-exports --plugin typedoc-theme-hierarchy --plugin typedoc-plugin-remove-references --gitRemote origin"
},
"devDependencies": {
"@polkadot/util-crypto": "^13.0.2",
"@polkadot/util-crypto": "^13.1.1",
"@substrate/dev": "^0.7.1",
"@types/node-fetch": "^2.6.3",
"@typescript-eslint/eslint-plugin": "^6.11.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/txwrapper-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"build": "yarn build:workspace"
},
"dependencies": {
"@polkadot/api": "^12.4.1",
"@polkadot/keyring": "^13.0.2",
"@polkadot/api": "^13.1.1",
"@polkadot/keyring": "^13.1.1",
"memoizee": "0.4.15"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ describe('createSignedTx', () => {
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS,
);
const signingPayload = createSigningPayload(unsigned, ASTAR_TEST_OPTIONS);
const signature = await signWithAliceAstar(signingPayload);
const signature = await signWithAliceAstar(unsigned);

const tx = createSignedTx(unsigned, signature, ASTAR_TEST_OPTIONS);
expect(tx).toBe(
Expand Down
15 changes: 12 additions & 3 deletions packages/txwrapper-core/src/core/construct/createSigningPayload.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Options, UnsignedTransaction } from '../../types';
import { GenericSignerPayload } from '@polkadot/types';

import { Options, UnsignedTransaction } from '../../types';
/**
* Construct the signing payload from an unsigned transaction and export it to
* a remote signer (this is often called "detached signing").
Expand Down Expand Up @@ -65,9 +66,17 @@ export function createSigningPayload(
);
}

const payload = new GenericSignerPayload(registry, {
...unsigned,
runtimeVersion: {
specVersion: unsigned.specVersion,
transactionVersion: unsigned.transactionVersion,
},
}).toPayload();

return registry
.createType('ExtrinsicPayload', unsigned, {
version: unsigned.version,
.createType('ExtrinsicPayload', payload, {
version: payload.version,
})
.toHex();
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GenericSignerPayload } from '@polkadot/types';

import { Options, UnsignedTransaction } from '../../types';

/**
Expand All @@ -18,8 +20,16 @@ export function createSigningPayloadToU8a(
options: Options,
): Uint8Array {
const { registry } = options;
const extrinsicPayload = registry.createType('ExtrinsicPayload', unsigned, {
version: unsigned.version,
const payload = new GenericSignerPayload(registry, {
...unsigned,
runtimeVersion: {
specVersion: unsigned.specVersion,
transactionVersion: unsigned.transactionVersion,
},
}).toPayload();

const extrinsicPayload = registry.createType('ExtrinsicPayload', payload, {
version: payload.version,
});

/**
Expand Down
6 changes: 1 addition & 5 deletions packages/txwrapper-core/src/core/construct/getTxHash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ describe('getTxHash', () => {
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS,
);
const signingPayload = construct.signingPayload(
unsigned,
ASTAR_TEST_OPTIONS,
);
const signature = await signWithAliceAstar(signingPayload);
const signature = await signWithAliceAstar(unsigned);
const signedTx = construct.signedTx(
unsigned,
signature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,7 @@ describe('decodeSignedTx', () => {
ASTAR_TEST_BASE_TX_INFO,
ASTAR_TEST_OPTIONS,
);
const signingPayload = construct.signingPayload(
unsigned,
ASTAR_TEST_OPTIONS,
);
const signature = await signWithAliceAstar(signingPayload);
const signature = await signWithAliceAstar(unsigned);
const signedTx = construct.signedTx(
unsigned,
signature,
Expand Down
42 changes: 31 additions & 11 deletions packages/txwrapper-core/src/core/decode/decodeSigningPayload.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/**
* @ignore
*/ /** */
import { GenericSignerPayload } from '@polkadot/types';
import { createTypeUnsafe } from '@polkadot/types/create';
import { EXTRINSIC_VERSION } from '@polkadot/types/extrinsic/v4/Extrinsic';
import { Call, ExtrinsicPayload } from '@polkadot/types/interfaces';

import { DecodedSigningPayload, OptionsWithMeta } from '../../types';
import {
DecodedSigningPayload,
OptionsWithMeta,
UnsignedTransaction,
} from '../../types';
import { createMetadata, toTxMethod } from '..';

/**
Expand All @@ -15,7 +19,7 @@ import { createMetadata, toTxMethod } from '..';
* @param options - Runtime-specific data used for decoding the transaction.
*/
export function decodeSigningPayload(
signingPayload: string,
signingPayload: UnsignedTransaction | string,
options: OptionsWithMeta,
): DecodedSigningPayload {
const { metadataRpc, registry, asCallsOnlyArg, asSpecifiedCallsOnlyV14 } =
Expand All @@ -30,23 +34,39 @@ export function decodeSigningPayload(
),
);

let payload: ExtrinsicPayload;

// We use `createTypeUnsafe` here because it allows us to specify `withoutLog: true`,
// which silences an internal error message from polkadot-js. This is helpful in `decode`
// which takes in just a string. We determine if the string is a signing payload or a
// signed tx by first attempting to decode it as a signing payload with this function.
// If that fails we catch, knowing through process of elimination it should be a
// signed tx. `withoutLog: true` prevents an alarming error message from bubbling up
// to the user when we catch.
const payload: ExtrinsicPayload = createTypeUnsafe(
registry,
'ExtrinsicPayload',
[
if (typeof signingPayload === 'string') {
payload = createTypeUnsafe(registry, 'ExtrinsicPayload', [
signingPayload,
{
version: EXTRINSIC_VERSION,
version: 4,
},
],
);
]);
} else {
const genericPayload = new GenericSignerPayload(registry, {
...signingPayload,
runtimeVersion: {
specVersion: signingPayload.specVersion,
transactionVersion: signingPayload.transactionVersion,
},
}).toPayload();

payload = createTypeUnsafe(registry, 'ExtrinsicPayload', [
genericPayload,
{
version: genericPayload.version,
},
]);
}

const methodCall: Call = createTypeUnsafe(registry, 'Call', [payload.method]);
const method = toTxMethod(registry, methodCall);

Expand All @@ -55,7 +75,7 @@ export function decodeSigningPayload(
? payload.era.asMortalEra.period.toNumber()
: 0;

let assetId: number | object | undefined;
let assetId: object | number | undefined;
if (payload.inner.assetId) {
assetId = payload.inner.assetId.isSome ? payload.inner.assetId : undefined;
}
Expand Down
12 changes: 7 additions & 5 deletions packages/txwrapper-core/src/core/decode/decodeUnsignedTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,23 @@ export function decodeUnsignedTx(
const eraPeriod = isImmortalEra
? hexToNumber(registry.createType('ImmortalEra', unsigned.era).toHex())
: registry.createType('MortalEra', unsigned.era).period.toNumber();
const assetId = unsigned.assetId;

let tip: number | string;
try {
tip = registry.createType('Compact<Balance>', unsigned.tip).toNumber();
} catch (_error) {
tip = registry.createType('Compact<Balance>', unsigned.tip).toString();
}

return {
address: unsigned.address,
assetId: !assetId
assetId: !unsigned.assetId
? undefined
: typeof assetId === 'object'
? registry.createType('MultiLocation', assetId)
: registry.createType('Compact<AssetId>', assetId).toNumber(),
: typeof unsigned.assetId === 'object'
? registry.createType('MultiLocation', unsigned.assetId)
: typeof unsigned.assetId === 'number'
? registry.createType('Compact<AssetId>', unsigned.assetId).toNumber()
: unsigned.assetId,
blockHash: unsigned.blockHash,
blockNumber: registry
.createType('BlockNumber', unsigned.blockNumber)
Expand Down
11 changes: 6 additions & 5 deletions packages/txwrapper-core/src/core/method/defineMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,17 @@ export function defineMethod(
const extrinsicEra = createEra(registry, eraOptions);
const blockHash = info.blockHash as `0x${string}`;
const genesisHash = info.genesisHash as `0x${string}`;
const assetId = info.assetId;
const metadataHash = info.metadataHash as `0x${string}`;

return {
address: info.address,
assetId: !assetId
assetId: !info.assetId
? undefined
: typeof assetId === 'object'
? registry.createType('MultiLocation', assetId)
: registry.createType('Compact<AssetId>', assetId).toNumber(),
: typeof info.assetId === 'object'
? registry.createType('MultiLocation', info.assetId).toHex()
: typeof info.assetId === 'number'
? registry.createType('Compact<AssetId>', info.assetId).toHex()
: info.assetId,
blockHash,
blockNumber: registry.createType('BlockNumber', info.blockNumber).toHex(),
era: extrinsicEra.toHex(),
Expand Down
11 changes: 3 additions & 8 deletions packages/txwrapper-core/src/types/method.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TypeRegistry } from '@polkadot/types';
import { ExtDef } from '@polkadot/types/extrinsic/signedExtensions/types';
import { AnyJson } from '@polkadot/types/types';
import { SignerPayloadJSON } from '@polkadot/types/types';
import type { AnyJson, SignerPayloadJSON } from '@polkadot/types/types';
import type { HexString } from '@polkadot/util/types';

export { TypeRegistry } from '@polkadot/types';
export { SignerPayloadJSON } from '@polkadot/types/types';
Expand Down Expand Up @@ -66,11 +66,6 @@ export interface TxInfo extends BaseTxInfo {
* JSON format for an unsigned transaction.
*/
export interface UnsignedTransaction extends SignerPayloadJSON {
/**
* The assetId used in ChargeAssetTxPayment
*
*/
assetId?: number | object;
/**
* The SCALE-encoded metadata, as a hex string. Can be retrieved via the RPC
* call `state_getMetadata`.
Expand All @@ -90,7 +85,7 @@ export interface BaseTxInfo {
* The assetId used in ChargeAssetTxPayment
*
*/
assetId?: number | object;
assetId?: number | object | HexString;
/**
* The checkpoint hash of the block, in hex.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/txwrapper-dev/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": "^12.4.1",
"@polkadot/api": "^13.1.1",
"memoizee": "0.4.15"
}
}
16 changes: 13 additions & 3 deletions packages/txwrapper-dev/src/util/signWithAliceAstar.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Keyring } from '@polkadot/api';
import { GenericSignerPayload } from '@polkadot/types';
import { cryptoWaitReady } from '@polkadot/util-crypto';

import { ASTAR_TEST_OPTIONS } from '../constants';
import { UnsignedTransaction } from '../mock-types';

/**
* Sign a payload with seed `//Alice`.
*/
export async function signWithAliceAstar(
signingPayload: string,
signingPayload: UnsignedTransaction,
): Promise<`0x${string}`> {
// We're creating an Alice account that will sign the payload
// Wait for the promise to resolve async WASM
Expand All @@ -16,9 +18,17 @@ export async function signWithAliceAstar(
const keyring = new Keyring({ type: 'ed25519' });
const alice = keyring.addFromUri('//Alice', { name: 'Alice default' });

const genericPayload = new GenericSignerPayload(ASTAR_TEST_OPTIONS.registry, {
...signingPayload,
runtimeVersion: {
specVersion: signingPayload.specVersion,
transactionVersion: signingPayload.transactionVersion,
},
}).toPayload();

const { signature } = ASTAR_TEST_OPTIONS.registry
.createType('ExtrinsicPayload', signingPayload, {
version: 4,
.createType('ExtrinsicPayload', genericPayload, {
version: genericPayload.version,
})
.sign(alice);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ async function main(): Promise<void> {
);

// Sign a payload. This operation should be performed on an offline device.
const signature = signWith(alice, signingPayload, {
const signature = signWith(alice, unsigned, {
metadataRpc,
registry,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ async function main(): Promise<void> {
);

// Sign a payload. This operation should be performed on an offline device.
const signature = signWith(alice, signingPayload, {
const signature = signWith(alice, unsigned, {
metadataRpc,
registry,
});
Expand Down
17 changes: 13 additions & 4 deletions packages/txwrapper-examples/common/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* @ignore Don't show this file in documentation.
*/ /** */
import { KeyringPair } from '@polkadot/keyring/types';
import { EXTRINSIC_VERSION } from '@polkadot/types/extrinsic/v4/Extrinsic';
import { GenericSignerPayload } from '@polkadot/types';
import { UnsignedTransaction } from '@substrate/txwrapper-dev';
import { createMetadata, OptionsWithMeta } from '@substrate/txwrapper-polkadot';
import fetch from 'node-fetch';

Expand Down Expand Up @@ -54,17 +55,25 @@ export function rpcToLocalNode(
*/
export function signWith(
pair: KeyringPair,
signingPayload: string,
signingPayload: UnsignedTransaction,
options: OptionsWithMeta,
): `0x${string}` {
const { registry, metadataRpc } = options;
// Important! The registry needs to be updated with latest metadata, so make
// sure to run `registry.setMetadata(metadata)` before signing.
registry.setMetadata(createMetadata(registry, metadataRpc));

const payload = new GenericSignerPayload(registry, {
...signingPayload,
runtimeVersion: {
specVersion: signingPayload.specVersion,
transactionVersion: signingPayload.transactionVersion,
},
}).toPayload();

const { signature } = registry
.createType('ExtrinsicPayload', signingPayload, {
version: EXTRINSIC_VERSION,
.createType('ExtrinsicPayload', payload, {
version: payload.version,
})
.sign(pair);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async function main(): Promise<void> {
);

// Sign a payload. This operation should be performed on an offline device.
const signature = signWith(alice, signingPayload, {
const signature = signWith(alice, unsigned, {
metadataRpc,
registry,
});
Expand Down
Loading

0 comments on commit f46f9c5

Please sign in to comment.