From 0d2896630df103a7c6085bf08b45583ca4e555a2 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 9 Dec 2019 15:38:07 +0100 Subject: [PATCH] Replace Data/U8a with un-aliassed Raw (#1656) * Replace Data with un-aliassed U8a * Export U8a * Explicitly add U8a to the registry * U8a -> Raw --- CHANGELOG.md | 8 ++++++++ packages/api-contract/src/base/Contract.ts | 2 +- packages/api-contract/src/util.ts | 4 ++-- packages/api/src/query.types.ts | 8 ++++---- packages/api/src/util/filterEvents.ts | 4 ++-- .../src/Decorated/consts/fromMetadata/index.ts | 4 ++-- .../storage/fromMetadata/createFunction.ts | 14 +++++++------- packages/rpc-core/src/index.ts | 8 ++++---- packages/types/README.md | 3 +-- packages/types/src/codec/AbstractArray.ts | 4 ++-- packages/types/src/codec/AbstractInt.ts | 4 ++-- packages/types/src/codec/BTreeMap.ts | 4 ++-- packages/types/src/codec/Base.ts | 4 ++-- packages/types/src/codec/Compact.spec.ts | 2 +- packages/types/src/codec/Enum.spec.ts | 2 +- .../types/src/codec/{U8a.spec.ts => Raw.spec.ts} | 12 ++++++------ packages/types/src/codec/{U8a.ts => Raw.ts} | 10 +++++----- packages/types/src/codec/Set.ts | 4 ++-- packages/types/src/codec/Struct.ts | 4 ++-- packages/types/src/codec/StructAny.ts | 4 ++-- packages/types/src/codec/U8aFixed.ts | 4 ++-- packages/types/src/codec/create/registry.ts | 3 ++- packages/types/src/codec/index.ts | 2 +- packages/types/src/interfaceRegistry.ts | 10 +++++----- .../types/src/interfaces/contracts/definitions.ts | 2 +- packages/types/src/interfaces/contracts/types.ts | 8 ++++---- packages/types/src/primitive/Bytes.ts | 8 ++++---- packages/types/src/primitive/Data.ts | 13 ------------- .../types/src/primitive/Extrinsic/ExtrinsicEra.ts | 4 ++-- .../src/primitive/Extrinsic/ExtrinsicPayload.ts | 6 +++--- .../types/src/primitive/Generic/AccountId.spec.ts | 6 +++--- packages/types/src/primitive/Text.spec.ts | 4 ++-- packages/types/src/primitive/Text.ts | 6 +++--- packages/types/src/primitive/index.ts | 1 - .../src/scripts/generateTypes/interfaceRegistry.ts | 8 +++++++- packages/types/src/scripts/util/formatting.ts | 6 +++--- 36 files changed, 100 insertions(+), 100 deletions(-) rename packages/types/src/codec/{U8a.spec.ts => Raw.spec.ts} (90%) rename packages/types/src/codec/{U8a.ts => Raw.ts} (93%) delete mode 100644 packages/types/src/primitive/Data.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 6496c16d33..7cf066e726 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# 0.99.1 + +- **Breaking change** The `Data` and `U8a` type has been renamed and just replaced with the `Raw` type +- **Breaking change** The `api.derive.staking.info` has been split into 2 - `staking.query` for non-balance related information (mor effective) and `staking.account` that enhances query for all the information previously found `.info` +- Cleanup `DoubleMap` hashing to always hash over the full value (in the case of `Vec`, this includes the length) +- Add additional derives for both council & treasury +- Alignment with latest Polkadot/Substrate master branches + # 0.98.1 - Make the TypeScript generation script mor re-usable (Thanks to @xlc) diff --git a/packages/api-contract/src/base/Contract.ts b/packages/api-contract/src/base/Contract.ts index 4087117abe..8a0cc99e0e 100644 --- a/packages/api-contract/src/base/Contract.ts +++ b/packages/api-contract/src/base/Contract.ts @@ -75,7 +75,7 @@ export default class Contract extends BaseWithTxAndRpc output = message.returnType ? formatData(this.registry, data, message.returnType) - : createType(this.registry, 'Data', data); + : createType(this.registry, 'Raw', data); } return { diff --git a/packages/api-contract/src/util.ts b/packages/api-contract/src/util.ts index e5752b7e2d..7a349d276e 100644 --- a/packages/api-contract/src/util.ts +++ b/packages/api-contract/src/util.ts @@ -4,10 +4,10 @@ import { Codec, Registry, TypeDefInfo, TypeDef } from '@polkadot/types/types'; -import { Data, Option, createClass } from '@polkadot/types'; +import { Option, Raw, createClass } from '@polkadot/types'; import { createTypeUnsafe } from '@polkadot/types/codec'; -export function formatData (registry: Registry, data: Data, { info, type }: TypeDef): Codec { +export function formatData (registry: Registry, data: Raw, { info, type }: TypeDef): Codec { if (info === TypeDefInfo.Option) { return new Option( registry, diff --git a/packages/api/src/query.types.ts b/packages/api/src/query.types.ts index 1246cf7820..d129c01f7c 100644 --- a/packages/api/src/query.types.ts +++ b/packages/api/src/query.types.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { Observable } from 'rxjs'; -import { Option, U8a, Vec } from '@polkadot/types/codec'; +import { Option, Raw, Vec } from '@polkadot/types/codec'; import { Bytes, Null, bool, u32, u64 } from '@polkadot/types'; import { AccountId, AccountIndex, Balance, BalanceOf, BlockNumber, Hash, Index, KeyTypeId, Moment, Perbill, ValidatorId, Weight } from '@polkadot/types/interfaces/runtime'; import { UncleEntryItem } from '@polkadot/types/interfaces/authorship'; @@ -47,10 +47,10 @@ declare module './types' { authorities: StorageEntryExact Observable>>> & QueryableStorageEntry; genesisSlot: StorageEntryExact Observable> & QueryableStorageEntry; currentSlot: StorageEntryExact Observable> & QueryableStorageEntry; - randomness: StorageEntryExact Observable> & QueryableStorageEntry; - nextRandomness: StorageEntryExact Observable> & QueryableStorageEntry; + randomness: StorageEntryExact Observable> & QueryableStorageEntry; + nextRandomness: StorageEntryExact Observable> & QueryableStorageEntry; segmentIndex: StorageEntryExact Observable> & QueryableStorageEntry; - underConstruction: StorageEntryExact Observable>> & QueryableStorageEntry; + underConstruction: StorageEntryExact Observable>> & QueryableStorageEntry; initialized: StorageEntryExact Observable>> & QueryableStorageEntry; }; timestamp: { diff --git a/packages/api/src/util/filterEvents.ts b/packages/api/src/util/filterEvents.ts index cbd9ce414f..c05a10b320 100644 --- a/packages/api/src/util/filterEvents.ts +++ b/packages/api/src/util/filterEvents.ts @@ -4,11 +4,11 @@ import { EventRecord, SignedBlock } from '@polkadot/types/interfaces'; -import { U8a } from '@polkadot/types'; +import { Raw } from '@polkadot/types'; import l from './logging'; -export default function filterEvents (extHash: U8a, { block: { extrinsics, header } }: SignedBlock, allEvents: EventRecord[]): EventRecord[] | undefined { +export default function filterEvents (extHash: Raw, { block: { extrinsics, header } }: SignedBlock, allEvents: EventRecord[]): EventRecord[] | undefined { // extrinsics to hashes const myHash = extHash.toHex(); const allHashes = extrinsics.map((ext): string => ext.hash.toHex()); diff --git a/packages/metadata/src/Decorated/consts/fromMetadata/index.ts b/packages/metadata/src/Decorated/consts/fromMetadata/index.ts index 02a1ecaf57..a12dacdbec 100644 --- a/packages/metadata/src/Decorated/consts/fromMetadata/index.ts +++ b/packages/metadata/src/Decorated/consts/fromMetadata/index.ts @@ -27,8 +27,8 @@ export default function fromMetadata (registry: Registry, metadata: Metadata): C // For access, we change the index names, i.e. Democracy.EnactmentPeriod -> democracy.enactmentPeriod result[stringCamelCase(name.toString())] = moduleMetadata.constants.reduce((newModule: ModuleConstants, meta): ModuleConstants => { - // we convert to Data for the raw data-only bytes - in the case of Bytes, the data has - // a length prefix encoded when received, leading to double-encoding unless removed + // in the case of Bytes, the data has a length prefix encoded when received, + // leading to double-encoding unless removed const type = meta.type.toString(); const codec: ConstantCodec = createTypeUnsafe(registry, type, [ AS_STRIPPED.includes(type) diff --git a/packages/metadata/src/Decorated/storage/fromMetadata/createFunction.ts b/packages/metadata/src/Decorated/storage/fromMetadata/createFunction.ts index c711933a6f..1e38529d08 100644 --- a/packages/metadata/src/Decorated/storage/fromMetadata/createFunction.ts +++ b/packages/metadata/src/Decorated/storage/fromMetadata/createFunction.ts @@ -6,7 +6,7 @@ import { StorageEntryMetadataLatest } from '@polkadot/types/interfaces/metadata' import { Codec, Registry } from '@polkadot/types/types'; import BN from 'bn.js'; -import { Compact, U8a, createType, createTypeUnsafe } from '@polkadot/types/codec'; +import { Compact, Raw, createType, createTypeUnsafe } from '@polkadot/types/codec'; import StorageKey, { StorageEntry } from '@polkadot/types/primitive/StorageKey'; import { assert, isNull, isUndefined, stringLowerFirst, stringToU8a, u8aConcat } from '@polkadot/util'; import { xxhashAsU8a } from '@polkadot/util-crypto'; @@ -27,7 +27,7 @@ export interface CreateItemFn { } interface IterFn { - (): U8a; + (): Raw; meta: StorageEntryMetadataLatest; } @@ -142,7 +142,7 @@ function expandWithMeta ({ meta, method, prefix, section }: CreateItemFn, storag return storageFn; } -function extendHeadMeta (registry: Registry, { meta: { documentation, name, type }, section }: CreateItemFn, { method }: StorageEntry, iterFn: () => U8a): StorageKey { +function extendHeadMeta (registry: Registry, { meta: { documentation, name, type }, section }: CreateItemFn, { method }: StorageEntry, iterFn: () => Raw): StorageKey { const map = type.asMap; const outputType = map.key.toString(); @@ -165,8 +165,8 @@ function extendLinkedMap (registry: Registry, itemFn: CreateItemFn, storageFn: S ? hasher(`head of ${stringKey}`) : u8aConcat(xxhashAsU8a(itemFn.prefix, 128), xxhashAsU8a(`HeadOf${itemFn.method}`, 128)); - storageFn.iterKey = extendHeadMeta(registry, itemFn, storageFn, (): U8a => - new U8a(registry, key) + storageFn.iterKey = extendHeadMeta(registry, itemFn, storageFn, (): Raw => + new Raw(registry, key) ); return storageFn; @@ -174,8 +174,8 @@ function extendLinkedMap (registry: Registry, itemFn: CreateItemFn, storageFn: S // attach the full list hashing for prefixed maps function extendPrefixedMap (registry: Registry, itemFn: CreateItemFn, storageFn: StorageEntry): StorageEntry { - storageFn.iterKey = extendHeadMeta(registry, itemFn, storageFn, (): U8a => - new U8a(registry, createPrefixedKey(itemFn)) + storageFn.iterKey = extendHeadMeta(registry, itemFn, storageFn, (): Raw => + new Raw(registry, createPrefixedKey(itemFn)) ); return storageFn; diff --git a/packages/rpc-core/src/index.ts b/packages/rpc-core/src/index.ts index b323fbcb7d..9e49a85458 100644 --- a/packages/rpc-core/src/index.ts +++ b/packages/rpc-core/src/index.ts @@ -359,8 +359,8 @@ export default class Rpc implements RpcInterface { private formatStorageData (key: StorageKey, value: string | null): Codec { // single return value (via state.getStorage), decode the value based on the - // outputType that we have specified. Fallback to Data on nothing - const type = key.outputType || 'Data'; + // outputType that we have specified. Fallback to Raw on nothing + const type = key.outputType || 'Raw'; const meta = key.meta || EMPTY_META; const isEmpty = isNull(value); @@ -386,8 +386,8 @@ export default class Rpc implements RpcInterface { } private formatStorageSet (key: StorageKey, changes: [string, string | null][], witCache: boolean): Codec { - // Fallback to Data (i.e. just the encoding) if we don't have a specific type - const type = key.outputType || 'Data'; + // Fallback to Raw (i.e. just the encoding) if we don't have a specific type + const type = key.outputType || 'Raw'; const hexKey = key.toHex(); const meta = key.meta || EMPTY_META; diff --git a/packages/types/README.md b/packages/types/README.md index 0a33bad741..2df1d0393a 100644 --- a/packages/types/README.md +++ b/packages/types/README.md @@ -17,7 +17,7 @@ These are the base types of the codec. They are typically not used directly, but | [[Set]] | An Set is an array of string values, represented an an encoded type by a bitwise representation of the values | | [[Struct]] | A Struct defines an Object with key-value pairs - where the values are Codec values. | | [[Tuple]] | A Tuple defines an anonymous fixed-length array, where each element has its own type | -| [[U8a]] | A basic wrapper around Uint8Array. It will consume the full Uint8Array as passed to it | +| [[Raw]] | A basic wrapper around Uint8Array. It will consume the full Uint8Array as passed to it | | [[U8aFixed]] | A U8a that manages a a sequence of bytes up to the specified bitLength | | [[UInt]] | A generic unsigned integer codec. It handles the encoding and decoding of Little Endian encoded numbers in Substrate | | [[Vec]] | This manages codec arrays. Internally it keeps track of the length (as decoded) | @@ -36,7 +36,6 @@ These primitive types are available: | [[bool]] | Representation for a boolean value in the system | | [[Bytes]] | A Bytes wrapper for `Vec` | | [[Call]] | Extrinsic function descriptor, as defined in [the extrinsic format for a node](https://github.com/paritytech/wiki/blob/master/Extrinsic.md#the-extrinsic-format-for-node) | -| [[Data]] | A raw data structure. It is an encoding of a U8a without any length encoding | | [[Event]] | Wrapper for the actual data that forms part of an [[Event]] | | [[EventRecord]] | A record for an [[Event]] (as specified by [[Metadata]]) with the specific [[Phase]] of application | | [[Extrinsic]] | Representation of an Extrinsic in the system | diff --git a/packages/types/src/codec/AbstractArray.ts b/packages/types/src/codec/AbstractArray.ts index 9cc5b897fb..ca4f6bc2a5 100644 --- a/packages/types/src/codec/AbstractArray.ts +++ b/packages/types/src/codec/AbstractArray.ts @@ -8,7 +8,7 @@ import { u8aConcat, u8aToHex } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; import Compact from './Compact'; -import U8a from './U8a'; +import Raw from './Raw'; import { compareArray } from './utils'; /** @@ -40,7 +40,7 @@ export default abstract class AbstractArray extends Array im * @description returns a hash of the contents */ public get hash (): IHash { - return new U8a(this.registry, blake2AsU8a(this.toU8a(), 256)); + return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256)); } /** diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index d71b951af0..1cb9cb0792 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -8,7 +8,7 @@ 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'; +import Raw from './Raw'; export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256; @@ -78,7 +78,7 @@ export default abstract class AbstractInt extends BN implements Codec { * @description returns a hash of the contents */ public get hash (): IHash { - return new U8a(this.registry, blake2AsU8a(this.toU8a(), 256)); + return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256)); } /** diff --git a/packages/types/src/codec/BTreeMap.ts b/packages/types/src/codec/BTreeMap.ts index 79a492b7f4..00147beb40 100644 --- a/packages/types/src/codec/BTreeMap.ts +++ b/packages/types/src/codec/BTreeMap.ts @@ -8,7 +8,7 @@ import { isHex, hexToU8a, isU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot import { blake2AsU8a } from '@polkadot/util-crypto'; import Compact from './Compact'; -import U8a from './U8a'; +import Raw from './Raw'; import { compareMap, decodeU8a, typeToConstructor } from './utils'; export default class BTreeMap extends Map implements Codec { @@ -119,7 +119,7 @@ export default class BTreeMap * @description Returns a hash of the value */ public get hash (): IHash { - return new U8a(this.registry, blake2AsU8a(this.toU8a(), 256)); + return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256)); } /** diff --git a/packages/types/src/codec/Base.ts b/packages/types/src/codec/Base.ts index e319407db0..38521ecc71 100644 --- a/packages/types/src/codec/Base.ts +++ b/packages/types/src/codec/Base.ts @@ -6,7 +6,7 @@ import { AnyJson, BareOpts, Codec, IHash, Registry } from '../types'; import { blake2AsU8a } from '@polkadot/util-crypto'; -import U8a from './U8a'; +import Raw from './Raw'; /** * @name Base @@ -33,7 +33,7 @@ export default abstract class Base implements Codec { * @description returns a hash of the contents */ public get hash (): IHash { - return new U8a(this.registry, blake2AsU8a(this.toU8a(), 256)); + return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256)); } /** diff --git a/packages/types/src/codec/Compact.spec.ts b/packages/types/src/codec/Compact.spec.ts index 9d7dce75cf..44fb8d359a 100644 --- a/packages/types/src/codec/Compact.spec.ts +++ b/packages/types/src/codec/Compact.spec.ts @@ -131,7 +131,7 @@ describe('Compact', (): void => { ).toEqual(4); }); - it('constructs properly via U8a as U32', (): void => { + it('constructs properly via Uint8Array as U32', (): void => { expect( new (Compact.with(U32))(registry, new Uint8Array([254, 255, 3, 0])).toNumber() ).toEqual(new BN(0xffff).toNumber()); diff --git a/packages/types/src/codec/Enum.spec.ts b/packages/types/src/codec/Enum.spec.ts index 6aa6eef66e..643c4ac042 100644 --- a/packages/types/src/codec/Enum.spec.ts +++ b/packages/types/src/codec/Enum.spec.ts @@ -208,7 +208,7 @@ describe('Enum', (): void => { ).toEqual(1); }); - it('converts to and from U8a', (): void => { + it('converts to and from Uint8Array', (): void => { expect( new Enum(registry, ['foo', 'bar'], new Uint8Array([1])).toU8a() ).toEqual(new Uint8Array([1])); diff --git a/packages/types/src/codec/U8a.spec.ts b/packages/types/src/codec/Raw.spec.ts similarity index 90% rename from packages/types/src/codec/U8a.spec.ts rename to packages/types/src/codec/Raw.spec.ts index 7b6007f73e..bced23e44a 100644 --- a/packages/types/src/codec/U8a.spec.ts +++ b/packages/types/src/codec/Raw.spec.ts @@ -5,29 +5,29 @@ import { CodecTo } from '../types'; import { TypeRegistry } from './create'; -import U8a from './U8a'; +import Raw from './Raw'; const registry = new TypeRegistry(); const testDecode = (type: string, input: any, expected: string): void => it(`can decode from ${type}`, (): void => { - const e = new U8a(registry, input); + const e = new Raw(registry, input); expect(e.toString()).toBe(expected); }); const testEncode = (to: CodecTo, expected: any): void => it(`can encode ${to}`, (): void => { - const e = new U8a(registry, [1, 2, 3, 4, 5]); + const e = new Raw(registry, [1, 2, 3, 4, 5]); expect(e[to]()).toEqual(expected); }); describe('U8a', (): void => { - let u8a: U8a; + let u8a: Raw; beforeEach((): void => { - u8a = new U8a(registry, [1, 2, 3, 4, 5]); + u8a = new Raw(registry, [1, 2, 3, 4, 5]); }); testDecode('Array', [1, 2, 3, 4, 5], '0x0102030405'); @@ -50,7 +50,7 @@ describe('U8a', (): void => { it('allows wrapping of a pre-existing instance', (): void => { expect( - new U8a(registry, u8a).length + new Raw(registry, u8a).length ).toEqual(5); }); diff --git a/packages/types/src/codec/U8a.ts b/packages/types/src/codec/Raw.ts similarity index 93% rename from packages/types/src/codec/U8a.ts rename to packages/types/src/codec/Raw.ts index 419fce056c..8b936f377d 100644 --- a/packages/types/src/codec/U8a.ts +++ b/packages/types/src/codec/Raw.ts @@ -8,7 +8,7 @@ import { isU8a, isUndefined, u8aToHex, u8aToU8a } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; /** - * @name U8a + * @name Raw * @description * A basic wrapper around Uint8Array, with no frills and no fuss. It does differ * from other implementations where it will consume the full Uint8Array as passed to it. @@ -16,11 +16,11 @@ import { blake2AsU8a } from '@polkadot/util-crypto'; * actual lengths instead of used directly. * @noInheritDoc */ -export default class U8a extends Uint8Array implements IU8a { +export default class Raw extends Uint8Array implements IU8a { public readonly registry: Registry; constructor (registry: Registry, value?: AnyU8a) { - super(U8a.decodeU8a(value)); + super(Raw.decodeU8a(value)); this.registry = registry; } @@ -44,7 +44,7 @@ export default class U8a extends Uint8Array implements IU8a { * @description returns a hash of the contents */ public get hash (): IHash { - return new U8a(this.registry, blake2AsU8a(this.toU8a(), 256)); + return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256)); } /** @@ -78,7 +78,7 @@ export default class U8a extends Uint8Array implements IU8a { !this.some((value, index): boolean => value !== other[index]); } - return this.eq(U8a.decodeU8a(other)); + return this.eq(Raw.decodeU8a(other)); } /** diff --git a/packages/types/src/codec/Set.ts b/packages/types/src/codec/Set.ts index beee9f1368..cfe6860a5b 100644 --- a/packages/types/src/codec/Set.ts +++ b/packages/types/src/codec/Set.ts @@ -7,7 +7,7 @@ import { Codec, Constructor, IHash, Registry } from '../types'; import { assert, isU8a, isNumber, isUndefined, stringCamelCase, stringUpperFirst, u8aToHex } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; -import U8a from './U8a'; +import Raw from './Raw'; import { compareArray } from './utils'; type SetValues = Record; @@ -109,7 +109,7 @@ export default class CodecSet extends Set implements Codec { * @description returns a hash of the contents */ public get hash (): IHash { - return new U8a(this.registry, blake2AsU8a(this.toU8a(), 256)); + return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256)); } /** diff --git a/packages/types/src/codec/Struct.ts b/packages/types/src/codec/Struct.ts index 05d4d220e5..d23174d966 100644 --- a/packages/types/src/codec/Struct.ts +++ b/packages/types/src/codec/Struct.ts @@ -7,7 +7,7 @@ import { AnyJsonObject, BareOpts, Codec, Constructor, ConstructorDef, IHash, Int import { hexToU8a, isBoolean, isHex, isObject, isU8a, isUndefined, u8aConcat, u8aToHex } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; -import U8a from './U8a'; +import Raw from './Raw'; import { compareMap, decodeU8a, mapToTypeMap } from './utils'; type TypesDef = Record>; @@ -180,7 +180,7 @@ export default class Struct< * @description returns a hash of the contents */ public get hash (): IHash { - return new U8a(this.registry, blake2AsU8a(this.toU8a(), 256)); + return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256)); } /** diff --git a/packages/types/src/codec/StructAny.ts b/packages/types/src/codec/StructAny.ts index 7a8bc8b075..9a4b9dc1ae 100644 --- a/packages/types/src/codec/StructAny.ts +++ b/packages/types/src/codec/StructAny.ts @@ -7,7 +7,7 @@ import { AnyJsonObject, Codec, IHash, Registry } from '../types'; import { isUndefined } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; -import U8a from './U8a'; +import Raw from './Raw'; import { compareMap } from './utils'; @@ -58,7 +58,7 @@ export default class StructAny extends Map implements Codec { * @description returns a hash of the contents */ public get hash (): IHash { - return new U8a(this.registry, blake2AsU8a(this.toU8a(), 256)); + return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256)); } /** diff --git a/packages/types/src/codec/U8aFixed.ts b/packages/types/src/codec/U8aFixed.ts index ae9defc727..e0cf13539f 100644 --- a/packages/types/src/codec/U8aFixed.ts +++ b/packages/types/src/codec/U8aFixed.ts @@ -6,7 +6,7 @@ import { isString, u8aToU8a } from '@polkadot/util'; import { AnyU8a, Constructor, Registry } from '../types'; -import U8a from './U8a'; +import Raw from './Raw'; // The 520 here is a weird one - it is explicitly for a [u8; 65] as found as a EcdsaSignature // Likewise 160 is for [u8; 20], which is also a H160, i.e. an Ethereum address. Both these are @@ -19,7 +19,7 @@ export type BitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 512 | 520 | 1024 | * A U8a that manages a a sequence of bytes up to the specified bitLength. Not meant * to be used directly, rather is should be subclassed with the specific lengths. */ -export default class U8aFixed extends U8a { +export default class U8aFixed extends Raw { constructor (registry: Registry, value: AnyU8a = new Uint8Array(), bitLength: BitLength = 256) { super(registry, U8aFixed.decodeU8aFixed(value, bitLength)); } diff --git a/packages/types/src/codec/create/registry.ts b/packages/types/src/codec/create/registry.ts index 0e2c9fffdb..896eaaca08 100644 --- a/packages/types/src/codec/create/registry.ts +++ b/packages/types/src/codec/create/registry.ts @@ -8,6 +8,7 @@ import { CallFunction, Codec, Constructor, RegistryTypes, Registry, RegistryMeta import extrinsicsFromMeta from '@polkadot/metadata/Decorated/extrinsics/fromMetadata'; import { assert, isFunction, isString, isUndefined, stringCamelCase, u8aToHex } from '@polkadot/util'; +import Raw from '../Raw'; import { EventData } from '../../primitive/Generic/Event'; import { createClass } from './createClass'; import { getTypeClass } from './getTypeClass'; @@ -76,7 +77,7 @@ export class TypeRegistry implements Registry { const definitions: Record = require('../../interfaces/definitions'); // since these are classes, they are injected first - this.register({ ...baseTypes }); + this.register({ Raw, ...baseTypes }); // since these are definitions, they would only get created when needed Object.values(definitions).forEach(({ types }): void => diff --git a/packages/types/src/codec/index.ts b/packages/types/src/codec/index.ts index 9672f62c38..dab67fc609 100644 --- a/packages/types/src/codec/index.ts +++ b/packages/types/src/codec/index.ts @@ -21,7 +21,7 @@ export { default as Tuple } from './Tuple'; export { default as Vec } from './Vec'; // Convenience base classes, used as "anything of this type" bases -export { default as U8a } from './U8a'; +export { default as Raw } from './Raw'; export { default as UInt } from './UInt'; // Type management helper functions diff --git a/packages/types/src/interfaceRegistry.ts b/packages/types/src/interfaceRegistry.ts index 3144da6cdb..13ad5836a2 100644 --- a/packages/types/src/interfaceRegistry.ts +++ b/packages/types/src/interfaceRegistry.ts @@ -1,8 +1,8 @@ // Auto-generated via `yarn build:interfaces`, do not edit /* eslint-disable @typescript-eslint/no-empty-interface */ -import { Compact, Option, Vec } from '@polkadot/types/codec'; -import { Bytes, Data, Fixed64, H160, H256, H512, Null, StorageData, StorageHasher, StorageKey, Text, Type, U256, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from '@polkadot/types/primitive'; +import { Compact, Option, Raw, Vec } from '@polkadot/types/codec'; +import { Bytes, Fixed64, H160, H256, H512, Null, StorageData, StorageHasher, StorageKey, Text, Type, U256, bool, i128, i16, i256, i32, i64, i8, u128, u16, u256, u32, u64, u8, usize } from '@polkadot/types/primitive'; import { AccountId, AccountIdOf, AccountIndex, Address, AssetId, Balance, BalanceOf, Block, BlockNumber, Call, Consensus, ConsensusEngineId, Digest, DigestItem, DispatchClass, DispatchInfo, EcdsaSignature, Ed25519Signature, Extrinsic, ExtrinsicEra, ExtrinsicPayload, ExtrinsicPayloadUnknown, ExtrinsicPayloadV1, ExtrinsicPayloadV2, ExtrinsicPayloadV3, ExtrinsicPayloadV4, ExtrinsicUnknown, ExtrinsicV1, ExtrinsicV2, ExtrinsicV3, ExtrinsicV4, Hash, Header, ImmortalEra, Index, Justification, KeyTypeId, KeyValue, LockIdentifier, Moment, MortalEra, MultiSignature, Origin, Perbill, Permill, Phantom, PhantomData, PreRuntime, Seal, SealV0, Signature, SignedBlock, SignerPayload, Sr25519Signature, ValidatorId, Weight, WeightMultiplier } from '@polkadot/types/interfaces/runtime'; import { InclusionHeight, Uncle, UncleEntryItem } from '@polkadot/types/interfaces/authorship'; import { RawAuraPreDigest } from '@polkadot/types/interfaces/aura'; @@ -37,9 +37,6 @@ export interface InterfaceRegistry { Bytes: Bytes; 'Option': Option; 'Vec': Vec; - Data: Data; - 'Option': Option; - 'Vec': Vec; H160: H160; 'Option': Option; 'Vec': Vec; @@ -120,6 +117,9 @@ export interface InterfaceRegistry { 'Compact': Compact; 'Option': Option; 'Vec': Vec; + Raw: Raw; + 'Option': Option; + 'Vec': Vec; AccountId: AccountId; 'Option': Option; 'Vec': Vec; diff --git a/packages/types/src/interfaces/contracts/definitions.ts b/packages/types/src/interfaces/contracts/definitions.ts index 3d04416440..4862b25b55 100644 --- a/packages/types/src/interfaces/contracts/definitions.ts +++ b/packages/types/src/interfaces/contracts/definitions.ts @@ -22,7 +22,7 @@ export default { }, ContractExecResultSuccess: { status: 'u8', - data: 'Data' + data: 'Raw' }, ContractExecResult: { _enum: { diff --git a/packages/types/src/interfaces/contracts/types.ts b/packages/types/src/interfaces/contracts/types.ts index 374831600f..03f5b160fd 100644 --- a/packages/types/src/interfaces/contracts/types.ts +++ b/packages/types/src/interfaces/contracts/types.ts @@ -2,8 +2,8 @@ /* eslint-disable @typescript-eslint/no-empty-interface */ import { Codec } from '@polkadot/types/types'; -import { Compact, Enum, Option, Struct } from '@polkadot/types/codec'; -import { Bytes, Data, Null, bool, u32, u64, u8 } from '@polkadot/types/primitive'; +import { Compact, Enum, Option, Raw, Struct } from '@polkadot/types/codec'; +import { Bytes, Null, bool, u32, u64, u8 } from '@polkadot/types/primitive'; import { AccountId, Balance, BlockNumber, Hash } from '@polkadot/types/interfaces/runtime'; /** Struct */ @@ -53,8 +53,8 @@ export interface ContractExecResult extends Enum { export interface ContractExecResultSuccess extends Struct { /** u8 */ readonly status: u8; - /** Data */ - readonly data: Data; + /** Raw */ + readonly data: Raw; } /** Enum */ diff --git a/packages/types/src/primitive/Bytes.ts b/packages/types/src/primitive/Bytes.ts index 272d3ee9d2..116f35c76f 100644 --- a/packages/types/src/primitive/Bytes.ts +++ b/packages/types/src/primitive/Bytes.ts @@ -7,7 +7,7 @@ import { AnyU8a, Registry } from '../types'; import { assert, isString, isU8a, u8aToU8a } from '@polkadot/util'; import Compact from '../codec/Compact'; -import U8a from '../codec/U8a'; +import Raw from '../codec/Raw'; /** * @name Bytes @@ -16,7 +16,7 @@ import U8a from '../codec/U8a'; * is that this version allows for length-encoding. (i.e. it is a variable-item codec, the same * as what is found in [[Text]] and [[Vec]]) */ -export default class Bytes extends U8a { +export default class Bytes extends Raw { constructor (registry: Registry, value?: AnyU8a) { super(registry, Bytes.decodeBytes(value)); } @@ -24,8 +24,8 @@ export default class Bytes extends U8a { private static decodeBytes (value?: AnyU8a): Uint8Array | undefined { if (Array.isArray(value) || isString(value)) { return u8aToU8a(value); - } else if (!(value instanceof U8a) && isU8a(value)) { - // We are ensuring we are not a U8a instance. In the case of a U8a we already have gotten + } else if (!(value instanceof Raw) && isU8a(value)) { + // We are ensuring we are not a Rawv instance. In the case of a Raw we already have gotten // rid of the length, i.e. new Bytes(new Bytes(...)) will work as expected return Bytes.decodeBytesU8a(value); } diff --git a/packages/types/src/primitive/Data.ts b/packages/types/src/primitive/Data.ts deleted file mode 100644 index 5a58674477..0000000000 --- a/packages/types/src/primitive/Data.ts +++ /dev/null @@ -1,13 +0,0 @@ -// 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 U8a from '../codec/U8a'; - -/** - * @name Data - * @description - * A raw data structure. It is just an encoding of a U8a, without any length encoding - */ -export default class Data extends U8a { -} diff --git a/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts b/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts index b77e4ad685..37702ea996 100644 --- a/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts +++ b/packages/types/src/primitive/Extrinsic/ExtrinsicEra.ts @@ -9,7 +9,7 @@ import { assert, bnToBn, hexToU8a, isHex, isU8a, isObject, u8aToBn } from '@polk import Enum from '../../codec/Enum'; import Tuple from '../../codec/Tuple'; -import U8a from '../../codec/U8a'; +import Raw from '../../codec/Raw'; import U64 from '../U64'; import { IMMORTAL_ERA } from './constants'; @@ -33,7 +33,7 @@ interface ImmortalEnumDef { * @description * The ImmortalEra for an extrinsic */ -export class ImmortalEra extends U8a { +export class ImmortalEra extends Raw { // eslint-disable-next-line @typescript-eslint/no-unused-vars constructor (registry: Registry, value?: AnyU8a) { // For immortals, we always provide the known value (i.e. treated as a diff --git a/packages/types/src/primitive/Extrinsic/ExtrinsicPayload.ts b/packages/types/src/primitive/Extrinsic/ExtrinsicPayload.ts index 76e934c4f7..41a94d969e 100644 --- a/packages/types/src/primitive/Extrinsic/ExtrinsicPayload.ts +++ b/packages/types/src/primitive/Extrinsic/ExtrinsicPayload.ts @@ -10,7 +10,7 @@ import { u8aToHex } from '@polkadot/util'; import { createType } from '../../codec/create'; import Base from '../../codec/Base'; import Compact from '../../codec/Compact'; -import U8a from '../../codec/U8a'; +import Raw from '../../codec/Raw'; import u32 from '../../primitive/U32'; import ExtrinsicEra from './ExtrinsicEra'; import { DEFAULT_VERSION } from './constants'; @@ -72,9 +72,9 @@ export default class ExtrinsicPayload extends Base { } /** - * @description The [[U8a]] contained in the payload + * @description The [[Raw]] contained in the payload */ - public get method (): U8a { + public get method (): Raw { return this.raw.method; } diff --git a/packages/types/src/primitive/Generic/AccountId.spec.ts b/packages/types/src/primitive/Generic/AccountId.spec.ts index e95b6939c9..e66947a2d0 100644 --- a/packages/types/src/primitive/Generic/AccountId.spec.ts +++ b/packages/types/src/primitive/Generic/AccountId.spec.ts @@ -5,7 +5,7 @@ import { setSS58Format } from '@polkadot/util-crypto'; import { createType, TypeRegistry } from '../../codec/create'; -import U8a from '../../codec/U8a'; +import Raw from '../../codec/Raw'; import jsonVec from '../../json/AccountIdVec.001.json'; import AccountId from './AccountId'; @@ -43,8 +43,8 @@ describe('AccountId', (): void => { testDecode('hex', '0x0102030405060708010203040506070801020304050607080102030405060708', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'); testDecode('string', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF', '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCtGwF'); testDecode( - 'U8a', - new U8a(registry, [ + 'Raw', + new Raw(registry, [ 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 ]), diff --git a/packages/types/src/primitive/Text.spec.ts b/packages/types/src/primitive/Text.spec.ts index 06fb673c53..2fb796771b 100644 --- a/packages/types/src/primitive/Text.spec.ts +++ b/packages/types/src/primitive/Text.spec.ts @@ -4,7 +4,7 @@ import { CodecTo } from '../types'; import { TypeRegistry } from '../codec/create'; -import U8a from '../codec/U8a'; +import Raw from '../codec/Raw'; import Text from './Text'; describe('Text', (): void => { @@ -19,7 +19,7 @@ describe('Text', (): void => { testDecode('string', 'foo', 'foo'); testDecode('Text', new Text(registry, 'foo'), 'foo'); testDecode('Uint8Array', Uint8Array.from([12, 102, 111, 111]), 'foo'); - testDecode('U8a', new U8a(registry, Uint8Array.from([102, 111, 111])), 'foo'); // no length + testDecode('Raw', new Raw(registry, Uint8Array.from([102, 111, 111])), 'foo'); // no length testDecode('object with `toString()`', { toString (): string { return 'foo'; } }, 'foo'); testDecode('hex input value', new Text(registry, '0x12345678'), '0x12345678', 'toHex'); }); diff --git a/packages/types/src/primitive/Text.ts b/packages/types/src/primitive/Text.ts index f0c394371a..3dfd870805 100644 --- a/packages/types/src/primitive/Text.ts +++ b/packages/types/src/primitive/Text.ts @@ -9,7 +9,7 @@ import { blake2AsU8a } from '@polkadot/util-crypto'; import { createType } from '../codec/create'; import Compact from '../codec/Compact'; -import U8a from '../codec/U8a'; +import Raw from '../codec/Raw'; /** * @name Text @@ -38,9 +38,9 @@ export default class Text extends String implements Codec { return ''; } - // for U8a, the internal buffer does not have an internal length + // for Raw, the internal buffer does not have an internal length // (the same applies in e.g. Bytes, where length is added at encoding-time) - if (value instanceof U8a) { + if (value instanceof Raw) { return u8aToString(value); } diff --git a/packages/types/src/primitive/index.ts b/packages/types/src/primitive/index.ts index dfae7f2eb0..b78d039bf9 100644 --- a/packages/types/src/primitive/index.ts +++ b/packages/types/src/primitive/index.ts @@ -10,7 +10,6 @@ */ export { default as bool } from './Bool'; export { default as Bytes } from './Bytes'; -export { default as Data } from './Data'; export { default as H160 } from './H160'; export { default as H256 } from './H256'; export { default as H512 } from './H512'; diff --git a/packages/types/src/scripts/generateTypes/interfaceRegistry.ts b/packages/types/src/scripts/generateTypes/interfaceRegistry.ts index 045e53a777..f889ad3f09 100644 --- a/packages/types/src/scripts/generateTypes/interfaceRegistry.ts +++ b/packages/types/src/scripts/generateTypes/interfaceRegistry.ts @@ -4,8 +4,9 @@ import fs from 'fs'; +import Raw from '../../codec/Raw'; import * as defaultDefinitions from '../../interfaces/definitions'; -import * as primitiveClasses from '../../primitive'; +import * as defaultPrimitives from '../../primitive'; import { createImportCode, createImports, FOOTER, @@ -15,6 +16,11 @@ import { setImports } from '../util'; +const primitiveClasses = { + ...defaultPrimitives, + Raw +}; + export function generateInterfaceRegistry (importDefinitions: { [importPath: string]: object }, output: string): void { console.log(`Writing ${output}`); diff --git a/packages/types/src/scripts/util/formatting.ts b/packages/types/src/scripts/util/formatting.ts index ddab706b20..94014be51f 100644 --- a/packages/types/src/scripts/util/formatting.ts +++ b/packages/types/src/scripts/util/formatting.ts @@ -116,10 +116,10 @@ export function formatType (definitions: object, type: string | TypeDef, imports } case TypeDefInfo.VecFixed: { if ((typeDef.ext as TypeDefExtVecFixed).type === 'u8') { - setImports(definitions, imports, ['U8a']); + setImports(definitions, imports, ['Raw']); - // `[u8, 32]` gets transformed into U8a - return 'U8a'; + // `[u8, 32]` gets transformed into Raw + return 'Raw'; } setImports(definitions, imports, ['Vec']); return formatVec(formatType(definitions, (typeDef.ext as TypeDefExtVecFixed).type, imports));