From d34eb153f988e5bd751477e5c74b90cfcc2d2c72 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 23 Jul 2019 22:13:28 +0200 Subject: [PATCH] Add `readonly hash: Hash` to Method/Proposal (#1169) --- packages/types/src/index.ts | 14 +++++++++++++- packages/types/src/injector.ts | 7 +++++++ packages/types/src/primitive/Method.ts | 11 +++++++++++ packages/types/src/types.ts | 1 + 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 packages/types/src/injector.ts diff --git a/packages/types/src/index.ts b/packages/types/src/index.ts index b88317cd24..80d03abd3b 100644 --- a/packages/types/src/index.ts +++ b/packages/types/src/index.ts @@ -2,11 +2,23 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -// NOTE When adding any types here, we need to update the coumentation links as +// NOTE When adding any types here, we need to update the documentation links as // well - /docs/SUMMARY.md as well as ../README.md import { assertSingletonPackage } from '@polkadot/util'; +// HACK Register our types before anything else - if just means that any use of +// createType (either here, like in derive) or in users of this app, will have +// the type available without resorting to juggling imports themselves. +// +// This was found here in api-derive (see ReferendumInfoExtended) as well in apps, +// where `createType` was used on constant definitions, but before the actual API +// has been injected (which registers the srml types). +// +// It needs resolution, but atm, no worse than having classes that are statically +// defined, so really, eating the elephant one bite at a time... +import './injector'; + assertSingletonPackage('@polkadot/types'); export * from './codec'; diff --git a/packages/types/src/injector.ts b/packages/types/src/injector.ts new file mode 100644 index 0000000000..d717c67835 --- /dev/null +++ b/packages/types/src/injector.ts @@ -0,0 +1,7 @@ +// Copyright 2017-2019 @polkadot/types authors & contributors +// This software may be modified and distributed under the terms +// of the Apache-2.0 license. See the LICENSE file for details. + +import { injectDefinitions } from './srml'; + +injectDefinitions(); diff --git a/packages/types/src/primitive/Method.ts b/packages/types/src/primitive/Method.ts index bf3bd6488c..95312656d1 100644 --- a/packages/types/src/primitive/Method.ts +++ b/packages/types/src/primitive/Method.ts @@ -5,11 +5,13 @@ import { AnyU8a, ArgsDef, Codec, IMethod } from '../types'; import { assert, isHex, isObject, isU8a, hexToU8a } from '@polkadot/util'; +import { blake2AsU8a } from '@polkadot/util-crypto'; import { getTypeDef, getTypeClass } from '../codec/createType'; import Struct from '../codec/Struct'; import U8aFixed from '../codec/U8aFixed'; import { FunctionMetadata as FunctionMetadataV6, FunctionArgumentMetadata } from '../Metadata/v6/Calls'; +import Hash from './Hash'; interface DecodeMethodInput { args: any; @@ -204,6 +206,15 @@ 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 new Hash( + blake2AsU8a(this.toU8a(), 256) + ); + } + /** * @description `true` if the `Origin` type is on the method (extrinsic method) */ diff --git a/packages/types/src/types.ts b/packages/types/src/types.ts index dbaf74bf81..663c411aed 100644 --- a/packages/types/src/types.ts +++ b/packages/types/src/types.ts @@ -139,6 +139,7 @@ export interface IMethod extends Codec { readonly argsDef: ArgsDef; readonly callIndex: Uint8Array; readonly data: Uint8Array; + readonly hash: IHash; readonly hasOrigin: boolean; readonly meta: FunctionMetadata; }