Dedupe WrapperOpaque classes (#4479)

* Dedupe WrapperOpaque classes

* Keep WrapperOpaque in own file
This commit is contained in:
Jaco
2022-01-24 08:13:08 +02:00
committed by GitHub
parent e16d20e996
commit ff24cec841
3 changed files with 26 additions and 40 deletions
@@ -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<T extends Codec> (registry: Registry, typeName: CodecClass<T> | string, value?: AnyU8a | Codec): [CodecClass<T>, T | null, AnyU8a] {
type OpaqueName = 'WrapperKeepOpaque' | 'WrapperOpaque';
function decodeRaw<T extends Codec> (registry: Registry, typeName: CodecClass<T> | string, value?: unknown): [CodecClass<T>, T | null, AnyU8a] {
const Type = typeToConstructor(registry, typeName);
if (isU8a(value) || isHex(value)) {
@@ -26,7 +28,7 @@ function decodeRaw<T extends Codec> (registry: Registry, typeName: CodecClass<T>
const instance = new Type(registry, value);
return [Type, instance, instance.toHex()];
return [Type, instance, compactAddLength(instance.toU8a())];
}
export class WrapperKeepOpaque<T extends Codec> extends Bytes {
@@ -34,13 +36,16 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
readonly #decoded: T | null;
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: AnyU8a | Codec) {
readonly #opaqueName: OpaqueName;
constructor (registry: Registry, typeName: CodecClass<T> | 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<T extends Codec> (Type: CodecClass<T> | string): CodecClass<WrapperKeepOpaque<T>> {
@@ -71,7 +76,7 @@ export class WrapperKeepOpaque<T extends Codec> 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<T extends Codec> 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`);
}
}
@@ -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<u32>', 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()
@@ -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<T extends Codec> (registry: Registry, Type: CodecClass<T> | string, value?: unknown): T {
const Clazz = typeToConstructor<T>(registry, Type);
if (isU8a(value)) {
const [, u8a] = compactStripLength(value);
return new Clazz(registry, u8a);
export class WrapperOpaque<T extends Codec> extends WrapperKeepOpaque<T> {
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: unknown) {
super(registry, typeName, value, 'WrapperOpaque');
}
return new Clazz(registry, value);
}
export class WrapperOpaque<T extends Codec> extends Base<T> {
constructor (registry: Registry, Type: CodecClass<T> | string, value?: unknown) {
super(registry, decodeRaw(registry, Type, value));
}
public static with<T extends Codec> (Type: CodecClass<T> | string): CodecClass<WrapperOpaque<T>> {
public static override with<T extends Codec> (Type: CodecClass<T> | string): CodecClass<WrapperKeepOpaque<T>> {
return class extends WrapperOpaque<T> {
constructor (registry: Registry, value?: unknown) {
super(registry, Type, value);
@@ -34,20 +19,9 @@ export class WrapperOpaque<T extends Codec> extends Base<T> {
}
/**
* @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();
}
}