From cbc329860af9bccca4fa06dc91d3adf46b2d835e Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 15 Nov 2024 12:09:06 -0500 Subject: [PATCH 1/9] Adjust ExtrinsicV5 to fit current spec --- packages/types/src/extrinsic/ExtrinsicPayload.ts | 3 +-- packages/types/src/extrinsic/types.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/types/src/extrinsic/ExtrinsicPayload.ts b/packages/types/src/extrinsic/ExtrinsicPayload.ts index a088caddadb..ffe17c54360 100644 --- a/packages/types/src/extrinsic/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/ExtrinsicPayload.ts @@ -36,8 +36,7 @@ const VERSIONS = [ const PREAMBLES = { bare: 'ExtrinsicPayloadV5', // Not supported yet - general: 'ExtrinsicPayloadV5', - signed: 'ExtrinsicPayloadV5' + general: 'ExtrinsicPayloadV5' }; /** @internal */ diff --git a/packages/types/src/extrinsic/types.ts b/packages/types/src/extrinsic/types.ts index c468dc8582d..b59ae095d7e 100644 --- a/packages/types/src/extrinsic/types.ts +++ b/packages/types/src/extrinsic/types.ts @@ -22,4 +22,4 @@ export interface ExtrinsicExtraValue { tip?: AnyNumber; } -export type Preamble = 'signed' | 'bare' | 'general'; +export type Preamble = 'bare' | 'general'; From 853da9e3278ef3f668140a658464bdae6dfa6762 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 15 Nov 2024 12:13:39 -0500 Subject: [PATCH 2/9] Remove signed from `Extrinsic` --- packages/types/src/extrinsic/Extrinsic.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/types/src/extrinsic/Extrinsic.ts b/packages/types/src/extrinsic/Extrinsic.ts index a1d17faa4ac..d745d291740 100644 --- a/packages/types/src/extrinsic/Extrinsic.ts +++ b/packages/types/src/extrinsic/Extrinsic.ts @@ -53,9 +53,7 @@ const PreambleMask = { const preambleUnMask: Record = { 0: 'bare', // eslint-disable-next-line sort-keys - 64: 'general', - // eslint-disable-next-line sort-keys - 128: 'signed' + 64: 'general' }; export { LATEST_EXTRINSIC_VERSION }; From 6734a221af0386ce36db576534ef0fa432fbdbcd Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 15 Nov 2024 15:19:16 -0500 Subject: [PATCH 3/9] Fix tests and error on signed v5 --- .../types/src/extrinsic/Extrinsic.spec.ts | 31 +++++-------------- packages/types/src/extrinsic/Extrinsic.ts | 14 +++++---- packages/types/src/extrinsic/constants.ts | 2 -- 3 files changed, 15 insertions(+), 32 deletions(-) diff --git a/packages/types/src/extrinsic/Extrinsic.spec.ts b/packages/types/src/extrinsic/Extrinsic.spec.ts index 5ff4a5dccb7..e50792ad474 100644 --- a/packages/types/src/extrinsic/Extrinsic.spec.ts +++ b/packages/types/src/extrinsic/Extrinsic.spec.ts @@ -57,36 +57,19 @@ describe('Extrinsic', (): void => { registry.setMetadata(metadata); describe('SignedExtrinsic', () => { - it('Should work when the version and preamble is passed in', () => { - const extrinsic = new Extrinsic( + it('Should error with a signed extrinsic, when the version is passed in', () => { + expect(() => new Extrinsic( registry, '0x51028500d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d011e0b7d9438899333c50121f8e10144952d51c3bb8d0ea11dd1f24940d8ff615ad351d95ed9f41f078748ed7cf182864a20b38eebfaef6629433365eb90c0148c007502000000000603008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480b00a0724e1809', - { preamble: 'signed', version: 5 } - ); - - expect(extrinsic.signer.toString()).toEqual('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'); - expect(extrinsic.era.toHuman()).toEqual({ MortalEra: { period: '64', phase: '39' } }); - expect(extrinsic.nonce.toNumber()).toEqual(0); - expect(extrinsic.tip.toHuman()).toEqual('0'); - expect(extrinsic.callIndex).toEqual(new Uint8Array([6, 3])); - expect(extrinsic.args[0].toHex()).toEqual('0x008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48'); - expect(extrinsic.args[1].toHuman()).toEqual('10,000,000,000,000'); + { version: 5 } + )).toThrow('Signed Extrinsics are currently only available for ExtrinsicV4'); }); - it('Should work when the version and preamble is not passed in', () => { - const extrinsic = new Extrinsic( + it('Should error when the version and preamble is not passed in, and its a signed extrinsic', () => { + expect(() => new Extrinsic( registry, '0x51028500d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d011e0b7d9438899333c50121f8e10144952d51c3bb8d0ea11dd1f24940d8ff615ad351d95ed9f41f078748ed7cf182864a20b38eebfaef6629433365eb90c0148c007502000000000603008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a480b00a0724e1809' - ); - - expect(extrinsic.version).toEqual(133); - expect(extrinsic.signer.toString()).toEqual('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY'); - expect(extrinsic.era.toHuman()).toEqual({ MortalEra: { period: '64', phase: '39' } }); - expect(extrinsic.nonce.toNumber()).toEqual(0); - expect(extrinsic.tip.toHuman()).toEqual('0'); - expect(extrinsic.callIndex).toEqual(new Uint8Array([6, 3])); - expect(extrinsic.args[0].toHex()).toEqual('0x008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48'); - expect(extrinsic.args[1].toHuman()).toEqual('10,000,000,000,000'); + )).toThrow('Signed Extrinsics are currently only available for ExtrinsicV4'); }); }); diff --git a/packages/types/src/extrinsic/Extrinsic.ts b/packages/types/src/extrinsic/Extrinsic.ts index d745d291740..ac7c0f368de 100644 --- a/packages/types/src/extrinsic/Extrinsic.ts +++ b/packages/types/src/extrinsic/Extrinsic.ts @@ -16,7 +16,7 @@ import type { ExtrinsicValueV5 } from './v5/Extrinsic.js'; import { AbstractBase } from '@polkadot/types-codec'; import { compactAddLength, compactFromU8a, compactToU8a, isHex, isU8a, objectProperty, objectSpread, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; -import { BARE_EXTRINSIC, BIT_SIGNED, BIT_UNSIGNED, DEFAULT_PREAMBLE, GENERAL_EXTRINSIC, LATEST_EXTRINSIC_VERSION, LOWEST_SUPPORTED_EXTRINSIC_FORMAT_VERSION, SIGNED_EXTRINSIC, TYPE_MASK, VERSION_MASK } from './constants.js'; +import { BARE_EXTRINSIC, BIT_SIGNED, BIT_UNSIGNED, DEFAULT_PREAMBLE, GENERAL_EXTRINSIC, LATEST_EXTRINSIC_VERSION, LOWEST_SUPPORTED_EXTRINSIC_FORMAT_VERSION, TYPE_MASK, VERSION_MASK } from './constants.js'; interface CreateOptions { version?: number; @@ -40,14 +40,12 @@ const VERSIONS = [ const PREAMBLE = { bare: 'ExtrinsicV5', - general: 'GeneralExtrinsic', - signed: 'ExtrinsicV5' + general: 'GeneralExtrinsic' }; const PreambleMask = { bare: BARE_EXTRINSIC, - general: GENERAL_EXTRINSIC, - signed: SIGNED_EXTRINSIC + general: GENERAL_EXTRINSIC }; const preambleUnMask: Record = { @@ -293,7 +291,11 @@ abstract class ExtrinsicBase extends AbstractBase Date: Fri, 15 Nov 2024 16:15:50 -0500 Subject: [PATCH 4/9] Set sign functionality to error for V5 --- .../types/src/extrinsic/v5/Extrinsic.spec.ts | 19 +++---------------- packages/types/src/extrinsic/v5/Extrinsic.ts | 18 ++++++------------ 2 files changed, 9 insertions(+), 28 deletions(-) diff --git a/packages/types/src/extrinsic/v5/Extrinsic.spec.ts b/packages/types/src/extrinsic/v5/Extrinsic.spec.ts index 8239b4b1beb..9dcb23db0d4 100644 --- a/packages/types/src/extrinsic/v5/Extrinsic.spec.ts +++ b/packages/types/src/extrinsic/v5/Extrinsic.spec.ts @@ -48,9 +48,9 @@ describe('ExtrinsicV5', (): void => { ); }); - it('creates a signed extrinsic', (): void => { + it('Errors when creating a signed Extrinsic', (): void => { expect( - new Extrinsic( + () => new Extrinsic( registry, tx['balances']['transferAllowDeath'](keyring.bob.publicKey, 6969n) ).sign(keyring.alice, { @@ -69,19 +69,6 @@ describe('ExtrinsicV5', (): void => { }, tip: 2 }).toHex() - ).toEqual( - '0x' + - '00' + - 'd172a74cda4c865912c32ba0a80a57ae69abae410e5ccb59dee84e2f4432db4f' + - '00' + // ed25519 - '84181ebef350cc212e70e042b6ebcd33ca955bf9497711a64aa7c64e2b8c2732' + - 'ab837715364eab7be5cc76f74eefa36d3ba9ee698264ed28a286c8360fc9aa0c' + - '00' + // TransactionExtension version - '0004080000' + // era. nonce, tip, mode - '0600' + - '00' + - 'd7568e5f0a7eda67a82691ff379ac4bba4f9c9b859fe779b5d46363b61ad2db9' + - 'e56c' - ); + ).toThrow('ExtrinsicV5 does not include signing support'); }); }); diff --git a/packages/types/src/extrinsic/v5/Extrinsic.ts b/packages/types/src/extrinsic/v5/Extrinsic.ts index 473732ce58a..7a37bf21f70 100644 --- a/packages/types/src/extrinsic/v5/Extrinsic.ts +++ b/packages/types/src/extrinsic/v5/Extrinsic.ts @@ -90,27 +90,21 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl { /** * @description Add an [[ExtrinsicSignatureV5]] to the extrinsic (already generated) */ - public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV5 { - this.signature.addSignature(signer, signature, payload); - - return this; + public addSignature (_signer: Address | Uint8Array | string, _signature: Uint8Array | HexString, _payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV5 { + throw new Error('ExtrinsicV5 does not include signing support'); } /** * @description Sign the extrinsic with a specific keypair */ - public sign (account: IKeyringPair, options: SignatureOptions): GenericExtrinsicV5 { - this.signature.sign(this.method, account, options); - - return this; + public sign (_account: IKeyringPair, _options: SignatureOptions): GenericExtrinsicV5 { + throw new Error('ExtrinsicV5 does not include signing support'); } /** * @describe Adds a fake signature to the extrinsic */ - public signFake (signer: Address | Uint8Array | string, options: SignatureOptions): GenericExtrinsicV5 { - this.signature.signFake(this.method, signer, options); - - return this; + public signFake (_signer: Address | Uint8Array | string, _options: SignatureOptions): GenericExtrinsicV5 { + throw new Error('ExtrinsicV5 does not include signing support'); } } From f036311c7598f71057009272f0f2d08946179d77 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 15 Nov 2024 16:28:01 -0500 Subject: [PATCH 5/9] Removing signing from payload --- packages/types/src/extrinsic/util.ts | 7 ------ .../src/extrinsic/v5/ExtrinsicPayload.ts | 23 +++---------------- 2 files changed, 3 insertions(+), 27 deletions(-) diff --git a/packages/types/src/extrinsic/util.ts b/packages/types/src/extrinsic/util.ts index 476e8404f62..3a996cbc3f3 100644 --- a/packages/types/src/extrinsic/util.ts +++ b/packages/types/src/extrinsic/util.ts @@ -14,13 +14,6 @@ export function sign (registry: Registry, signerPair: IKeyringPair, u8a: Uint8Ar return signerPair.sign(encoded, options); } -// a helper function for both types of payloads, Raw and metadata-known -export function signV5 (registry: Registry, signerPair: IKeyringPair, u8a: Uint8Array, options?: SignOptions): Uint8Array { - const encoded = registry.hash(u8a); - - return signerPair.sign(encoded, options); -} - export function signGeneral (registry: Registry, u8a: Uint8Array): Uint8Array { const encoded = registry.hash(u8a); diff --git a/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts b/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts index 1d2222b3eb0..2abfb3f5c26 100644 --- a/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts @@ -1,7 +1,6 @@ // Copyright 2017-2024 @polkadot/types authors & contributors // SPDX-License-Identifier: Apache-2.0 -import type { SignOptions } from '@polkadot/keyring/types'; import type { Hash, MultiLocation } from '@polkadot/types/interfaces'; import type { Bytes } from '@polkadot/types-codec'; import type { Inspect, Registry } from '@polkadot/types-codec/types'; @@ -10,11 +9,9 @@ import type { BlockHash } from '../../interfaces/chain/index.js'; import type { ExtrinsicEra } from '../../interfaces/extrinsics/index.js'; import type { ExtrinsicPayloadValue, ICompact, IKeyringPair, INumber, IOption } from '../../types/index.js'; -import { Enum, Struct } from '@polkadot/types-codec'; +import { Struct } from '@polkadot/types-codec'; import { objectSpread } from '@polkadot/util'; -import { signV5 } from '../util.js'; - /** * @name GenericExtrinsicPayloadV5 * @description @@ -22,21 +19,12 @@ import { signV5 } from '../util.js'; * variable length based on the contents included */ export class GenericExtrinsicPayloadV5 extends Struct { - #signOptions: SignOptions; - constructor (registry: Registry, value?: ExtrinsicPayloadValue | Uint8Array | HexString) { super(registry, objectSpread( { method: 'Bytes' }, registry.getSignedExtensionTypes(), registry.getSignedExtensionExtra() ), value); - - // Do detection for the type of extrinsic, in the case of MultiSignature - // this is an enum, in the case of AnySignature, this is a Hash only - // (which may be 64 or 65 bytes) - this.#signOptions = { - withType: registry.createTypeUnsafe('ExtrinsicSignature', []) instanceof Enum - }; } /** @@ -119,12 +107,7 @@ export class GenericExtrinsicPayloadV5 extends Struct { /** * @description Sign the payload with the keypair */ - public sign (signerPair: IKeyringPair): Uint8Array { - // NOTE The `toU8a({ method: true })` argument is absolutely critical, we - // don't want the method (Bytes) to have the length prefix included. This - // means that the data-as-signed is un-decodable, but is also doesn't need - // the extra information, only the pure data (and is not decoded) ... - // The same applies to V1..V3, if we have a V6, carry this comment - return signV5(this.registry, signerPair, this.toU8a({ method: true }), this.#signOptions); + public sign (_signerPair: IKeyringPair): Uint8Array { + throw new Error('ExtrinsicV5 does not include signing support'); } } From 7c4839bc38d3b535d136d7ff270bba49035fc2a7 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 15 Nov 2024 19:42:28 -0500 Subject: [PATCH 6/9] Update error messaging --- packages/types/src/extrinsic/v5/Extrinsic.spec.ts | 2 +- packages/types/src/extrinsic/v5/Extrinsic.ts | 6 +++--- packages/types/src/extrinsic/v5/ExtrinsicPayload.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/types/src/extrinsic/v5/Extrinsic.spec.ts b/packages/types/src/extrinsic/v5/Extrinsic.spec.ts index 9dcb23db0d4..d7f5e176f6e 100644 --- a/packages/types/src/extrinsic/v5/Extrinsic.spec.ts +++ b/packages/types/src/extrinsic/v5/Extrinsic.spec.ts @@ -69,6 +69,6 @@ describe('ExtrinsicV5', (): void => { }, tip: 2 }).toHex() - ).toThrow('ExtrinsicV5 does not include signing support'); + ).toThrow('Extrinsic: ExtrinsicV5 does not include signing support'); }); }); diff --git a/packages/types/src/extrinsic/v5/Extrinsic.ts b/packages/types/src/extrinsic/v5/Extrinsic.ts index 7a37bf21f70..5be5252f495 100644 --- a/packages/types/src/extrinsic/v5/Extrinsic.ts +++ b/packages/types/src/extrinsic/v5/Extrinsic.ts @@ -91,20 +91,20 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl { * @description Add an [[ExtrinsicSignatureV5]] to the extrinsic (already generated) */ public addSignature (_signer: Address | Uint8Array | string, _signature: Uint8Array | HexString, _payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV5 { - throw new Error('ExtrinsicV5 does not include signing support'); + throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); } /** * @description Sign the extrinsic with a specific keypair */ public sign (_account: IKeyringPair, _options: SignatureOptions): GenericExtrinsicV5 { - throw new Error('ExtrinsicV5 does not include signing support'); + throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); } /** * @describe Adds a fake signature to the extrinsic */ public signFake (_signer: Address | Uint8Array | string, _options: SignatureOptions): GenericExtrinsicV5 { - throw new Error('ExtrinsicV5 does not include signing support'); + throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); } } diff --git a/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts b/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts index 2abfb3f5c26..2d5eb3ca949 100644 --- a/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts @@ -108,6 +108,6 @@ export class GenericExtrinsicPayloadV5 extends Struct { * @description Sign the payload with the keypair */ public sign (_signerPair: IKeyringPair): Uint8Array { - throw new Error('ExtrinsicV5 does not include signing support'); + throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); } } From c65abd9c048f959b20a38f9ec8ee485c02733b3c Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 15 Nov 2024 20:15:33 -0500 Subject: [PATCH 7/9] Disable all signing functionality --- packages/types/src/extrinsic/v5/Extrinsic.ts | 6 ++ .../src/extrinsic/v5/ExtrinsicPayload.ts | 2 + .../src/extrinsic/v5/ExtrinsicSignature.ts | 68 +++++-------------- 3 files changed, 25 insertions(+), 51 deletions(-) diff --git a/packages/types/src/extrinsic/v5/Extrinsic.ts b/packages/types/src/extrinsic/v5/Extrinsic.ts index 5be5252f495..f19d6788323 100644 --- a/packages/types/src/extrinsic/v5/Extrinsic.ts +++ b/packages/types/src/extrinsic/v5/Extrinsic.ts @@ -89,6 +89,8 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl { /** * @description Add an [[ExtrinsicSignatureV5]] to the extrinsic (already generated) + * + * [Disabled for ExtrinsicV5] */ public addSignature (_signer: Address | Uint8Array | string, _signature: Uint8Array | HexString, _payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV5 { throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); @@ -96,6 +98,8 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl { /** * @description Sign the extrinsic with a specific keypair + * + * [Disabled for ExtrinsicV5] */ public sign (_account: IKeyringPair, _options: SignatureOptions): GenericExtrinsicV5 { throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); @@ -103,6 +107,8 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl { /** * @describe Adds a fake signature to the extrinsic + * + * [Disabled for ExtrinsicV5] */ public signFake (_signer: Address | Uint8Array | string, _options: SignatureOptions): GenericExtrinsicV5 { throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); diff --git a/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts b/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts index 2d5eb3ca949..f8dc14c340e 100644 --- a/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts @@ -106,6 +106,8 @@ export class GenericExtrinsicPayloadV5 extends Struct { /** * @description Sign the payload with the keypair + * + * [Disabled for ExtrinsicV5] */ public sign (_signerPair: IKeyringPair): Uint8Array { throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); diff --git a/packages/types/src/extrinsic/v5/ExtrinsicSignature.ts b/packages/types/src/extrinsic/v5/ExtrinsicSignature.ts index 9191850e47c..e793c2738e3 100644 --- a/packages/types/src/extrinsic/v5/ExtrinsicSignature.ts +++ b/packages/types/src/extrinsic/v5/ExtrinsicSignature.ts @@ -28,11 +28,9 @@ function toAddress (registry: Registry, address: Address | Uint8Array | string): */ export class GenericExtrinsicSignatureV5 extends Struct implements IExtrinsicSignature { #signKeys: string[]; - #transactionExtensionVersion: number; constructor (registry: Registry, value?: GenericExtrinsicSignatureV5 | Uint8Array, { isSigned }: ExtrinsicSignatureOptions = {}) { const signTypes = registry.getSignedExtensionTypes(); - const signedVersion = registry.getTransactionExtensionVersion(); super( registry, @@ -44,7 +42,6 @@ export class GenericExtrinsicSignatureV5 extends Struct implements IExtrinsicSig GenericExtrinsicSignatureV5.decodeExtrinsicSignature(value, isSigned) ); - this.#transactionExtensionVersion = signedVersion; this.#signKeys = Object.keys(signTypes); objectProperties(this, this.#signKeys, (k) => this.get(k)); @@ -150,35 +147,20 @@ export class GenericExtrinsicSignatureV5 extends Struct implements IExtrinsicSig return this.getT('transactionExtensionVersion'); } - protected _injectSignature (signer: Address, signature: ExtrinsicSignature, payload: GenericExtrinsicPayloadV5): IExtrinsicSignature { - // use the fields exposed to guide the getters - for (let i = 0, count = this.#signKeys.length; i < count; i++) { - const k = this.#signKeys[i]; - const v = payload.get(k); - - if (k === 'transactionExtensionVersion') { - this.set(k, this.registry.createType('u8', this.#transactionExtensionVersion)); - } else if (!isUndefined(v)) { - this.set(k, v); - } - } - - // additional fields (exposed in struct itself) - this.set('signer', signer); - this.set('signature', signature); - - return this; + /** + * [Disabled for ExtrinsicV5] + */ + protected _injectSignature (_signer: Address, _signature: ExtrinsicSignature, _payload: GenericExtrinsicPayloadV5): IExtrinsicSignature { + throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); } /** * @description Adds a raw signature + * + * [Disabled for ExtrinsicV5] */ - public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): IExtrinsicSignature { - return this._injectSignature( - toAddress(this.registry, signer), - this.registry.createTypeUnsafe('ExtrinsicSignature', [signature]), - new GenericExtrinsicPayloadV5(this.registry, payload) - ); + public addSignature (_signer: Address | Uint8Array | string, _signature: Uint8Array | HexString, _payload: ExtrinsicPayloadValue | Uint8Array | HexString): IExtrinsicSignature { + throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); } /** @@ -197,36 +179,20 @@ export class GenericExtrinsicSignatureV5 extends Struct implements IExtrinsicSig /** * @description Generate a payload and applies the signature from a keypair + * + * [Disabled for ExtrinsicV5] */ - public sign (method: Call, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature { - if (!account?.addressRaw) { - throw new Error(`Expected a valid keypair for signing, found ${stringify(account)}`); - } - - const payload = this.createPayload(method, options); - - return this._injectSignature( - toAddress(this.registry, account.addressRaw), - this.registry.createTypeUnsafe('ExtrinsicSignature', [payload.sign(account)]), - payload - ); + public sign (_method: Call, _account: IKeyringPair, _options: SignatureOptions): IExtrinsicSignature { + throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); } /** * @description Generate a payload and applies a fake signature + * + * [Disabled for ExtrinsicV5] */ - public signFake (method: Call, address: Address | Uint8Array | string, options: SignatureOptions): IExtrinsicSignature { - if (!address) { - throw new Error(`Expected a valid address for signing, found ${stringify(address)}`); - } - - const payload = this.createPayload(method, options); - - return this._injectSignature( - toAddress(this.registry, address), - this.registry.createTypeUnsafe('ExtrinsicSignature', [FAKE_SIGNATURE]), - payload - ); + public signFake (_method: Call, _address: Address | Uint8Array | string, _options: SignatureOptions): IExtrinsicSignature { + throw new Error('Extrinsic: ExtrinsicV5 does not include signing support'); } /** From 8d7d3d3ee6c101320e9fb75e2978a5c72083a8d8 Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 15 Nov 2024 20:19:40 -0500 Subject: [PATCH 8/9] linter --- packages/types/src/extrinsic/v5/Extrinsic.ts | 6 +++--- .../types/src/extrinsic/v5/ExtrinsicPayload.ts | 2 +- .../types/src/extrinsic/v5/ExtrinsicSignature.ts | 15 ++++----------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/packages/types/src/extrinsic/v5/Extrinsic.ts b/packages/types/src/extrinsic/v5/Extrinsic.ts index f19d6788323..e9ab494dfd4 100644 --- a/packages/types/src/extrinsic/v5/Extrinsic.ts +++ b/packages/types/src/extrinsic/v5/Extrinsic.ts @@ -89,7 +89,7 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl { /** * @description Add an [[ExtrinsicSignatureV5]] to the extrinsic (already generated) - * + * * [Disabled for ExtrinsicV5] */ public addSignature (_signer: Address | Uint8Array | string, _signature: Uint8Array | HexString, _payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV5 { @@ -98,7 +98,7 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl { /** * @description Sign the extrinsic with a specific keypair - * + * * [Disabled for ExtrinsicV5] */ public sign (_account: IKeyringPair, _options: SignatureOptions): GenericExtrinsicV5 { @@ -107,7 +107,7 @@ export class GenericExtrinsicV5 extends Struct implements IExtrinsicV5Impl { /** * @describe Adds a fake signature to the extrinsic - * + * * [Disabled for ExtrinsicV5] */ public signFake (_signer: Address | Uint8Array | string, _options: SignatureOptions): GenericExtrinsicV5 { diff --git a/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts b/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts index f8dc14c340e..789da526862 100644 --- a/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts +++ b/packages/types/src/extrinsic/v5/ExtrinsicPayload.ts @@ -106,7 +106,7 @@ export class GenericExtrinsicPayloadV5 extends Struct { /** * @description Sign the payload with the keypair - * + * * [Disabled for ExtrinsicV5] */ public sign (_signerPair: IKeyringPair): Uint8Array { diff --git a/packages/types/src/extrinsic/v5/ExtrinsicSignature.ts b/packages/types/src/extrinsic/v5/ExtrinsicSignature.ts index e793c2738e3..ab00da9bbcb 100644 --- a/packages/types/src/extrinsic/v5/ExtrinsicSignature.ts +++ b/packages/types/src/extrinsic/v5/ExtrinsicSignature.ts @@ -9,18 +9,11 @@ import type { ExtrinsicPayloadValue, ICompact, IExtrinsicSignature, IKeyringPair import type { ExtrinsicSignatureOptions } from '../types.js'; import { Struct } from '@polkadot/types-codec'; -import { isU8a, isUndefined, objectProperties, objectSpread, stringify, u8aToHex } from '@polkadot/util'; +import { objectProperties, objectSpread } from '@polkadot/util'; import { EMPTY_U8A, IMMORTAL_ERA } from '../constants.js'; import { GenericExtrinsicPayloadV5 } from './ExtrinsicPayload.js'; -// Ensure we have enough data for all types of signatures -const FAKE_SIGNATURE = new Uint8Array(256).fill(1); - -function toAddress (registry: Registry, address: Address | Uint8Array | string): Address { - return registry.createTypeUnsafe('Address', [isU8a(address) ? u8aToHex(address) : address]); -} - /** * @name GenericExtrinsicSignatureV5 * @description @@ -156,7 +149,7 @@ export class GenericExtrinsicSignatureV5 extends Struct implements IExtrinsicSig /** * @description Adds a raw signature - * + * * [Disabled for ExtrinsicV5] */ public addSignature (_signer: Address | Uint8Array | string, _signature: Uint8Array | HexString, _payload: ExtrinsicPayloadValue | Uint8Array | HexString): IExtrinsicSignature { @@ -179,7 +172,7 @@ export class GenericExtrinsicSignatureV5 extends Struct implements IExtrinsicSig /** * @description Generate a payload and applies the signature from a keypair - * + * * [Disabled for ExtrinsicV5] */ public sign (_method: Call, _account: IKeyringPair, _options: SignatureOptions): IExtrinsicSignature { @@ -188,7 +181,7 @@ export class GenericExtrinsicSignatureV5 extends Struct implements IExtrinsicSig /** * @description Generate a payload and applies a fake signature - * + * * [Disabled for ExtrinsicV5] */ public signFake (_method: Call, _address: Address | Uint8Array | string, _options: SignatureOptions): IExtrinsicSignature { From 77a7a0bd3ab3bb3e2d0d20e1721d0ae20968002f Mon Sep 17 00:00:00 2001 From: tarikgul Date: Fri, 15 Nov 2024 20:41:21 -0500 Subject: [PATCH 9/9] Fix signature tests --- .../extrinsic/v5/ExtrinsicSignature.spec.ts | 76 ++----------------- 1 file changed, 8 insertions(+), 68 deletions(-) diff --git a/packages/types/src/extrinsic/v5/ExtrinsicSignature.spec.ts b/packages/types/src/extrinsic/v5/ExtrinsicSignature.spec.ts index 7fd7d58f9d7..ebeca3e4af5 100644 --- a/packages/types/src/extrinsic/v5/ExtrinsicSignature.spec.ts +++ b/packages/types/src/extrinsic/v5/ExtrinsicSignature.spec.ts @@ -61,25 +61,15 @@ describe('ExtrinsicSignatureV4', (): void => { registry.setMetadata(metadata); expect( - new ExtrinsicSignature(registry).signFake( + () => new ExtrinsicSignature(registry).signFake( registry.createType('Call'), pairs.alice.publicKey, signOptions ).toHex() - ).toEqual( - '0x' + - '00' + // MultiAddress - 'd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d' + - '01' + - '0101010101010101010101010101010101010101010101010101010101010101' + - '0101010101010101010101010101010101010101010101010101010101010101' + - '00' + // TransactionExtension version - '00a5010000' + - '00' // Mode - ); + ).toThrow('Extrinsic: ExtrinsicV5 does not include signing support'); }); - it('fake signs default (AccountId address)', (): void => { + it('Errors on fake sign', (): void => { const registry = new TypeRegistry(); const metadata = new Metadata(registry, metadataStatic); @@ -90,76 +80,26 @@ describe('ExtrinsicSignatureV4', (): void => { }); expect( - new ExtrinsicSignature(registry).signFake( + () => new ExtrinsicSignature(registry).signFake( registry.createType('Call'), pairs.alice.address, signOptions ).toHex() - ).toEqual( - '0x' + - // Address = AccountId, no prefix - 'd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d' + - // This is a prefix-less signature, anySignture as opposed to Multi above - '0101010101010101010101010101010101010101010101010101010101010101' + - '0101010101010101010101010101010101010101010101010101010101010101' + - '00' + // TransactionExtension version - '00a5010000' + - '00' // mode - ); + ).toThrow('Extrinsic: ExtrinsicV5 does not include signing support'); }); - it('fake signs with non-enum signature', (): void => { - const registry = new TypeRegistry(); - const metadata = new Metadata(registry, metadataStatic); - - registry.setMetadata(metadata); - registry.register({ - Address: 'AccountId', - ExtrinsicSignature: '[u8;65]' - }); - - expect( - new ExtrinsicSignature(registry).signFake( - registry.createType('Call'), - pairs.alice.address, - signOptions - ).toHex() - ).toEqual( - '0x' + - // Address = AccountId, no prefix - 'd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d' + - // 65 bytes here - '01' + - '0101010101010101010101010101010101010101010101010101010101010101' + - '0101010101010101010101010101010101010101010101010101010101010101' + - '00' + // TransactionExtension version - '00a5010000' + - '00' // mode - ); - }); - - it('injects a signature', (): void => { + it('Errors on injecting a signature', (): void => { const registry = new TypeRegistry(); const metadata = new Metadata(registry, metadataStatic); registry.setMetadata(metadata); expect( - new ExtrinsicSignature(registry).addSignature( + () => new ExtrinsicSignature(registry).addSignature( pairs.alice.publicKey, new Uint8Array(65).fill(1), new Uint8Array(0) ).toHex() - ).toEqual( - '0x' + - '00' + // MultiAddress - 'd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d' + - '01' + - '0101010101010101010101010101010101010101010101010101010101010101' + - '0101010101010101010101010101010101010101010101010101010101010101' + - '00' + // TransactionExtension version - '00000000' + - '00' // mode - ); + ).toThrow('Extrinsic: ExtrinsicV5 does not include signing support'); }); });