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
This commit is contained in:
Jaco Greeff
2019-07-26 11:20:34 +02:00
committed by GitHub
parent 700eb7e1f4
commit a0d7a52a5c
16 changed files with 151 additions and 65 deletions
+18 -8
View File
@@ -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<T extends Codec> extends Array<T> 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<T extends Codec> extends Array<T> 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
*/
+11 -1
View File
@@ -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)
*/
+12 -1
View File
@@ -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<T extends Codec> 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
*/
+12 -2
View File
@@ -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<string, number>;
@@ -91,6 +94,13 @@ export default class CodecSet extends Set<string> 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
*/
+12 -2
View File
@@ -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
*/
+11 -1
View File
@@ -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<string, any> 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
*/
+17 -2
View File
@@ -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
*/
-8
View File
@@ -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}]`;
}
}
+12 -2
View File
@@ -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)
*/
@@ -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<ExtrinsicV1 | ExtrinsicV2> 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]])
*/
+1 -10
View File
@@ -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)
*/
+1 -10
View File
@@ -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)
*/
+11 -1
View File
@@ -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
*/
+12 -1
View File
@@ -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)
*/
+12 -2
View File
@@ -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
*/
+8 -4
View File
@@ -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<T = Codec> {
@@ -131,9 +139,6 @@ export interface SignatureOptions {
export type ArgsDef = Record<string, Constructor>;
// 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;