From d0ff8937a45bd8a60398e5c12088885c23b0508c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 21 Nov 2018 17:21:44 +0100 Subject: [PATCH] Use compact* from util for encoding (#409) * Use compact* from util for encoding * Remove @polkadot/extrinsics from types imports * Cleanup manual length concat * Add missing @polkadot/types dependencies --- packages/api-observable/src/Base.ts | 2 +- packages/api/src/Base.ts | 2 +- packages/api/src/promise/index.ts | 2 +- packages/api/src/promise/types.d.ts | 6 +- packages/api/src/rx/index.ts | 4 +- packages/api/src/rx/types.d.ts | 6 +- packages/api/src/types.d.ts | 2 +- packages/rpc-core/package.json | 3 +- packages/rpc-provider/package.json | 6 +- packages/type-extrinsics/package.json | 3 +- packages/type-extrinsics/src/fromMetadata.ts | 5 +- packages/type-extrinsics/src/index.ts | 2 +- packages/type-extrinsics/src/types.d.ts | 23 ------- .../src/utils/createUnchecked.ts | 3 +- packages/type-storage/package.json | 6 +- packages/types/package.json | 4 +- packages/types/src/Bytes.ts | 13 ++-- packages/types/src/Extrinsic.ts | 18 ++---- packages/types/src/Metadata.ts | 4 +- packages/types/src/Method.ts | 24 ++++++- packages/types/src/Text.ts | 13 ++-- packages/types/src/Type.ts | 4 +- packages/types/src/codec/Compact.ts | 62 +++---------------- .../codec/{Tuple.spec.js => Tuple.spec.ts} | 11 ++-- packages/types/src/codec/Vector.ts | 8 +-- yarn.lock | 30 ++++----- 26 files changed, 103 insertions(+), 163 deletions(-) delete mode 100644 packages/type-extrinsics/src/types.d.ts rename packages/types/src/codec/{Tuple.spec.js => Tuple.spec.ts} (78%) diff --git a/packages/api-observable/src/Base.ts b/packages/api-observable/src/Base.ts index ba7215af0a..634d022a14 100644 --- a/packages/api-observable/src/Base.ts +++ b/packages/api-observable/src/Base.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Extrinsics } from '@polkadot/extrinsics/types'; +import { Extrinsics } from '@polkadot/types/Method'; import { RpcMethod } from '@polkadot/jsonrpc/types'; import { RpcRxInterface, RpcRxInterface$Method, RpcRxInterface$Section } from '@polkadot/rpc-rx/types'; import { Storage } from '@polkadot/storage/types'; diff --git a/packages/api/src/Base.ts b/packages/api/src/Base.ts index e0cd3e5f53..69d03b535a 100644 --- a/packages/api/src/Base.ts +++ b/packages/api/src/Base.ts @@ -7,13 +7,13 @@ import { ApiBaseInterface, ApiInterface$Events } from './types'; import EventEmitter from 'eventemitter3'; import WsProvider from '@polkadot/rpc-provider/ws'; import Rpc from '@polkadot/rpc-core/index'; -import { Extrinsics } from '@polkadot/extrinsics/types'; import extrinsicsFromMeta from '@polkadot/extrinsics/fromMetadata'; import { Storage } from '@polkadot/storage/types'; import storageFromMeta from '@polkadot/storage/fromMetadata'; import { Hash, Method, RuntimeVersion } from '@polkadot/types/index'; import Event from '@polkadot/types/Event'; import RuntimeMetadata from '@polkadot/types/Metadata'; +import { Extrinsics } from '@polkadot/types/Method'; import { assert, isUndefined, logger } from '@polkadot/util'; type MetaDecoration = { diff --git a/packages/api/src/promise/index.ts b/packages/api/src/promise/index.ts index 6a119fd448..016749ac49 100644 --- a/packages/api/src/promise/index.ts +++ b/packages/api/src/promise/index.ts @@ -6,9 +6,9 @@ import { ApiPromiseInterface, QueryableStorageFunction, QueryableModuleStorage, import WsProvider from '@polkadot/rpc-provider/ws'; import Rpc from '@polkadot/rpc-core/index'; -import { Extrinsics, ExtrinsicFunction } from '@polkadot/extrinsics/types'; import { Storage } from '@polkadot/storage/types'; import { Codec } from '@polkadot/types/types'; +import { Extrinsics, ExtrinsicFunction } from '@polkadot/types/Method'; import { StorageFunction } from '@polkadot/types/StorageKey'; import { isFunction } from '@polkadot/util'; diff --git a/packages/api/src/promise/types.d.ts b/packages/api/src/promise/types.d.ts index f69986a9c2..e802382952 100644 --- a/packages/api/src/promise/types.d.ts +++ b/packages/api/src/promise/types.d.ts @@ -2,15 +2,15 @@ // 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 '@polkadot/types/types'; +import { ExtrinsicFunction } from '@polkadot/types/Method'; +import { StorageFunction } from '@polkadot/types/StorageKey'; import { ApiBaseInterface } from '../types'; import { Observable } from 'rxjs'; -import { ExtrinsicFunction } from '@polkadot/extrinsics/types'; import Rpc from '@polkadot/rpc-core/index'; -import { Codec } from '@polkadot/types/types'; import { Hash, RuntimeVersion } from '@polkadot/types/index'; import RuntimeMetadata, { FunctionMetadata } from '@polkadot/types/Metadata'; -import { StorageFunction } from '@polkadot/types/StorageKey'; import SubmittableExtrinsic from './SubmittableExtrinsic'; diff --git a/packages/api/src/rx/index.ts b/packages/api/src/rx/index.ts index d291120335..9c0e896f80 100644 --- a/packages/api/src/rx/index.ts +++ b/packages/api/src/rx/index.ts @@ -9,14 +9,14 @@ import { defaultIfEmpty, map } from 'rxjs/operators'; import WsProvider from '@polkadot/rpc-provider/ws'; import Rpc from '@polkadot/rpc-core/index'; import RpcRx from '@polkadot/rpc-rx/index'; -import { Extrinsics, ExtrinsicFunction } from '@polkadot/extrinsics/types'; import { Storage } from '@polkadot/storage/types'; import { Codec } from '@polkadot/types/types'; +import { Extrinsics, ExtrinsicFunction } from '@polkadot/types/Method'; +import { StorageFunction } from '@polkadot/types/StorageKey'; import { logger } from '@polkadot/util'; import ApiBase from '../Base'; import SubmittableExtrinsic from './SubmittableExtrinsic'; -import { StorageFunction } from '@polkadot/types/StorageKey'; const l = logger('api-rx'); diff --git a/packages/api/src/rx/types.d.ts b/packages/api/src/rx/types.d.ts index c9fddf98a9..78eea89016 100644 --- a/packages/api/src/rx/types.d.ts +++ b/packages/api/src/rx/types.d.ts @@ -2,15 +2,15 @@ // 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 '@polkadot/types/types'; +import { ExtrinsicFunction } from '@polkadot/types/Method'; +import { StorageFunction } from '@polkadot/types/StorageKey'; import { ApiBaseInterface } from '../types'; import { Observable } from 'rxjs'; -import { ExtrinsicFunction } from '@polkadot/extrinsics/types'; import RpcRx from '@polkadot/rpc-rx/index'; -import { Codec } from '@polkadot/types/types'; import { Hash, RuntimeVersion } from '@polkadot/types/index'; import RuntimeMetadata, { FunctionMetadata } from '@polkadot/types/Metadata'; -import { StorageFunction } from '@polkadot/types/StorageKey'; import SubmittableExtrinsic from './SubmittableExtrinsic'; diff --git a/packages/api/src/types.d.ts b/packages/api/src/types.d.ts index 634f0a9e91..d338d576a6 100644 --- a/packages/api/src/types.d.ts +++ b/packages/api/src/types.d.ts @@ -6,7 +6,7 @@ import RpcRx from '@polkadot/rpc-rx/index'; import { RpcRxInterface$Events } from '@polkadot/rpc-rx/types'; import { Hash, RuntimeVersion } from '@polkadot/types/index'; import RuntimeMetadata, { FunctionMetadata } from '@polkadot/types/Metadata'; -import { ExtrinsicFunction } from '@polkadot/extrinsics/types'; +import { ExtrinsicFunction } from '@polkadot/types/Method'; import { StorageFunction } from '@polkadot/types/StorageKey'; export type ApiInterface$Events = RpcRxInterface$Events | 'ready'; diff --git a/packages/rpc-core/package.json b/packages/rpc-core/package.json index 4d86d48b94..6420afe8b2 100644 --- a/packages/rpc-core/package.json +++ b/packages/rpc-core/package.json @@ -32,6 +32,7 @@ "@babel/runtime": "^7.1.5", "@polkadot/jsonrpc": "^0.32.1", "@polkadot/rpc-provider": "^0.32.1", - "@polkadot/util": "^0.32.21" + "@polkadot/types": "^0.32.1", + "@polkadot/util": "^0.32.23" } } diff --git a/packages/rpc-provider/package.json b/packages/rpc-provider/package.json index a8333f68fe..2358e16232 100644 --- a/packages/rpc-provider/package.json +++ b/packages/rpc-provider/package.json @@ -30,10 +30,10 @@ "homepage": "https://github.com/polkadot-js/api/tree/master/packages/rpc-provider#readme", "dependencies": { "@babel/runtime": "^7.1.5", - "@polkadot/keyring": "^0.32.21", + "@polkadot/keyring": "^0.32.23", "@polkadot/storage": "^0.32.1", - "@polkadot/util": "^0.32.21", - "@polkadot/util-crypto": "^0.32.21", + "@polkadot/util": "^0.32.23", + "@polkadot/util-crypto": "^0.32.23", "@types/nock": "^9.3.0", "eventemitter3": "^3.1.0", "isomorphic-fetch": "^2.2.1", diff --git a/packages/type-extrinsics/package.json b/packages/type-extrinsics/package.json index 21de1b7a38..4df24595e3 100644 --- a/packages/type-extrinsics/package.json +++ b/packages/type-extrinsics/package.json @@ -11,6 +11,7 @@ "license": "Apache-2.0", "dependencies": { "@babel/runtime": "^7.1.5", - "@polkadot/util": "^0.32.21" + "@polkadot/types": "^0.32.1", + "@polkadot/util": "^0.32.23" } } diff --git a/packages/type-extrinsics/src/fromMetadata.ts b/packages/type-extrinsics/src/fromMetadata.ts index f1e9b8502f..f231b724fe 100644 --- a/packages/type-extrinsics/src/fromMetadata.ts +++ b/packages/type-extrinsics/src/fromMetadata.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 { Extrinsics, ModuleExtrinsics } from './types'; +import { RuntimeModuleMetadata } from '@polkadot/types/Metadata'; +import { Extrinsics, ModuleExtrinsics } from '@polkadot/types/Method'; +import { Metadata } from '@polkadot/types/index'; -import Metadata, { RuntimeModuleMetadata } from '@polkadot/types/Metadata'; import { stringCamelCase } from '@polkadot/util'; import createUnchecked from './utils/createUnchecked'; diff --git a/packages/type-extrinsics/src/index.ts b/packages/type-extrinsics/src/index.ts index f5aa69d93a..dbb3b3f4a3 100644 --- a/packages/type-extrinsics/src/index.ts +++ b/packages/type-extrinsics/src/index.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { Extrinsics } from './types'; +import { Extrinsics } from '@polkadot/types/Method'; const extrinsics: Extrinsics = {}; diff --git a/packages/type-extrinsics/src/types.d.ts b/packages/type-extrinsics/src/types.d.ts deleted file mode 100644 index d283f2055a..0000000000 --- a/packages/type-extrinsics/src/types.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics 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 { Extrinsic } from '@polkadot/types/index'; -import { FunctionMetadata } from '@polkadot/types/Metadata'; - -export interface ExtrinsicFunction { - (...args: any[]): Extrinsic, - callIndex: Uint8Array, - meta: FunctionMetadata, - method: string; - section: string; - toJSON: () => any; -} - -export interface ModuleExtrinsics { - [key: string]: ExtrinsicFunction; -} - -export interface Extrinsics { - [key: string]: ModuleExtrinsics; // Will hold modules returned by state_getMetadata -} diff --git a/packages/type-extrinsics/src/utils/createUnchecked.ts b/packages/type-extrinsics/src/utils/createUnchecked.ts index 492eabdc88..7f9381f7d0 100644 --- a/packages/type-extrinsics/src/utils/createUnchecked.ts +++ b/packages/type-extrinsics/src/utils/createUnchecked.ts @@ -2,9 +2,8 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ExtrinsicFunction } from '../types'; - import { FunctionMetadata } from '@polkadot/types/Metadata'; +import { ExtrinsicFunction } from '@polkadot/types/Method'; import { Extrinsic, Method } from '@polkadot/types/index'; import { assert } from '@polkadot/util'; diff --git a/packages/type-storage/package.json b/packages/type-storage/package.json index c614655b31..6fc9a9c2ab 100644 --- a/packages/type-storage/package.json +++ b/packages/type-storage/package.json @@ -30,9 +30,9 @@ "homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-storage#readme", "dependencies": { "@babel/runtime": "^7.1.5", - "@polkadot/keyring": "^0.32.21", + "@polkadot/keyring": "^0.32.23", "@polkadot/types": "^0.32.1", - "@polkadot/util": "^0.32.21", - "@polkadot/util-crypto": "^0.32.21" + "@polkadot/util": "^0.32.23", + "@polkadot/util-crypto": "^0.32.23" } } diff --git a/packages/types/package.json b/packages/types/package.json index 7724c89423..e14c8cc398 100644 --- a/packages/types/package.json +++ b/packages/types/package.json @@ -30,7 +30,7 @@ "homepage": "https://github.com/polkadot-js/api/tree/master/packages/types#readme", "dependencies": { "@babel/runtime": "^7.1.5", - "@polkadot/keyring": "^0.32.21", - "@polkadot/util": "^0.32.21" + "@polkadot/keyring": "^0.32.23", + "@polkadot/util": "^0.32.23" } } diff --git a/packages/types/src/Bytes.ts b/packages/types/src/Bytes.ts index 9271b30a5f..1429744416 100644 --- a/packages/types/src/Bytes.ts +++ b/packages/types/src/Bytes.ts @@ -2,10 +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 { hexToU8a, isHex, isString, isU8a, u8aConcat, u8aToU8a } from '@polkadot/util'; +import { hexToU8a, isHex, isString, isU8a, u8aToU8a } from '@polkadot/util'; import { AnyU8a } from './types'; -import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact'; +import Compact from './codec/Compact'; import U8a from './codec/U8a'; // A Bytes wrapper for Vec. The significant difference between this and a normal Uint8Array @@ -30,7 +30,7 @@ export default class Bytes extends U8a { // i.e. new Bytes(new Bytes(...)) will work as expected return value; } else if (isU8a(value)) { - const [offset, length] = Compact.decodeU8a(value, DEFAULT_LENGTH_BITS); + const [offset, length] = Compact.decodeU8a(value); return value.subarray(offset, offset + length.toNumber()); } else if (Array.isArray(value) || isString(value)) { @@ -41,15 +41,12 @@ export default class Bytes extends U8a { } get encodedLength (): number { - return this.length + Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS).length; + return this.length + Compact.encodeU8a(this.length).length; } toU8a (isBare?: boolean): Uint8Array { return isBare ? super.toU8a(isBare) - : u8aConcat( - Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS), - this - ); + : Compact.addLengthPrefix(this); } } diff --git a/packages/types/src/Extrinsic.ts b/packages/types/src/Extrinsic.ts index c408d595c3..3aa98193b8 100644 --- a/packages/types/src/Extrinsic.ts +++ b/packages/types/src/Extrinsic.ts @@ -5,10 +5,10 @@ import { KeyringPair } from '@polkadot/keyring/types'; import { AnyNumber, AnyU8a, Codec } from './types'; -import { hexToU8a, isHex, isU8a, u8aConcat, u8aToHex } from '@polkadot/util'; +import { hexToU8a, isHex, isU8a, u8aToHex } from '@polkadot/util'; import { blake2AsU8a } from '@polkadot/util-crypto'; -import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact'; +import Compact from './codec/Compact'; import Struct from './codec/Struct'; import ExtrinsicSignature from './ExtrinsicSignature'; import Hash from './Hash'; @@ -49,13 +49,10 @@ export default class Extrinsic extends Struct { const u8a = hexToU8a(value); return Extrinsic.decodeExtrinsic( - u8aConcat( - Compact.encodeU8a(u8a.length, DEFAULT_LENGTH_BITS), - u8a - ) + Compact.addLengthPrefix(u8a) ); } else if (isU8a(value)) { - const [offset, length] = Compact.decodeU8a(value, DEFAULT_LENGTH_BITS); + const [offset, length] = Compact.decodeU8a(value); return value.subarray(offset, offset + length.toNumber()); } @@ -107,7 +104,7 @@ export default class Extrinsic extends Struct { get encodedLength (): number { const length = this.length; - return length + Compact.encodeU8a(length, DEFAULT_LENGTH_BITS).length; + return length + Compact.encodeU8a(length).length; } sign (signerPair: KeyringPair, nonce: AnyNumber, blockHash: AnyU8a): Extrinsic { @@ -121,10 +118,7 @@ export default class Extrinsic extends Struct { return isBare ? encoded - : u8aConcat( - Compact.encodeU8a(encoded.length, DEFAULT_LENGTH_BITS), - encoded - ); + : Compact.addLengthPrefix(encoded); } toHex (): string { diff --git a/packages/types/src/Metadata.ts b/packages/types/src/Metadata.ts index 0550b97642..dda9e111ef 100644 --- a/packages/types/src/Metadata.ts +++ b/packages/types/src/Metadata.ts @@ -6,7 +6,7 @@ import { AnyNumber } from './types'; import { hexToU8a, isHex, isU8a } from '@polkadot/util'; -import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact'; +import Compact from './codec/Compact'; import Enum from './codec/Enum'; import EnumType from './codec/EnumType'; import Option from './codec/Option'; @@ -337,7 +337,7 @@ export default class RuntimeMetadata extends Struct { // is properly encoded. Here we pull the prefix, check it agianst the length - // if matches, then we have the length, otherwise we assume it is an older node // and use the whole buffer - const [offset, length] = Compact.decodeU8a(value, DEFAULT_LENGTH_BITS); + const [offset, length] = Compact.decodeU8a(value); return value.length === (offset + length.toNumber()) ? value.subarray(offset) diff --git a/packages/types/src/Method.ts b/packages/types/src/Method.ts index e68cc169f1..0b6b255f0a 100644 --- a/packages/types/src/Method.ts +++ b/packages/types/src/Method.ts @@ -2,14 +2,15 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { ExtrinsicFunction, Extrinsics } from '@polkadot/extrinsics/types'; +import { AnyU8a, Codec, Constructor } from './types'; + import { assert, isHex, isObject, isU8a } from '@polkadot/util'; -import { AnyU8a, Codec, Constructor } from './types'; -import { FunctionMetadata, FunctionArgumentMetadata } from './Metadata'; import { getTypeDef, getTypeClass } from './codec/createType'; import Struct from './codec/Struct'; import U8aFixed from './codec/U8aFixed'; +import Extrinsic from './Extrinsic'; +import { FunctionMetadata, FunctionArgumentMetadata } from './Metadata'; const FN_UNKNOWN = { method: 'unknown', @@ -30,6 +31,23 @@ interface DecodedMethod extends DecodeMethodInput { meta: FunctionMetadata; } +export interface ExtrinsicFunction { + (...args: any[]): Extrinsic; + callIndex: Uint8Array; + meta: FunctionMetadata; + method: string; + section: string; + toJSON: () => any; +} + +export interface ModuleExtrinsics { + [key: string]: ExtrinsicFunction; +} + +export interface Extrinsics { + [key: string]: ModuleExtrinsics; // Will hold modules returned by state_getMetadata +} + const extrinsicFns: { [index: string]: ExtrinsicFunction } = {}; class MethodIndex extends U8aFixed { diff --git a/packages/types/src/Text.ts b/packages/types/src/Text.ts index 0b2043a4ba..5021f0af7f 100644 --- a/packages/types/src/Text.ts +++ b/packages/types/src/Text.ts @@ -2,10 +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 { isFunction, isString, stringToU8a, u8aToString, u8aConcat, u8aToHex } from '@polkadot/util'; +import { isFunction, isString, stringToU8a, u8aToString, u8aToHex } from '@polkadot/util'; import { AnyU8a, Codec } from './types'; -import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact'; +import Compact from './codec/Compact'; // This is a string wrapper, along with the length. It is used both for strings as well // as stuff like documentation. @@ -26,7 +26,7 @@ export default class Text extends String implements Codec { if (isString(value)) { return value.toString(); } else if (value instanceof Uint8Array) { - const [offset, length] = Compact.decodeU8a(value, DEFAULT_LENGTH_BITS); + const [offset, length] = Compact.decodeU8a(value); return u8aToString(value.subarray(offset, offset + length.toNumber())); } else if (isFunction(value.toString)) { @@ -37,7 +37,7 @@ export default class Text extends String implements Codec { } get encodedLength (): number { - return this.length + Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS).length; + return this.length + Compact.encodeU8a(this.length).length; } toHex (): string { @@ -53,9 +53,6 @@ export default class Text extends String implements Codec { return isBare ? encoded - : u8aConcat( - Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS), - encoded - ); + : Compact.addLengthPrefix(encoded); } } diff --git a/packages/types/src/Type.ts b/packages/types/src/Type.ts index fcde1ac0eb..e716724393 100644 --- a/packages/types/src/Type.ts +++ b/packages/types/src/Type.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import Compact, { DEFAULT_LENGTH_BITS } from './codec/Compact'; +import Compact from './codec/Compact'; import U8a from './codec/U8a'; import Text from './Text'; @@ -62,7 +62,7 @@ export default class Type extends Text { // length of the data. Since toU8a is disabled, this does not affect encoding, but rather // only the decoding leg, allowing the decoders to work with original pointers get encodedLength (): number { - return this._originalLength + Compact.encodeU8a(this._originalLength, DEFAULT_LENGTH_BITS).length; + return this._originalLength + Compact.encodeU8a(this._originalLength).length; } // Note Since we are mangling what we get in beyond recognition, we really should diff --git a/packages/types/src/codec/Compact.ts b/packages/types/src/codec/Compact.ts index 7bd6db6027..6566d7ce00 100644 --- a/packages/types/src/codec/Compact.ts +++ b/packages/types/src/codec/Compact.ts @@ -3,19 +3,14 @@ // of the Apache-2.0 license. See the LICENSE file for details. import BN from 'bn.js'; -import { bnToBn, bnToU8a, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn, u8aConcat, u8aToU8a } from '@polkadot/util'; +import { bnToBn, compactAddLength, compactFromU8a, compactStripLength, compactToU8a, hexToBn, isBn, isHex, isNumber, isString, isU8a } from '@polkadot/util'; +import { DEFAULT_BITLENGTH } from '@polkadot/util/compact/defaults'; import { AnyNumber, Codec, Constructor } from '../types'; import Base from './Base'; import UInt, { UIntBitLength } from './UInt'; import Moment from '../Moment'; -export const DEFAULT_LENGTH_BITS = 32; - -const MAX_U8 = new BN(2).pow(new BN(8 - 2)).subn(1); -const MAX_U16 = new BN(2).pow(new BN(16 - 2)).subn(1); -const MAX_U32 = new BN(2).pow(new BN(32 - 2)).subn(1); - // A new compact length-encoding algorithm. It performs the same function as Length, however // differs in that it uses a variable number of bytes to do the actual encoding. From the Rust // implementation for compact encoding @@ -49,17 +44,14 @@ export default class Compact extends Base implements Codec { * * @param u8a - The Uint8Array to be prefixed */ - static addLengthPrefix (u8a: Uint8Array, bitLength: UIntBitLength = DEFAULT_LENGTH_BITS): Uint8Array { - return u8aConcat( - Compact.encodeU8a(u8a.length, bitLength), - u8a - ); - } + static addLengthPrefix = compactAddLength; + static decodeU8a = compactFromU8a; + static encodeU8a = compactToU8a; - static stripLengthPrefix (u8a: Uint8Array, bitLength: UIntBitLength = DEFAULT_LENGTH_BITS): Uint8Array { - const [offset] = Compact.decodeU8a(u8a, bitLength); + static stripLengthPrefix (u8a: Uint8Array, bitLength: UIntBitLength = DEFAULT_BITLENGTH): Uint8Array { + const [_, value] = compactStripLength(u8a, bitLength); - return u8a.subarray(offset); + return value; } static decodeCompact (Type: Constructor, value: AnyNumber): Moment | UInt { @@ -80,44 +72,6 @@ export default class Compact extends Base implements Codec { return new Type(value.toBn()); } - static decodeU8a (_input: Uint8Array | string, bitLength: UIntBitLength): [number, BN] { - const input = u8aToU8a(_input); - const flag = input[0] & 0b11; - - if (flag === 0b00) { - return [1, new BN(input[0]).shrn(2)]; - } else if (flag === 0b01) { - return [2, u8aToBn(input.slice(0, 2), true).shrn(2)]; - } else if (flag === 0b10) { - return [4, u8aToBn(input.slice(0, 4), true).shrn(2)]; - } - - const byteLength = bitLength / 8; - - return [byteLength + 1, u8aToBn(input.subarray(1, 1 + byteLength), true)]; - } - - static encodeU8a (_value: UInt | BN | number, bitLength: UIntBitLength): Uint8Array { - const value = _value instanceof UInt - ? _value.toBn() - : bnToBn(_value); - - if (value.lte(MAX_U8)) { - return new Uint8Array([value.toNumber() << 2]); - } else if (value.lte(MAX_U16)) { - return bnToU8a(value.shln(2).addn(0b01), 16, true); - } else if (value.lte(MAX_U32)) { - return bnToU8a(value.shln(2).addn(0b10), 32, true); - } - - return u8aConcat( - new Uint8Array([ - 0b11 - ]), - bnToU8a(value, bitLength, true) - ); - } - bitLength (): UIntBitLength { return this.raw.bitLength(); } diff --git a/packages/types/src/codec/Tuple.spec.js b/packages/types/src/codec/Tuple.spec.ts similarity index 78% rename from packages/types/src/codec/Tuple.spec.js rename to packages/types/src/codec/Tuple.spec.ts index 1937169f80..3bd29f2748 100644 --- a/packages/types/src/codec/Tuple.spec.js +++ b/packages/types/src/codec/Tuple.spec.ts @@ -13,15 +13,16 @@ import Proposal from '../Proposal'; import VoteThreshold from '../VoteThreshold'; describe('Tuple', () => { - let tuple; + let tuple: Tuple; beforeEach(() => { Method.injectExtrinsics(extrinsics); + // FIXME something weird here, injecting any :( tuple = new Tuple({ foo: Text, bar: U32 - }, { foo: 'foo', bar: 69 }); + } as any, { foo: 'foo', bar: 69 }); }); it('returns array with toJSON', () => { @@ -58,8 +59,8 @@ describe('Tuple', () => { threshold: VoteThreshold }))('0x62190000000000000003507b0a092230783432223a202230783433220a7d0a01'); - expect(test.getAtIndex(0).toNumber()).toEqual(6498); - expect(test.getAtIndex(1).callIndex).toEqual(new Uint8Array([0, 3])); - expect(test.getAtIndex(2).toNumber()).toEqual(1); + expect((test.getAtIndex(0) as BlockNumber).toNumber()).toEqual(6498); + expect((test.getAtIndex(1) as Method).callIndex).toEqual(new Uint8Array([0, 3])); + expect((test.getAtIndex(2) as VoteThreshold).toNumber()).toEqual(1); }); }); diff --git a/packages/types/src/codec/Vector.ts b/packages/types/src/codec/Vector.ts index 253fa1c552..9acf82f472 100644 --- a/packages/types/src/codec/Vector.ts +++ b/packages/types/src/codec/Vector.ts @@ -4,7 +4,7 @@ import { u8aConcat, u8aToU8a, u8aToHex } from '@polkadot/util'; -import Compact, { DEFAULT_LENGTH_BITS } from './Compact'; +import Compact from './Compact'; import { Codec, Constructor } from '../types'; // This manages codec arrays. Intrernally it keeps track of the length (as decoded) and allows @@ -36,7 +36,7 @@ export default class Vector< const u8a = u8aToU8a(value); - let [offset, _length] = Compact.decodeU8a(u8a, DEFAULT_LENGTH_BITS); + let [offset, _length] = Compact.decodeU8a(u8a); const length = _length.toNumber(); const result = []; @@ -66,7 +66,7 @@ export default class Vector< get encodedLength (): number { return this.reduce((total, raw) => { return total + raw.encodedLength; - }, Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS).length); + }, Compact.encodeU8a(this.length).length); } toArray (): Array { @@ -100,7 +100,7 @@ export default class Vector< return isBare ? u8aConcat(...encoded) : u8aConcat( - Compact.encodeU8a(this.length, DEFAULT_LENGTH_BITS), + Compact.encodeU8a(this.length), ...encoded ); } diff --git a/yarn.lock b/yarn.lock index a3a0d43422..1637b182d1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1352,14 +1352,14 @@ typedoc-plugin-markdown "^1.1.19" typescript "^3.1.6" -"@polkadot/keyring@^0.32.21": - version "0.32.21" - resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.32.21.tgz#7ac4a8cde3f76d024d0fa350ae54797b97e6c9e9" - integrity sha512-1uxD4iflnXd4YATOXT/PgcEv24Uh0ON6lKPLpEgHwYbLpOoU6/q4vRR4xfEekVoCvR6I9Ndmlxa3JmCkelJpfQ== +"@polkadot/keyring@^0.32.23": + version "0.32.23" + resolved "https://registry.yarnpkg.com/@polkadot/keyring/-/keyring-0.32.23.tgz#6a4ca96638b6824f30dd7ed81e864652262ffb04" + integrity sha512-sTdfIp5J955X8g1MGMgxrffTinqhaaA5pu6teDdeOvYMSfaJfg20uN3CxKz4wa6cU4f+Tav1zGLyWLYYVvxFdw== dependencies: "@babel/runtime" "^7.1.5" - "@polkadot/util" "^0.32.21" - "@polkadot/util-crypto" "^0.32.21" + "@polkadot/util" "^0.32.23" + "@polkadot/util-crypto" "^0.32.23" "@types/bs58" "^3.0.30" bs58 "^4.0.1" @@ -1368,13 +1368,13 @@ resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.39.tgz#39d3578eaafd96cbc0270b1479fabab7e16e5723" integrity sha512-NQM1uT+qi8onV0jtGjH7QmJvDTNFNYTEzjaoVZN2tDHi1/tVu1CP66qZGSGsS19JZJSK3ERSFb0vP8CkE7EUuA== -"@polkadot/util-crypto@^0.32.21": - version "0.32.21" - resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.32.21.tgz#15bca0ccda759722e0009b70c121dcc7a2fa8d0e" - integrity sha512-wLH/qED0Zz7Cvjh/GQTfrMVIRpJw2xiNr9g/dpdhZ4hc2cRLyEzJCdBZKIYLnTZYUapjArFmgW6jewVNRjJKfQ== +"@polkadot/util-crypto@^0.32.23": + version "0.32.23" + resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.32.23.tgz#4def26ba6945d4a0c4abfefeca9a506450e738ac" + integrity sha512-h7GOWWw3AQkc9SwOoN6V7X/IgopVwBQLLsXROPnzSpXTt6LluRSAXdzk/62f5KTVz5KJE/cj4EORd4oLfNgMaA== dependencies: "@babel/runtime" "^7.1.5" - "@polkadot/util" "^0.32.21" + "@polkadot/util" "^0.32.23" "@types/bip39" "^2.4.0" bip39 "^2.5.0" blakejs "^1.1.0" @@ -1382,10 +1382,10 @@ tweetnacl "^1.0.0" xxhashjs "^0.2.2" -"@polkadot/util@^0.32.21": - version "0.32.21" - resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.32.21.tgz#8e2da9a204faad6e34df6fbb959e06eed4de4faa" - integrity sha512-jNPKEPkSxAfOsEZXSFCLIsow4CxKy0GtZzWgLfMBz4plT+Y/2JH4ZdPIIoqRRDoyy1uk3UmWi7g3sOe8gdzakA== +"@polkadot/util@^0.32.23": + version "0.32.23" + resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.32.23.tgz#b49386e3350f8656111832237fb65960f049e862" + integrity sha512-hp33s1wWN/pXeX5irdn2eOPcZIfr28XCZY80oFQFOmFA8U8CWzLgdE31s8Aa8XEtvGX4P7D40VEwk1oKKKiBHg== dependencies: "@babel/runtime" "^7.1.5" "@types/bn.js" "^4.11.3"