Replace Data/U8a with un-aliassed Raw (#1656)

* Replace Data with un-aliassed U8a

* Export U8a

* Explicitly add U8a to the registry

* U8a -> Raw
This commit is contained in:
Jaco Greeff
2019-12-09 15:38:07 +01:00
committed by GitHub
parent 74dbff32df
commit 0d2896630d
36 changed files with 100 additions and 100 deletions
+2 -2
View File
@@ -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<T extends Codec> extends Array<T> 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));
}
/**
+2 -2
View File
@@ -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));
}
/**
+2 -2
View File
@@ -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<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements Codec {
@@ -119,7 +119,7 @@ export default class BTreeMap<K extends Codec = Codec, V extends Codec = Codec>
* @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));
}
/**
+2 -2
View File
@@ -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<T extends Codec> 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));
}
/**
+1 -1
View File
@@ -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());
+1 -1
View File
@@ -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]));
@@ -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);
});
@@ -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));
}
/**
+2 -2
View File
@@ -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<string, number>;
@@ -109,7 +109,7 @@ export default class CodecSet extends Set<string> 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));
}
/**
+2 -2
View File
@@ -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<T = Codec> = Record<string, InterfaceTypes | Constructor<T>>;
@@ -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));
}
/**
+2 -2
View File
@@ -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<string, any> 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));
}
/**
+2 -2
View File
@@ -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));
}
+2 -1
View File
@@ -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<string, { types: RegistryTypes }> = 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 =>
+1 -1
View File
@@ -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
+5 -5
View File
@@ -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<Bytes>': Option<Bytes>;
'Vec<Bytes>': Vec<Bytes>;
Data: Data;
'Option<Data>': Option<Data>;
'Vec<Data>': Vec<Data>;
H160: H160;
'Option<H160>': Option<H160>;
'Vec<H160>': Vec<H160>;
@@ -120,6 +117,9 @@ export interface InterfaceRegistry {
'Compact<usize>': Compact<usize>;
'Option<usize>': Option<usize>;
'Vec<usize>': Vec<usize>;
Raw: Raw;
'Option<Raw>': Option<Raw>;
'Vec<Raw>': Vec<Raw>;
AccountId: AccountId;
'Option<AccountId>': Option<AccountId>;
'Vec<AccountId>': Vec<AccountId>;
@@ -22,7 +22,7 @@ export default {
},
ContractExecResultSuccess: {
status: 'u8',
data: 'Data'
data: 'Raw'
},
ContractExecResult: {
_enum: {
@@ -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 */
+4 -4
View File
@@ -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);
}
-13
View File
@@ -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 {
}
@@ -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
@@ -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<ExtrinsicPayloadVx> {
}
/**
* @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;
}
@@ -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
]),
+2 -2
View File
@@ -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');
});
+3 -3
View File
@@ -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);
}
-1
View File
@@ -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';
@@ -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}`);
@@ -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));