From ff24cec8416386c93080fc76fd0d93b491eabe9a Mon Sep 17 00:00:00 2001 From: Jaco Date: Mon, 24 Jan 2022 07:13:08 +0100 Subject: [PATCH] Dedupe WrapperOpaque classes (#4479) * Dedupe WrapperOpaque classes * Keep WrapperOpaque in own file --- .../src/extended/WrapperKeepOpaque.ts | 17 +++++--- .../src/extended/WrapperOpaque.spec.ts | 7 ++++ .../types-codec/src/extended/WrapperOpaque.ts | 42 ++++--------------- 3 files changed, 26 insertions(+), 40 deletions(-) diff --git a/packages/types-codec/src/extended/WrapperKeepOpaque.ts b/packages/types-codec/src/extended/WrapperKeepOpaque.ts index e1ed1cd1d1..1eb56420a2 100644 --- a/packages/types-codec/src/extended/WrapperKeepOpaque.ts +++ b/packages/types-codec/src/extended/WrapperKeepOpaque.ts @@ -3,13 +3,15 @@ import type { AnyJson, AnyU8a, Codec, CodecClass, Registry } from '../types'; -import { assertReturn, compactStripLength, isHex, isU8a } from '@polkadot/util'; +import { assertReturn, compactAddLength, compactStripLength, isHex, isU8a } from '@polkadot/util'; import { Raw } from '../native/Raw'; import { typeToConstructor } from '../utils'; import { Bytes } from './Bytes'; -function decodeRaw (registry: Registry, typeName: CodecClass | string, value?: AnyU8a | Codec): [CodecClass, T | null, AnyU8a] { +type OpaqueName = 'WrapperKeepOpaque' | 'WrapperOpaque'; + +function decodeRaw (registry: Registry, typeName: CodecClass | string, value?: unknown): [CodecClass, T | null, AnyU8a] { const Type = typeToConstructor(registry, typeName); if (isU8a(value) || isHex(value)) { @@ -26,7 +28,7 @@ function decodeRaw (registry: Registry, typeName: CodecClass const instance = new Type(registry, value); - return [Type, instance, instance.toHex()]; + return [Type, instance, compactAddLength(instance.toU8a())]; } export class WrapperKeepOpaque extends Bytes { @@ -34,13 +36,16 @@ export class WrapperKeepOpaque extends Bytes { readonly #decoded: T | null; - constructor (registry: Registry, typeName: CodecClass | string, value?: AnyU8a | Codec) { + readonly #opaqueName: OpaqueName; + + constructor (registry: Registry, typeName: CodecClass | string, value?: unknown, opaqueName: OpaqueName = 'WrapperKeepOpaque') { const [Type, decoded, u8a] = decodeRaw(registry, typeName, value); super(registry, u8a); this.#Type = Type; this.#decoded = decoded; + this.#opaqueName = opaqueName; } public static with (Type: CodecClass | string): CodecClass> { @@ -71,7 +76,7 @@ export class WrapperKeepOpaque extends Bytes { * @description Returns the base runtime type name for this instance */ public override toRawType (): string { - return `WrapperKeepOpaque<${this.registry.getClassName(this.#Type) || (this.#decoded ? this.#decoded.toRawType() : new this.#Type(this.registry).toRawType())}>`; + return `${this.#opaqueName}<${this.registry.getClassName(this.#Type) || (this.#decoded ? this.#decoded.toRawType() : new this.#Type(this.registry).toRawType())}>`; } /** @@ -87,6 +92,6 @@ export class WrapperKeepOpaque extends Bytes { * @description Returns the decoded that the WrapperKeepOpaque represents (if available), throws if non-decodable */ public unwrap (): T { - return assertReturn(this.#decoded, 'WrapperKeepOpaque: unwrapping an undecodable value'); + return assertReturn(this.#decoded, () => `${this.#opaqueName}: unwrapping an undecodable value`); } } diff --git a/packages/types-codec/src/extended/WrapperOpaque.spec.ts b/packages/types-codec/src/extended/WrapperOpaque.spec.ts index 8207e8ad59..30ed87c019 100644 --- a/packages/types-codec/src/extended/WrapperOpaque.spec.ts +++ b/packages/types-codec/src/extended/WrapperOpaque.spec.ts @@ -3,6 +3,7 @@ import { TypeRegistry } from '@polkadot/types'; import { WrapperOpaque } from '@polkadot/types-codec'; +import { u8aConcat } from '@polkadot/util'; describe('WrapperOpaque', (): void => { const registry = new TypeRegistry(); @@ -20,6 +21,12 @@ describe('WrapperOpaque', (): void => { ).toEqual(u8au32); }); + it('u8a encodes a wrapped option correctly', (): void => { + expect( + new WrapperOpaque(registry, 'Option', 1234567).toU8a() + ).toEqual(u8aConcat([5 << 2, 1], u8au32.slice(1))); + }); + it('hex encodes a wrapped u32 correctly', (): void => { expect( new WrapperOpaque(registry, 'u32', '0x12345678').toHex() diff --git a/packages/types-codec/src/extended/WrapperOpaque.ts b/packages/types-codec/src/extended/WrapperOpaque.ts index 406ab70e4d..9bbeaeb3a9 100644 --- a/packages/types-codec/src/extended/WrapperOpaque.ts +++ b/packages/types-codec/src/extended/WrapperOpaque.ts @@ -3,29 +3,14 @@ import type { Codec, CodecClass, Registry } from '../types'; -import { compactAddLength, compactStripLength, isU8a } from '@polkadot/util'; +import { WrapperKeepOpaque } from './WrapperKeepOpaque'; -import { Base } from '../base/Base'; -import { typeToConstructor } from '../utils'; - -function decodeRaw (registry: Registry, Type: CodecClass | string, value?: unknown): T { - const Clazz = typeToConstructor(registry, Type); - - if (isU8a(value)) { - const [, u8a] = compactStripLength(value); - - return new Clazz(registry, u8a); +export class WrapperOpaque extends WrapperKeepOpaque { + constructor (registry: Registry, typeName: CodecClass | string, value?: unknown) { + super(registry, typeName, value, 'WrapperOpaque'); } - return new Clazz(registry, value); -} - -export class WrapperOpaque extends Base { - constructor (registry: Registry, Type: CodecClass | string, value?: unknown) { - super(registry, decodeRaw(registry, Type, value)); - } - - public static with (Type: CodecClass | string): CodecClass> { + public static override with (Type: CodecClass | string): CodecClass> { return class extends WrapperOpaque { constructor (registry: Registry, value?: unknown) { super(registry, Type, value); @@ -34,20 +19,9 @@ export class WrapperOpaque extends Base { } /** - * @description Returns the base runtime type name for this instance + * @description The inner value for this wrapper, in all cases it _should_ be decodable (unlike KeepOpaque) */ - public override toRawType (): string { - return `WrapperOpaque<${this.inner.toRawType()}>`; - } - - /** - * @description The length of the value when encoded as a Uint8Array - */ - public override toU8a (isBare?: boolean): Uint8Array { - const u8a = super.toU8a(isBare); - - return isBare - ? u8a - : compactAddLength(u8a); + public get inner (): T { + return this.unwrap(); } }