From a0d7a52a5c0b02b56f75097ed04cb7f360dd109e Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 26 Jul 2019 11:20:34 +0200 Subject: [PATCH] Add hash: IHash on Codec base (#1193) * Convert RPC classes to primitive classes or /srml types * Lint passes... lokking at tests next. * Tests pass. * Types with basic docs (lots of work here) * Add hash: IHash on Codec base * codec hash resturns U8a * Fix imports --- packages/types/src/codec/AbstractArray.ts | 26 +++++++++++++------ packages/types/src/codec/AbstractInt.ts | 12 ++++++++- packages/types/src/codec/Base.ts | 13 +++++++++- packages/types/src/codec/Set.ts | 14 ++++++++-- packages/types/src/codec/Struct.ts | 14 ++++++++-- packages/types/src/codec/StructAny.ts | 12 ++++++++- packages/types/src/codec/U8a.ts | 19 ++++++++++++-- packages/types/src/codec/U8aFixed.ts | 8 ------ packages/types/src/primitive/Bool.ts | 14 ++++++++-- .../src/primitive/Extrinsic/Extrinsic.ts | 11 +------- packages/types/src/primitive/Header.ts | 11 +------- packages/types/src/primitive/Method.ts | 11 +------- packages/types/src/primitive/Moment.ts | 12 ++++++++- packages/types/src/primitive/Null.ts | 13 +++++++++- packages/types/src/primitive/Text.ts | 14 ++++++++-- packages/types/src/types.ts | 12 ++++++--- 16 files changed, 151 insertions(+), 65 deletions(-) diff --git a/packages/types/src/codec/AbstractArray.ts b/packages/types/src/codec/AbstractArray.ts index 13416129c4..3ea2d13115 100644 --- a/packages/types/src/codec/AbstractArray.ts +++ b/packages/types/src/codec/AbstractArray.ts @@ -2,10 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { AnyJson, AnyJsonArray, Codec, IHash } from '../types'; + import { u8aConcat, u8aToHex } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; import Compact from './Compact'; -import { AnyJson, AnyJsonArray, Codec } from '../types'; +import U8a from './U8a'; import { compareArray } from './utils'; /** @@ -16,13 +19,6 @@ import { compareArray } from './utils'; * @noInheritDoc */ export default abstract class AbstractArray extends Array implements Codec { - /** - * @description Checks if the value is an empty value - */ - public get isEmpty (): boolean { - return this.length === 0; - } - /** * @description The length of the value when encoded as a Uint8Array */ @@ -32,6 +28,20 @@ export default abstract class AbstractArray extends Array im }, Compact.encodeU8a(this.length).length); } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return new U8a(blake2AsU8a(this.toU8a(), 256)); + } + + /** + * @description Checks if the value is an empty value + */ + public get isEmpty (): boolean { + return this.length === 0; + } + /** * @description The length of the value */ diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index 6f07f3a072..d700ad8d57 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -2,10 +2,13 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AnyNumber, Codec } from '../types'; +import { AnyNumber, Codec, IHash } from '../types'; import BN from 'bn.js'; import { bnToBn, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; + +import U8a from './U8a'; export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256; @@ -69,6 +72,13 @@ export default abstract class AbstractInt extends BN implements Codec { return this._bitLength / 8; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return new U8a(blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Checks if the value is a zero value (align elsewhere) */ diff --git a/packages/types/src/codec/Base.ts b/packages/types/src/codec/Base.ts index 73d1d24c32..b4455bfb65 100644 --- a/packages/types/src/codec/Base.ts +++ b/packages/types/src/codec/Base.ts @@ -2,7 +2,11 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AnyJson, Codec } from '../types'; +import { AnyJson, Codec, IHash } from '../types'; + +import { blake2AsU8a } from '@polkadot/util-crypto'; + +import U8a from './U8a'; /** * @name Base @@ -22,6 +26,13 @@ export default abstract class Base implements Codec { return this.toU8a().length; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return new U8a(blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Checks if the value is an empty value */ diff --git a/packages/types/src/codec/Set.ts b/packages/types/src/codec/Set.ts index 5ff6c11f3c..bc6a0008c9 100644 --- a/packages/types/src/codec/Set.ts +++ b/packages/types/src/codec/Set.ts @@ -2,9 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { assert, isU8a, isNumber, isUndefined, stringCamelCase, stringUpperFirst, u8aToHex } from '@polkadot/util'; +import { Codec, Constructor, IHash } from '../types'; -import { Codec, Constructor } from '../types'; +import { assert, isU8a, isNumber, isUndefined, stringCamelCase, stringUpperFirst, u8aToHex } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; + +import U8a from './U8a'; import { compareArray } from './utils'; type SetValues = Record; @@ -91,6 +94,13 @@ export default class CodecSet extends Set implements Codec { return 1; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return new U8a(blake2AsU8a(this.toU8a(), 256)); + } + /** * @description true is the Set contains no values */ diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index 89c2e083de..4831f8b3d9 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -2,9 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { hexToU8a, isHex, isObject, isU8a, isUndefined, u8aConcat, u8aToHex } from '@polkadot/util'; +import { AnyJsonObject, Codec, Constructor, ConstructorDef, IHash } from '../types'; -import { AnyJsonObject, Codec, Constructor, ConstructorDef } from '../types'; +import { hexToU8a, isHex, isObject, isU8a, isUndefined, u8aConcat, u8aToHex } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; + +import U8a from './U8a'; import { compareMap, decodeU8a } from './utils'; /** @@ -181,6 +184,13 @@ export default class Struct< }, 0); } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return new U8a(blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Compares the value of the input to see if there is a match */ diff --git a/packages/types/src/codec/StructAny.ts b/packages/types/src/codec/StructAny.ts index b467c4e4b7..9d14f9a806 100644 --- a/packages/types/src/codec/StructAny.ts +++ b/packages/types/src/codec/StructAny.ts @@ -2,9 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AnyJsonObject, Codec } from '../types'; +import { AnyJsonObject, Codec, IHash } from '../types'; import { isUndefined } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; + +import U8a from './U8a'; import { compareMap } from './utils'; @@ -47,6 +50,13 @@ export default class StructAny extends Map implements Codec { return 0; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return new U8a(blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Checks if the value is an empty value */ diff --git a/packages/types/src/codec/U8a.ts b/packages/types/src/codec/U8a.ts index 69c913267d..1e5e735b01 100644 --- a/packages/types/src/codec/U8a.ts +++ b/packages/types/src/codec/U8a.ts @@ -2,9 +2,10 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { isU8a, isUndefined, u8aToHex, u8aToU8a } from '@polkadot/util'; +import { AnyU8a, Codec, IHash } from '../types'; -import { AnyU8a, Codec } from '../types'; +import { isU8a, isUndefined, u8aToHex, u8aToU8a } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; /** * @name U8a @@ -37,6 +38,13 @@ export default class U8a extends Uint8Array implements Codec { return this.length; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return new U8a(blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Returns true if the type wraps an empty/default all-0 value */ @@ -52,6 +60,13 @@ export default class U8a extends Uint8Array implements Codec { return super.length; } + /** + * @description Returns the number of bits in the value + */ + public bitLength (): number { + return this.length * 8; + } + /** * @description Compares the value of the input to see if there is a match */ diff --git a/packages/types/src/codec/U8aFixed.ts b/packages/types/src/codec/U8aFixed.ts index 3eb55381ad..6f33104503 100644 --- a/packages/types/src/codec/U8aFixed.ts +++ b/packages/types/src/codec/U8aFixed.ts @@ -54,18 +54,10 @@ export default class U8aFixed extends U8a { }; } - /** - * @description Returns the number of bits in the value - */ - public bitLength (): number { - return this.length * 8; - } - /** * @description Returns the base runtime type name for this instance */ public toRawType (): string { - // FIEXME We don't cater for this in createType return `[u8;${this.length}]`; } } diff --git a/packages/types/src/primitive/Bool.ts b/packages/types/src/primitive/Bool.ts index 94b1f407de..869b1b3177 100644 --- a/packages/types/src/primitive/Bool.ts +++ b/packages/types/src/primitive/Bool.ts @@ -2,9 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { isU8a, u8aToHex } from '@polkadot/util'; +import { Codec, IHash } from '../types'; -import { Codec } from '../types'; +import { isU8a, u8aToHex } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; + +import createType from '../codec/createType'; /** * @name Bool @@ -37,6 +40,13 @@ export default class Bool extends Boolean implements Codec { return 1; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return createType('Hash', blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Checks if the value is an empty value (always false) */ diff --git a/packages/types/src/primitive/Extrinsic/Extrinsic.ts b/packages/types/src/primitive/Extrinsic/Extrinsic.ts index 799d54c56d..08aeb78087 100644 --- a/packages/types/src/primitive/Extrinsic/Extrinsic.ts +++ b/packages/types/src/primitive/Extrinsic/Extrinsic.ts @@ -2,13 +2,11 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Balance, Hash, Index } from '../../srml/runtime/types'; +import { Balance, Index } from '../../srml/runtime/types'; import { AnyU8a, ArgsDef, Codec, ExtrinsicPayloadValue, IExtrinsic, IHash, IKeyringPair, SignatureOptions } from '../../types'; import { assert, isHex, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util'; -import { blake2AsU8a } from '@polkadot/util-crypto'; -import createType from '../../codec/createType'; import Base from '../../codec/Base'; import Compact from '../../codec/Compact'; import { FunctionMetadata } from '../../Metadata/v7/Calls'; @@ -138,13 +136,6 @@ export default class Extrinsic extends Base implement return this.toU8a().length; } - /** - * @description Convernience function, encodes the extrinsic and returns the actual hash - */ - public get hash (): Hash { - return createType('Hash', blake2AsU8a(this.toU8a(), 256)); - } - /** * @description `true` is method has `Origin` argument (compatibility with [[Method]]) */ diff --git a/packages/types/src/primitive/Header.ts b/packages/types/src/primitive/Header.ts index 4df6313ee9..89ab1165fc 100644 --- a/packages/types/src/primitive/Header.ts +++ b/packages/types/src/primitive/Header.ts @@ -5,9 +5,7 @@ import { BlockNumber, Hash } from '../srml/runtime/types'; import { AnyNumber, AnyU8a } from '../types'; -import { blake2AsU8a } from '@polkadot/util-crypto'; - -import createType, { ClassOf } from '../codec/createType'; +import { ClassOf } from '../codec/createType'; import Compact from '../codec/Compact'; import Struct from '../codec/Struct'; import Digest, { DigestItem } from './Digest'; @@ -57,13 +55,6 @@ export default class Header extends Struct { return this.get('extrinsicsRoot') as Hash; } - /** - * @description Convenience method, encodes the header and calculates the [[Hash]] - */ - public get hash (): Hash { - return createType('Hash', blake2AsU8a(this.toU8a(), 256)); - } - /** * @description Alias for `blockNumber` (this is displayed in JSON) */ diff --git a/packages/types/src/primitive/Method.ts b/packages/types/src/primitive/Method.ts index f9d3cb128d..4c68d743a2 100644 --- a/packages/types/src/primitive/Method.ts +++ b/packages/types/src/primitive/Method.ts @@ -2,13 +2,11 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Hash } from '../srml/runtime/types'; import { AnyU8a, ArgsDef, Codec, IMethod } from '../types'; import { assert, isHex, isObject, isU8a, hexToU8a } from '@polkadot/util'; -import { blake2AsU8a } from '@polkadot/util-crypto'; -import createType, { getTypeDef, getTypeClass } from '../codec/createType'; +import { getTypeDef, getTypeClass } from '../codec/createType'; import Struct from '../codec/Struct'; import U8aFixed from '../codec/U8aFixed'; import { FunctionMetadata as FunctionMetadataV7, FunctionArgumentMetadata } from '../Metadata/v7/Calls'; @@ -206,13 +204,6 @@ export default class Method extends Struct implements IMethod { return (this.get('args') as Struct).toU8a(); } - /** - * @description Convenience function, encodes the Method and returns the actual hash - */ - public get hash (): Hash { - return createType('Hash', blake2AsU8a(this.toU8a(), 256)); - } - /** * @description `true` if the `Origin` type is on the method (extrinsic method) */ diff --git a/packages/types/src/primitive/Moment.ts b/packages/types/src/primitive/Moment.ts index 8b9ad24346..1efdbd8723 100644 --- a/packages/types/src/primitive/Moment.ts +++ b/packages/types/src/primitive/Moment.ts @@ -2,11 +2,14 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. +import { AnyNumber, Codec, IHash } from '../types'; + import BN from 'bn.js'; import { bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; +import createType from '../codec/createType'; import { UIntBitLength } from '../codec/AbstractInt'; -import { AnyNumber, Codec } from '../types'; const BITLENGTH: UIntBitLength = 64; @@ -52,6 +55,13 @@ export default class Moment extends Date implements Codec { return BITLENGTH / 8; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return createType('Hash', blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Checks if the value is an empty value */ diff --git a/packages/types/src/primitive/Null.ts b/packages/types/src/primitive/Null.ts index f6c7291330..1bad426d23 100644 --- a/packages/types/src/primitive/Null.ts +++ b/packages/types/src/primitive/Null.ts @@ -2,8 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Codec } from '../types'; +import { Codec, IHash } from '../types'; + import { isNull } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; + +import createType from '../codec/createType'; /** * @name Null @@ -18,6 +22,13 @@ export default class Null implements Codec { return 0; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return createType('Hash', blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Checks if the value is an empty value (always true) */ diff --git a/packages/types/src/primitive/Text.ts b/packages/types/src/primitive/Text.ts index 5e542e8528..9dc306ceb6 100644 --- a/packages/types/src/primitive/Text.ts +++ b/packages/types/src/primitive/Text.ts @@ -2,9 +2,12 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { assert, hexToU8a, isHex, isString, stringToU8a, u8aToString, u8aToHex } from '@polkadot/util'; +import { AnyU8a, Codec, IHash } from '../types'; -import { AnyU8a, Codec } from '../types'; +import { assert, hexToU8a, isHex, isString, stringToU8a, u8aToString, u8aToHex } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; + +import createType from '../codec/createType'; import Compact from '../codec/Compact'; /** @@ -50,6 +53,13 @@ export default class Text extends String implements Codec { return this.toU8a().length; } + /** + * @description returns a hash of the contents + */ + public get hash (): IHash { + return createType('Hash', blake2AsU8a(this.toU8a(), 256)); + } + /** * @description Checks if the value is an empty value */ diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index 209e578624..1bfeac4232 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -57,6 +57,11 @@ export interface Codec { */ encodedLength: number; + /** + * @description Returns a hash of the value + */ + hash: IHash; + /** * @description Checks if the value is an empty value */ @@ -94,6 +99,9 @@ export interface Codec { toU8a (isBare?: boolean): Uint8Array; } +// eslint-disable-next-line @typescript-eslint/interface-name-prefix,@typescript-eslint/no-empty-interface +export interface IHash extends U8a { } + export type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a'; export interface Constructor { @@ -131,9 +139,6 @@ export interface SignatureOptions { export type ArgsDef = Record; -// eslint-disable-next-line @typescript-eslint/interface-name-prefix,@typescript-eslint/no-empty-interface -export interface IHash extends U8a { } - // eslint-disable-next-line @typescript-eslint/interface-name-prefix export interface IMethod extends Codec { readonly args: Codec[]; @@ -185,7 +190,6 @@ export interface IExtrinsicImpl extends Codec { // eslint-disable-next-line @typescript-eslint/interface-name-prefix export interface IExtrinsic extends ExtrinsicSignatureBase, IMethod { - readonly hash: IHash; readonly length: number; readonly method: Method; readonly type: number;