Registry hash returns H256 object (#2791)

* Use logger in error/warning messages

* Update packages/types/src/codec/BTreeSet.ts

* Update expectation

* Some additional type tests

* resgirty.hash returns H256

* Additional override test
This commit is contained in:
Jaco Greeff
2020-10-27 13:47:18 +01:00
committed by GitHub
parent d876e19fa1
commit cc9ea53e1a
19 changed files with 25 additions and 40 deletions
+1 -2
View File
@@ -7,7 +7,6 @@ import { AnyJson, Codec, Registry } from '../types';
import { u8aConcat, u8aToHex } from '@polkadot/util'; import { u8aConcat, u8aToHex } from '@polkadot/util';
import Compact from './Compact'; import Compact from './Compact';
import Raw from './Raw';
import { compareArray } from './utils'; import { compareArray } from './utils';
/** /**
@@ -39,7 +38,7 @@ export default abstract class AbstractArray<T extends Codec> extends Array<T> im
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -3
View File
@@ -7,8 +7,6 @@ import { AnyNumber, Codec, Registry } from '../types';
import BN from 'bn.js'; import BN from 'bn.js';
import { BN_ZERO, assert, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util'; import { BN_ZERO, assert, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util';
import Raw from './Raw';
export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256; export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
export const DEFAULT_UINT_BITS = 64; export const DEFAULT_UINT_BITS = 64;
@@ -95,7 +93,7 @@ export default abstract class AbstractInt extends BN implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -2
View File
@@ -7,7 +7,6 @@ import { AnyJson, Constructor, Codec, InterfaceTypes, Registry } from '../types'
import { isHex, hexToU8a, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; import { isHex, hexToU8a, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import Compact from './Compact'; import Compact from './Compact';
import Raw from './Raw';
import { compareSet, decodeU8a, typeToConstructor } from './utils'; import { compareSet, decodeU8a, typeToConstructor } from './utils';
const l = logger('BTreeSet'); const l = logger('BTreeSet');
@@ -117,7 +116,7 @@ export default class BTreeSet<V extends Codec = Codec> extends Set<V> implements
* @description Returns a hash of the value * @description Returns a hash of the value
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -3
View File
@@ -4,8 +4,6 @@
import { H256 } from '../interfaces/runtime'; import { H256 } from '../interfaces/runtime';
import { AnyJson, BareOpts, Codec, Registry } from '../types'; import { AnyJson, BareOpts, Codec, Registry } from '../types';
import Raw from './Raw';
/** /**
* @name Base * @name Base
* @description A type extends the Base class, when it holds a value * @description A type extends the Base class, when it holds a value
@@ -31,7 +29,7 @@ export default abstract class Base<T extends Codec> implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -2
View File
@@ -10,7 +10,6 @@ import { DEFAULT_BITLENGTH } from '@polkadot/util/compact/defaults';
import typeToConstructor from './utils/typeToConstructor'; import typeToConstructor from './utils/typeToConstructor';
import { UIntBitLength } from './AbstractInt'; import { UIntBitLength } from './AbstractInt';
import Raw from './Raw';
export interface CompactEncodable extends Codec { export interface CompactEncodable extends Codec {
bitLength (): number; bitLength (): number;
@@ -88,7 +87,7 @@ export default class Compact<T extends CompactEncodable> implements ICompact<T>
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -1
View File
@@ -56,7 +56,7 @@ export default class CodecDate extends Date implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return this.registry.createType('H256', this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -2
View File
@@ -8,7 +8,6 @@ import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, isUndefin
import Null from '../primitive/Null'; import Null from '../primitive/Null';
import { mapToTypeMap } from './utils'; import { mapToTypeMap } from './utils';
import Raw from './Raw';
import Struct from './Struct'; import Struct from './Struct';
// export interface, this is used in Enum.with, so required as public by TS // export interface, this is used in Enum.with, so required as public by TS
@@ -190,7 +189,7 @@ export default class Enum implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -2
View File
@@ -7,7 +7,6 @@ import { AnyJson, Constructor, Codec, InterfaceTypes, Registry } from '../types'
import { isHex, hexToU8a, isObject, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; import { isHex, hexToU8a, isObject, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import Compact from './Compact'; import Compact from './Compact';
import Raw from './Raw';
import compareMap from './utils/compareMap'; import compareMap from './utils/compareMap';
import decodeU8a from './utils/decodeU8a'; import decodeU8a from './utils/decodeU8a';
import typeToConstructor from './utils/typeToConstructor'; import typeToConstructor from './utils/typeToConstructor';
@@ -126,7 +125,7 @@ export default class CodecMap<K extends Codec = Codec, V extends Codec = Codec>
* @description Returns a hash of the value * @description Returns a hash of the value
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -2
View File
@@ -8,7 +8,6 @@ import { isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
import Null from '../primitive/Null'; import Null from '../primitive/Null';
import { typeToConstructor } from './utils'; import { typeToConstructor } from './utils';
import Raw from './Raw';
/** @internal */ /** @internal */
function decodeOptionU8a (registry: Registry, Type: Constructor, value: Uint8Array): Codec { function decodeOptionU8a (registry: Registry, Type: Constructor, value: Uint8Array): Codec {
@@ -81,7 +80,7 @@ export default class Option<T extends Codec> implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -1
View File
@@ -44,7 +44,7 @@ export default class Raw extends Uint8Array implements IU8a {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -2
View File
@@ -7,7 +7,6 @@ import { Codec, Constructor, Registry } from '../types';
import BN from 'bn.js'; import BN from 'bn.js';
import { assert, bnToBn, bnToU8a, isBn, isU8a, isNumber, isString, isUndefined, stringCamelCase, stringUpperFirst, u8aToHex, u8aToBn, u8aToU8a } from '@polkadot/util'; import { assert, bnToBn, bnToU8a, isBn, isU8a, isNumber, isString, isUndefined, stringCamelCase, stringUpperFirst, u8aToHex, u8aToBn, u8aToU8a } from '@polkadot/util';
import Raw from './Raw';
import { compareArray } from './utils'; import { compareArray } from './utils';
type SetValues = Record<string, number | BN>; type SetValues = Record<string, number | BN>;
@@ -125,7 +124,7 @@ export default class CodecSet extends Set<string> implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -2
View File
@@ -6,7 +6,6 @@ import { AnyJson, BareOpts, Codec, Constructor, ConstructorDef, InterfaceTypes,
import { hexToU8a, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, stringCamelCase, u8aConcat, u8aToHex } from '@polkadot/util'; import { hexToU8a, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, stringCamelCase, u8aConcat, u8aToHex } from '@polkadot/util';
import Raw from './Raw';
import { compareMap, decodeU8a, mapToTypeMap } from './utils'; import { compareMap, decodeU8a, mapToTypeMap } from './utils';
type TypesDef<T = Codec> = Record<string, keyof InterfaceTypes | Constructor<T>>; type TypesDef<T = Codec> = Record<string, keyof InterfaceTypes | Constructor<T>>;
@@ -215,7 +214,7 @@ export default class Struct<
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -3
View File
@@ -6,8 +6,6 @@ import { AnyJson, Codec, Registry } from '../types';
import { isUndefined } from '@polkadot/util'; import { isUndefined } from '@polkadot/util';
import Raw from './Raw';
import { compareMap } from './utils'; import { compareMap } from './utils';
/** @internal */ /** @internal */
@@ -59,7 +57,7 @@ export default class StructAny extends Map<string, any> implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return new Raw(this.registry, this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+4 -6
View File
@@ -162,7 +162,7 @@ describe('TypeRegistry', (): void => {
it('hashes via blake2 by default', (): void => { it('hashes via blake2 by default', (): void => {
expect( expect(
registry.hash(u8aToU8a('abc')) registry.hash(u8aToU8a('abc')).toU8a()
).toEqual( ).toEqual(
new Uint8Array([189, 221, 129, 60, 99, 66, 57, 114, 49, 113, 239, 63, 238, 152, 87, 155, 148, 150, 78, 59, 177, 203, 62, 66, 114, 98, 200, 192, 104, 213, 35, 25]) new Uint8Array([189, 221, 129, 60, 99, 66, 57, 114, 49, 113, 239, 63, 238, 152, 87, 155, 148, 150, 78, 59, 177, 203, 62, 66, 114, 98, 200, 192, 104, 213, 35, 25])
); );
@@ -172,15 +172,13 @@ describe('TypeRegistry', (): void => {
registry.setHasher(keccakAsU8a); registry.setHasher(keccakAsU8a);
expect( expect(
registry.hash(u8aToU8a('test value')) registry.hash(u8aToU8a('test value')).toHex()
).toEqual( ).toEqual('0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e');
u8aToU8a('0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e')
);
registry.setHasher(); registry.setHasher();
expect( expect(
registry.hash(u8aToU8a('abc')) registry.hash(u8aToU8a('abc')).toU8a()
).toEqual( ).toEqual(
new Uint8Array([189, 221, 129, 60, 99, 66, 57, 114, 49, 113, 239, 63, 238, 152, 87, 155, 148, 150, 78, 59, 177, 203, 62, 66, 114, 98, 200, 192, 104, 213, 35, 25]) new Uint8Array([189, 221, 129, 60, 99, 66, 57, 114, 49, 113, 239, 63, 238, 152, 87, 155, 148, 150, 78, 59, 177, 203, 62, 66, 114, 98, 200, 192, 104, 213, 35, 25])
); );
+3 -3
View File
@@ -3,7 +3,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */ /* eslint-disable @typescript-eslint/no-var-requires */
import { ChainProperties, DispatchErrorModule } from '../interfaces/types'; import { ChainProperties, DispatchErrorModule, H256 } from '../interfaces/types';
import { CallFunction, Codec, Constructor, InterfaceTypes, RegistryError, RegistryTypes, Registry, RegistryMetadata, RegisteredTypes, TypeDef } from '../types'; import { CallFunction, Codec, Constructor, InterfaceTypes, RegistryError, RegistryTypes, Registry, RegistryMetadata, RegisteredTypes, TypeDef } from '../types';
import extrinsicsFromMeta from '@polkadot/metadata/Decorated/extrinsics/fromMetadata'; import extrinsicsFromMeta from '@polkadot/metadata/Decorated/extrinsics/fromMetadata';
@@ -304,8 +304,8 @@ export class TypeRegistry implements Registry {
return !this.#unknownTypes.get(name) && (this.hasClass(name) || this.hasDef(name)); return !this.#unknownTypes.get(name) && (this.hasClass(name) || this.hasDef(name));
} }
public hash (data: Uint8Array): Uint8Array { public hash (data: Uint8Array): H256 {
return this.#hasher(data); return this.createType('H256', this.#hasher(data));
} }
public register (type: Constructor | RegistryTypes): void; public register (type: Constructor | RegistryTypes): void;
+1 -1
View File
@@ -39,7 +39,7 @@ export default class Block extends Struct {
* @description Encodes a content [[Hash]] for the block * @description Encodes a content [[Hash]] for the block
*/ */
public get contentHash (): H256 { public get contentHash (): H256 {
return this.registry.createType('H256', this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -1
View File
@@ -44,7 +44,7 @@ export default class Bool extends Boolean implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return this.registry.createType('H256', this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+1 -1
View File
@@ -70,7 +70,7 @@ export default class Text extends String implements Codec {
* @description returns a hash of the contents * @description returns a hash of the contents
*/ */
public get hash (): H256 { public get hash (): H256 {
return this.registry.createType('H256', this.registry.hash(this.toU8a())); return this.registry.hash(this.toU8a());
} }
/** /**
+2 -1
View File
@@ -3,6 +3,7 @@
import type BN from 'bn.js'; import type BN from 'bn.js';
import { H256 } from '../interfaces/runtime';
import { ChainProperties } from '../interfaces/system'; import { ChainProperties } from '../interfaces/system';
import { u8 } from '../primitive'; import { u8 } from '../primitive';
import { CallFunction } from './calls'; import { CallFunction } from './calls';
@@ -161,7 +162,7 @@ export interface Registry {
hasClass (name: string): boolean; hasClass (name: string): boolean;
hasDef (name: string): boolean; hasDef (name: string): boolean;
hasType (name: string): boolean; hasType (name: string): boolean;
hash (data: Uint8Array): Uint8Array; hash (data: Uint8Array): H256;
init (): Registry; init (): Registry;
register (type: Constructor | RegistryTypes): void; register (type: Constructor | RegistryTypes): void;
register (name: string, type: Constructor): void; register (name: string, type: Constructor): void;