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:
@@ -7,7 +7,6 @@ import { AnyJson, Codec, Registry } from '../types';
|
||||
import { u8aConcat, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import Compact from './Compact';
|
||||
import Raw from './Raw';
|
||||
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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,8 +7,6 @@ import { AnyNumber, Codec, Registry } from '../types';
|
||||
import BN from 'bn.js';
|
||||
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 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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,6 @@ import { AnyJson, Constructor, Codec, InterfaceTypes, Registry } from '../types'
|
||||
import { isHex, hexToU8a, isU8a, logger, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
import Compact from './Compact';
|
||||
import Raw from './Raw';
|
||||
import { compareSet, decodeU8a, typeToConstructor } from './utils';
|
||||
|
||||
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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
import { H256 } from '../interfaces/runtime';
|
||||
import { AnyJson, BareOpts, Codec, Registry } from '../types';
|
||||
|
||||
import Raw from './Raw';
|
||||
|
||||
/**
|
||||
* @name Base
|
||||
* @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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,7 +10,6 @@ import { DEFAULT_BITLENGTH } from '@polkadot/util/compact/defaults';
|
||||
|
||||
import typeToConstructor from './utils/typeToConstructor';
|
||||
import { UIntBitLength } from './AbstractInt';
|
||||
import Raw from './Raw';
|
||||
|
||||
export interface CompactEncodable extends Codec {
|
||||
bitLength (): number;
|
||||
@@ -88,7 +87,7 @@ export default class Compact<T extends CompactEncodable> implements ICompact<T>
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -56,7 +56,7 @@ export default class CodecDate extends Date implements Codec {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return this.registry.createType('H256', this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,6 @@ import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, isUndefin
|
||||
|
||||
import Null from '../primitive/Null';
|
||||
import { mapToTypeMap } from './utils';
|
||||
import Raw from './Raw';
|
||||
import Struct from './Struct';
|
||||
|
||||
// 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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 Compact from './Compact';
|
||||
import Raw from './Raw';
|
||||
import compareMap from './utils/compareMap';
|
||||
import decodeU8a from './utils/decodeU8a';
|
||||
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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,7 +8,6 @@ import { isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
|
||||
|
||||
import Null from '../primitive/Null';
|
||||
import { typeToConstructor } from './utils';
|
||||
import Raw from './Raw';
|
||||
|
||||
/** @internal */
|
||||
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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,7 +44,7 @@ export default class Raw extends Uint8Array implements IU8a {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,6 @@ import { Codec, Constructor, Registry } from '../types';
|
||||
import BN from 'bn.js';
|
||||
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';
|
||||
|
||||
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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 Raw from './Raw';
|
||||
import { compareMap, decodeU8a, mapToTypeMap } from './utils';
|
||||
|
||||
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
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,8 +6,6 @@ import { AnyJson, Codec, Registry } from '../types';
|
||||
|
||||
import { isUndefined } from '@polkadot/util';
|
||||
|
||||
import Raw from './Raw';
|
||||
|
||||
import { compareMap } from './utils';
|
||||
|
||||
/** @internal */
|
||||
@@ -59,7 +57,7 @@ export default class StructAny extends Map<string, any> implements Codec {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return new Raw(this.registry, this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -162,7 +162,7 @@ describe('TypeRegistry', (): void => {
|
||||
|
||||
it('hashes via blake2 by default', (): void => {
|
||||
expect(
|
||||
registry.hash(u8aToU8a('abc'))
|
||||
registry.hash(u8aToU8a('abc')).toU8a()
|
||||
).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])
|
||||
);
|
||||
@@ -172,15 +172,13 @@ describe('TypeRegistry', (): void => {
|
||||
registry.setHasher(keccakAsU8a);
|
||||
|
||||
expect(
|
||||
registry.hash(u8aToU8a('test value'))
|
||||
).toEqual(
|
||||
u8aToU8a('0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e')
|
||||
);
|
||||
registry.hash(u8aToU8a('test value')).toHex()
|
||||
).toEqual('0x2d07364b5c231c56ce63d49430e085ea3033c750688ba532b24029124c26ca5e');
|
||||
|
||||
registry.setHasher();
|
||||
|
||||
expect(
|
||||
registry.hash(u8aToU8a('abc'))
|
||||
registry.hash(u8aToU8a('abc')).toU8a()
|
||||
).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])
|
||||
);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
/* 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 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));
|
||||
}
|
||||
|
||||
public hash (data: Uint8Array): Uint8Array {
|
||||
return this.#hasher(data);
|
||||
public hash (data: Uint8Array): H256 {
|
||||
return this.createType('H256', this.#hasher(data));
|
||||
}
|
||||
|
||||
public register (type: Constructor | RegistryTypes): void;
|
||||
|
||||
@@ -39,7 +39,7 @@ export default class Block extends Struct {
|
||||
* @description Encodes a content [[Hash]] for the block
|
||||
*/
|
||||
public get contentHash (): H256 {
|
||||
return this.registry.createType('H256', this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -44,7 +44,7 @@ export default class Bool extends Boolean implements Codec {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return this.registry.createType('H256', this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -70,7 +70,7 @@ export default class Text extends String implements Codec {
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): H256 {
|
||||
return this.registry.createType('H256', this.registry.hash(this.toU8a()));
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
import type BN from 'bn.js';
|
||||
|
||||
import { H256 } from '../interfaces/runtime';
|
||||
import { ChainProperties } from '../interfaces/system';
|
||||
import { u8 } from '../primitive';
|
||||
import { CallFunction } from './calls';
|
||||
@@ -161,7 +162,7 @@ export interface Registry {
|
||||
hasClass (name: string): boolean;
|
||||
hasDef (name: string): boolean;
|
||||
hasType (name: string): boolean;
|
||||
hash (data: Uint8Array): Uint8Array;
|
||||
hash (data: Uint8Array): H256;
|
||||
init (): Registry;
|
||||
register (type: Constructor | RegistryTypes): void;
|
||||
register (name: string, type: Constructor): void;
|
||||
|
||||
Reference in New Issue
Block a user