diff --git a/packages/api-codec/src/types.d.ts b/packages/api-codec/src/types.d.ts index 4347a14e4293..55555bc2060f 100644 --- a/packages/api-codec/src/types.d.ts +++ b/packages/api-codec/src/types.d.ts @@ -3,6 +3,7 @@ // of the ISC license. See the LICENSE file for details. import BN from 'bn.js'; +import * as Classes from './index'; import U8a from './codec/U8a'; import UInt from './codec/UInt'; @@ -10,3 +11,5 @@ import UInt from './codec/UInt'; export type AnyNumber = UInt | BN | Uint8Array | number | string; export type AnyU8a = U8a | Uint8Array | Array | string; + +export type CodecTypes = keyof typeof Classes; diff --git a/packages/api-provider/src/mock/mocks.ts b/packages/api-provider/src/mock/mocks.ts index 4604ace56db0..847d19664de2 100644 --- a/packages/api-provider/src/mock/mocks.ts +++ b/packages/api-provider/src/mock/mocks.ts @@ -4,26 +4,20 @@ // FIXME: This file is way too long and way too messy -import { Storages } from '@polkadot/storage/types'; -import { SectionItem } from '@polkadot/params/types'; +import { StorageFunction } from '@polkadot/api-codec/StorageKey'; import { KeyringPair } from '@polkadot/util-keyring/types'; import { ProviderInterface$Emitted } from '../types'; -import { MockState, MockState$Storage, MockState$Subscriptions } from './types'; +import { MockState, MockState$Db, MockState$Subscriptions } from './types'; import BN from 'bn.js'; -import headerEncode from '@polkadot/primitives/json/header/encode'; -import createKey from '@polkadot/storage/key'; -import state from '@polkadot/storage/index'; +import Header from '@polkadot/api-codec/Header'; +import storage from '@polkadot/storage/testing'; import bnToU8a from '@polkadot/util/bn/toU8a'; import u8aToHex from '@polkadot/util/u8a/toHex'; import randomAsU8a from '@polkadot/util-crypto/random/asU8a'; import testKeyring from '@polkadot/util-keyring/testing'; const keyring = testKeyring(); -// FIXME No more .public -const stateStaking = state.staking.public; -const stateSystem = state.staking.public; -const stateTimestamp = state.timestamp.public; const emitEvents: Array = ['connected', 'disconnected']; let emitIndex = 0; @@ -58,15 +52,15 @@ function updateSubs (subscriptions: MockState$Subscriptions, method: string, val }); } -function setStorageBn (storage: MockState$Storage, key: SectionItem, value: BN | number, ...keyParams: Array): void { +function setStorageBn (db: MockState$Db, createKey: StorageFunction, value: BN | number, ...keyParams: Array): void { const keyValue = u8aToHex( - createKey(key).apply(null, keyParams) + createKey(...keyParams) ); - storage[keyValue] = bnToU8a(value, 64, true); + db[keyValue] = bnToU8a(value, 64, true); } -export default function mocks ({ emitter, storage, subscriptions }: MockState): void { +export default function mocks ({ emitter, db, subscriptions }: MockState): void { let newHead = makeBlockHeader(new BN(-1)); setInterval(() => { @@ -79,15 +73,12 @@ export default function mocks ({ emitter, storage, subscriptions }: MockState): newHead = makeBlockHeader(newHead.number); keyring.getPairs().forEach(({ publicKey }: KeyringPair, index: number) => { - // @ts-ignore FIXME This does not exist anymore - setStorageBn(storage, stateStaking.freeBalanceOf, newHead.number.muln(3).iaddn(index), publicKey()); - // @ts-ignore FIXME This does not exist anymore - setStorageBn(storage, stateSystem.accountIndexOf, newHead.number.addn(index), publicKey()); + setStorageBn(db, storage.balances.freeBalance, newHead.number.muln(3).iaddn(index), publicKey()); + setStorageBn(db, storage.system.accountNonce, newHead.number.addn(index), publicKey()); }); - // @ts-ignore FIXME This does not exist anymore - setStorageBn(storage, stateTimestamp.current, Math.floor(Date.now() / 1000)); + setStorageBn(db, storage.timestamp.now, Math.floor(Date.now() / 1000)); - updateSubs(subscriptions, 'chain_newHead', headerEncode(newHead)); + updateSubs(subscriptions, 'chain_newHead', new Header(newHead).toJSON()); }, 5000); } diff --git a/packages/api-provider/src/mock/send.ts b/packages/api-provider/src/mock/send.ts index a1b91ad75eb2..256c9ac5571e 100644 --- a/packages/api-provider/src/mock/send.ts +++ b/packages/api-provider/src/mock/send.ts @@ -4,10 +4,10 @@ import { MockState } from './types'; -export default async function send ({ requests, storage }: MockState, method: string, params: Array): Promise { +export default async function send ({ requests, db }: MockState, method: string, params: Array): Promise { if (!requests[method]) { throw new Error(`provider.send: Invalid method '${method}'`); } - return requests[method](storage, params); + return requests[method](db, params); } diff --git a/packages/api-provider/src/mock/state.ts b/packages/api-provider/src/mock/state.ts index 65f281b24d22..e24b46877c47 100644 --- a/packages/api-provider/src/mock/state.ts +++ b/packages/api-provider/src/mock/state.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { MockState, MockState$Storage, MockState$Subscriptions } from './types'; +import { MockState, MockState$Db, MockState$Subscriptions } from './types'; import E3 from 'eventemitter3'; import interfaces from '@polkadot/jsonrpc/index'; @@ -14,7 +14,7 @@ const l = logger('api-mock'); const SUBSCRIPTIONS: string[] = Array.prototype.concat.apply( [], Object.values(interfaces).map((area) => Object - .values(area.public) + .values(area.methods) .filter((method) => method.isSubscription ) @@ -25,7 +25,7 @@ const SUBSCRIPTIONS: string[] = Array.prototype.concat.apply( ); const REQUESTS = { - 'state_getStorage': (storage: MockState$Storage, params: Array): string => { + 'state_getStorage': (storage: MockState$Db, params: Array): string => { return u8aToHex( storage[(params[0] as string)] ); @@ -36,7 +36,7 @@ const REQUESTS = { }; export default function state (): MockState { - const storage = {}; + const db = {}; const subscriptions: MockState$Subscriptions = SUBSCRIPTIONS.reduce((subs, name) => { subs[name] = { callbacks: {}, @@ -50,7 +50,7 @@ export default function state (): MockState { emitter: new E3.EventEmitter(), l, requests: Object.assign({}, REQUESTS), - storage, + db, subscriptionId: 0, subscriptionMap: {}, subscriptions diff --git a/packages/api-provider/src/mock/types.d.ts b/packages/api-provider/src/mock/types.d.ts index 285c1ce5c3a2..8bb1520de224 100644 --- a/packages/api-provider/src/mock/types.d.ts +++ b/packages/api-provider/src/mock/types.d.ts @@ -16,19 +16,19 @@ export type MockState$Subscriptions = { } }; -export type MockState$Storage = { +export type MockState$Db = { [index: string]: Uint8Array }; export type MockState$Requests = { - [index: string]: (storage: MockState$Storage, params: Array) => string + [index: string]: (db: MockState$Db, params: Array) => string }; export type MockState = { emitter: EventEmitter, l: Logger, requests: MockState$Requests, - storage: MockState$Storage, + db: MockState$Db, subscriptionId: number, subscriptionMap: { [index: number]: string diff --git a/packages/api-rx/src/index.ts b/packages/api-rx/src/index.ts index 41cec2109bf7..5b3a65a08dee 100644 --- a/packages/api-rx/src/index.ts +++ b/packages/api-rx/src/index.ts @@ -4,7 +4,6 @@ import { ApiInterface, ApiInterface$Section } from '@polkadot/api/types'; import { ProviderInterface } from '@polkadot/api-provider/types'; -import { Interface$Sections } from '@polkadot/jsonrpc/types'; import { RxApiInterface, RxApiInterface$Section } from './types'; import { BehaviorSubject, Observable, Subscriber, from } from 'rxjs'; @@ -51,19 +50,17 @@ export default class RxApi implements RxApiInterface { provider.on('connected', () => this._isConnected.next(true)); provider.on('disconnected', () => this._isConnected.next(false)); - this.author = this.createInterface('author'); - this.chain = this.createInterface('chain'); - this.state = this.createInterface('state'); - this.system = this.createInterface('system'); + this.author = this.createInterface('author', this._api.author); + this.chain = this.createInterface('chain', this._api.chain); + this.state = this.createInterface('state', this._api.state); + this.system = this.createInterface('system', this._api.system); } isConnected (): BehaviorSubject { return this._isConnected; } - private createInterface (sectionName: Interface$Sections): RxApiInterface$Section { - const section: ApiInterface$Section = this._api[sectionName]; - + private createInterface (sectionName: string, section: ApiInterface$Section): RxApiInterface$Section { return Object .keys(section) .filter((name) => !['subscribe', 'unsubscribe'].includes(name)) diff --git a/packages/api-rx/src/types.d.ts b/packages/api-rx/src/types.d.ts index fbf5bf186860..407c894e4b28 100644 --- a/packages/api-rx/src/types.d.ts +++ b/packages/api-rx/src/types.d.ts @@ -2,9 +2,9 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { Interfaces } from '@polkadot/jsonrpc/types'; import { BehaviorSubject, Observable } from 'rxjs'; -import { Interface$Sections } from '@polkadot/jsonrpc/types'; + +import interfaces from '@polkadot/jsonrpc/types'; export type RxApiInterface$Method = (...params: Array) => Observable | BehaviorSubject; @@ -13,7 +13,7 @@ export type RxApiInterface$Section = { }; type RxApiInterface$Keys = { - readonly [key in keyof Interfaces]: RxApiInterface$Section; + readonly [key in keyof typeof interfaces]: RxApiInterface$Section; }; export type RxApiInterface = RxApiInterface$Keys & { diff --git a/packages/api/package.json b/packages/api/package.json index 3db2bd490c09..c53716912ae2 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -32,7 +32,6 @@ "@babel/runtime": "^7.0.0", "@polkadot/api-provider": "^0.30.2", "@polkadot/jsonrpc": "^0.30.2", - "@polkadot/params": "^0.30.2", "@polkadot/util": "^0.30.7" } } diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index e8bd5b2bd603..28c72aa8edb8 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -3,18 +3,15 @@ // of the ISC license. See the LICENSE file for details. import { ProviderInterface, ProviderInterface$Callback } from '@polkadot/api-provider/types'; -import { Interfaces, Interface$Sections } from '@polkadot/jsonrpc/types'; -import { SectionItem } from '@polkadot/params/types'; +import { Section, Method } from '@polkadot/jsonrpc/types'; import { ApiInterface, ApiInterface$Section, ApiInterface$Section$Method } from './types'; import { Base, Vector, createType } from '@polkadot/api-codec/codec'; import { StorageChangeSet, StorageKey } from '@polkadot/api-codec/index'; import interfaces from '@polkadot/jsonrpc/index'; -import signature from '@polkadot/params/signature'; import assert from '@polkadot/util/assert'; import ExtError from '@polkadot/util/ext/error'; import isFunction from '@polkadot/util/is/function'; -import isUndefined from '@polkadot/util/is/undefined'; /** * @example @@ -41,27 +38,39 @@ export default class Api implements ApiInterface { this._provider = provider; - this.author = this.createInterface('author'); - this.chain = this.createInterface('chain'); - this.state = this.createInterface('state'); - this.system = this.createInterface('system'); + this.author = this.createInterface(interfaces.author); + this.chain = this.createInterface(interfaces.chain); + this.state = this.createInterface(interfaces.state); + this.system = this.createInterface(interfaces.system); } - private createInterface (section: Interface$Sections): ApiInterface$Section { - const definition = interfaces[section]; - - assert(!isUndefined(definition), `Unable to find section '${section}`); + /** + * @name signature + * @signature jsonrpcSignature (method: InterfaceMethodDefinition): string + * @summary Returns a string representation of the method with inputs and outputs. + * @description + * Formats the name, inputs and outputs into a human-readable string. This contains the input parameter names input types and output type. + * @example + * import Api from '@polkadot/Api'; + * + * Api.signature({ name: 'test_method', params: [ { name: 'dest', type: 'Address' } ], type: 'Address' }); // => test_method (dest: Address): Address + */ + static signature ({ name, params, type }: Method): string { + const inputs = params.map(({ name, type }) => + `${name}: ${type}` + ).join(', '); - // @ts-ignore undefined check done in assert - const methods = definition.public; + return `${name} (${inputs}): ${type}`; + } + private createInterface ({ methods, name }: Section): ApiInterface$Section { return Object .keys(methods) - .reduce((exposed, name) => { - const rpcName = `${section}_${name}`; - const def = methods[name]; + .reduce((exposed, method) => { + const rpcName = `${name}_${method}`; + const def = methods[method]; - exposed[name] = def.isSubscription + exposed[method] = def.isSubscription ? this.createMethodSubscribe(rpcName, def) : this.createMethodSend(rpcName, def); @@ -69,7 +78,7 @@ export default class Api implements ApiInterface { }, {} as ApiInterface$Section); } - private createMethodSend (rpcName: string, method: SectionItem): ApiInterface$Section$Method { + private createMethodSend (rpcName: string, method: Method): ApiInterface$Section$Method { const call = async (...values: Array): Promise => { // TODO Warn on deprecated methods try { @@ -79,7 +88,7 @@ export default class Api implements ApiInterface { return this.formatOutput(method, params, result); } catch (error) { - const message = `${signature(method)}:: ${error.message}`; + const message = `${Api.signature(method)}:: ${error.message}`; console.error(message, error); @@ -90,7 +99,7 @@ export default class Api implements ApiInterface { return call as ApiInterface$Section$Method; } - private createMethodSubscribe (rpcName: string, method: SectionItem): ApiInterface$Section$Method { + private createMethodSubscribe (rpcName: string, method: Method): ApiInterface$Section$Method { const unsubscribe = (subscriptionId: any): Promise => this._provider.unsubscribe(rpcName, method.subscribe[1], subscriptionId); const _call = async (...values: Array): Promise => { @@ -107,7 +116,7 @@ export default class Api implements ApiInterface { return this._provider.subscribe(rpcName, method.subscribe[0], paramsJson, update); } catch (error) { - const message = `${signature(method)}:: ${error.message}`; + const message = `${Api.signature(method)}:: ${error.message}`; console.error(message, error); @@ -122,7 +131,7 @@ export default class Api implements ApiInterface { return call; } - private formatInputs (method: SectionItem, inputs: Array): Array { + private formatInputs (method: Method, inputs: Array): Array { assert(method.params.length === inputs.length, `Expected ${method.params.length} parameters, ${inputs.length} found instead`); return method.params.map(({ type }, index) => @@ -130,7 +139,7 @@ export default class Api implements ApiInterface { ); } - private formatOutput (method: SectionItem, params: Array, result?: any): Base { + private formatOutput (method: Method, params: Array, result?: any): Base { const base = createType(method.type as string, result); if (method.type === 'StorageData') { diff --git a/packages/api/src/types.d.ts b/packages/api/src/types.d.ts index 7318fcdce1e7..c37212d4459b 100644 --- a/packages/api/src/types.d.ts +++ b/packages/api/src/types.d.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { Interfaces, Interface$Sections } from '@polkadot/jsonrpc/types'; +import interfaces from '@polkadot/jsonrpc/index'; export interface ApiInterface$Section$Method { (...params: Array): Promise; @@ -16,5 +16,5 @@ export type ApiInterface$Section = { }; export type ApiInterface = { - readonly [key in keyof Interfaces]: ApiInterface$Section + readonly [key in keyof typeof interfaces]: ApiInterface$Section } diff --git a/packages/type-extrinsics/package.json b/packages/type-extrinsics/package.json index 0960e2477789..e360bbe469e2 100644 --- a/packages/type-extrinsics/package.json +++ b/packages/type-extrinsics/package.json @@ -11,8 +11,6 @@ "license": "ISC", "dependencies": { "@babel/runtime": "^7.0.0", - "@polkadot/params": "^0.30.2", - "@polkadot/primitives": "^0.30.2", "@polkadot/util": "^0.30.7" } } diff --git a/packages/type-extrinsics/src/codec/decode/length.ts b/packages/type-extrinsics/src/codec/decode/length.ts deleted file mode 100644 index 0175df72bb50..000000000000 --- a/packages/type-extrinsics/src/codec/decode/length.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { UncheckedRaw } from '@polkadot/primitives/extrinsic'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -export default function decodeLength (unchecked: Uint8Array): UncheckedRaw { - const length = u8aToBn(unchecked.subarray(0, 4), true).toNumber(); - - return unchecked.slice(4, length + 4); -} diff --git a/packages/type-extrinsics/src/codec/encode/call.spec.js b/packages/type-extrinsics/src/codec/encode/call.spec.js deleted file mode 100644 index 262e22cca713..000000000000 --- a/packages/type-extrinsics/src/codec/encode/call.spec.js +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. -// @flow - -import testingPairs from '@polkadot/util-keyring/testingPairs'; - -import call from './call'; - -const keyring = testingPairs(); - -describe('call', () => { - it('encodes the call correctly (poc-1)', () => { - expect( - call( - keyring.one.publicKey(), - 1234, - new Uint8Array([0xab, 0xcd]), - 'poc-1' - ) - ).toEqual( - new Uint8Array([ - // key - 47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238, - // nonce (u64) - 210, 4, 0, 0, 0, 0, 0, 0, - // data - 171, 205 - ]) - ); - }); - - it('encodes the call correctly (latest)', () => { - expect( - call( - keyring.one.publicKey(), - 1234, - new Uint8Array([0xab, 0xcd]), - 'latest' - ) - ).toEqual( - new Uint8Array([ - // key - 47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238, - // nonce (u32) - 210, 4, 0, 0, - // data - 171, 205 - ]) - ); - }); -}); diff --git a/packages/type-extrinsics/src/codec/encode/call.ts b/packages/type-extrinsics/src/codec/encode/call.ts deleted file mode 100644 index c5f720961305..000000000000 --- a/packages/type-extrinsics/src/codec/encode/call.ts +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; -import { EncodingVersions } from '@polkadot/params/types'; -import { ExtrinsicWithAccount, ExtrinsicWithIndex } from '../types'; - -import sizes from '@polkadot/params/sizes'; -import defaultSizes from '@polkadot/primitives/sizes'; -import bnToU8a from '@polkadot/util/bn/toU8a'; -import u8aConcat from '@polkadot/util/u8a/concat'; - -export default function encodeCall (publicKey: Uint8Array, nonce: number | BN, data: ExtrinsicWithIndex, version: EncodingVersions): ExtrinsicWithAccount { - return u8aConcat( - publicKey, - bnToU8a(nonce, sizes.AccountIndex.get(version) || defaultSizes.AccountIndex, true), - data - ); -} diff --git a/packages/type-extrinsics/src/codec/encode/extrinsic.spec.js b/packages/type-extrinsics/src/codec/encode/extrinsic.spec.js deleted file mode 100644 index 3994c52629bd..000000000000 --- a/packages/type-extrinsics/src/codec/encode/extrinsic.spec.js +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. -// @flow - -import extrinsics from '../../index'; - -import extrinsic from './extrinsic'; - -describe('extrinsic', () => { - it('encodes extrinsic correctly', () => { - expect( - extrinsic( - extrinsics.timestamp.public.set, - [10101], - 'latest' - ) - ).toEqual( - new Uint8Array([ - // index - 3, 0, - // values - 117, 39, 0, 0, 0, 0, 0, 0 - ]) - ); - }); -}); diff --git a/packages/type-extrinsics/src/codec/encode/extrinsic.ts b/packages/type-extrinsics/src/codec/encode/extrinsic.ts deleted file mode 100644 index 70cf6d90dcd5..000000000000 --- a/packages/type-extrinsics/src/codec/encode/extrinsic.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { EncodingVersions, SectionItem } from '@polkadot/params/types'; -import { Extrinsics } from '../../types'; -import { ExtrinsicWithIndex } from '../types'; - -import encodeParams from '@polkadot/params/encode'; -import u8aConcat from '@polkadot/util/u8a/concat'; - -export default function encodeExtrinsic (extrinsic: SectionItem, values: Array, version: EncodingVersions): ExtrinsicWithIndex { - return u8aConcat( - extrinsic.index, - encodeParams(extrinsic.params, values, version) - ); -} diff --git a/packages/type-extrinsics/src/codec/encode/index.spec.js b/packages/type-extrinsics/src/codec/encode/index.spec.js deleted file mode 100644 index e073ed0efcfe..000000000000 --- a/packages/type-extrinsics/src/codec/encode/index.spec.js +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. -// @flow - -import testingPairs from '@polkadot/util-keyring/testingPairs'; - -import extrinsics from '../../index'; -import encode from './index'; - -const keyring = testingPairs(); - -describe('encode', () => { - it('encodes extrinsic correctly', () => { - expect( - encode( - keyring.nobody.publicKey(), 1234, - extrinsics.timestamp.public.set, - [10101], - 'latest' - ) - ).toEqual( - new Uint8Array([ - // publicKey - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - // account nonce - 210, 4, 0, 0, - // extrinsic index - 3, 0, - // values - 117, 39, 0, 0, 0, 0, 0, 0 - ]) - ); - }); -}); diff --git a/packages/type-extrinsics/src/codec/encode/index.ts b/packages/type-extrinsics/src/codec/encode/index.ts deleted file mode 100644 index 0db887c4f563..000000000000 --- a/packages/type-extrinsics/src/codec/encode/index.ts +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; -import { EncodingVersions, SectionItem } from '@polkadot/params/types'; -import { Extrinsics } from '../../types'; -import { ExtrinsicWithAccount } from '../types'; - -import encodeCall from './call'; -import encodeExtrinsic from './extrinsic'; - -export default function encode (publicKey: Uint8Array, index: number | BN, extrinsic: SectionItem, values: Array, version: EncodingVersions): ExtrinsicWithAccount { - return encodeCall( - publicKey, - index, - encodeExtrinsic(extrinsic, values, version), - version - ); -} diff --git a/packages/type-extrinsics/src/codec/encode/length.ts b/packages/type-extrinsics/src/codec/encode/length.ts deleted file mode 100644 index d41622e7dd81..000000000000 --- a/packages/type-extrinsics/src/codec/encode/length.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import bnToU8a from '@polkadot/util/bn/toU8a'; -import u8aConcat from '@polkadot/util/u8a/concat'; - -export default function encodeLength (...values: Array): Uint8Array { - const length = values.reduce((length, u8a) => { - return length + u8a.length; - }, 0); - - return u8aConcat( - bnToU8a(length, 32, true), - ...values - ); -} diff --git a/packages/type-extrinsics/src/codec/encode/prefixes.ts b/packages/type-extrinsics/src/codec/encode/prefixes.ts deleted file mode 100644 index b88dc5fbb2ec..000000000000 --- a/packages/type-extrinsics/src/codec/encode/prefixes.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -// https://github.com/paritytech/polkadot/pull/195 -// 0xff, 32-byte account id -// 0xfe, 8-byte account index -// 0xfd, 4-byte account index -// 0xfc, 2-byte account index -// [0xf0...0xfb] (invalid, reserved for future use) -// [0x00...0xef] 1-byte account index (less than 0xf0) - -const bytes8 = new Uint8Array([0xfe]); -const bytes4 = new Uint8Array([0xfd]); -const bytes2 = new Uint8Array([0xfc]); -const none = new Uint8Array([]); -const publicKey = new Uint8Array([0xff]); - -export default { - bytes8, - bytes4, - bytes2, - none, - publicKey -}; diff --git a/packages/type-extrinsics/src/codec/encode/unchecked.spec.js b/packages/type-extrinsics/src/codec/encode/unchecked.spec.js deleted file mode 100644 index dc58ca32415b..000000000000 --- a/packages/type-extrinsics/src/codec/encode/unchecked.spec.js +++ /dev/null @@ -1,121 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. -// @flow - -import extrinsics from '../../index'; -import hexToU8a from '@polkadot/util/hex/toU8a'; -import testingPairs from '@polkadot/util-keyring/testingPairs'; - -import encode from './unchecked'; - -const keyring = testingPairs(); - -describe('unchecked', () => { - it('encodes extrinsic correctly (nobody)', () => { - expect( - encode( - keyring.nobody, 1234, - extrinsics.timestamp.public.set, - [10101] - ) - ).toEqual( - new Uint8Array([ - 255, - // publicKey - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - // account nonce - 210, 4, 0, 0, - // extrinsic index - 3, 0, - // values - 117, 39, 0, 0, 0, 0, 0, 0, - // signature - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]) - ); - }); - - it('encodes extrinsic correctly (one)', () => { - expect( - encode( - keyring.one, 258, - extrinsics.staking.public.transfer, - [keyring.two.publicKey(), 69], - 'poc-1' - ) - ).toEqual( - new Uint8Array([ - // publicKey - 47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238, - // account nonce - 2, 1, 0, 0, 0, 0, 0, 0, - // extrinsic index - 2, 0, - // values - // (recipient) - 215, 90, 152, 1, 130, 177, 10, 183, 213, 75, 254, 211, 201, 100, 7, 58, 14, 225, 114, 243, 218, 166, 35, 37, 175, 2, 26, 104, 247, 7, 81, 26, - // (amount) - 69, 0, 0, 0, 0, 0, 0, 0, - // signature - 144, 233, 93, 195, 106, 86, 2, 230, 182, 5, 62, 128, 120, 148, 21, 128, 86, 5, 74, 15, 128, 100, 255, 179, 172, 13, 102, 206, 117, 208, 175, 107, 107, 77, 209, 126, 129, 51, 1, 198, 184, 202, 100, 240, 0, 118, 150, 188, 67, 10, 154, 84, 224, 103, 17, 151, 163, 45, 78, 249, 219, 169, 96, 2 - ]) - ); - }); - - it('encodes extrinsic correctly (one + latest)', () => { - expect( - encode( - keyring.one, 258, - extrinsics.staking.public.transfer, - [keyring.two.publicKey(), 69] - ) - ).toEqual( - new Uint8Array([ - 255, - // publicKey - 47, 140, 97, 41, 216, 22, 207, 81, 195, 116, 188, 127, 8, 195, 230, 62, 209, 86, 207, 120, 174, 251, 74, 101, 80, 217, 123, 135, 153, 121, 119, 238, - // account nonce - 2, 1, 0, 0, - // extrinsic index - 2, 0, - // values - // (recipient) - 255, - 215, 90, 152, 1, 130, 177, 10, 183, 213, 75, 254, 211, 201, 100, 7, 58, 14, 225, 114, 243, 218, 166, 35, 37, 175, 2, 26, 104, 247, 7, 81, 26, - // (amount) - 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - // signature - 56, 1, 89, 195, 60, 171, 128, 242, 136, 64, 45, 43, 252, 199, 13, 115, 2, 76, 9, 19, 16, 80, 38, 164, 58, 229, 107, 225, 141, 107, 115, 176, 241, 242, 153, 177, 48, 92, 6, 61, 104, 112, 45, 223, 86, 198, 203, 78, 240, 223, 59, 190, 156, 116, 135, 188, 244, 219, 182, 58, 39, 140, 159, 11 - ]) - ); - }); - - it('encodes timetamp.set extrinsic correctly (actual values)', () => { - expect( - encode( - keyring.nobody, 0, - extrinsics.timestamp.public.set, - [0x5b13c37b] - ) - ).toEqual( - hexToU8a( - '0xff00000000000000000000000000000000000000000000000000000000000000000000000003007bc3135b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' - ) - ); - }); - - it('encoded parachains.setHeads correctly (actual values)', () => { - expect( - encode( - keyring.nobody, 0, - extrinsics.parachains.public.setHeads, - [[]] - ) - ).toEqual( - hexToU8a( - '0xff00000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' - ) - ); - }); -}); diff --git a/packages/type-extrinsics/src/codec/encode/unchecked.ts b/packages/type-extrinsics/src/codec/encode/unchecked.ts deleted file mode 100644 index abcb86aec0e7..000000000000 --- a/packages/type-extrinsics/src/codec/encode/unchecked.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; -import { EncodingVersions, SectionItem } from '@polkadot/params/types'; -import { UncheckedRaw } from '@polkadot/primitives/extrinsic'; -import { KeyringPair } from '@polkadot/util-keyring/types'; -import { Extrinsics } from '../../types'; - -import u8aConcat from '@polkadot/util/u8a/concat'; - -import encode from './index'; -import prefixes from './prefixes'; - -export default function unchecked (pair: KeyringPair, index: number | BN, extrinsic: SectionItem, values: Array, version: EncodingVersions = 'latest'): UncheckedRaw { - const message = encode(pair.publicKey(), index, extrinsic, values, version); - const signature = pair.sign(message); - - return u8aConcat( - version === 'poc-1' - ? prefixes.none - : prefixes.publicKey, - message, - signature - ); -} diff --git a/packages/type-extrinsics/src/codec/encode/uncheckedLength.ts b/packages/type-extrinsics/src/codec/encode/uncheckedLength.ts deleted file mode 100644 index 33a3ed5d4ef2..000000000000 --- a/packages/type-extrinsics/src/codec/encode/uncheckedLength.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; -import { EncodingVersions, SectionItem } from '@polkadot/params/types'; -import { UncheckedRaw } from '@polkadot/primitives/extrinsic'; -import { KeyringPair } from '@polkadot/util-keyring/types'; -import { Extrinsics } from '../../types'; - -import encodeUnchecked from './unchecked'; -import encodeLength from './length'; - -export default function uncheckedLength (pair: KeyringPair, index: number | BN, extrinsic: SectionItem, values: Array, version: EncodingVersions = 'latest'): UncheckedRaw { - return encodeLength( - encodeUnchecked( - pair, - index, - extrinsic, - values, - version - ) - ); -} diff --git a/packages/type-extrinsics/src/codec/types.d.ts b/packages/type-extrinsics/src/codec/types.d.ts deleted file mode 100644 index c38aa8ef5021..000000000000 --- a/packages/type-extrinsics/src/codec/types.d.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -export type ExtrinsicWithAccount = Uint8Array; - -export type ExtrinsicWithIndex = Uint8Array; - -export type ExtrinsicUnchecked = Uint8Array; diff --git a/packages/type-extrinsics/src/consensus.ts b/packages/type-extrinsics/src/consensus.ts deleted file mode 100644 index dd024e57eaa5..000000000000 --- a/packages/type-extrinsics/src/consensus.ts +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, CreateItemOptions, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; - -const reportMisbehavior: CreateItemOptions = { - description: 'Report misbehavior', - params: [ - param('report', 'MisbehaviorReport') - ], - type: [] -}; - -const setCode: CreateItemOptions = { - description: 'Set new code', - params: [ - param('code', 'Code') - ], - type: [] -}; - -const setStorage: CreateItemOptions = { - description: 'Set storage entries', - params: [ - param('entries', ['StorageKeyValue']) - ], - type: [] -}; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Consensus', - public: { - reportMisbehavior: - createMethod('reportMisbehavior', 0)(reportMisbehavior) - }, - private: { - setCode: - createMethod('setCode', 0)(setCode), - setStorage: - createMethod('setStorage', 1)(setStorage) - } - })); diff --git a/packages/type-extrinsics/src/council.ts b/packages/type-extrinsics/src/council.ts deleted file mode 100644 index 5492cc21044c..000000000000 --- a/packages/type-extrinsics/src/council.ts +++ /dev/null @@ -1,114 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, CreateItemOptions, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; - -const setApprovals: CreateItemOptions = { - description: 'Set approvals', - params: [ - param('votes', ['bool']), - param('index', 'u32') - ], - type: [] -}; - -const reapInactiveVoter: CreateItemOptions = { - description: 'Remove insactive voter', - params: [ - param('signedIndex', 'u32'), - param('who', 'AccountId'), - param('whoIndex', 'u32'), - param('assumedVoteIndex', 'u32') - ], - type: [] -}; - -const retractVoter: CreateItemOptions = { - description: 'Retract voter', - params: [ - param('index', 'u32') - ], - type: [] -}; - -const submitCandidacy: CreateItemOptions = { - description: 'Submit candidacy', - params: [ - param('slot', 'u32') - ], - type: [] -}; - -const presentWinner: CreateItemOptions = { - description: 'Present winner', - params: [ - param('candidate', 'AccountId'), - param('total', 'Balance'), - param('index', 'u32') - ], - type: [] -}; - -const setDesiredSeats: CreateItemOptions = { - description: 'Set desired seats', - params: [ - param('count', 'u32') - ], - type: [] -}; - -const removeMember: CreateItemOptions = { - description: 'Remove member', - params: [ - param('member', 'AccountId') - ], - type: [] -}; - -const setPresentationDuration: CreateItemOptions = { - description: 'Set presentation duration', - params: [ - param('duration', 'u64') - ], - type: [] -}; - -const setTermDuration: CreateItemOptions = { - description: 'Set term duration', - params: [ - param('duration', 'u64') - ], - type: [] -}; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Council', - public: { - setApprovals: - createMethod('setApprovals', 0)(setApprovals), - reapInactiveVoter: - createMethod('reapInactiveVoter', 1)(reapInactiveVoter), - retractVoter: - createMethod('retractVoter', 2)(retractVoter), - submitCandidacy: - createMethod('submitCandidacy', 3)(submitCandidacy), - presentWinner: - createMethod('presentWinner', 4)(presentWinner) - }, - private: { - setDesiredSeats: - createMethod('setDesiredSeats', 0)(setDesiredSeats), - removeMember: - createMethod('removeMember', 1)(removeMember), - setPresentationDuration: - createMethod('setPresentationDuration', 2)(setPresentationDuration), - setTermDuration: - createMethod('setTermDuration', 3)(setTermDuration) - } - })); diff --git a/packages/type-extrinsics/src/councilVoting.ts b/packages/type-extrinsics/src/councilVoting.ts deleted file mode 100644 index 55aff62b95f0..000000000000 --- a/packages/type-extrinsics/src/councilVoting.ts +++ /dev/null @@ -1,69 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, CreateItemOptions, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; - -const propose: CreateItemOptions = { - description: 'Propose', - params: [ - param('proposal', 'Proposal') - ], - type: [] -}; - -const vote: CreateItemOptions = { - description: 'Vote', - params: [ - param('proposal', 'Hash'), - param('approve', 'bool') - ], - type: [] -}; - -const veto: CreateItemOptions = { - description: 'Veto', - params: [ - param('proposal', 'Hash') - ], - type: [] -}; - -const setCooloffPeriod: CreateItemOptions = { - description: 'Set cooloff period', - params: [ - param('blocks', 'BlockNumber') - ], - type: [] -}; - -const setVotingPeriod: CreateItemOptions = { - description: 'Set voting period', - params: [ - param('blocks', 'BlockNumber') - ], - type: [] -}; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Council Voting', - public: { - propose: - createMethod('propose', 0)(propose), - vote: - createMethod('vote', 1)(vote), - veto: - createMethod('veto', 2)(veto) - }, - private: { - setCooloffPeriod: - createMethod('setCooloffPeriod', 0)(setCooloffPeriod), - setVotingPeriod: - createMethod('setVotingPeriod', 1)(setVotingPeriod) - } - })); diff --git a/packages/type-extrinsics/src/democracy.ts b/packages/type-extrinsics/src/democracy.ts deleted file mode 100644 index bde1625cf278..000000000000 --- a/packages/type-extrinsics/src/democracy.ts +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, CreateItemOptions, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; - -const propose: CreateItemOptions = { - description: 'Propose', - params: [ - param('proposal', 'Proposal'), - param('value', 'Balance') - ], - type: [] -}; - -const second: CreateItemOptions = { - description: 'Second', - params: [ - param('proposal', 'PropIndex') - ], - type: [] -}; - -const vote: CreateItemOptions = { - description: 'Vote', - params: [ - param('referendumIndex', 'ReferendumIndex'), - param('vote', 'bool') - ], - type: [] -}; - -const startReferendum: CreateItemOptions = { - description: 'Start referendum', - params: [ - param('proposal', 'Proposal'), - param('voteThreshold', 'VoteThreshold') - ], - type: [] -}; - -const cancelReferendum: CreateItemOptions = { - description: 'Cancel referendum', - params: [ - param('referendumIndex', 'ReferendumIndex') - ], - type: [] -}; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Democracy', - public: { - propose: - createMethod('propose', 0)(propose), - second: - createMethod('second', 1)(second), - vote: - createMethod('vote', 2)(vote) - }, - private: { - startReferendum: - createMethod('startReferendum', 0)(startReferendum), - cancelReferendum: - createMethod('cancelReferendum', 1)(cancelReferendum) - } - })); diff --git a/packages/type-extrinsics/src/fromMetadata.spec.js b/packages/type-extrinsics/src/fromMetadata.spec.js new file mode 100644 index 000000000000..1d61da8b5cd1 --- /dev/null +++ b/packages/type-extrinsics/src/fromMetadata.spec.js @@ -0,0 +1,34 @@ +// Copyright 2017-2018 @polkadot/storage authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import Metadata from '@polkadot/api-codec/Metadata'; +import json from '@polkadot/api-codec/Metadata.rpc.json'; + +import { fromMetadata } from './fromMetadata'; + +// Use the pre-generated metadata +const metadata = new Metadata().fromJSON(json.result); +const newExtrinsics = fromMetadata({}, metadata); + +describe('fromMetadata', () => { + it('should throw if an incorrect number of args is supplied', () => { + expect(() => newExtrinsics.balances.setBalance()).toThrowError(/expects 3 arguments/); + }); + + it('should return a value if the storage function does not expect an argument', () => { + expect(() => newExtrinsics.balances.setBalance('5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s', 2, 3)).not.toThrow(); + }); + + it('should return the correct storage key', () => { + expect(newExtrinsics.timestamp.set([10101]).toU8a()).toEqual( + new Uint8Array([ + // index + // 3, 0, // FIXME This was taken from the old tests, should work, but doesn't + 40, 0, 0, + // values + 117, 39, 0, 0, 0, 0, 0, 0 + ]) + ); + }); +}); diff --git a/packages/type-extrinsics/src/fromMetadata.ts b/packages/type-extrinsics/src/fromMetadata.ts new file mode 100644 index 000000000000..9e9fbb9e21af --- /dev/null +++ b/packages/type-extrinsics/src/fromMetadata.ts @@ -0,0 +1,42 @@ +// Copyright 2017-2018 @polkadot/storage authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import camelCase from '@polkadot/util/string/camelCase'; +import Metadata, { RuntimeModuleMetadata } from '@polkadot/api-codec/Metadata'; + +import createExtrinsic from './utils/createExtrinsic'; +import { Extrinsics, ModuleExtrinsics } from './types'; + +/** + * Extend a storage object with the storage modules & module functions present + * in the metadata. + * + * @param extrinsics - An extrinsics object to be extended. + * @param metadata - The metadata to extend the storage object against. + */ +export function fromMetadata (extrinsics: Extrinsics, metadata: Metadata) { + // Dont' clobber the input, create new + const result = Object.keys(extrinsics).reduce((result, key) => { + result[key] = extrinsics[key]; + + return result; + }, {} as Extrinsics); + + return metadata.modules.reduce((result, moduleMetadata: RuntimeModuleMetadata) => { + if (!moduleMetadata.module.call) { + return result; + } + + const prefix = moduleMetadata.prefix.toString(); + + result[moduleMetadata.prefix.toString()] = moduleMetadata.module.call.functions.reduce((newModule, func) => { + // extrinsics.balances.set_balance -> extrinsics.balances.setBalance + newModule[camelCase(func.name.toString())] = createExtrinsic(prefix, func.name, func); + + return newModule; + }, {} as ModuleExtrinsics); + + return result; + }, result); +} diff --git a/packages/type-extrinsics/src/index.ts b/packages/type-extrinsics/src/index.ts index 57396d857849..ff1aecce68e3 100644 --- a/packages/type-extrinsics/src/index.ts +++ b/packages/type-extrinsics/src/index.ts @@ -1,29 +1,9 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors +// Copyright 2017-2018 @polkadot/storage authors & contributors // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { Extrinsics } from '@polkadot/extrinsics/types'; +import { Extrinsics } from './types'; -import consensus from './consensus'; -import council from './council'; -import councilVoting from './councilVoting'; -import democracy from './democracy'; -import session from './session'; -import parachains from './parachains'; -import staking from './staking'; -import timestamp from './timestamp'; -import treasury from './treasury'; - -const extrinsics: Extrinsics = { - consensus: consensus('consensus', 0), - session: session('session', 1), - staking: staking('staking', 2), - timestamp: timestamp('timestamp', 3), - treasury: treasury('treasury', 4), - democracy: democracy('democracy', 5), - council: council('council', 6), - councilVoting: councilVoting('councilVoting', 7), - parachains: parachains('parachains', 8) -}; +const extrinsics: Extrinsics = {}; export default extrinsics; diff --git a/packages/type-extrinsics/src/parachains.ts b/packages/type-extrinsics/src/parachains.ts deleted file mode 100644 index 9cc8aca2b579..000000000000 --- a/packages/type-extrinsics/src/parachains.ts +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, CreateItemOptions, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; - -const deregisterParachain: CreateItemOptions = { - description: 'Deregister a parachain', - params: [ - param('id', 'ParachainId') - ], - type: [] -}; - -const registerParachain: CreateItemOptions = { - description: 'Register a parachain', - params: [ - param('id', 'ParachainId'), - param('code', 'Code'), - param('headData', 'Bytes') - ], - type: [] -}; - -const setHeads: CreateItemOptions = { - description: 'Sets parachain heads', - isHidden: true, // inherent - params: [ - param('heads', ['CandidateReceipt']) - ], - type: [] -}; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Parchains', - public: { - setHeads: - createMethod('setHeads', 0)(setHeads) - }, - private: { - deregisterParachain: - createMethod('deregisterParachain', 1)(deregisterParachain), - registerParachain: - createMethod('registerParachain', 0)(registerParachain) - } - })); diff --git a/packages/type-extrinsics/src/session.ts b/packages/type-extrinsics/src/session.ts deleted file mode 100644 index 27cdbf2cc34d..000000000000 --- a/packages/type-extrinsics/src/session.ts +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, CreateItemOptions, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; - -const forceNewSession: CreateItemOptions = { - description: 'Force new session', - params: [], - type: [] -}; - -const noteOffline: CreateItemOptions = { - description: 'Indicate offline validators', - params: [ - param('indexes', ['u32']) - ], - type: [] -}; - -const setKey: CreateItemOptions = { - description: 'Set session key', - params: [ - param('key', 'SessionKey') - ], - type: [] -}; - -const setLength: CreateItemOptions = { - description: 'Set session length', - params: [ - param('length', 'u64') - ], - type: [] -}; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Session', - public: { - setKey: - createMethod('setKey', 0)(setKey), - noteOffline: - createMethod('noteOffline', 1)(noteOffline) - }, - private: { - setLength: - createMethod('setLength', 0)(setLength), - forceNewSession: - createMethod('forceNewSession', 1)(forceNewSession) - } - })); diff --git a/packages/type-extrinsics/src/staking.ts b/packages/type-extrinsics/src/staking.ts deleted file mode 100644 index 394455361102..000000000000 --- a/packages/type-extrinsics/src/staking.ts +++ /dev/null @@ -1,149 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, CreateItemOptions, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; - -const nominate: CreateItemOptions = { - description: 'Nominate', - params: [ - param('target', 'AccountId') - ], - type: [] -}; - -const unnominate: CreateItemOptions = { - description: 'Unnominate', - params: [ - param('targetIndex', 'u32') - ], - type: [] -}; - -const transfer: CreateItemOptions = { - description: 'Transfer', - params: [ - param('recipient', 'AccountId'), - param('value', 'Balance') - ], - type: [] -}; - -const stake: CreateItemOptions = { - description: 'Stake', - params: [], - type: [] -}; - -const unstake: CreateItemOptions = { - description: 'Unstake', - params: [ - param('position', 'u32') - ], - type: [] -}; - -const registerSlashPreference: CreateItemOptions = { - description: 'Preference for slashing', - params: [ - param('intentionIndex', 'u32'), - // FIXME This is actually a 'SlashPreference' struct (for now only a single value) - param('unstakeThreshold', 'u32') - ], - type: [] -}; - -const noteMissedProposal: CreateItemOptions = { - description: 'Indicate that a proposal has been missed', - params: [ - param('offlineValIndices', ['u32']) - ], - type: [] -}; - -const setSessionsPerEra: CreateItemOptions = { - description: 'Set sessions per era', - params: [ - param('sessions', 'BlockNumber') - ], - type: [] -}; - -const setBondingDuration: CreateItemOptions = { - description: 'Set bonding duration', - params: [ - param('duration', 'BlockNumber') - ], - type: [] -}; - -const setValidatorCount: CreateItemOptions = { - description: 'Set validator count', - params: [ - param('count', 'u32') - ], - type: [] -}; - -const forceNewEra: CreateItemOptions = { - description: 'Force new era', - params: [], - type: [] -}; - -const setOfflineSlashGrace: CreateItemOptions = { - description: 'Sets the offline grace period before slashing', - params: [ - param('value', 'u32') - ], - type: [] -}; - -const setBalance: CreateItemOptions = { - description: 'Sets the balance for an account', - params: [ - param('who', 'AccountId'), - param('free', 'Balance'), - param('reserved', 'Balance') - ], - type: [] -}; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Staking', - public: { - transfer: - createMethod('transfer', 0)(transfer), - stake: - createMethod('stake', 1)(stake), - unstake: - createMethod('unstake', 2)(unstake), - nominate: - createMethod('nominate', 3)(nominate), - unnominate: - createMethod('unnominate', 4)(unnominate), - registerSlashPreference: - createMethod('registerSlashPreference', 5)(registerSlashPreference), - noteMissedProposal: - createMethod('noteMissedProposal', 6)(noteMissedProposal) - }, - private: { - setSessionsPerEra: - createMethod('setSessionsPerEra', 0)(setSessionsPerEra), - setBondingDuration: - createMethod('setBondingDuration', 1)(setBondingDuration), - setValidatorCount: - createMethod('setValidatorCount', 2)(setValidatorCount), - forceNewEra: - createMethod('forceNewEra', 3)(forceNewEra), - setOfflineSlashGrace: - createMethod('setOfflineSlashGrace', 4)(setOfflineSlashGrace), - setBalance: - createMethod('setBalance', 5)(setBalance) - } - })); diff --git a/packages/type-extrinsics/src/timestamp.ts b/packages/type-extrinsics/src/timestamp.ts deleted file mode 100644 index c69592a43a55..000000000000 --- a/packages/type-extrinsics/src/timestamp.ts +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, CreateItemOptions, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; - -const set: CreateItemOptions = { - description: 'Set timestamp', - isHidden: true, // inherent - params: [ - param('timestamp', 'Timestamp') - ], - type: [] -}; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Timestamp', - public: { - set: - createMethod('set', 0)(set) - }, - private: {} - })); diff --git a/packages/type-extrinsics/src/treasury.ts b/packages/type-extrinsics/src/treasury.ts deleted file mode 100644 index 954341355c2b..000000000000 --- a/packages/type-extrinsics/src/treasury.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { CreateItems, Section } from '@polkadot/params/types'; -import { Extrinsics, Extrinsic$Sections } from './types'; - -import createSection from '@polkadot/params/section'; - -export default (name: Extrinsic$Sections, index: number): Section => - createSection(name, index)((createMethod: CreateItems) => ({ - description: 'Timestamp' - })); diff --git a/packages/type-extrinsics/src/types.d.ts b/packages/type-extrinsics/src/types.d.ts index c8cc1b4e744e..5fadfda91a09 100644 --- a/packages/type-extrinsics/src/types.d.ts +++ b/packages/type-extrinsics/src/types.d.ts @@ -1,21 +1,17 @@ -// Copyright 2017-2018 @polkadot/extrinsics authors & contributors +// Copyright 2017-2018 @polkadot/storage authors & contributors // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { Section } from '@polkadot/params/types'; +import BN from 'bn.js'; -export type Extrinsics = { - // substrate - consensus: Section, - council: Section, - councilVoting: Section, - democracy: Section, - session: Section, - staking: Section, - timestamp: Section, - treasury: Section, - // polkadot - parachains: Section -}; +export interface ExtrinsicFunction { + (...args: any[]): void +} -export type Extrinsic$Sections = keyof Extrinsics; +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/createExtrinsic.ts b/packages/type-extrinsics/src/utils/createExtrinsic.ts new file mode 100644 index 000000000000..cb8d234795ec --- /dev/null +++ b/packages/type-extrinsics/src/utils/createExtrinsic.ts @@ -0,0 +1,48 @@ +// Copyright 2017-2018 @polkadot/storage authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import { createType } from '@polkadot/api-codec/codec'; +import { FunctionMetadata } from '@polkadot/api-codec/Metadata'; +import { Extrinsic, Text } from '@polkadot/api-codec/index'; +import u8aConcat from '@polkadot/util/u8a/concat'; + +import { ExtrinsicFunction } from '../types'; + +/** + * From the metadata of a function in the module's storage, generate the function + * that will return the correctly-encoded Extrinsic. + */ +export default function createExtrinsic ( + prefix: string | Text, + name: string | Text, + meta: FunctionMetadata +): ExtrinsicFunction { + let extrinsicFn: any; + + // If the extrinsic function has an argument of type `Origin`, we ignore it + // FIXME should be `arg.type !== Origin`, but doesn't work... + const expectedArgs = meta.arguments.filter((arg) => arg.type.toString() !== 'Origin'); + + extrinsicFn = (...args: any[]): Extrinsic => { + if (expectedArgs.length.valueOf() !== args.length) { + throw new Error(`Extrinsic ${prefix}.${name} expects ${expectedArgs.length.valueOf()} arguments, got ${args.length}.`); + } + + return new Extrinsic( + u8aConcat( + meta.id.toU8a(), + ...expectedArgs.map((argument, index) => { + const type = argument.type.toString(); // Argument type, as string + + return createType(type, args[index]).toU8a(true); + }) + ) + ); + + }; + + extrinsicFn.meta = meta; + + return extrinsicFn as ExtrinsicFunction; +} diff --git a/packages/type-jsonrpc/package.json b/packages/type-jsonrpc/package.json index 5b8421ab9bc7..7ab0d70eed8d 100644 --- a/packages/type-jsonrpc/package.json +++ b/packages/type-jsonrpc/package.json @@ -29,7 +29,6 @@ }, "homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-jsonrpc#readme", "dependencies": { - "@polkadot/params": "^0.30.2", "babel-runtime": "^6.26.0" } } diff --git a/packages/type-jsonrpc/src/author.ts b/packages/type-jsonrpc/src/author.ts index 2e5580d0c955..9a7b81ca484e 100644 --- a/packages/type-jsonrpc/src/author.ts +++ b/packages/type-jsonrpc/src/author.ts @@ -2,45 +2,43 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { CreateItems, CreateItemOptions, CreateItemOptionsMap, Section, SectionItems } from '@polkadot/params/types'; -import { Interfaces, Interface$Sections } from './types'; +import { MethodOpt, Section, SectionMethods } from './types'; -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; +import createMethod from './create/method'; +import createParam from './create/param'; -const pendingExtrinsics: CreateItemOptions = { +const pendingExtrinsics: MethodOpt = { description: 'Returns all pending extrinsics, potentially grouped by sender', params: [], type: 'PendingExtrinsics' }; -const submitExtrinsic: CreateItemOptions = { +const submitExtrinsic: MethodOpt = { isSigned: true, description: 'Submit a fully formatted extrinsic for block inclusion', params: [ - param('extrinsic', 'Bytes') + createParam('extrinsic', 'Bytes') ], type: 'Hash' }; -const privateMethods: CreateItemOptionsMap = {}; - -const publicMethods: CreateItemOptionsMap = { +const methods: { [index: string]: MethodOpt } = { pendingExtrinsics, submitExtrinsic }; -export type PrivateMethods = typeof privateMethods; -export type PublicMethods = typeof publicMethods; +export type Methods = typeof methods; /** * @summary Methods to work with authors & contributors. */ -export default (name: Interface$Sections): Section => - createSection(name)((createMethod: CreateItems) => ({ - description: 'Authoring of network items', - public: Object.keys(publicMethods).reduce((result, key) => { - result[key] = createMethod(key)(publicMethods[key]); - - return result; - }, {} as SectionItems) - })); +export default { + isDeprecated: false, + isHidden: false, + description: 'Authoring of network items', + name: 'author', + methods: Object.keys(methods).reduce((result, key) => { + result[key] = createMethod('author', key, methods[key]); + + return result; + }, {} as SectionMethods) +} as Section; diff --git a/packages/type-jsonrpc/src/chain.ts b/packages/type-jsonrpc/src/chain.ts index b04ef5b3b594..434587c38657 100644 --- a/packages/type-jsonrpc/src/chain.ts +++ b/packages/type-jsonrpc/src/chain.ts @@ -2,49 +2,48 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { CreateItems, CreateItemOptions, CreateItemOptionsMap, Section, SectionItems } from '@polkadot/params/types'; -import { Interfaces, Interface$Sections } from './types'; +import { MethodOpt, Section, SectionMethods } from './types'; -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; +import createMethod from './create/method'; +import createParam from './create/param'; -const getBlock: CreateItemOptions = { +const getBlock: MethodOpt = { description: 'Get header and body of a relay chain block', params: [ - param('hash', 'Hash') + createParam('hash', 'Hash') ], type: 'SignedBlock' }; -const getHead: CreateItemOptions = { +const getHead: MethodOpt = { description: 'Retrieves the best headerHash', params: [], type: 'Hash' }; -const getHeader: CreateItemOptions = { +const getHeader: MethodOpt = { description: 'Retrieves the header for a specific block', params: [ - param('hash', 'Hash') + createParam('hash', 'Hash') ], type: 'Header' }; -const getRuntimeVersion: CreateItemOptions = { +const getRuntimeVersion: MethodOpt = { description: ' Get the runtime version', params: [], type: 'RuntimeVersion' }; -const getRuntimeVersionAt: CreateItemOptions = { +const getRuntimeVersionAt: MethodOpt = { description: ' Get the runtime version at a specific block', params: [ - param('hash', 'Hash') + createParam('hash', 'Hash') ], type: 'RuntimeVersion' }; -const newHead: CreateItemOptions = { +const newHead: MethodOpt = { description: 'Retrieves the best header via subscription', subscribe: [ 'chain_subscribeNewHead', @@ -54,24 +53,23 @@ const newHead: CreateItemOptions = { type: 'Header' }; -const privateMethods: CreateItemOptionsMap = {}; - -const publicMethods: CreateItemOptionsMap = { +const methods: { [index: string]: MethodOpt } = { getBlock, getHead, getHeader, getRuntimeVersion, getRuntimeVersionAt, newHead }; -export type PublicMethods = typeof publicMethods; -export type PrivateMethods = typeof privateMethods; +export type Methods = typeof methods; /** * @summary Methods to retrieve chain data. */ -export default (name: Interface$Sections): Section => - createSection(name)((createMethod: CreateItems) => ({ - description: 'Retrieval of chain data', - public: Object.keys(publicMethods).reduce((result, key) => { - result[key] = createMethod(key)(publicMethods[key]); +export default { + isDeprecated: false, + isHidden: false, + description: 'Retrieval of chain data', + name: 'chain', + methods: Object.keys(methods).reduce((result, key) => { + result[key] = createMethod('chain', key, methods[key]); - return result; - }, {} as SectionItems) - })); + return result; + }, {} as SectionMethods) +} as Section; diff --git a/packages/type-jsonrpc/src/create/method.ts b/packages/type-jsonrpc/src/create/method.ts new file mode 100644 index 000000000000..72231b451625 --- /dev/null +++ b/packages/type-jsonrpc/src/create/method.ts @@ -0,0 +1,22 @@ +// Copyright 2017-2018 @polkadot/jsonrpc authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import { MethodOpt, Method } from '../types'; + +import isUndefined from '@polkadot/util/is/undefined'; + +export default function createMethod (section: string, name: string, { description, params, type, isDeprecated = false, isHidden = false, isSigned = false, subscribe }: MethodOpt): Method { + return { + description, + isDeprecated, + isHidden, + isSigned, + isSubscription: !isUndefined(subscribe), + name, + params, + section, + subscribe: subscribe || ['', ''], + type + }; +} diff --git a/packages/type-jsonrpc/src/create/param.ts b/packages/type-jsonrpc/src/create/param.ts new file mode 100644 index 000000000000..d2d9a858ceca --- /dev/null +++ b/packages/type-jsonrpc/src/create/param.ts @@ -0,0 +1,18 @@ +// Copyright 2017-2018 @polkadot/jsonrpc authors & contributors +// This software may be modified and distributed under the terms +// of the ISC license. See the LICENSE file for details. + +import { CodecTypes } from '@polkadot/api-codec/types'; +import { Param } from '../types'; + +type ParamOptions = { + isOptional?: boolean +}; + +export default function createParam (name: string, type: CodecTypes, { isOptional = false }: ParamOptions = { isOptional: false }): Param { + return { + isOptional, + name, + type + }; +} diff --git a/packages/type-jsonrpc/src/index.ts b/packages/type-jsonrpc/src/index.ts index 8ea408119cef..f393c0bcfd77 100644 --- a/packages/type-jsonrpc/src/index.ts +++ b/packages/type-jsonrpc/src/index.ts @@ -2,18 +2,16 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { Interfaces } from './types'; - import author from './author'; import chain from './chain'; import state from './state'; import system from './system'; -const interfaces: Interfaces = { - author: author('author'), - chain: chain('chain'), - state: state('state'), - system: system('system') +const interfaces = { + author, + chain, + state, + system }; export default interfaces; diff --git a/packages/type-jsonrpc/src/state.ts b/packages/type-jsonrpc/src/state.ts index fc81cf74909a..080102e944a7 100644 --- a/packages/type-jsonrpc/src/state.ts +++ b/packages/type-jsonrpc/src/state.ts @@ -2,145 +2,125 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { CreateItems, CreateItemOptions, CreateItemOptionsMap, Section } from '@polkadot/params/types'; -import { Interfaces, Interface$Sections } from './types'; +import { MethodOpt, Section, SectionMethods } from './types'; -import param from '@polkadot/params/param'; -import createSection from '@polkadot/params/section'; +import createMethod from './create/method'; +import createParam from './create/param'; -const call: CreateItemOptions = { +const call: MethodOpt = { description: 'Perform a call to a builtin on the chain', params: [ - param('method', 'String'), - param('data', 'Bytes') + createParam('method', 'Text'), + createParam('data', 'Bytes') ], type: 'Bytes' }; -const callAt: CreateItemOptions = { +const callAt: MethodOpt = { description: 'Perform a call to a builtin on the chain (At block)', params: [ - param('method', 'String'), - param('data', 'Bytes'), - param('block', 'Hash') + createParam('method', 'Text'), + createParam('data', 'Bytes'), + createParam('block', 'Hash') ], type: 'Bytes' }; -const getMetadata: CreateItemOptions = { +const getMetadata: MethodOpt = { description: 'Returns the runtime metadata', params: [], type: 'Metadata' }; -const getMetadataAt: CreateItemOptions = { +const getMetadataAt: MethodOpt = { description: 'Returns the runtime metadata', params: [ - param('block', 'Hash') + createParam('block', 'Hash') ], type: 'Metadata' }; -const getStorage: CreateItemOptions = { +const getStorage: MethodOpt = { description: 'Retrieves the storage for a key', params: [ - param('key', 'StorageKey') + createParam('key', 'StorageKey') ], type: 'StorageData' }; -const getStorageAt: CreateItemOptions = { +const getStorageAt: MethodOpt = { description: 'Retrieves the storage for a key at a specific block', params: [ - param('key', 'Bytes'), - param('block', 'Hash') + createParam('key', 'Bytes'), + createParam('block', 'Hash') ], type: 'Bytes' }; -const getStorageHash: CreateItemOptions = { +const getStorageHash: MethodOpt = { description: 'Retrieves the storage hash', params: [ - param('key', 'Bytes') + createParam('key', 'Bytes') ], type: 'Hash' }; -const getStorageHashAt: CreateItemOptions = { +const getStorageHashAt: MethodOpt = { description: 'Retrieves the storage hash at a specific block', params: [ - param('key', 'Bytes'), - param('block', 'Hash') + createParam('key', 'Bytes'), + createParam('block', 'Hash') ], type: 'Hash' }; -const getStorageSize: CreateItemOptions = { +const getStorageSize: MethodOpt = { description: 'Retrieves the storage size', params: [ - param('key', 'Bytes') + createParam('key', 'Bytes') ], type: 'u64' }; -const getStorageSizeAt: CreateItemOptions = { +const getStorageSizeAt: MethodOpt = { description: 'Retrieves the storage size at a specific block', params: [ - param('key', 'Bytes'), - param('block', 'Hash') + createParam('key', 'Bytes'), + createParam('block', 'Hash') ], type: 'u64' }; -const storage: CreateItemOptions = { +const storage: MethodOpt = { description: 'Subscribes to storage changes for the provided keys', subscribe: [ 'state_subscribeStorage', 'state_unsubscribeStorage' ], params: [ - param('keys', 'Vec') + // @ts-ignore The Vec<> wrap is fine + createParam('keys', 'Vec') ], type: 'StorageChangeSet' }; -const privateMethods: CreateItemOptionsMap = {}; - -const publicMethods: CreateItemOptionsMap = { - call, callAt, getStorage, getStorageAt, getStorageHash, getStorageHashAt, getStorageSize, getStorageSizeAt, storage +const methods: { [index: string]: MethodOpt } = { + call, callAt, getMetadata, getMetadataAt, getStorage, getStorageAt, getStorageHash, getStorageHashAt, getStorageSize, getStorageSizeAt, storage }; -export type PrivateMethods = typeof privateMethods; -export type PublicMethods = typeof publicMethods; +export type Methods = typeof methods; /** * @summary Query the state and state storage. */ -export default (name: Interface$Sections): Section => - createSection(name)((createMethod: CreateItems) => ({ - description: 'Query of state', - public: { - call: - createMethod('call')(call), - callAt: - createMethod('callAt')(callAt), - getMetadata: - createMethod('getMetadata')(getMetadata), - getMetadataAt: - createMethod('getMetadataAt')(getMetadataAt), - getStorage: - createMethod('getStorage')(getStorage), - getStorageAt: - createMethod('getStorageAt')(getStorageAt), - getStorageHash: - createMethod('getStorageHash')(getStorageHash), - getStorageHashAt: - createMethod('getStrorageHashAt')(getStorageHashAt), - getStorageSize: - createMethod('getStorageSize')(getStorageSize), - getStorageSizeAt: - createMethod('getStorageSizeAt')(getStorageSizeAt), - storage: - createMethod('storage')(storage) - } - })); +export default { + isDeprecated: false, + isHidden: false, + description: 'Query of state', + name: 'state', + methods: Object.keys(methods).reduce((result, key) => { + result[key] = createMethod('state', key, methods[key]); + + return result; + }, {} as SectionMethods) +} as Section; diff --git a/packages/type-jsonrpc/src/system.ts b/packages/type-jsonrpc/src/system.ts index 0020ee4ad9f5..f7555f1c20f2 100644 --- a/packages/type-jsonrpc/src/system.ts +++ b/packages/type-jsonrpc/src/system.ts @@ -2,50 +2,45 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { CreateItems, CreateItemOptions, CreateItemOptionsMap, Section } from '@polkadot/params/types'; -import { Interfaces, Interface$Sections } from './types'; +import { MethodOpt, Section, SectionMethods } from './types'; -import createSection from '@polkadot/params/section'; +import createMethod from './create/method'; -const chain: CreateItemOptions = { +const chain: MethodOpt = { description: 'Retrieves the chain', params: [], - type: 'String' + type: 'Text' }; -const name: CreateItemOptions = { +const name: MethodOpt = { description: 'Retrieves the node name', params: [], - type: 'String' + type: 'Text' }; -const version: CreateItemOptions = { +const version: MethodOpt = { description: 'Retrieves the version of the node', params: [], - type: 'String' + type: 'Text' }; -const privateMethods: CreateItemOptionsMap = {}; - -const publicMethods: CreateItemOptionsMap = { +const methods: { [index: string]: MethodOpt } = { chain, name, version }; -export type PrivateMethods = typeof privateMethods; -export type PublicMethods = typeof publicMethods; +export type Methods = typeof methods; /** * @summary Methods to retrieve system info. */ -export default (sname: Interface$Sections): Section => - createSection(sname)((createMethod: CreateItems) => ({ - description: 'Methods to retrieve system info', - public: { - chain: - createMethod('chain')(chain), - name: - createMethod('name')(name), - version: - createMethod('version')(version) - } - })); +export default { + isDeprecated: false, + isHidden: false, + description: 'Methods to retrieve system info', + name: 'system', + methods: Object.keys(methods).reduce((result, key) => { + result[key] = createMethod('system', key, methods[key]); + + return result; + }, {} as SectionMethods) +} as Section; diff --git a/packages/type-jsonrpc/src/types.d.ts b/packages/type-jsonrpc/src/types.d.ts index 63d0189405cc..c86caf7b2259 100644 --- a/packages/type-jsonrpc/src/types.d.ts +++ b/packages/type-jsonrpc/src/types.d.ts @@ -2,17 +2,46 @@ // This software may be modified and distributed under the terms // of the ISC license. See the LICENSE file for details. -import { Section } from '@polkadot/params/types'; -import { PrivateMethods as AuthorPrivate, PublicMethods as AuthorPublic} from './author'; -import { PrivateMethods as ChainPrivate, PublicMethods as ChainPublic} from './chain'; -import { PrivateMethods as StatePrivate, PublicMethods as StatePublic} from './state'; -import { PrivateMethods as SystemPrivate, PublicMethods as SystemPublic} from './system'; +import { CodecTypes } from '@polkadot/api-codec/types'; -export type Interfaces = { - author: Section, - chain: Section, - state: Section, - system: Section -} +export type Param = { + isOptional: boolean, + name: string, + type: CodecTypes +}; -export type Interface$Sections = keyof Interfaces; +export type MethodOpt = { + description: string, + isDeprecated?: boolean, + isHidden?: boolean, + isSigned?: boolean, + isSubscription?: boolean, + params: Array, + subscribe?: [string, string], + type: CodecTypes +}; + +export type Method = { + description: string, + isDeprecated: boolean, + isHidden: boolean, + isSigned: boolean, + isSubscription: boolean, + name: string, + params: Array, + section: string, + subscribe: [string, string], + type: CodecTypes +}; + +export type SectionMethods = { + [key in keyof M]: Method +}; + +export type Section = { + isDeprecated: boolean, + isHidden: boolean, + description: string, + name: string, + methods: SectionMethods +}; diff --git a/packages/type-params/.nodoc b/packages/type-params/.nodoc deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/packages/type-params/LICENSE b/packages/type-params/LICENSE deleted file mode 100644 index 15ab4076cf85..000000000000 --- a/packages/type-params/LICENSE +++ /dev/null @@ -1,15 +0,0 @@ -ISC License (ISC) - -Copyright 2017-2018 @polkadot/params authors & contributors - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. diff --git a/packages/type-params/README.md b/packages/type-params/README.md deleted file mode 100644 index 1d5f04ee7aed..000000000000 --- a/packages/type-params/README.md +++ /dev/null @@ -1,9 +0,0 @@ -[![polkadotjs](https://img.shields.io/badge/polkadot-js-orange.svg?style=flat-square)](https://polkadot.js.org) -![isc](https://img.shields.io/badge/license-ISC-lightgrey.svg?style=flat-square) -[![style](https://img.shields.io/badge/code%20style-semistandard-lightgrey.svg?style=flat-square)](https://github.com/Flet/semistandard) -[![npm](https://img.shields.io/npm/v/@polkadot/primitives.svg?style=flat-square)](https://www.npmjs.com/package/@polkadot/primitives) -[![travis](https://img.shields.io/travis/polkadot-js/api.svg?style=flat-square)](https://travis-ci.org/polkadot-js/api) -[![maintainability](https://img.shields.io/codeclimate/maintainability/polkadot-js/api.svg?style=flat-square)](https://codeclimate.com/github/polkadot-js/api/maintainability) -[![coverage](https://img.shields.io/coveralls/polkadot-js/api.svg?style=flat-square)](https://coveralls.io/github/polkadot-js/api?branch=master) -[![dependency](https://david-dm.org/polkadot-js/api.svg?style=flat-square&path=packages/type-params)](https://david-dm.org/polkadot-js/api?path=packages/type-params) -[![devDependency](https://david-dm.org/polkadot-js/api/dev-status.svg?style=flat-square&path=packages/type-params)](https://david-dm.org/polkadot-js/api?path=packages/type-params#info=devDependencies) diff --git a/packages/type-params/package.json b/packages/type-params/package.json deleted file mode 100644 index caef29d9d3f3..000000000000 --- a/packages/type-params/package.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "name": "@polkadot/params", - "version": "0.30.2", - "description": "Type defintions for parameters as passed in calls", - "main": "index.js", - "engines": { - "node": ">=6.4" - }, - "publishConfig": { - "access": "public", - "registry": "https://registry.npmjs.org" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/polkadot-js/api.git" - }, - "keywords": [ - "Polkadot" - ], - "author": "Jaco Greeff ", - "maintainers": [ - "Jaco Greeff " - ], - "contributors": [], - "license": "ISC", - "bugs": { - "url": "https://github.com/polkadot-js/api/issues" - }, - "homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-params#readme", - "dependencies": { - "@babel/runtime": "^7.0.0", - "@polkadot/extrinsics": "^0.30.2", - "@polkadot/params": "^0.30.2", - "@polkadot/primitives": "^0.30.2", - "@polkadot/util": "^0.30.7", - "@polkadot/util-keyring": "^0.30.7" - } -} diff --git a/packages/type-params/src/decode/index.spec.js b/packages/type-params/src/decode/index.spec.js deleted file mode 100644 index 7f47daecda76..000000000000 --- a/packages/type-params/src/decode/index.spec.js +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import toU8a from '@polkadot/util/u8a/toU8a'; - -import decode from './index'; - -describe('decode', () => { - it('decodes single types', () => { - expect( - JSON.stringify( - decode('BlockNumber', new Uint8Array([ - 0x69, 0, 0, 0, 0, 0, 0, 0 - ])) - ) - ).toEqual('{"length":8,"value":"69"}'); - }); - - it('decodes simple arrays', () => { - expect( - JSON.stringify( - decode(['BlockNumber'], new Uint8Array([ - 0x03, 0, 0, 0, - 0x69, 0, 0, 0, 0, 0, 0, 0, - 0x42, 0, 0, 0, 0, 0, 0, 0, - 0x15, 0, 0, 0, 0, 0, 0, 0 - ])) - ) - ).toEqual('{"length":28,"value":["69","42","15"]}'); - }); - - it('decodes tuple arrays', () => { - expect( - JSON.stringify( - decode(['BlockNumber', 'bool'], new Uint8Array([ - 0x69, 0, 0, 0, 0, 0, 0, 0, - 0x01 - ])) - ) - ).toEqual('{"length":9,"value":["69",true]}'); - }); - - it('decodes arrays with tuples', () => { - expect( - JSON.stringify( - decode([['BlockNumber', 'bool']], new Uint8Array([ - 0x03, 0, 0, 0, - 0x69, 0, 0, 0, 0, 0, 0, 0, - 0x01, - 0x42, 0, 0, 0, 0, 0, 0, 0, - 0x00, - 0x15, 0, 0, 0, 0, 0, 0, 0, - 0x01 - ])) - ) - ).toEqual('{"length":31,"value":[["69",true],["42",false],["15",true]]}'); - }); - - it('decodes tuples with arrays', () => { - expect( - JSON.stringify( - decode(['BlockNumber', ['bool']], new Uint8Array([ - 0x69, 0, 0, 0, 0, 0, 0, 0, - 0x03, 0, 0, 0, - 0x01, - 0x00, - 0x01 - ])) - ) - ).toEqual('{"length":15,"value":["69",[true,false,true]]}'); - }); - - it('decodes StorageKeyValue arrays', () => { - expect( - decode(['StorageKeyValue'], new Uint8Array([ - 2, 0, 0, 0, - 4, 0, 0, 0, - 0x11, 0x22, 0x33, 0x44, - 9, 0, 0, 0, - 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, - 1, 0, 0, 0, - 0x11, - 2, 0, 0, 0, - 0x99, 0x88 - ])) - ).toEqual({ - length: 36, - value: [ - { - key: toU8a('0x11223344'), - value: toU8a('0x998877665544332211') - }, - { - key: toU8a('0x11'), - value: toU8a('0x9988') - } - ] - }); - }); -}); diff --git a/packages/type-params/src/decode/index.ts b/packages/type-params/src/decode/index.ts deleted file mode 100644 index d3dc6b68f2f1..000000000000 --- a/packages/type-params/src/decode/index.ts +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { EncodingVersions, Param$Decoded, Param$Type$Array, Param$Types, Param$Value, Param$Value$Array } from '../types'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; -import toU8a from '@polkadot/util/u8a/toU8a'; -import isNull from '@polkadot/util/is/null'; -import isUndefined from '@polkadot/util/is/undefined'; - -import decodeValue from './value'; - -function decodeTuple (type: Param$Type$Array, input: Uint8Array | null | undefined, version: EncodingVersions, isStorage: boolean): Param$Decoded { - if (!input) { - return { - length: 0, - value: [] - }; - } - - const value: Param$Value$Array = []; - let length = 0; - - type.forEach((_type: Param$Types) => { - const decoded = decode(_type, input.subarray(length), version, isStorage); - - // NOTE small TS hack, since we don't have recursive arrays, assume primitives to push - value.push(decoded.value as Param$Value); - length += decoded.length; - }); - - return { - length, - value - }; -} - -function decodeArray ([ type ]: Param$Type$Array, input: Uint8Array | null | undefined, version: EncodingVersions, isStorage: boolean): Param$Decoded { - if (!input) { - return { - length: 0, - value: [] - }; - } - - const arrayLength = u8aToBn(input.subarray(0, 4), true).toNumber(); - const value: Param$Value$Array = []; - let length = 4; - - for (let index = 0; index < arrayLength; index++) { - const decoded = decode(type, input.subarray(length), version, isStorage); - - // NOTE small TS hack, since we don't have recursive arrays, assume primitives to push - value.push(decoded.value as Param$Value); - length += decoded.length; - } - - return { - length, - value - }; -} - -export default function decode (type: Param$Types, _input: Uint8Array | string | null | undefined, version: EncodingVersions, isStorage: boolean = false): Param$Decoded { - const input: Uint8Array | null | undefined = isUndefined(_input) || isNull(_input) - ? _input - : toU8a(_input); - - if (Array.isArray(type)) { - // Arrays have single entries, Tuples will have multiple types - if (type.length === 1) { - return decodeArray(type, input, version, isStorage); - } else { - return decodeTuple(type, input, version, isStorage); - } - } - - return decodeValue(decode, type, input, version, isStorage); -} diff --git a/packages/type-params/src/decode/types.d.ts b/packages/type-params/src/decode/types.d.ts deleted file mode 100644 index b879e3468d53..000000000000 --- a/packages/type-params/src/decode/types.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { EncodingVersions, Param$Types, Param$Decoded } from '../types'; - -export type Decoder = (type: Param$Types, input: Uint8Array, version: EncodingVersions, isStorage: boolean) => Param$Decoded; diff --git a/packages/type-params/src/decode/value/accountId.spec.js b/packages/type-params/src/decode/value/accountId.spec.js deleted file mode 100644 index ffc906a945c2..000000000000 --- a/packages/type-params/src/decode/value/accountId.spec.js +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import decode from './accountId'; - -describe('AccountId', () => { - it('decodes null as null', () => { - expect( - decode(null) - ).toEqual({ - length: 0, - value: null - }); - }); - - it('encodes without a prefix (storage)', () => { - expect( - decode( - new Uint8Array([ - 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 - ]), - 'latest', true - ) - ).toEqual({ - length: 32, - value: '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s' - }); - }); - - it('encodes without a prefix (poc-1)', () => { - expect( - decode( - new Uint8Array([ - 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 - ]), - 'poc-1', false - ) - ).toEqual({ - length: 32, - value: '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s' - }); - }); - - it('decodes with a prefix (255)', () => { - expect( - decode( - new Uint8Array([ - 255, - 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 - ]), - 'latest', false - ) - ).toEqual({ - length: 33, - value: '5C62W7ELLAAfix9LYrcx5smtcffbhvThkM5x7xfMeYXCt72s' - }); - }); - - it('codes with a prefix (1-byte)', () => { - expect( - decode( - new Uint8Array([ - 1, 17 - ]) - ) - ).toEqual({ - length: 2, - value: '0x11' - }); - }); -}); diff --git a/packages/type-params/src/decode/value/accountId.ts b/packages/type-params/src/decode/value/accountId.ts deleted file mode 100644 index f0c4f1f8ce2f..000000000000 --- a/packages/type-params/src/decode/value/accountId.ts +++ /dev/null @@ -1,52 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { EncodingVersions, Param$Decoded } from '../../types'; - -import encodeAddress from '@polkadot/util-keyring/address/encode'; -import ExtError from '@polkadot/util/ext/error'; -import u8aToHex from '@polkadot/util/u8a/toHex'; - -import u8a from './u8a'; - -export default function accountId (input: Uint8Array | null | undefined, version: EncodingVersions, isStorage: boolean): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - const withoutPrefix = isStorage || version === 'poc-1'; - const offset = withoutPrefix ? 0 : 1; - const length = withoutPrefix - ? 256 - : (() => { - const first = input[0]; - - if (first <= 0xef) { - return 1 * 8; - } else if (first === 0xfc) { - return 2 * 8; - } else if (first === 0xfd) { - return 4 * 8; - } else if (first === 0xfe) { - return 8 * 8; - } else if (first === 0xff) { - return 32 * 8; - } - - throw new ExtError(`Invalid account index byte, 0x${first.toString(16)}`); - })(); - - const u8aDecoded = u8a(input, length, offset); - const publicKey = u8aDecoded.value as Uint8Array; - - return { - length: u8aDecoded.length, - value: length === 256 - ? encodeAddress(publicKey) - : u8aToHex(publicKey) - }; -} diff --git a/packages/type-params/src/decode/value/bn.ts b/packages/type-params/src/decode/value/bn.ts deleted file mode 100644 index 22ad99e0ecde..000000000000 --- a/packages/type-params/src/decode/value/bn.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -import BN from 'bn.js'; -import u8aToBn from '@polkadot/util/u8a/toBn'; - -export default function bn (input: Uint8Array | null | undefined, bitLength: 32 | 64 | 128): Param$Decoded { - if (!input) { - return { - length: 0, - value: new BN(0) - }; - } - - const length = bitLength / 8; - - return { - length, - value: u8aToBn(input.subarray(0, length), true) - }; -} diff --git a/packages/type-params/src/decode/value/bool.ts b/packages/type-params/src/decode/value/bool.ts deleted file mode 100644 index 7a694faec3a2..000000000000 --- a/packages/type-params/src/decode/value/bool.ts +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -import isNull from '@polkadot/util/is/null'; -import isUndefined from '@polkadot/util/is/undefined'; - -export default function bool (input: Uint8Array | null | undefined): Param$Decoded { - if (isUndefined(input) || isNull(input)) { - return { - length: 0, - value: false - }; - } - - return { - length: 1, - value: input[0] !== 0 - }; -} diff --git a/packages/type-params/src/decode/value/byte.ts b/packages/type-params/src/decode/value/byte.ts deleted file mode 100644 index 40d50972e976..000000000000 --- a/packages/type-params/src/decode/value/byte.ts +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -export default function byte (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0, - value: 0 - }; - } - - return { - length: 1, - value: input[0] - }; -} diff --git a/packages/type-params/src/decode/value/bytes.ts b/packages/type-params/src/decode/value/bytes.ts deleted file mode 100644 index b4a5e42d4519..000000000000 --- a/packages/type-params/src/decode/value/bytes.ts +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -export default function bytes (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - const length = u8aToBn(input.subarray(0, 4), true).toNumber(); - - return { - length: length + 4, - value: input.subarray(4, length + 4) - }; -} diff --git a/packages/type-params/src/decode/value/code.ts b/packages/type-params/src/decode/value/code.ts deleted file mode 100644 index 120c9ee79e62..000000000000 --- a/packages/type-params/src/decode/value/code.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -export default function code (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - const length = u8aToBn(input.subarray(0, 4), true).toNumber() + 4; - const value = input.subarray(4, length); - - return { - length, - value - }; -} diff --git a/packages/type-params/src/decode/value/digest.ts b/packages/type-params/src/decode/value/digest.ts deleted file mode 100644 index 7f5101f27932..000000000000 --- a/packages/type-params/src/decode/value/digest.ts +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Digest } from '@polkadot/primitives/digest'; -import { Param$Decoded } from '../../types'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -import bytes from './bytes'; - -export default function digest (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - const logLength = input - ? u8aToBn(input.subarray(0, 4), true).toNumber() - : 0; - const value: Digest = { - logs: [] - }; - - let length = 4; - - for (let index = 0; index < logLength; index++) { - const decoded = bytes(input.subarray(length)); - - length += decoded.length; - value.logs.push((decoded.value as Uint8Array)); - } - - return { - length, - value - }; -} diff --git a/packages/type-params/src/decode/value/header.ts b/packages/type-params/src/decode/value/header.ts deleted file mode 100644 index 2c35cc1aea86..000000000000 --- a/packages/type-params/src/decode/value/header.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -import decodeHeader from '@polkadot/primitives/codec/header/decode'; - -export default function header (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - // FIXME We don't have a way to determine the length atm - const length = input.length; - const value = decodeHeader(input); - - return { - length, - value - }; -} diff --git a/packages/type-params/src/decode/value/index.ts b/packages/type-params/src/decode/value/index.ts deleted file mode 100644 index 06bb55ac49e0..000000000000 --- a/packages/type-params/src/decode/value/index.ts +++ /dev/null @@ -1,127 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { EncodingVersions, Param$Decoded, Param$Type } from '../../types'; -import { Decoder } from '../types'; - -import defaultSizes from '@polkadot/primitives/sizes'; -import toU8a from '@polkadot/util/u8a/toU8a'; -import isNull from '@polkadot/util/is/null'; -import isUndefined from '@polkadot/util/is/undefined'; - -import sizes from '../../sizes'; -import accountId from './accountId'; -import bool from './bool'; -import bn from './bn'; -import byte from './byte'; -import bytes from './bytes'; -import code from './code'; -import digest from './digest'; -import header from './header'; -import keyValue from './keyValue'; -import method from './method'; -import misbehavior from './misbehavior'; -import passThrough from './passThrough'; -import string from './string'; -import time from './time'; -import u8a from './u8a'; - -export default function decodeValue (decode: Decoder, type: Param$Type, _input: Uint8Array | string | null | undefined, version: EncodingVersions, isStorage: boolean): Param$Decoded { - const input: Uint8Array | null | undefined = isUndefined(_input) || isNull(_input) - ? _input - : toU8a(_input); - - try { - switch (type) { - // TODO Pass back the actual address, not publicKey? - case 'AccountId': - return accountId(input, version, isStorage); - - case 'AccountIndex': - return bn(input, sizes.AccountIndex.get(version) || defaultSizes.AccountIndex); - - case 'Balance': - return bn(input, sizes.Balance.get(version) || defaultSizes.Balance); - - case 'BlockNumber': - case 'Gas': - case 'SessionKey': - case 'u64': - return bn(input, 64); - - case 'bool': - return bool(input); - - case 'Bytes': - case 'StorageKey': - return bytes(input); - - case 'Call': - case 'Proposal': - return method(decode, input, type === 'Call', version, isStorage); - - case 'Code': - return code(input); - - case 'CandidateReceipt': - return passThrough(input); - - case 'Digest': - return digest(input); - - case 'Header': - return header(input); - - case 'Hash': - return u8a(input, 256, 0); - - case 'KeyValue': - case 'StorageKeyValue': - return keyValue(input); - - // HACKY, but a stopgap... - case 'Metadata': - return u8a(input, (input as Uint8Array).length * 8, 0); - - case 'MisbehaviorReport': - return misbehavior(input); - - case 'ParachainId': - case 'PropIndex': - case 'ReferendumIndex': - case 'u32': - case 'VoteIndex': - return bn(input, 32); - - case 'RuntimeVersion': - return u8a(input, 256, 0); - - case 'Signature': - return u8a(input, 512, 0); - - case 'String': - return string(input); - - case 'Timestamp': - return time(input); - - case 'u128': - return bn(input, 128); - - case 'VoteThreshold': - return byte(input); - - case 'StorageData': - case 'StorageChangeSet': - default: - // tslint:disable-next-line - (type as never); - throw new Error(`No value decoder for type='${type}'`); - } - } catch (error) { - console.error('Failed decoding', type, 'encoded as', input, error); - - throw error; - } -} diff --git a/packages/type-params/src/decode/value/keyValue.spec.js b/packages/type-params/src/decode/value/keyValue.spec.js deleted file mode 100644 index c6a94c2292cd..000000000000 --- a/packages/type-params/src/decode/value/keyValue.spec.js +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import toU8a from '@polkadot/util/u8a/toU8a'; - -import decode from './index'; - -describe('keyValue', () => { - const nullDecoder = () => {}; - - it('encodes StorageKeyValue -> Uint8Array properly', () => { - expect( - decode( - nullDecoder, - 'KeyValue', - new Uint8Array([ - 4, 0, 0, 0, - 0x11, 0x22, 0x33, 0x44, - 9, 0, 0, 0, - 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 - ]), - 'poc-1' - ) - ).toEqual({ - length: (4 + 4 + 4 + 9), - value: { - key: toU8a('0x11223344'), - value: toU8a('0x998877665544332211') - } - }); - }); - - it('encodes KeyValue -> Uint8Array properly', () => { - expect( - decode( - nullDecoder, - 'KeyValue', - new Uint8Array([ - 1, 0, 0, 0, - 0x11, - 2, 0, 0, 0, - 0x99, 0x88 - ]), - 'poc-1' - ) - ).toEqual({ - length: (4 + 1 + 4 + 2), - value: { - key: toU8a('0x11'), - value: toU8a('0x9988') - } - }); - }); -}); diff --git a/packages/type-params/src/decode/value/keyValue.ts b/packages/type-params/src/decode/value/keyValue.ts deleted file mode 100644 index 7fee34286b4b..000000000000 --- a/packages/type-params/src/decode/value/keyValue.ts +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -export default function bytes (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - const keyLength = u8aToBn(input.subarray(0, 4), true).toNumber(); - const key = input.subarray(4, keyLength + 4); - const valLength = u8aToBn(input.subarray(keyLength + 4, keyLength + 8), true).toNumber(); - const length = valLength + keyLength + 8; - const value = input.subarray(keyLength + 8, length); - - return { - length, - value: { - key, - value - } - }; -} diff --git a/packages/type-params/src/decode/value/method.ts b/packages/type-params/src/decode/value/method.ts deleted file mode 100644 index d1d61d5b9cda..000000000000 --- a/packages/type-params/src/decode/value/method.ts +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Extrinsics } from '@polkadot/extrinsics/types'; -import { EncodingVersions, ExtrinsicDecoded, Param, Param$Decoded, Section } from '../../types'; -import { Decoder } from '../types'; - -import assert from '@polkadot/util/assert'; -import extrinsics from '@polkadot/extrinsics/index'; -import isUndefined from '@polkadot/util/is/undefined'; - -// type Section = -type Result = { - length: number, - value: ExtrinsicDecoded -}; - -export default function call (decode: Decoder, input: Uint8Array | null | undefined, isPublic: boolean, version: EncodingVersions, isStorage: boolean): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - const _section: Section | undefined = Object.values(extrinsics).find(({ index }) => - index[0] === input[0] - ); - - assert(!isUndefined(_section), `Unable to find extrinsic section ${input[0]}`); - - const section = _section as Section; - const methods = section[isPublic ? 'public' : 'private']; - const extrinsic = Object.values(methods).find(({ index }) => - index[1] === input[1] - ); - - assert(!isUndefined(extrinsic), `Unable to find method ${input[1]} for section '${section.name}'`); - - // @ts-ignore checked above - const params = extrinsic.params; - const result = { - length: 2, // sectionId + methodId - value: ({ - extrinsic, - params: [] - } as ExtrinsicDecoded) - }; - - return params.reduce(({ length, value }: Result, { type }: Param) => { - const decoded = decode(type, input.subarray(length), version, isStorage); - - value.params.push(decoded.value); - - return { - length: length + decoded.length, - value - }; - }, result); -} diff --git a/packages/type-params/src/decode/value/misbehavior.ts b/packages/type-params/src/decode/value/misbehavior.ts deleted file mode 100644 index d4491dd3e547..000000000000 --- a/packages/type-params/src/decode/value/misbehavior.ts +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { MisbehaviorReport } from '@polkadot/primitives/misbehavior'; -import { Param$Decoded } from '../../types'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -const O_PHASH = 0; -const O_PNUM = 32; -const O_AUTH = O_PNUM + 8; -const O_TYPE = O_AUTH + 32; -const A_HASH = O_TYPE + 1; -const A_SIG = A_HASH + 32; -const B_HASH = A_SIG + 64; -const B_SIG = B_HASH + 32; - -const length = B_SIG + 64; - -export default function misbehavior (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0 - } as Param$Decoded; - } - - return { - length, - value: ({ - parentHash: input.subarray(O_PHASH, O_PNUM), - number: u8aToBn(input.subarray(O_PNUM, O_AUTH), true), - authorityId: input.subarray(O_AUTH, O_TYPE), - type: input[O_TYPE], - data: [ - { - header: input.subarray(A_HASH, A_SIG), - signature: input.subarray(A_SIG, B_HASH) - }, - { - header: input.subarray(B_HASH, B_SIG), - signature: input.subarray(B_SIG) - } - ] - } as MisbehaviorReport) - }; -} diff --git a/packages/type-params/src/decode/value/passThrough.ts b/packages/type-params/src/decode/value/passThrough.ts deleted file mode 100644 index f3e4332836fe..000000000000 --- a/packages/type-params/src/decode/value/passThrough.ts +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -export default function passThrough (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - return { - length: input.length, - value: input - }; -} diff --git a/packages/type-params/src/decode/value/string.ts b/packages/type-params/src/decode/value/string.ts deleted file mode 100644 index f622179f467d..000000000000 --- a/packages/type-params/src/decode/value/string.ts +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; -import u8aToUtf8 from '@polkadot/util/u8a/toUtf8'; -import isNull from '@polkadot/util/is/null'; -import isUndefined from '@polkadot/util/is/undefined'; - -export default function string (input: Uint8Array | null | undefined): Param$Decoded { - if (isUndefined(input) || isNull(input)) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - const length = u8aToBn(input.subarray(0, 4), true).toNumber(); - - return { - length: length + 4, - value: u8aToUtf8( - input.subarray(4, length + 4) - ) - }; -} diff --git a/packages/type-params/src/decode/value/time.ts b/packages/type-params/src/decode/value/time.ts deleted file mode 100644 index 1860ea7acfdb..000000000000 --- a/packages/type-params/src/decode/value/time.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -export default function time (input: Uint8Array | null | undefined): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - return { - length: 8, - value: new Date( - u8aToBn(input.subarray(0, 8), true) - .imuln(1000) - .toNumber() - ) - }; -} diff --git a/packages/type-params/src/decode/value/u8a.ts b/packages/type-params/src/decode/value/u8a.ts deleted file mode 100644 index 84b101afa4ac..000000000000 --- a/packages/type-params/src/decode/value/u8a.ts +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Decoded } from '../../types'; - -export default function u8a (input: Uint8Array | null | undefined, bitLength: number, offset: number): Param$Decoded { - if (!input) { - return { - length: 0, - value: input - } as Param$Decoded; - } - - const length = (bitLength / 8) + offset; - - return { - length, - value: input.subarray(offset, length) - }; -} diff --git a/packages/type-params/src/encode/index.ts b/packages/type-params/src/encode/index.ts deleted file mode 100644 index 72afb5ab41d9..000000000000 --- a/packages/type-params/src/encode/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { EncodingVersions, Params } from '../types'; - -import assert from '@polkadot/util/assert'; -import u8aConcat from '@polkadot/util/u8a/concat'; - -import encodeParam from './param'; - -export default function encodeParams (params: Params, values: Array, version: EncodingVersions): Uint8Array { - assert(params.length === values.length, `Expected values to have ${params.length} items, found ${values.length} instead`); - - return u8aConcat.apply(null, params.map((param, index) => - encodeParam(param, values[index], version) - )); -} diff --git a/packages/type-params/src/encode/param.spec.js b/packages/type-params/src/encode/param.spec.js deleted file mode 100644 index 048ddac7e28d..000000000000 --- a/packages/type-params/src/encode/param.spec.js +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import encodeParam from './param'; - -describe('encodeParam', () => { - it('encodes a single parameter', () => { - expect( - encodeParam({ type: 'Balance' }, 0) - ).toEqual(new Uint8Array(16)); - }); - - it('checks for array types with array values', () => { - expect( - () => encodeParam({ type: ['Balance'] }, 64) - ).toThrow(/Expected array values/); - }); - - it('encodes array types with empty array values', () => { - expect( - encodeParam({ type: ['Balance'] }, []) - ).toEqual(new Uint8Array(4)); - }); - - it('encodes array types with actual array values', () => { - expect( - encodeParam({ type: ['Balance'] }, [3, 4]) - ).toEqual( - new Uint8Array([ - 2, 0, 0, 0, - 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]) - ); - }); - - it('encodes tuple types with actual array values', () => { - expect( - encodeParam({ type: ['Balance', 'AccountIndex'] }, [3, 4]) - ).toEqual( - new Uint8Array([ - 2, 0, 0, 0, - 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 4, 0, 0, 0 - ]) - ); - }); -}); diff --git a/packages/type-params/src/encode/param.ts b/packages/type-params/src/encode/param.ts deleted file mode 100644 index 7b1d96dbf68d..000000000000 --- a/packages/type-params/src/encode/param.ts +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { EncodingVersions, Param, Param$Type } from '../types'; - -import assert from '@polkadot/util/assert'; -import u8aConcat from '@polkadot/util/u8a/concat'; -import bnToU8a from '@polkadot/util/bn/toU8a'; - -import typeToString from '../typeToString'; -import encodeType from './type'; - -export default function encodeParam (param: Param, value: any, version: EncodingVersions = 'latest'): Uint8Array { - if (Array.isArray(param.type)) { - assert(Array.isArray(value), () => `Expected array values of type '${typeToString(param.type)}'`); - - const arr = (value as Array); - const types = (param.type as Array); - - if (types.length === 1) { - return u8aConcat( - bnToU8a(arr.length, 32, true), - u8aConcat.apply(null, arr.map((v, index) => - encodeType(types[0], v, version) - )) - ); - } - - return u8aConcat( - bnToU8a(arr.length, 32, true), - u8aConcat.apply(null, arr.map((v, index) => - // NOTE since we may have embedded again, move to Param shape - encodeParam(({ type: types[index] } as Param), v, version) - )) - ); - } - - return encodeType(param.type, value, version); -} diff --git a/packages/type-params/src/encode/type/index.ts b/packages/type-params/src/encode/type/index.ts deleted file mode 100644 index 150c1ccf4532..000000000000 --- a/packages/type-params/src/encode/type/index.ts +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { EncodingVersions, Param$Type } from '../../types'; - -import addrPrefixes from '@polkadot/extrinsics/codec/encode/prefixes'; -import bnToU8a from '@polkadot/util/bn/toU8a'; -import u8aConcat from '@polkadot/util/u8a/concat'; -import u8aFromString from '@polkadot/util/u8a/fromString'; -import u8aToU8a from '@polkadot/util/u8a/toU8a'; -import addressDecode from '@polkadot/util-keyring/address/decode'; -import defaultSizes from '@polkadot/primitives/sizes'; - -import sizes from '../../sizes'; -import keyValue from './keyValue'; -import storageKey from './storageKey'; - -export default function encodeType (type: Param$Type, value: any, version: EncodingVersions): Uint8Array { - try { - switch (type) { - case 'AccountId': - return u8aConcat( - version === 'poc-1' - ? addrPrefixes.none - : addrPrefixes.publicKey, - addressDecode(value) - ); - - case 'Balance': - return bnToU8a(value, sizes.Balance.get(version) || defaultSizes.Balance, true); - - case 'BlockNumber': - case 'Gas': - case 'SessionKey': - case 'u64': - return bnToU8a(value, 64, true); - - case 'bool': - return bnToU8a(value ? 1 : 0, 8, true); - - case 'Bytes': - case 'Code': - return u8aToU8a(value); - - // FIXME Here we should pass through the actual objects - case 'Call': - case 'CandidateReceipt': - case 'Digest': - case 'Header': - case 'MisbehaviorReport': - case 'Proposal': - return u8aToU8a(value); - - // TODO Here we should do actual length conversions, i.e. 256/512 - case 'Hash': - case 'Signature': - return u8aToU8a(value); - - case 'AccountIndex': - return bnToU8a(value, sizes.AccountIndex.get(version) || defaultSizes.Balance, true); - - case 'KeyValue': - case 'StorageKeyValue': - return keyValue(value); - - case 'ParachainId': - case 'PropIndex': - case 'ReferendumIndex': - case 'VoteIndex': - case 'u32': - return bnToU8a(value, 32, true); - - case 'StorageKey': - return storageKey(value); - - case 'String': - return (() => { - const u8a = u8aFromString(value); - - return u8aConcat( - bnToU8a(u8a.length, 32, true), - u8a - ); - })(); - - case 'Timestamp': - if (value instanceof Date) { - value = Math.ceil(value.getTime() / 1000); - } - - return bnToU8a(value, 64, true); - - case 'u128': - return bnToU8a(value, 128, true); - - // TODO enums? - case 'VoteThreshold': - return bnToU8a(value || 0, 8, true); - - default: - // tslint:disable-next-line - (type as never); - throw new Error(`No formatter for ${type}`); - } - } catch (error) { - console.error('Failed encoding', type, 'with', value, error); - - throw error; - } -} diff --git a/packages/type-params/src/encode/type/keyValue.spec.js b/packages/type-params/src/encode/type/keyValue.spec.js deleted file mode 100644 index d40f55a67620..000000000000 --- a/packages/type-params/src/encode/type/keyValue.spec.js +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import toU8a from '@polkadot/util/u8a/toU8a'; - -import encode from './index'; - -describe('keyValue', () => { - it('encodes StorageKeyValue -> Uint8Array properly', () => { - expect( - encode('StorageKeyValue', { - key: toU8a('0x11223344'), - value: toU8a('0x998877665544332211') - }) - ).toEqual( - new Uint8Array([ - 4, 0, 0, 0, - 0x11, 0x22, 0x33, 0x44, - 9, 0, 0, 0, - 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11 - ]) - ); - }); - - it('encodes KeyValue -> Uint8Array properly', () => { - expect( - encode('KeyValue', { - key: toU8a('0x11'), - value: toU8a('0x9988') - }) - ).toEqual( - new Uint8Array([ - 1, 0, 0, 0, - 0x11, - 2, 0, 0, 0, - 0x99, 0x88 - ]) - ); - }); -}); diff --git a/packages/type-params/src/encode/type/keyValue.ts b/packages/type-params/src/encode/type/keyValue.ts deleted file mode 100644 index d9f734183fb2..000000000000 --- a/packages/type-params/src/encode/type/keyValue.ts +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { KeyValue } from '../../types'; - -import bnToU8a from '@polkadot/util/bn/toU8a'; -import u8aConcat from '@polkadot/util/u8a/concat'; - -// NOTE reverse of ../../decode/value/keyValue - -export default function keyValue ({ key, value }: KeyValue): Uint8Array { - return u8aConcat( - bnToU8a(key.length, 32, true), - key, - bnToU8a(value.length, 32, true), - value - ); -} diff --git a/packages/type-params/src/encode/type/storageKey.spec.js b/packages/type-params/src/encode/type/storageKey.spec.js deleted file mode 100644 index 5d61a67b827a..000000000000 --- a/packages/type-params/src/encode/type/storageKey.spec.js +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import storage from '@polkadot/storage'; -import toU8a from '@polkadot/util/u8a/toU8a'; - -import encode from './index'; - -describe.skip('storageKey', () => { - it('encodes storageKey -> Uint8Array properly', () => { - expect( - encode('StorageKey', [ - storage.balances.freeBalance, - '5EhmTa7fL6SdjgKXo9g6hetR6nHnRAmrtisoGFWEESjzECtY' - ]) - ).toEqual( - toU8a('0x11cf1094db4db43356b7787c3e59c39f') - ); - }); -}); diff --git a/packages/type-params/src/encode/type/storageKey.ts b/packages/type-params/src/encode/type/storageKey.ts deleted file mode 100644 index f4e055fded05..000000000000 --- a/packages/type-params/src/encode/type/storageKey.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { SectionItem } from '@polkadot/params/types'; -import { Storages } from '@polkadot/storage/types'; - -import createStorageKey from '@polkadot/storage/key'; - -export default function storageKey ([key, ...params]: [SectionItem, any]): Uint8Array { - return createStorageKey(key).apply(null, params); -} diff --git a/packages/type-params/src/method.ts b/packages/type-params/src/method.ts deleted file mode 100644 index 7f9293bd2342..000000000000 --- a/packages/type-params/src/method.ts +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { SectionItem, CreateItem, CreateItems, CreateItemOptions } from './types'; - -import bnToU8a from '@polkadot/util/bn/toU8a'; -import u8aConcat from '@polkadot/util/u8a/concat'; -import isUndefined from '@polkadot/util/is/undefined'; - -export default function createMethod (section: keyof T, sectionIndex: Uint8Array): CreateItems { - return (name: string, index: number = 0): CreateItem => - ({ description, key = '', params, type, isDeprecated = false, isHidden = false, isSigned = false, isUnhashed = false, subscribe }: CreateItemOptions): SectionItem => ({ - description, - index: u8aConcat(sectionIndex, bnToU8a(index, 8, true)), - isDeprecated, - isHidden, - isSigned, - isSubscription: !isUndefined(subscribe), - isUnhashed, - key, - name, - params, - section, - subscribe: subscribe || ['', ''], - type - }); -} diff --git a/packages/type-params/src/param.ts b/packages/type-params/src/param.ts deleted file mode 100644 index c103a89268f2..000000000000 --- a/packages/type-params/src/param.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param, Param$Options, Param$Types } from './types'; - -export default function createParam (name: string, type: Param$Types, { isOptional = false }: Param$Options = { isOptional: false }): Param { - return { - isOptional, - name, - type - }; -} diff --git a/packages/type-params/src/section.ts b/packages/type-params/src/section.ts deleted file mode 100644 index ab07e641fdd3..000000000000 --- a/packages/type-params/src/section.ts +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Section, CreateSection, CreateSectionOptions } from './types'; - -import bnToU8a from '@polkadot/util/bn/toU8a'; - -import createMethod from './method'; - -export default function createSection (name: keyof T, _index: number = 0): CreateSection { - const index = bnToU8a(_index, 8, true); - const creator = (optOrFn: CreateSectionOptions): Section => { - if (typeof optOrFn === 'function') { - return creator( - optOrFn( - createMethod(name, index) - ) - ); - } - - const { description, isDeprecated = false, isHidden = false } = optOrFn; - - return { - description, - isDeprecated, - isHidden, - index, - name, - private: optOrFn.private || {}, - public: optOrFn.public || {} - }; - }; - - return creator; -} diff --git a/packages/type-params/src/signature.spec.js b/packages/type-params/src/signature.spec.js deleted file mode 100644 index 1d3185d2db3f..000000000000 --- a/packages/type-params/src/signature.spec.js +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import param from './param'; -import signature from './signature'; - -describe('jsonrpcSignature', () => { - it('formats the signature according to the specification', () => { - expect( - signature({ - name: 'test', - description: 'test', - params: [ - param('a', 'AccountId'), - param('b', 'Bytes') - ], - type: 'Signature' - }) - ).toEqual('test (a: AccountId, b: Bytes): Signature'); - }); - - it('empty inputs format correctly', () => { - expect( - signature({ - name: 'test', - description: 'test', - params: [], - type: 'AccountId' - }) - ).toEqual('test (): AccountId'); - }); -}); diff --git a/packages/type-params/src/signature.ts b/packages/type-params/src/signature.ts deleted file mode 100644 index 2272874e28ee..000000000000 --- a/packages/type-params/src/signature.ts +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { SectionItem } from './types'; - -import typeToString from './typeToString'; - -/** - * @name signature - * @signature jsonrpcSignature (method: InterfaceMethodDefinition): string - * @summary Returns a string representation of the method with inputs and outputs. - * @description - * Formats the name, inputs and outputs into a human-readable string. This contains the input parameter names input types and output type. - * @example - * import signature from '@polkadot/params/signature'; - * - * signature({ name: 'test_method', params: [ { name: 'dest', type: 'Address' } ], type: 'Address' }); // => test_method (dest: Address): Address - */ -export default function signature ({ name, params, type }: SectionItem): string { - const inputs = params.map(({ name, type }) => - `${name}: ${typeToString(type)}` - ).join(', '); - - return `${name} (${inputs}): ${typeToString(type)}`; -} diff --git a/packages/type-params/src/sizes.ts b/packages/type-params/src/sizes.ts deleted file mode 100644 index db0a82b1812d..000000000000 --- a/packages/type-params/src/sizes.ts +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import sizes, { SizeType } from '@polkadot/primitives/sizes'; -import { EncodingVersions } from './types'; - -const Balance: Map = new Map([ - ['latest' as EncodingVersions, sizes.Balance], - ['poc-1' as EncodingVersions, 64 as SizeType] -]); - -const AccountIndex: Map = new Map([ - ['latest' as EncodingVersions, sizes.AccountIndex], - ['poc-1' as EncodingVersions, 64 as SizeType] -]); - -export default { - AccountIndex, - Balance -}; diff --git a/packages/type-params/src/typeToString.spec.js b/packages/type-params/src/typeToString.spec.js deleted file mode 100644 index c413d871cbb4..000000000000 --- a/packages/type-params/src/typeToString.spec.js +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import typeToString from './typeToString'; - -describe('typeToString', () => { - it('formats single types', () => { - expect( - typeToString('BlockNumber') - ).toEqual('BlockNumber'); - }); - - it('formats array types', () => { - expect( - typeToString(['BlockNumber']) - ).toEqual('Array'); - }); - - it('formats tuple types', () => { - expect( - typeToString(['AccountId', 'BlockNumber']) - ).toEqual('(AccountId, BlockNumber)'); - }); - - it('formats tuple arrays', () => { - expect( - typeToString([['AccountId', 'BlockNumber']]) - ).toEqual('Array<(AccountId, BlockNumber)>'); - }); -}); diff --git a/packages/type-params/src/typeToString.ts b/packages/type-params/src/typeToString.ts deleted file mode 100644 index 0212647043e2..000000000000 --- a/packages/type-params/src/typeToString.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Param$Types } from './types'; - -export default function typeToString (type: Param$Types): string { - if (!Array.isArray(type)) { - return type; - } - - const text = type.map(typeToString).join(', '); - - return type.length !== 1 - ? `(${text})` - : `Array<${text}>`; -} diff --git a/packages/type-params/src/types.d.ts b/packages/type-params/src/types.d.ts deleted file mode 100644 index b73376c444cb..000000000000 --- a/packages/type-params/src/types.d.ts +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright 2017-2018 @polkadot/params authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; -import { Extrinsics } from '@polkadot/extrinsics/types'; -import { Digest } from '@polkadot/primitives/digest'; -import { Header } from '@polkadot/primitives/header'; -import { MisbehaviorReport } from '@polkadot/primitives/misbehavior'; - -export type EncodingVersions = 'poc-1' | 'latest'; - -// TODO: Also update the following -// - @polkadot/storage/key/params.ts -// - decode/value/index.js -// - encode/type/index.js -export type Param$Type = 'AccountId' | 'AccountIndex' | 'Balance' | 'BlockNumber' | 'bool' | 'Bytes' | 'Call' | 'CandidateReceipt' | 'Code' | 'Digest' | 'Gas' | 'Hash' | 'Header' | 'KeyValue' | 'Metadata' | 'MisbehaviorReport' | 'ParachainId' | 'PendingExtrinsics' | 'PropIndex' | 'Proposal' | 'ReferendumIndex' | 'RuntimeVersion' | 'SessionKey' | 'Signature' | 'SignedBlock' | 'StorageKey' | 'StorageKeyValue' | 'StorageData' | 'StorageChangeSet' | 'String' | 'Timestamp' | 'u32' | 'u64' | 'u128' | 'VoteIndex' | 'VoteThreshold' | 'Vec'; - -export type Param$Type$Array = Array>>; - -export type Param$Types = Param$Type | Param$Type$Array; - -export type ExtrinsicDecoded = { - extrinsic: SectionItem, - params: Array -} - -export type BlockExtrinsicDecoded = { - address: string, - accountIndex: BN, - extrinsic: SectionItem, - params: Array, - signature: Uint8Array -} - -export type BlockJustificationDecoded = { - hash: Uint8Array, - round: BN, - signatures: Array<{ - address: string, - signature: Uint8Array - }> -} - -export type BlockDecoded = { - header: Header, - extrinsics: Array, - justification: BlockJustificationDecoded -}; - -export type Metadata = { -}; - -export type KeyValue = { - key: Uint8Array, - value: Uint8Array -} - -export type Param$Value = Digest | Header | KeyValue | Metadata | MisbehaviorReport | ExtrinsicDecoded | BN | Date | Uint8Array | boolean | number | string | null; - -export type Param$Value$Array = Array>>; - -export type Param$Values = Param$Value | Param$Value$Array; - -export type Param$Decoded = { - length: number, - value: Param$Values -} - -export type Param$Options = { - isOptional: boolean -}; - -export type Param = Param$Options & { - name: string, - type: Param$Types -}; - -export type Params = Array; - -// FIXME: This is a bit horrible, since not all the params (apart from deprecated/hidden) is applicable to all. Not a major inconveninece, but it is itching -export type SectionItem = { - description: string, - index: Uint8Array, // only for extrinsics, - isDeprecated: boolean, - isHidden: boolean, - isSigned: boolean, // only for extrinsics - isSubscription: boolean, // only for jsonrpc - isUnhashed: boolean, // only for storage - key: string, // only for storage - name: string, - params: Params, - section: keyof T, - subscribe: [string, string], // only for jsonrpc - type: Param$Types -}; - -export type SectionItems = { - [key in keyof K]: SectionItem -}; - -export type Section = { - isDeprecated: boolean, - isHidden: boolean, - description: string, - index: Uint8Array, - name: keyof T, - private: SectionItems, - public: SectionItems -}; - -export type Sections = Map>; - -export type CreateItemOptions = { - description: string, - isDeprecated?: boolean, - isHidden?: boolean, - isSigned?: boolean, - isUnhashed?: boolean, - key?: string, - params: Params, - subscribe?: [string, string], - type: Param$Types -}; - -export type CreateItemOptionsMap = { - [index: string]: CreateItemOptions -}; - -export type CreateItem = (options: CreateItemOptions) => SectionItem; - -export type CreateItems = (name: string, index?: number) => CreateItem; - -export type CreateSectionOptions$Only = { - description: string, - isDeprecated?: boolean, - isHidden?: boolean, - 'private'?: SectionItems, - 'public'?: SectionItems -}; - -export type CreateSectionOptions$Fn = (method: CreateItems) => CreateSectionOptions$Only; - -export type CreateSectionOptions = CreateSectionOptions$Only | CreateSectionOptions$Fn; - -export type CreateSection = (options: CreateSectionOptions) => Section; diff --git a/packages/type-primitives/.nodoc b/packages/type-primitives/.nodoc deleted file mode 100644 index e69de29bb2d1..000000000000 diff --git a/packages/type-primitives/LICENSE b/packages/type-primitives/LICENSE deleted file mode 100644 index 182ebe3bb17a..000000000000 --- a/packages/type-primitives/LICENSE +++ /dev/null @@ -1,15 +0,0 @@ -ISC License (ISC) - -Copyright 2017-2018 @polkadot/primitives authors & contributors - -Permission to use, copy, modify, and/or distribute this software for any -purpose with or without fee is hereby granted, provided that the above -copyright notice and this permission notice appear in all copies. - -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM -LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR -OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR -PERFORMANCE OF THIS SOFTWARE. diff --git a/packages/type-primitives/README.md b/packages/type-primitives/README.md deleted file mode 100644 index 70e18d2b79a6..000000000000 --- a/packages/type-primitives/README.md +++ /dev/null @@ -1,33 +0,0 @@ -[![polkadotjs](https://img.shields.io/badge/polkadot-js-orange.svg?style=flat-square)](https://polkadot.js.org) -![isc](https://img.shields.io/badge/license-ISC-lightgrey.svg?style=flat-square) -[![style](https://img.shields.io/badge/code%20style-semistandard-lightgrey.svg?style=flat-square)](https://github.com/Flet/semistandard) -[![npm](https://img.shields.io/npm/v/@polkadot/primitives.svg?style=flat-square)](https://www.npmjs.com/package/@polkadot/primitives) -[![travis](https://img.shields.io/travis/polkadot-js/api.svg?style=flat-square)](https://travis-ci.org/polkadot-js/api) -[![maintainability](https://img.shields.io/codeclimate/maintainability/polkadot-js/api.svg?style=flat-square)](https://codeclimate.com/github/polkadot-js/api/maintainability) -[![coverage](https://img.shields.io/coveralls/polkadot-js/api.svg?style=flat-square)](https://coveralls.io/github/polkadot-js/api?branch=master) -[![dependency](https://david-dm.org/polkadot-js/api.svg?style=flat-square&path=packages/type-primitives)](https://david-dm.org/polkadot-js/api?path=packages/type-primitives) -[![devDependency](https://david-dm.org/polkadot-js/api/dev-status.svg?style=flat-square&path=packages/type-primitives)](https://david-dm.org/polkadot-js/api?path=packages/type-primitives#info=devDependencies) - -# @polkadot/primitives - -Base [flow](https://flow.org/) definitions for the base Polkadot types as defined in the [specification](https://github.com/w3f/polkadot-spec). It is useful for implementations, applications and libraries, where type-checking of the JavaScript base types is of importance. - -## Usage - -Installation - - -``` -npm install --save @polkadot/primitives -``` - -Usage - - -```js -// @flow - -import type { AccountId, Balance } from '@polkadot/primitives/base'; - -function getBalance (accountId: AccountId): Balance { - ... -} -``` diff --git a/packages/type-primitives/package.json b/packages/type-primitives/package.json deleted file mode 100644 index 28d5cbc0c1ce..000000000000 --- a/packages/type-primitives/package.json +++ /dev/null @@ -1,38 +0,0 @@ -{ - "name": "@polkadot/primitives", - "version": "0.30.2", - "description": "Type defintions for the Polkadot network", - "main": "index.js", - "engines": { - "node": ">=6.4" - }, - "publishConfig": { - "access": "public", - "registry": "https://registry.npmjs.org" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/polkadot-js/api.git" - }, - "keywords": [ - "Polkadot" - ], - "author": "Jaco Greeff ", - "maintainers": [ - "Jaco Greeff " - ], - "contributors": [], - "license": "ISC", - "bugs": { - "url": "https://github.com/polkadot-js/api/issues" - }, - "homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-primitives#readme", - "dependencies": { - "@babel/runtime": "^7.0.0", - "@polkadot/trie-hash": "^0.30.7", - "@polkadot/util": "^0.30.7" - }, - "devDependencies": { - "bn.js": "^4.11.8" - } -} diff --git a/packages/type-primitives/src/base.d.ts b/packages/type-primitives/src/base.d.ts deleted file mode 100644 index 7e1c524091ad..000000000000 --- a/packages/type-primitives/src/base.d.ts +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; - -export type Bytes = Uint8Array; -export type Hash = Uint8Array; -export type H160 = Uint8Array; -export type H256 = Uint8Array; -export type H512 = Uint8Array; -export type U32 = BN; -export type U64 = BN; -export type U128 = BN; -export type U256 = BN; - -export type AccountId = H256; -export type Balance = U128; -export type BlockNumber = U64; -export type ChainId = U32; -export type HeaderHash = H256; -export type Index = U32; -export type ObjectId = U64; -export type ParaChainId = U64; -export type Proportion = U64; -export type Signature = H512; -export type Timestamp = U64; diff --git a/packages/type-primitives/src/bft.d.ts b/packages/type-primitives/src/bft.d.ts deleted file mode 100644 index b0b849a0d927..000000000000 --- a/packages/type-primitives/src/bft.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { AccountId, HeaderHash, Signature } from './base'; - -export type Justification$Signature = [AccountId, Signature]; - -export type Justification = { - roundNumber: number, - hash: HeaderHash, - signatures: Array -} diff --git a/packages/type-primitives/src/block.d.ts b/packages/type-primitives/src/block.d.ts deleted file mode 100644 index 9db668a99991..000000000000 --- a/packages/type-primitives/src/block.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Header } from './header'; -import { UncheckedRaw } from './extrinsic'; - -export type Block = { - header: Header, - extrinsics: Array -}; diff --git a/packages/type-primitives/src/candidate.d.ts b/packages/type-primitives/src/candidate.d.ts deleted file mode 100644 index 0624915f9901..000000000000 --- a/packages/type-primitives/src/candidate.d.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { AccountId, Balance, Bytes, ChainId, Hash, Signature } from './base'; - -export type Candidate = { - parachainIndex: ChainId, - collatorSignature: Signature, - unprocessedIngress: Array>>, - blockData: Bytes -}; - -export type CandidateReceipt$BalanceUpload = [AccountId, Balance]; -export type CandidateReceipt$EgressQueueRoot = [ChainId, Hash]; - -export type CandidateReceipt = { - parachainIndex: ChainId, - collator: AccountId, - headData: Bytes, - balanceUploads: Array, - egressQueueRoots: Array, - fees: Balance -}; diff --git a/packages/type-primitives/src/codec/block/decode.ts b/packages/type-primitives/src/codec/block/decode.ts deleted file mode 100644 index 9098ffd8e2b2..000000000000 --- a/packages/type-primitives/src/codec/block/decode.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Block } from '../../block'; - -import createBlock from '../../create/block'; -import decodeHeader from '../header/decode'; -import decodeRaw from './decodeRaw'; - -export default function decodeBlock (u8a: Uint8Array): Block { - const { header, extrinsics } = decodeRaw(u8a); - - return createBlock({ - header: decodeHeader(header), - extrinsics - }); -} diff --git a/packages/type-primitives/src/codec/block/decodeRaw.spec.js b/packages/type-primitives/src/codec/block/decodeRaw.spec.js deleted file mode 100644 index 7323c0f0ddfc..000000000000 --- a/packages/type-primitives/src/codec/block/decodeRaw.spec.js +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import extrinsics from '@polkadot/extrinsics'; -import encodeUnchecked from '@polkadot/extrinsics/codec/encode/unchecked'; -import hexToU8a from '@polkadot/util/hex/toU8a'; -import testingPairs from '@polkadot/util-keyring/testingPairs'; - -import decode from './decodeRaw'; - -const keyring = testingPairs(); - -describe('decodeRaw', () => { - it('encodes the block properly', () => { - expect( - decode( - hexToU8a( - '0x' + - // parent_hash - '0900000000000000000000000000000000000000000000000000000000000009' + - // number - '4300000000000000' + - // state_root - '0800000000000000000000000000000000000000000000000000000000000008' + - // transaction_root - '0700000000000000000000000000000000000000000000000000000000000007' + - // digest - '02000000' + - '0100000001' + - '0100000002' + - // transactions - '01000000' + - '6f000000' + - // prefix - 'ff' + - '0000000000000000000000000000000000000000000000000000000000000000' + - '00000000' + - '0300' + - '7527000000000000' + - '0000000000000000000000000000000000000000000000000000000000000000' + - '0000000000000000000000000000000000000000000000000000000000000000' - ) - ) - ).toMatchObject({ - extrinsics: [ - encodeUnchecked( - keyring.nobody, 0, - extrinsics.timestamp.public.set, - [10101] - ) - ] - }); - }); -}); diff --git a/packages/type-primitives/src/codec/block/decodeRaw.ts b/packages/type-primitives/src/codec/block/decodeRaw.ts deleted file mode 100644 index a310846f3d4c..000000000000 --- a/packages/type-primitives/src/codec/block/decodeRaw.ts +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; - -import decodeExtrinsic from '@polkadot/extrinsics/codec/decode/length'; -import u8aToBn from '@polkadot/util/u8a/toBn'; - -import decodeHeader from '../header/decodeRaw'; - -type RawData = { - body: Uint8Array, - extrinsics: Array, - header: Uint8Array, - number: BN -}; - -export default function decodeRaw (u8a: Uint8Array): RawData { - // tslint:disable-next-line:variable-name - const { body, header, number } = decodeHeader(u8a); - const length = u8aToBn(body.subarray(0, 4), true).toNumber(); - const extrinsics = []; - let offset = 4; - - for (let index = 0; index < length; index++) { - const extrinsic = decodeExtrinsic( - body.subarray(offset) - ); - - offset += extrinsic.length + 4; - extrinsics.push(extrinsic); - } - - return { - body, - extrinsics, - header, - number - }; -} diff --git a/packages/type-primitives/src/codec/block/encode.spec.js b/packages/type-primitives/src/codec/block/encode.spec.js deleted file mode 100644 index 40b32dfeec3f..000000000000 --- a/packages/type-primitives/src/codec/block/encode.spec.js +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import extrinsics from '@polkadot/extrinsics'; -import encodeUnchecked from '@polkadot/extrinsics/codec/encode/unchecked'; -import hexToU8a from '@polkadot/util/hex/toU8a'; -import testingPairs from '@polkadot/util-keyring/testingPairs'; - -import blockBuilder from '../../create/block'; -import { encodeBlock } from './index'; - -const keyring = testingPairs(); - -describe('encodeBlock', () => { - it('encodes a block correctly (actual values)', () => { - expect( - encodeBlock( - blockBuilder({ - header: { - number: 9, - parentHash: hexToU8a('0x737d699e4e42f78dc2fb098f01bb85389f2c7f0a77bb41918adac6bfa5b3ee46'), - stateRoot: hexToU8a('0xf707774183723f201b875024069f30e7089b7f83c3c3cdc534b10d15fca91262') - }, - extrinsics: [ - encodeUnchecked - (keyring.nobody, 0, - extrinsics.timestamp.public.set, - [0x5b13c3a4] - ), - encodeUnchecked( - keyring.nobody, 0, - extrinsics.parachains.public.setHeads, - [[]] - ) - ] - }) - ) - ).toEqual( - new Uint8Array([ - 115, 125, 105, 158, 78, 66, 247, 141, 194, 251, 9, 143, 1, 187, 133, 56, 159, 44, 127, 10, 119, 187, 65, 145, 138, 218, 198, 191, 165, 179, 238, 70, 9, 0, 0, 0, 0, 0, 0, 0, 247, 7, 119, 65, 131, 114, 63, 32, 27, 135, 80, 36, 6, 159, 48, 231, 8, 155, 127, 131, 195, 195, 205, 197, 52, 177, 13, 21, 252, 169, 18, 98, 140, 102, 133, 98, 214, 123, 73, 173, 122, 252, 247, 48, 181, 86, 77, 188, 213, 161, 17, 19, 73, 96, 158, 181, 249, 69, 97, 15, 186, 12, 45, 157, 0, 0, 0, 0, 2, 0, 0, 0, 111, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 164, 195, 19, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 107, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]) - // hexToU8a( - // '0x737d699e4e42f78dc2fb098f01bb85389f2c7f0a77bb41918adac6bfa5b3ee460900000000000000f707774183723f201b875024069f30e7089b7f83c3c3cdc534b10d15fca91262ab602f7974bbfb513f021f39777e2195094dc64a2c1b7c82a1781cb9fd4768a800000000020000006e0000000000000000000000000000000000000000000000000000000000000000000000000000000300a4c3135b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006a00000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000' - // ) - ); - }); -}); diff --git a/packages/type-primitives/src/codec/block/encode.ts b/packages/type-primitives/src/codec/block/encode.ts deleted file mode 100644 index 245a70eefbff..000000000000 --- a/packages/type-primitives/src/codec/block/encode.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Block } from '../../block'; - -import headerEncode from '../header/encode'; -import encodeRaw from './encodeRaw'; - -export default function encodeBlock ({ extrinsics, header }: Block): Uint8Array { - return encodeRaw( - headerEncode(header, extrinsics), - extrinsics - ); -} diff --git a/packages/type-primitives/src/codec/block/encodeRaw.spec.js b/packages/type-primitives/src/codec/block/encodeRaw.spec.js deleted file mode 100644 index 7a1e08843045..000000000000 --- a/packages/type-primitives/src/codec/block/encodeRaw.spec.js +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import encodeRaw from './encodeRaw'; - -describe('encodeRaw', () => { - it('encodes correctly (actual values)', () => { - expect( - encodeRaw( - new Uint8Array([ - 168, 135, 224, 93, 140, 222, 226, 83, 13, 116, 138, 197, 164, 6, 48, 190, 101, 18, 221, 166, 40, 179, 158, 112, 133, 154, 215, 198, 177, 76, 212, 228, - 1, 0, 0, 0, 0, 0, 0, 0, - 159, 216, 180, 61, 159, 12, 131, 80, 22, 196, 220, 94, 6, 222, 68, 159, 97, 60, 16, 69, 163, 149, 125, 138, 175, 36, 30, 93, 235, 4, 1, 168, - 245, 208, 141, 24, 250, 136, 158, 84, 67, 170, 59, 146, 17, 149, 62, 67, 254, 66, 126, 53, 20, 146, 252, 243, 137, 89, 224, 145, 252, 79, 109, 15, - 0, 0, 0, 0 - ]), - [ - new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 188, 29, 21, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]), - new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]) - ] - ) - ).toEqual( - new Uint8Array([ - 168, 135, 224, 93, 140, 222, 226, 83, 13, 116, 138, 197, 164, 6, 48, 190, 101, 18, 221, 166, 40, 179, 158, 112, 133, 154, 215, 198, 177, 76, 212, 228, 1, 0, 0, 0, 0, 0, 0, 0, 159, 216, 180, 61, 159, 12, 131, 80, 22, 196, 220, 94, 6, 222, 68, 159, 97, 60, 16, 69, 163, 149, 125, 138, 175, 36, 30, 93, 235, 4, 1, 168, 245, 208, 141, 24, 250, 136, 158, 84, 67, 170, 59, 146, 17, 149, 62, 67, 254, 66, 126, 53, 20, 146, 252, 243, 137, 89, 224, 145, 252, 79, 109, 15, 0, 0, 0, 0, 2, 0, 0, 0, - 110, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 188, 29, 21, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 106, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]) - ); - }); -}); diff --git a/packages/type-primitives/src/codec/block/encodeRaw.ts b/packages/type-primitives/src/codec/block/encodeRaw.ts deleted file mode 100644 index 06d619b3386b..000000000000 --- a/packages/type-primitives/src/codec/block/encodeRaw.ts +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { UncheckedRaw } from '../../extrinsic'; - -import uncheckedLength from '@polkadot/extrinsics/codec/encode/length'; -import u8aConcat from '@polkadot/util/u8a/concat'; - -import encodeArray from '../encoder/array'; - -export default function encodeBlockRaw (header: Uint8Array, extrinsics: Array = []): Uint8Array { - return u8aConcat( - header, - encodeArray( - extrinsics.map((extrinsic) => - uncheckedLength(extrinsic) - ) - ) - ); -} diff --git a/packages/type-primitives/src/codec/block/index.ts b/packages/type-primitives/src/codec/block/index.ts deleted file mode 100644 index c2525b53575e..000000000000 --- a/packages/type-primitives/src/codec/block/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import encodeBlock from './encode'; - -export { - encodeBlock -}; diff --git a/packages/type-primitives/src/codec/decoder/array.spec.js b/packages/type-primitives/src/codec/decoder/array.spec.js deleted file mode 100644 index 8a4c913d4c43..000000000000 --- a/packages/type-primitives/src/codec/decoder/array.spec.js +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import decodeArray from './array'; - -describe('decodeArray', () => { - it('decodes an array with the length', () => { - expect( - decodeArray( - new Uint8Array([ - 2, 0, 0, 0, // length - 1, 2, // first - 3, 4 // second - ]) - ) - ).toEqual({ - length: 2, - data: new Uint8Array([1, 2, 3, 4]) - }); - }); - - it('decodes where array is an subarray', () => { - expect( - decodeArray( - new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, // padding - 1, 0, 0, 0, // length - 1, 2, 3, 4 // entry - ]).subarray(8) - ) - ).toEqual({ - length: 1, - data: new Uint8Array([1, 2, 3, 4]) - }); - }); -}); diff --git a/packages/type-primitives/src/codec/decoder/array.ts b/packages/type-primitives/src/codec/decoder/array.ts deleted file mode 100644 index 76365a9a6f2d..000000000000 --- a/packages/type-primitives/src/codec/decoder/array.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -type DecodedArray = { - length: number, - data: Uint8Array -}; - -export default function decodeArray (input: Uint8Array): DecodedArray { - return { - // FIXME: This conversion is horrible, however Dataview on subarray yields wrong getUint32 value - length: u8aToBn(input.subarray(0, 4), true).toNumber(), - data: input.slice(4) - }; -} diff --git a/packages/type-primitives/src/codec/decoder/u8a.spec.js b/packages/type-primitives/src/codec/decoder/u8a.spec.js deleted file mode 100644 index cd99895a55ba..000000000000 --- a/packages/type-primitives/src/codec/decoder/u8a.spec.js +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import decodeU8a from './u8a'; - -describe('decodeU8a', () => { - it('decodes an input with the length', () => { - expect( - decodeU8a( - new Uint8Array([ - 4, 0, 0, 0, // length - 1, 2, 3, 4, // data - 5, 5, 5, 5 // extra - ]) - ) - ).toEqual( - new Uint8Array([1, 2, 3, 4]) - ); - }); - - it('decodes an subarray', () => { - expect( - decodeU8a( - new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, // padding - 4, 0, 0, 0, // length - 1, 2, 3, 4, // data - 5, 5, 5, 5 // extra - ]).subarray(8) - ) - ).toEqual( - new Uint8Array([1, 2, 3, 4]) - ); - }); -}); diff --git a/packages/type-primitives/src/codec/decoder/u8a.ts b/packages/type-primitives/src/codec/decoder/u8a.ts deleted file mode 100644 index 6b4c75d1352f..000000000000 --- a/packages/type-primitives/src/codec/decoder/u8a.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -export default function decodeU8a (input: Uint8Array): Uint8Array { - // FIXME: This conversion is horrible, however Dataview on subarray yields wrong getUint32 value - const length = u8aToBn(input.subarray(0, 4), true).toNumber(); - - return input.slice(4, 4 + length); -} diff --git a/packages/type-primitives/src/codec/encoder/array.spec.js b/packages/type-primitives/src/codec/encoder/array.spec.js deleted file mode 100644 index 6d308cccbb03..000000000000 --- a/packages/type-primitives/src/codec/encoder/array.spec.js +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import encodeArray from './array'; - -describe('encodeArray', () => { - it('encodes an array with the length', () => { - expect( - encodeArray([ - new Uint8Array([1, 2]), - new Uint8Array([3, 4]) - ]) - ).toEqual( - new Uint8Array([ - 2, 0, 0, 0, // length - 1, 2, // first - 3, 4 // second - ]) - ); - }); -}); diff --git a/packages/type-primitives/src/codec/encoder/array.ts b/packages/type-primitives/src/codec/encoder/array.ts deleted file mode 100644 index 9682bcb96db7..000000000000 --- a/packages/type-primitives/src/codec/encoder/array.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -export default function encodeArray (input: Array): Uint8Array { - const total = input.reduce((total, input) => total + input.length, 0); - const output = new Uint8Array(total + 4); - let offset = 4; - - new DataView(output.buffer).setUint32(0, input.length, true); - - return input.reduce((output, input) => { - output.set(input, offset); - offset += input.length; - - return output; - }, output); -} diff --git a/packages/type-primitives/src/codec/encoder/u8a.spec.js b/packages/type-primitives/src/codec/encoder/u8a.spec.js deleted file mode 100644 index 35f0aee043cd..000000000000 --- a/packages/type-primitives/src/codec/encoder/u8a.spec.js +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import encodeU8a from './u8a'; - -describe('encodeU8a', () => { - it('encodes an input with the length', () => { - expect( - encodeU8a( - new Uint8Array([1, 2, 3, 4]) - ) - ).toEqual( - new Uint8Array([ - 4, 0, 0, 0, // length - 1, 2, 3, 4 // data - ]) - ); - }); -}); diff --git a/packages/type-primitives/src/codec/encoder/u8a.ts b/packages/type-primitives/src/codec/encoder/u8a.ts deleted file mode 100644 index 0a8085b6a80a..000000000000 --- a/packages/type-primitives/src/codec/encoder/u8a.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -export default function encodeU8a (input: Uint8Array): Uint8Array { - const output = new Uint8Array(input.length + 4); - - new DataView(output.buffer).setUint32(0, input.length, true); - - output.set(input, 4); - - return output; -} diff --git a/packages/type-primitives/src/codec/header/decode.spec.js b/packages/type-primitives/src/codec/header/decode.spec.js deleted file mode 100644 index 33943aa9ea36..000000000000 --- a/packages/type-primitives/src/codec/header/decode.spec.js +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import createHeader from '../../create/header'; - -import { decodeHeader } from './index'; - -describe('decodeHeader', () => { - it('decodes properly', () => { - expect( - JSON.stringify( - decodeHeader( - new Uint8Array([ - // parent_hash - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, - // number - 67, 0, 0, 0, 0, 0, 0, 0, - // state_root - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, - // transaction_root - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, - // digest (length, log1, log2) - 2, 0, 0, 0, - 1, 0, 0, 0, 1, - 1, 0, 0, 0, 2 - ]) - ) - ) - ).toEqual( - JSON.stringify( - createHeader({ - digest: { - logs: [ - new Uint8Array([1]), - new Uint8Array([2]) - ] - }, - extrinsicsRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6]), - number: 67, - parentHash: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5]), - stateRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3]) - }) - ) - ); - }); -}); diff --git a/packages/type-primitives/src/codec/header/decode.ts b/packages/type-primitives/src/codec/header/decode.ts deleted file mode 100644 index 859330c2d6d2..000000000000 --- a/packages/type-primitives/src/codec/header/decode.ts +++ /dev/null @@ -1,37 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Header } from '../../header'; - -import BN from 'bn.js'; - -import createHeader from '../../create/header'; -import decodeRaw from './decodeRaw'; -import { OFF_PARENT_HASH, OFF_STATE_ROOT, OFF_TX_ROOT } from './offsets'; - -export default function decodeHeader (u8a: Uint8Array | null): Header { - // tslint:disable-next-line:variable-name - const { number, logs } = u8a - ? decodeRaw(u8a) - : { - number: new BN(0), - logs: [] - }; - - return createHeader({ - digest: { - logs - }, - extrinsicsRoot: u8a - ? u8a.subarray(OFF_TX_ROOT, OFF_TX_ROOT + 32) - : new Uint8Array(), - number, - parentHash: u8a - ? u8a.subarray(OFF_PARENT_HASH, OFF_PARENT_HASH + 32) - : new Uint8Array(), - stateRoot: u8a - ? u8a.subarray(OFF_STATE_ROOT, OFF_STATE_ROOT + 32) - : new Uint8Array() - }); -} diff --git a/packages/type-primitives/src/codec/header/decodeRaw.spec.js b/packages/type-primitives/src/codec/header/decodeRaw.spec.js deleted file mode 100644 index 414a2486fdd3..000000000000 --- a/packages/type-primitives/src/codec/header/decodeRaw.spec.js +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; - -import decode from './decodeRaw'; - -describe('decodeRaw', () => { - it('returns the raw parts', () => { - expect( - JSON.stringify( - decode( - new Uint8Array([ - // parent_hash - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, - // number - 67, 0, 0, 0, 0, 0, 0, 0, - // state_root - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, - // transaction_root - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, - // digest (length, log1, log2) - 2, 0, 0, 0, - 1, 0, 0, 0, 1, - 1, 0, 0, 0, 2, - // remainder - 1, 2, 3, 4, 5, 4, 3, 2, 1 - ]) - ) - ) - ).toEqual( - JSON.stringify({ - body: new Uint8Array([1, 2, 3, 4, 5, 4, 3, 2, 1]), - header: new Uint8Array([ - // parent_hash - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, - // number - 67, 0, 0, 0, 0, 0, 0, 0, - // state_root - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, - // transaction_root - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, - // digest (length, log1, log2) - 2, 0, 0, 0, - 1, 0, 0, 0, 1, - 1, 0, 0, 0, 2 - ]), - logs: [ - new Uint8Array([1]), - new Uint8Array([2]) - ], - number: new BN(67) - }) - ); - }); -}); diff --git a/packages/type-primitives/src/codec/header/decodeRaw.ts b/packages/type-primitives/src/codec/header/decodeRaw.ts deleted file mode 100644 index ed7aefc43d56..000000000000 --- a/packages/type-primitives/src/codec/header/decodeRaw.ts +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -import decodeArray from '../decoder/array'; -import decodeU8a from '../decoder/u8a'; -import { OFF_DIGEST, OFF_NUMBER } from './offsets'; - -type DecodedRaw = { - body: Uint8Array, - header: Uint8Array, - logs: Array, - number: BN -}; - -export default function decodeHeader (u8a: Uint8Array): DecodedRaw { - const logs = []; - const dd = decodeArray(u8a.subarray(OFF_DIGEST)); - let offset = 0; - - for (let index = 0; index < dd.length; index++) { - const log = decodeU8a(dd.data.subarray(offset)); - - logs.push(log); - - offset += log.length + 4; - } - - return { - body: dd.data.subarray(offset), - header: u8a.subarray(0, OFF_DIGEST + offset + 4), - logs, - number: u8aToBn(u8a.subarray(OFF_NUMBER, OFF_NUMBER + 8), true) - }; -} diff --git a/packages/type-primitives/src/codec/header/encode.spec.js b/packages/type-primitives/src/codec/header/encode.spec.js deleted file mode 100644 index 758f683dfa5f..000000000000 --- a/packages/type-primitives/src/codec/header/encode.spec.js +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { encodeHeader } from './index'; - -describe('encodeHeader', () => { - it('encodes the block header as u8a', () => { - expect( - encodeHeader({ - digest: { - logs: [ - new Uint8Array([1, 2, 3]), - new Uint8Array([7, 8, 9, 10]) - ] - }, - extrinsicsRoot: new Uint8Array([ - 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0 - ]), - parentHash: new Uint8Array([ - 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0 - ]), - number: 67, - stateRoot: new Uint8Array([ - 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0 - ]) - }) - ).toEqual( - new Uint8Array([ - // parentHash - 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, 5, 0, 0, 0, - // number - 67, 0, 0, 0, 0, 0, 0, 0, - // stateRoot - 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, 3, 0, 0, 0, - // extrinsicsRoot - 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, 6, 0, 0, 0, - // digest (length, log1, log2) - 2, 0, 0, 0, - 3, 0, 0, 0, 1, 2, 3, - 4, 0, 0, 0, 7, 8, 9, 10 - ]) - ); - }); -}); diff --git a/packages/type-primitives/src/codec/header/encode.ts b/packages/type-primitives/src/codec/header/encode.ts deleted file mode 100644 index 9c2334bf4bf2..000000000000 --- a/packages/type-primitives/src/codec/header/encode.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { UncheckedRaw } from '../../extrinsic'; -import { Header } from '../../header'; - -import rootRaw from '../../create/extrinsic/rootRaw'; -import bnToU8a from '@polkadot/util/bn/toU8a'; -import u8aConcat from '@polkadot/util/u8a/concat'; -import u8aToU8a from '@polkadot/util/u8a/toU8a'; - -import encodeArray from '../encoder/array'; -import encodeU8a from '../encoder/u8a'; - -// tslint:disable-next-line:variable-name -export default function encodeHeader ({ digest: { logs }, extrinsicsRoot, parentHash, number, stateRoot }: Header, extrinsics?: Array): Uint8Array { - return u8aConcat( - u8aToU8a(parentHash), - bnToU8a(number, 64, true), - u8aToU8a(stateRoot), - extrinsics - ? rootRaw(extrinsics) - : u8aToU8a(extrinsicsRoot), - encodeArray( - logs.map((log) => - encodeU8a( - u8aToU8a(log) - ) - ) - ) - ); -} diff --git a/packages/type-primitives/src/codec/header/hash.ts b/packages/type-primitives/src/codec/header/hash.ts deleted file mode 100644 index d5e42eb436cc..000000000000 --- a/packages/type-primitives/src/codec/header/hash.ts +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Header } from '../../header'; - -import blake2Asu8a from '@polkadot/util-crypto/blake2/asU8a'; - -import encode from './encode'; - -export default function headerHash (header: Header): Uint8Array { - return blake2Asu8a( - encode(header), - 256 - ); -} diff --git a/packages/type-primitives/src/codec/header/index.ts b/packages/type-primitives/src/codec/header/index.ts deleted file mode 100644 index 24733e944ee6..000000000000 --- a/packages/type-primitives/src/codec/header/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import decodeHeader from './decode'; -import encodeHeader from './encode'; - -export { - decodeHeader, - encodeHeader -}; diff --git a/packages/type-primitives/src/codec/header/offsets.ts b/packages/type-primitives/src/codec/header/offsets.ts deleted file mode 100644 index eca00c85776e..000000000000 --- a/packages/type-primitives/src/codec/header/offsets.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -const OFF_PARENT_HASH = 0; -const OFF_NUMBER = OFF_PARENT_HASH + 32; -const OFF_STATE_ROOT = OFF_NUMBER + 8; -const OFF_TX_ROOT = OFF_STATE_ROOT + 32; -const OFF_DIGEST = OFF_TX_ROOT + 32; - -export { - OFF_PARENT_HASH, - OFF_NUMBER, - OFF_STATE_ROOT, - OFF_TX_ROOT, - OFF_DIGEST -}; diff --git a/packages/type-primitives/src/codec/index.ts b/packages/type-primitives/src/codec/index.ts deleted file mode 100644 index 302cd5cdf1f2..000000000000 --- a/packages/type-primitives/src/codec/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -export * from './block'; -export * from './header'; diff --git a/packages/type-primitives/src/codec/justification/decode.ts b/packages/type-primitives/src/codec/justification/decode.ts deleted file mode 100644 index bcd87c1d8a9b..000000000000 --- a/packages/type-primitives/src/codec/justification/decode.ts +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Justification, Justification$Signature } from '../../justification'; - -import u8aToBn from '@polkadot/util/u8a/toBn'; - -export default function decodeJustification (u8a: Uint8Array): Justification { - let offset = 4 + 32; - const round = u8aToBn(u8a.subarray(0, 4), true); - const hash = u8a.subarray(4, 4 + 32); - const signatures: Array = []; - const numSigs = u8aToBn(u8a.subarray(offset, offset + 4), true).toNumber(); - - offset += 4; - - for (let index = 0; index < numSigs; index++, offset += (32 + 64)) { - signatures.push({ - address: u8a.subarray(offset, offset + 32), - signature: u8a.subarray(offset + 32, offset + 32 + 64) - }); - } - - return { - hash, - round, - signatures - }; -} diff --git a/packages/type-primitives/src/create/block.spec.js b/packages/type-primitives/src/create/block.spec.js deleted file mode 100644 index 3b6049e0e5aa..000000000000 --- a/packages/type-primitives/src/create/block.spec.js +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; -import extrinsics from '@polkadot/extrinsics'; -import encodeUnchecked from '@polkadot/extrinsics/codec/encode/unchecked'; -import hexToU8a from '@polkadot/util/hex/toU8a'; -import testingPairs from '@polkadot/util-keyring/testingPairs'; - -import block from './block'; - -const keyring = testingPairs(); - -describe('block', () => { - it('creates a complete block from incomplete details', () => { - expect( - block({}) - ).toEqual({ - header: { - digest: { - logs: [] - }, - number: new BN(0), - parentHash: new Uint8Array(32), - stateRoot: new Uint8Array(32), - extrinsicsRoot: new Uint8Array([ - 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33 - ]) - }, - extrinsics: [] - }); - }); - - it('creates a valid block, with defaults submitted', () => { - const transactions = [ - encodeUnchecked( - keyring.nobody, 0, - extrinsics.timestamp.public.set, - [0x5b13c3a4] - ), - encodeUnchecked( - keyring.nobody, 0, - extrinsics.parachains.public.setHeads, - [[]] - ) - ]; - - expect( - block({ - header: { - number: 1, - parentHash: hexToU8a( - '0x4545454545454545454545454545454545454545454545454545454545454545' - ), - stateRoot: hexToU8a( - '0x2481853da20b9f4322f34650fea5f240dcbfb266d02db94bfa0153c31f4a29db' - ) - }, - extrinsics: transactions - }) - ).toEqual({ - header: { - digest: { - logs: [] - }, - number: new BN(1), - parentHash: hexToU8a( - '0x4545454545454545454545454545454545454545454545454545454545454545' - ), - stateRoot: hexToU8a( - '0x2481853da20b9f4322f34650fea5f240dcbfb266d02db94bfa0153c31f4a29db' - ), - extrinsicsRoot: new Uint8Array([ - 140, 102, 133, 98, 214, 123, 73, 173, 122, 252, 247, 48, 181, 86, 77, 188, 213, 161, 17, 19, 73, 96, 158, 181, 249, 69, 97, 15, 186, 12, 45, 157 - ]) - // hexToU8a( - // '0xab602f7974bbfb513f021f39777e2195094dc64a2c1b7c82a1781cb9fd4768a8' - // ) - }, - extrinsics: transactions - }); - }); -}); diff --git a/packages/type-primitives/src/create/block.ts b/packages/type-primitives/src/create/block.ts deleted file mode 100644 index 97144910e6a3..000000000000 --- a/packages/type-primitives/src/create/block.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Block } from '../block'; -import { BlockIncomplete } from './types'; - -import headerBuilder from './header'; - -export default function block ({ header = {}, extrinsics = [] }: BlockIncomplete): Block { - return { - header: headerBuilder(header, extrinsics), - extrinsics - }; -} diff --git a/packages/type-primitives/src/create/extrinsic/rootRaw.spec.js b/packages/type-primitives/src/create/extrinsic/rootRaw.spec.js deleted file mode 100644 index 74d52ef2776c..000000000000 --- a/packages/type-primitives/src/create/extrinsic/rootRaw.spec.js +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import extrinsics from '@polkadot/extrinsics'; -import encodeUnchecked from '@polkadot/extrinsics/codec/encode/unchecked'; -import hexToU8a from '@polkadot/util/hex/toU8a'; -import testingPairs from '@polkadot/util-keyring/testingPairs'; - -import root from './rootRaw'; - -const keyring = testingPairs(); - -describe('root', () => { - it('calculates the root correctly for no transactions', () => { - expect( - root() - ).toEqual( - hexToU8a( - '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421' - ) - ); - }); - - it('calculates the root correctly (actual)', () => { - expect( - root([ - encodeUnchecked( - keyring.nobody, 0, - extrinsics.timestamp.public.set, - [0x5b13c3a4] - ), - encodeUnchecked( - keyring.nobody, 0, - extrinsics.parachains.public.setHeads, - [[]] - ) - ]) - ).toEqual( - new Uint8Array([ - 140, 102, 133, 98, 214, 123, 73, 173, 122, 252, 247, 48, 181, 86, 77, 188, 213, 161, 17, 19, 73, 96, 158, 181, 249, 69, 97, 15, 186, 12, 45, 157 - ]) - // hexToU8a( - // '0xab602f7974bbfb513f021f39777e2195094dc64a2c1b7c82a1781cb9fd4768a8' - // ) - ); - }); - - // [ - // 168, 135, 224, 93, 140, 222, 226, 83, 13, 116, 138, 197, 164, 6, 48, 190, 101, 18, 221, 166, 40, 179, 158, 112, 133, 154, 215, 198, 177, 76, 212, 228, - // 1, 0, 0, 0, 0, 0, 0, 0, - // 159, 216, 180, 61, 159, 12, 131, 80, 22, 196, 220, 94, 6, 222, 68, 159, 97, 60, 16, 69, 163, 149, 125, 138, 175, 36, 30, 93, 235, 4, 1, 168, - // 245, 208, 141, 24, 250, 136, 158, 84, 67, 170, 59, 146, 17, 149, 62, 67, 254, 66, 126, 53, 20, 146, 252, 243, 137, 89, 224, 145, 252, 79, 109, 15, - // 0, 0, 0, 0, - // 2, 0, 0, 0, - // 110, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 188, 29, 21, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - // 106, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - - it('calculates the root correctly (actual encoded)', () => { - expect( - root([ - new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 3, 0, - 188, 29, 21, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]), - new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 8, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]) - ]) - ).toEqual( - new Uint8Array([ - 245, 208, 141, 24, 250, 136, 158, 84, 67, 170, 59, 146, 17, 149, 62, 67, 254, 66, 126, 53, 20, 146, 252, 243, 137, 89, 224, 145, 252, 79, 109, 15 - ]) - ); - }); -}); diff --git a/packages/type-primitives/src/create/extrinsic/rootRaw.ts b/packages/type-primitives/src/create/extrinsic/rootRaw.ts deleted file mode 100644 index f60c96140668..000000000000 --- a/packages/type-primitives/src/create/extrinsic/rootRaw.ts +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { UncheckedRaw } from '../../extrinsic'; - -import uncheckedLength from '@polkadot/extrinsics/codec/encode/length'; -import trieRootOrdered from '@polkadot/trie-hash/rootOrdered'; - -export default function extrinsicsRootRaw (extrinsics: Array = []): Uint8Array { - return trieRootOrdered( - extrinsics.map((extrinsic) => - uncheckedLength(extrinsic) - ) - ); -} diff --git a/packages/type-primitives/src/create/header.spec.js b/packages/type-primitives/src/create/header.spec.js deleted file mode 100644 index 428baa014548..000000000000 --- a/packages/type-primitives/src/create/header.spec.js +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import txRoot from './extrinsic/rootRaw'; -import extrinsics from '@polkadot/extrinsics'; -import encodeUnchecked from '@polkadot/extrinsics/codec/encode/unchecked'; -import testingPairs from '@polkadot/util-keyring/testingPairs'; - -import header from './header'; - -const keyring = testingPairs(); - -describe('header', () => { - it('applies the all transactions (no timestamp)', () => { - expect( - header({}).extrinsicsRoot - ).toEqual( - txRoot([]) - ); - }); - - it('applies the all transactions (extrinsicsRoot)', () => { - const transactions = [ - encodeUnchecked( - keyring.nobody, 0, - extrinsics.timestamp.public.set, - [54321] - ), - encodeUnchecked( - keyring.one, 0, - extrinsics.timestamp.public.set, - [12345] - ) - ]; - - expect( - header({}, transactions).extrinsicsRoot - ).toEqual( - txRoot(transactions) - ); - }); -}); diff --git a/packages/type-primitives/src/create/header.ts b/packages/type-primitives/src/create/header.ts deleted file mode 100644 index cf843c379f82..000000000000 --- a/packages/type-primitives/src/create/header.ts +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Header } from '../header'; -import { UncheckedRaw } from '../extrinsic'; -import { HeaderIncomplete } from './types'; - -import bnToBn from '@polkadot/util/bn/toBn'; - -import extrinsicsRawRoot from './extrinsic/rootRaw'; - -// tslint:disable-next-line:variable-name -export default function header ({ digest: { logs = [] } = {}, extrinsicsRoot, number, parentHash = new Uint8Array(32), stateRoot = new Uint8Array(32) }: HeaderIncomplete, extrinsics?: Array): Header { - return { - digest: { - logs - }, - extrinsicsRoot: extrinsics - ? extrinsicsRawRoot(extrinsics) - : (extrinsicsRoot || extrinsicsRawRoot([])), - number: bnToBn(number || 0), - parentHash, - stateRoot - }; -} diff --git a/packages/type-primitives/src/create/types.d.ts b/packages/type-primitives/src/create/types.d.ts deleted file mode 100644 index c0264bf1adf1..000000000000 --- a/packages/type-primitives/src/create/types.d.ts +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { BlockNumber, Hash, HeaderHash } from '../base'; -import { Digest$Log } from '../digest'; -import { UncheckedRaw } from '../extrinsic'; - -export type HeaderIncomplete = { - digest?: { - logs?: Array - }, - extrinsicsRoot?: Hash, - parentHash?: HeaderHash, - number?: BlockNumber | number, - stateRoot?: Hash -}; - -export type BlockIncomplete = { - header?: HeaderIncomplete, - extrinsics?: Array -}; diff --git a/packages/type-primitives/src/digest.d.ts b/packages/type-primitives/src/digest.d.ts deleted file mode 100644 index 1b941e3debe9..000000000000 --- a/packages/type-primitives/src/digest.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Bytes } from './base'; - -export type Digest$Log = Bytes; - -export type Digest = { - logs: Array -}; diff --git a/packages/type-primitives/src/extrinsic.d.ts b/packages/type-primitives/src/extrinsic.d.ts deleted file mode 100644 index 2521fceca8b2..000000000000 --- a/packages/type-primitives/src/extrinsic.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { AccountId, Index, Signature } from './base'; - -// TODO Should be the actual decoded extrinsic -export type ExtrinsicFunction = Uint8Array; - -export type Unchecked = { - index: Index, - function: ExtrinsicFunction, - signed: AccountId, - signature: Signature -}; - -export type UncheckedRaw = Uint8Array; diff --git a/packages/type-primitives/src/header.d.ts b/packages/type-primitives/src/header.d.ts deleted file mode 100644 index 28fa684a8df9..000000000000 --- a/packages/type-primitives/src/header.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { BlockNumber, Hash, HeaderHash } from './base'; -import { Digest } from './digest'; - -export type Header = { - extrinsicsRoot: Hash, - digest: Digest, - number: BlockNumber, - parentHash: HeaderHash, - stateRoot: Hash -}; diff --git a/packages/type-primitives/src/json/accountId/decode.ts b/packages/type-primitives/src/json/accountId/decode.ts deleted file mode 100644 index 6237afe6e518..000000000000 --- a/packages/type-primitives/src/json/accountId/decode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { AccountId } from '../../base'; -import { JsonAccountId } from '../types'; - -import hashDecode from '../hash/decode'; - -export default function accountIdDecode (value: JsonAccountId): AccountId { - return hashDecode(value, 160); -} diff --git a/packages/type-primitives/src/json/accountId/encode.ts b/packages/type-primitives/src/json/accountId/encode.ts deleted file mode 100644 index 98cba96414fb..000000000000 --- a/packages/type-primitives/src/json/accountId/encode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { AccountId } from '../../base'; -import { JsonAccountId } from '../types'; - -import hashEncode from '../hash/encode'; - -export default function accountIdEncode (value: AccountId): JsonAccountId { - return hashEncode(value, 160); -} diff --git a/packages/type-primitives/src/json/accountId/index.ts b/packages/type-primitives/src/json/accountId/index.ts deleted file mode 100644 index 6bb73f66880d..000000000000 --- a/packages/type-primitives/src/json/accountId/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import accountIdDecode from './decode'; -import accountIdEncode from './encode'; - -export { - accountIdDecode, - accountIdEncode -}; diff --git a/packages/type-primitives/src/json/block/decode.spec.js b/packages/type-primitives/src/json/block/decode.spec.js deleted file mode 100644 index 47fff231486a..000000000000 --- a/packages/type-primitives/src/json/block/decode.spec.js +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import decode from './decode'; - -const blockPoc1 = { - block: { - extrinsics: [ - [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 36, 241, 242, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] - ], - header: { - digest: { - logs: [] - }, - extrinsicsRoot: '0xcfc906dc1496e190742daefd8ec378a0a901c134d176178f6bbbaeea407b3649', - number: 1, - parentHash: '0x6c5a31dc701c2a01a3f985da4da5faae19dc205ea212a5b1e4271e3e358271cc', - stateRoot: '0x71abf5a507b6cf7a94d5cbfd8b4a1a809167a0044994acb420f891ff18f296d8' - } - }, - justification: { - hash: '0xa04cab547ca4ef9ff13c1c489a8620994968035a7f07ef9d507226530c8dd81d', - round_number: 1, - signatures: [ - ['0x82c39b31a2b79a90f8e66e7a77fdb85a4ed5517f2ae39f6a80565e8ecae85cf5', '0xa681bd611800ede4637f2500315a850ffa8b9202640f0bed06125eb5fb10216c7f8bc4a4a0057a53807242144f2411c26bf84df91e7d62373a8cb7453973010e'], - ['0x063d7787ebca768b7445dfebe7d62cbb1625ff4dba288ea34488da266dd6dca5', '0x5710dfbbbb17e1661d7cc0bcf879e8da9ba9255fae15603b35b8079a30f20547f882eaab8ff959ed85cb410718fda102c010420281b5cbef64e5d4e3147c3b0f'], - ['0x4de37a07567ebcbf8c64568428a835269a566723687058e017b6d69db00a77e7', '0x95a6fd0eca0f36c560d27fc437433a1bfe3787c98f59515922394c8c51c500f13976cac28c79af22488cb0f9b123d933e4eb0eaf563d3b9672f6ae281e581708'] - ] - } -}; - -describe('blockDecode', () => { - it('decodes poc-1 blocks properly', () => { - expect( - decode(blockPoc1).extrinsics[0].extrinsic - ).toMatchObject({ - name: 'set', - section: 'timestamp' - }); - }); -}); diff --git a/packages/type-primitives/src/json/block/decode.ts b/packages/type-primitives/src/json/block/decode.ts deleted file mode 100644 index 0902a61d9435..000000000000 --- a/packages/type-primitives/src/json/block/decode.ts +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { BlockDecoded } from '@polkadot/params/types'; -import { JsonBlock } from '../types'; - -import toU8a from '@polkadot/util/u8a/toU8a'; - -import decodeExtrinsic from '../extrinsic/decode'; -import decodeHeader from '../header/decode'; -import decodeJustification from '../justification/decode'; - -export default function blockDecode ({ block: { extrinsics, header }, justification }: JsonBlock): BlockDecoded { - return { - extrinsics: extrinsics.map((extrinsic) => - decodeExtrinsic( - toU8a(extrinsic as any) - ) - ), - header: decodeHeader(header), - justification: decodeJustification(justification) - }; -} diff --git a/packages/type-primitives/src/json/bn/decode.spec.js b/packages/type-primitives/src/json/bn/decode.spec.js deleted file mode 100644 index 029f141e59b3..000000000000 --- a/packages/type-primitives/src/json/bn/decode.spec.js +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { bnDecode } from './index'; - -describe('bnDecode', () => { - it('converts hex to a BN value', () => { - expect( - bnDecode('0x1234').toNumber() - ).toEqual(0x1234); - }); - - it('converts with the required bitLength', () => { - expect( - bnDecode('0x1234', 8).toNumber() - ).toEqual(0x34); - }); - - it('converts a non-hex/numeric value', () => { - expect( - bnDecode(10).toNumber() - ).toEqual(10); - }); -}); diff --git a/packages/type-primitives/src/json/bn/decode.ts b/packages/type-primitives/src/json/bn/decode.ts deleted file mode 100644 index d0f22fba8c57..000000000000 --- a/packages/type-primitives/src/json/bn/decode.ts +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { JsonBnType } from '../types'; - -import BN from 'bn.js'; -import hexFixLength from '@polkadot/util/hex/fixLength'; -import isHex from '@polkadot/util/is/hex'; -import hexToBn from '@polkadot/util/hex/toBn'; - -export default function bnDecode (value: JsonBnType, bitLength: number = -1): BN { - if (isHex(value)) { - return hexToBn( - hexFixLength(value, bitLength) - ); - } - - return new BN(value); -} diff --git a/packages/type-primitives/src/json/bn/encode.spec.js b/packages/type-primitives/src/json/bn/encode.spec.js deleted file mode 100644 index 686e09c88bc2..000000000000 --- a/packages/type-primitives/src/json/bn/encode.spec.js +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; - -import { bnEncode } from './index'; - -describe('bnEncode', () => { - it('converts to a hex string', () => { - expect( - bnEncode(new BN(0x1234)) - ).toEqual('0x1234'); - }); - - it('converts to a hex with the provided bitLength', () => { - expect( - bnEncode(new BN(0x1234), 8) - ).toEqual('0x34'); - }); -}); diff --git a/packages/type-primitives/src/json/bn/encode.ts b/packages/type-primitives/src/json/bn/encode.ts deleted file mode 100644 index 230306768140..000000000000 --- a/packages/type-primitives/src/json/bn/encode.ts +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; -import { JsonBnType } from '../types'; - -import hexFixLength from '@polkadot/util/hex/fixLength'; -import bnToHex from '@polkadot/util/bn/toHex'; - -export default function bnEncode (value: BN | number, bitLength: number = -1): JsonBnType { - return hexFixLength( - bnToHex(value), - bitLength - ); -} diff --git a/packages/type-primitives/src/json/bn/index.ts b/packages/type-primitives/src/json/bn/index.ts deleted file mode 100644 index c8673b313b67..000000000000 --- a/packages/type-primitives/src/json/bn/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import bnDecode from './decode'; -import bnEncode from './encode'; - -export { - bnDecode, - bnEncode -}; diff --git a/packages/type-primitives/src/json/bytes/decode.spec.js b/packages/type-primitives/src/json/bytes/decode.spec.js deleted file mode 100644 index 70f4ef6a7973..000000000000 --- a/packages/type-primitives/src/json/bytes/decode.spec.js +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { bytesDecode } from './index'; - -describe('bytesDecode', () => { - it('creates the correct bytes U8a', () => { - expect( - bytesDecode('0x0012345600') - ).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56, 0x00])); - }); - - it('decodes byte array (number', () => { - expect( - bytesDecode([0x00, 0x12, 0x34, 0x56, 0x00]) - ).toEqual(new Uint8Array([0x00, 0x12, 0x34, 0x56, 0x00])); - }); -}); diff --git a/packages/type-primitives/src/json/bytes/decode.ts b/packages/type-primitives/src/json/bytes/decode.ts deleted file mode 100644 index b0873095507b..000000000000 --- a/packages/type-primitives/src/json/bytes/decode.ts +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Bytes } from '../../base'; -import { JsonBytes } from '../types'; - -import hexToU8a from '@polkadot/util/hex/toU8a'; - -export default function bytesDecode (value: JsonBytes): Bytes { - return Array.isArray(value) - ? new Uint8Array(value) - : hexToU8a(value); -} diff --git a/packages/type-primitives/src/json/bytes/encode.spec.js b/packages/type-primitives/src/json/bytes/encode.spec.js deleted file mode 100644 index 401d07e4d87a..000000000000 --- a/packages/type-primitives/src/json/bytes/encode.spec.js +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { bytesEncode } from './index'; - -describe('bytesEncode', () => { - it('creates the correct bytes U8a', () => { - expect( - bytesEncode(new Uint8Array([0x00, 0x12, 0x34, 0x56, 0x00])) - ).toEqual('0x0012345600'); - }); -}); diff --git a/packages/type-primitives/src/json/bytes/encode.ts b/packages/type-primitives/src/json/bytes/encode.ts deleted file mode 100644 index ceb7da3a4656..000000000000 --- a/packages/type-primitives/src/json/bytes/encode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Bytes } from '../../base'; -import { JsonBytes } from '../types'; - -import u8aToHex from '@polkadot/util/u8a/toHex'; - -export default function bytesEncode (value: Bytes): JsonBytes { - return u8aToHex(value); -} diff --git a/packages/type-primitives/src/json/bytes/index.ts b/packages/type-primitives/src/json/bytes/index.ts deleted file mode 100644 index 55d0ee36579c..000000000000 --- a/packages/type-primitives/src/json/bytes/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import bytesDecode from './decode'; -import bytesEncode from './encode'; - -export { - bytesDecode, - bytesEncode -}; diff --git a/packages/type-primitives/src/json/extrinsic/decode.spec.js b/packages/type-primitives/src/json/extrinsic/decode.spec.js deleted file mode 100644 index 11136d054791..000000000000 --- a/packages/type-primitives/src/json/extrinsic/decode.spec.js +++ /dev/null @@ -1,49 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import decode from './decode'; - -const extrinsics = [ - new Uint8Array([ - 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 41, 68, 117, 91, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]), - new Uint8Array([ - 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - ]), - new Uint8Array([ - 255, 77, 4, 166, 251, 34, 133, 176, 143, 12, 198, 201, 222, 181, 12, 200, 97, 198, 90, 180, 149, 151, 27, 224, 180, 14, 209, 39, 82, 19, 43, 128, 246, 16, 0, 0, 0, 2, 3, 255, 234, 26, 21, 20, 14, 86, 45, 159, 88, 43, 56, 214, 77, 141, 31, 87, 91, 159, 131, 117, 86, 253, 236, 64, 252, 195, 229, 100, 37, 148, 113, 199, 54, 163, 211, 184, 103, 79, 218, 214, 142, 212, 194, 213, 30, 205, 144, 202, 98, 169, 249, 42, 218, 178, 115, 60, 189, 224, 37, 129, 199, 179, 25, 68, 43, 230, 218, 86, 161, 223, 189, 1, 170, 86, 36, 118, 21, 90, 182, 34, 135, 60, 104, 202, 1, 12, 105, 28, 88, 227, 177, 1, 37, 220, 50, 14 - ]) -]; - -const poc1 = new Uint8Array([ - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 36, 241, 242, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -]); - -describe('extrinsic', () => { - extrinsics.forEach((extrinsic, index) => { - it(`decodes the extrinsic - ${index}`, () => { - const decoded = decode(extrinsic); - - expect(decoded).toBeDefined(); - }); - }); - - it('decodes poc-1 extrinsic', () => { - expect( - decode(poc1).extrinsic - ).toMatchObject({ - name: 'set', - section: 'timestamp' - }); - }); - - it('decodes poc-2/latest extrinsic', () => { - expect( - decode(extrinsics[1]).extrinsic - ).toMatchObject({ - name: 'setHeads', - section: 'parachains' - }); - }); -}); diff --git a/packages/type-primitives/src/json/extrinsic/decode.ts b/packages/type-primitives/src/json/extrinsic/decode.ts deleted file mode 100644 index 52b28b42c8a5..000000000000 --- a/packages/type-primitives/src/json/extrinsic/decode.ts +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { BlockExtrinsicDecoded, ExtrinsicDecoded, EncodingVersions } from '@polkadot/params/types'; - -import BN from 'bn.js'; -import decoder from '@polkadot/params/decode'; -import ExtError from '@polkadot/util/ext/error'; -import u8aToHex from '@polkadot/util/u8a/toHex'; - -const VERSIONS: Array = ['latest', 'poc-1']; - -export default function extrinsicDecode (extrinsic: Uint8Array): BlockExtrinsicDecoded { - for (let version of VERSIONS) { - const addressDecoded = decoder('AccountId', extrinsic, version); - const nonceDecoded = decoder('AccountIndex', extrinsic.subarray(addressDecoded.length), version); - const extrinsicDecoded = decoder('Call', extrinsic.subarray(addressDecoded.length + nonceDecoded.length), version); - const signature = extrinsic.subarray(addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length); - const totalLength = addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length + signature.length; - - if (totalLength === extrinsic.length) { - return { - ...(extrinsicDecoded.value as ExtrinsicDecoded), - address: addressDecoded.value as string, - accountIndex: nonceDecoded.value as BN, - signature - }; - } - } - - throw new ExtError(`Unable to decode extrinsic: ${u8aToHex(extrinsic)}`); -} diff --git a/packages/type-primitives/src/json/hash/decode.spec.js b/packages/type-primitives/src/json/hash/decode.spec.js deleted file mode 100644 index 5e7d02e456f0..000000000000 --- a/packages/type-primitives/src/json/hash/decode.spec.js +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { hashDecode } from './index'; - -describe('hashDecode', () => { - it('returns the hash when no length specified', () => { - expect( - hashDecode('0x123456787890abcdef') - ).toEqual(new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x78, 0x90, 0xab, 0xcd, 0xef])); - }); - - it('converts to the required length as specified', () => { - expect( - hashDecode('0x123456787890abcdef', 32) - ).toEqual(new Uint8Array([0x12, 0x34, 0x56, 0x78])); - }); -}); diff --git a/packages/type-primitives/src/json/hash/decode.ts b/packages/type-primitives/src/json/hash/decode.ts deleted file mode 100644 index 9b0c6eecbc45..000000000000 --- a/packages/type-primitives/src/json/hash/decode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Hash } from '../../base'; -import { JsonHash } from '../types'; - -import hexToU8a from '@polkadot/util/hex/toU8a'; - -export default function hashDecode (value: JsonHash, bitLength: number = -1): Hash { - return hexToU8a(value, bitLength); -} diff --git a/packages/type-primitives/src/json/hash/encode.spec.js b/packages/type-primitives/src/json/hash/encode.spec.js deleted file mode 100644 index 301a66193a7a..000000000000 --- a/packages/type-primitives/src/json/hash/encode.spec.js +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { hashEncode } from './index'; - -describe('hashEncode', () => { - it('returns the hash when no length specified', () => { - expect( - hashEncode( - new Uint8Array([0x12, 0x34, 0x56, 0x78, 0x78, 0x90, 0xab, 0xcd, 0xef]) - ) - ).toEqual('0x123456787890abcdef'); - }); - - it('converts to the required length as specified', () => { - expect( - hashEncode(new Uint8Array([0x34, 0x56, 0x78, 0x78, 0x90, 0xab, 0xcd, 0xef]), 64) - ).toEqual('0x3456787890abcdef'); - }); -}); diff --git a/packages/type-primitives/src/json/hash/encode.ts b/packages/type-primitives/src/json/hash/encode.ts deleted file mode 100644 index 861e78743a6f..000000000000 --- a/packages/type-primitives/src/json/hash/encode.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Hash } from '../../base'; -import { JsonHash } from '../types'; - -import u8aToHex from '@polkadot/util/u8a/toHex'; -import toU8a from '@polkadot/util/u8a/toU8a'; - -export default function hashEncode (value: Hash | string, bitLength: number = -1): JsonHash { - return u8aToHex( - toU8a(value) - ); -} diff --git a/packages/type-primitives/src/json/hash/index.ts b/packages/type-primitives/src/json/hash/index.ts deleted file mode 100644 index 84cc032a47d5..000000000000 --- a/packages/type-primitives/src/json/hash/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import hashDecode from './decode'; -import hashEncode from './encode'; - -export { - hashDecode, - hashEncode -}; diff --git a/packages/type-primitives/src/json/header/decode.spec.js b/packages/type-primitives/src/json/header/decode.spec.js deleted file mode 100644 index d0d98c929af9..000000000000 --- a/packages/type-primitives/src/json/header/decode.spec.js +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; - -import { headerDecode } from './index'; - -describe('headerDecode', () => { - it('creates the correct structure', () => { - expect( - JSON.stringify( - headerDecode({ - digest: { - logs: ['0x5678', '0x789a'] - }, - extrinsicsRoot: '0xabcd', - number: '0x1234', - parentHash: '0x1234', - stateRoot: '0x5678' - }) - ) - ).toEqual( - JSON.stringify({ - digest: { - logs: [ - new Uint8Array([0x56, 0x78]), - new Uint8Array([0x78, 0x9a]) - ] - }, - extrinsicsRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xab, 0xcd]), - number: new BN(0x1234), - parentHash: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x12, 0x34]), - stateRoot: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x56, 0x78]) - }) - ); - }); -}); diff --git a/packages/type-primitives/src/json/header/decode.ts b/packages/type-primitives/src/json/header/decode.ts deleted file mode 100644 index 374b3971409f..000000000000 --- a/packages/type-primitives/src/json/header/decode.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Header } from '../../header'; -import { JsonHeader } from '../types'; - -import headerBuilder from '../../create/header'; - -import hashDecode from '../hash/decode'; -import bnDecode from '../bn/decode'; -import bytesDecode from '../bytes/decode'; - -// tslint:disable-next-line:variable-name -export default function headerDecode ({ digest, extrinsicsRoot, number, parentHash, stateRoot }: JsonHeader): Header { - return headerBuilder({ - digest: { - logs: digest.logs.map(bytesDecode) - }, - extrinsicsRoot: hashDecode(extrinsicsRoot, 256), - number: bnDecode(number, 64), - parentHash: hashDecode(parentHash, 256), - stateRoot: hashDecode(stateRoot, 256) - }); -} diff --git a/packages/type-primitives/src/json/header/encode.spec.js b/packages/type-primitives/src/json/header/encode.spec.js deleted file mode 100644 index afd2f755f97a..000000000000 --- a/packages/type-primitives/src/json/header/encode.spec.js +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; - -import { headerEncode } from './index'; - -describe('headerEncode', () => { - it('creates the correct structure', () => { - expect( - JSON.stringify( - headerEncode({ - digest: { - logs: [ - new Uint8Array([0x56, 0x78]), - new Uint8Array([0x78, 0x9a]) - ] - }, - extrinsicsRoot: new Uint8Array([0xab, 0xcd]), - number: new BN(0x1234), - parentHash: new Uint8Array([0x12, 0x34]), - stateRoot: new Uint8Array([0x56, 0x78]) - }) - ) - ).toEqual( - JSON.stringify({ - digest: { - logs: ['0x5678', '0x789a'] - }, - extrinsicsRoot: '0xabcd', - number: '0x1234', - parentHash: '0x1234', - stateRoot: '0x5678' - }) - ); - }); -}); diff --git a/packages/type-primitives/src/json/header/encode.ts b/packages/type-primitives/src/json/header/encode.ts deleted file mode 100644 index 072d00e94a08..000000000000 --- a/packages/type-primitives/src/json/header/encode.ts +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Header } from '../../header'; -import { JsonHeader } from '../types'; - -import bnEncode from '../bn/encode'; -import bytesEncode from '../bytes/encode'; -import hashEncode from '../hash/encode'; - -// tslint:disable-next-line:variable-name -export default function headerEncode ({ parentHash, number, stateRoot, extrinsicsRoot, digest }: Header): JsonHeader { - return { - digest: { - logs: digest.logs.map(bytesEncode) - }, - extrinsicsRoot: hashEncode(extrinsicsRoot, 256), - number: bnEncode(number, 64), - parentHash: hashEncode(parentHash, 256), - stateRoot: hashEncode(stateRoot, 256) - }; -} diff --git a/packages/type-primitives/src/json/header/index.ts b/packages/type-primitives/src/json/header/index.ts deleted file mode 100644 index b8d519372461..000000000000 --- a/packages/type-primitives/src/json/header/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import headerDecode from './decode'; -import headerEncode from './encode'; - -export { - headerDecode, - headerEncode -}; diff --git a/packages/type-primitives/src/json/index.ts b/packages/type-primitives/src/json/index.ts deleted file mode 100644 index c49f9f1ab6ab..000000000000 --- a/packages/type-primitives/src/json/index.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -export * from './accountId'; -export * from './header'; -export * from './bn'; -export * from './bytes'; -export * from './hash'; -export * from './objectId'; -export * from './parachainId'; -export * from './signature'; diff --git a/packages/type-primitives/src/json/justification/decode.ts b/packages/type-primitives/src/json/justification/decode.ts deleted file mode 100644 index 8841103be5bc..000000000000 --- a/packages/type-primitives/src/json/justification/decode.ts +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { BlockJustificationDecoded } from '@polkadot/params/types'; -import { JsonJustification } from '../types'; - -import BN from 'bn.js'; -import toU8a from '@polkadot/util/u8a/toU8a'; -import addressEncode from '@polkadot/util-keyring/address/encode'; - -export default function justificationDecode ({ hash, round_number, signatures }: JsonJustification): BlockJustificationDecoded { - return { - hash: toU8a(hash), - round: new BN(round_number), - signatures: signatures.map(([address, signature]) => ({ - address: addressEncode(toU8a(address)), - signature: toU8a(signature) - })) - }; -} diff --git a/packages/type-primitives/src/json/justification/encode.ts b/packages/type-primitives/src/json/justification/encode.ts deleted file mode 100644 index 1f7f0800332d..000000000000 --- a/packages/type-primitives/src/json/justification/encode.ts +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Justification } from '../../justification'; -import { JsonJustification, JsonJustification$Signature } from '../types'; - -import u8aToHex from '@polkadot/util/u8a/toHex'; - -export default function justificationEncode ({ hash, round, signatures }: Justification): JsonJustification { - return { - hash: u8aToHex(hash), - round_number: round.toNumber(), - signatures: signatures.map(({ address, signature }): JsonJustification$Signature => ([ - u8aToHex(address), - u8aToHex(signature) - ])) - }; -} diff --git a/packages/type-primitives/src/json/objectId/decode.ts b/packages/type-primitives/src/json/objectId/decode.ts deleted file mode 100644 index 15fac371ef9e..000000000000 --- a/packages/type-primitives/src/json/objectId/decode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { ObjectId } from '../../base'; -import { JsonObjectId } from '../types'; - -import bnDecode from '../bn/decode'; - -export default function objectIdDecode (value: JsonObjectId): ObjectId { - return bnDecode(value, 64); -} diff --git a/packages/type-primitives/src/json/objectId/encode.ts b/packages/type-primitives/src/json/objectId/encode.ts deleted file mode 100644 index ce67b5ad9f9b..000000000000 --- a/packages/type-primitives/src/json/objectId/encode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { ObjectId } from '../../base'; -import { JsonObjectId } from '../types'; - -import bnEncode from '../bn/encode'; - -export default function objectIdEncode (value: ObjectId): JsonObjectId { - return bnEncode(value, 64); -} diff --git a/packages/type-primitives/src/json/objectId/index.ts b/packages/type-primitives/src/json/objectId/index.ts deleted file mode 100644 index c01d289a82fe..000000000000 --- a/packages/type-primitives/src/json/objectId/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import objectIdDecode from './decode'; -import objectIdEncode from './encode'; - -export { - objectIdDecode, - objectIdEncode -}; diff --git a/packages/type-primitives/src/json/parachainId/decode.ts b/packages/type-primitives/src/json/parachainId/decode.ts deleted file mode 100644 index 869acad030ef..000000000000 --- a/packages/type-primitives/src/json/parachainId/decode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { ParaChainId } from '../../base'; -import { JsonParaChainId } from '../types'; - -import bnDecode from '../bn/decode'; - -export default function parachainIdDecode (value: JsonParaChainId): ParaChainId { - return bnDecode(value, 64); -} diff --git a/packages/type-primitives/src/json/parachainId/encode.ts b/packages/type-primitives/src/json/parachainId/encode.ts deleted file mode 100644 index 106e406d5f3b..000000000000 --- a/packages/type-primitives/src/json/parachainId/encode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { ParaChainId } from '../../base'; -import { JsonParaChainId } from '../types'; - -import bnEncode from '../bn/encode'; - -export default function parachainIdEncode (value: ParaChainId): JsonParaChainId { - return bnEncode(value, 64); -} diff --git a/packages/type-primitives/src/json/parachainId/index.ts b/packages/type-primitives/src/json/parachainId/index.ts deleted file mode 100644 index bd754ef6f7cc..000000000000 --- a/packages/type-primitives/src/json/parachainId/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import parachainIdDecode from './decode'; -import parachainIdEncode from './encode'; - -export { - parachainIdDecode, - parachainIdEncode -}; diff --git a/packages/type-primitives/src/json/signature/decode.ts b/packages/type-primitives/src/json/signature/decode.ts deleted file mode 100644 index 2337cac99fad..000000000000 --- a/packages/type-primitives/src/json/signature/decode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Signature } from '../../base'; -import { JsonSignature } from '../types'; - -import hashDecode from '../hash/decode'; - -export default function signatureDecode (value: JsonSignature): Signature { - return hashDecode(value); -} diff --git a/packages/type-primitives/src/json/signature/encode.ts b/packages/type-primitives/src/json/signature/encode.ts deleted file mode 100644 index 1425cc5b70dc..000000000000 --- a/packages/type-primitives/src/json/signature/encode.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Signature } from '../../base'; -import { JsonSignature } from '../types'; - -import hashEncode from '../hash/encode'; - -export default function signatureEncode (value: Signature): JsonSignature { - return hashEncode(value); -} diff --git a/packages/type-primitives/src/json/signature/index.ts b/packages/type-primitives/src/json/signature/index.ts deleted file mode 100644 index cc7a403d01de..000000000000 --- a/packages/type-primitives/src/json/signature/index.ts +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import signatureDecode from './decode'; -import signatureEncode from './encode'; - -export { - signatureDecode, - signatureEncode -}; diff --git a/packages/type-primitives/src/json/types.d.ts b/packages/type-primitives/src/json/types.d.ts deleted file mode 100644 index ca900fe3ad5e..000000000000 --- a/packages/type-primitives/src/json/types.d.ts +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { ExtrinsicDecoded } from '@polkadot/params/types'; - -export type JsonBnType = string; -export type JsonHash = string; - -export type JsonBytes = string | Array; -export type JsonH160 = JsonHash; -export type JsonH256 = JsonHash; -export type JsonH512 = JsonHash; -export type JsonHeaderHash = JsonH256; -export type JsonU64 = JsonBnType; -export type JsonU256 = JsonBnType; - -export type JsonAccountId = JsonHash; -export type JsonAuthorityId = JsonHash; -export type JsonBlockNumber = JsonU64; -export type JsonObjectId = JsonU64; -export type JsonParaChainId = JsonU64; -export type JsonSignature = JsonHash; - -export type JsonTransaction = { - from: JsonAuthorityId, - to: JsonAuthorityId, - amount: JsonBnType, - nonce: JsonBnType -} - -export type JsonUnchecked = { - tx: JsonTransaction, - signature: JsonH512 -} - -export type JsonDigest = { - logs: Array -}; - -export type JsonHeader = { - parentHash: JsonHeaderHash, - number: JsonBlockNumber, - stateRoot: JsonHash, - extrinsicsRoot: JsonHash, - digest: JsonDigest -}; - -export type JsonJustification$Signature = [string, string]; - -export type JsonJustification = { - hash: string, - round_number: number, - signatures: Array -}; - -export type JsonBlock = { - block: { - extrinsics: Array>, - header: JsonHeader, - }, - justification: JsonJustification -}; diff --git a/packages/type-primitives/src/justification.d.ts b/packages/type-primitives/src/justification.d.ts deleted file mode 100644 index f0781b474d50..000000000000 --- a/packages/type-primitives/src/justification.d.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Header } from './header'; -import { UncheckedRaw } from './extrinsic'; -import BN from 'bn.js'; - -export type Justification$Signature = { - address: Uint8Array, - signature: Uint8Array -}; - -export type Justification = { - hash: Uint8Array, - round: BN, - signatures: Array -} diff --git a/packages/type-primitives/src/misbehavior.d.ts b/packages/type-primitives/src/misbehavior.d.ts deleted file mode 100644 index d016f692d238..000000000000 --- a/packages/type-primitives/src/misbehavior.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { AccountId, BlockNumber, HeaderHash, Signature } from './base'; - -export type MisbehaviorReport = { - authorityId: AccountId, - number: BlockNumber, - parentHash: HeaderHash, - type: number, - data: Array<{ - header: HeaderHash, - signature: Signature - }> -}; diff --git a/packages/type-primitives/src/parachain.unused-ts b/packages/type-primitives/src/parachain.unused-ts deleted file mode 100644 index aa8d843734f1..000000000000 --- a/packages/type-primitives/src/parachain.unused-ts +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { AccountId, Balance, Bytes, Hash } from './base'; - -export type ParachainState$BalanceDownload = [Balance, Bytes]; - -export type ParachainState$EgressRoot = Hash; - -export type ParachainState = { - headData: Bytes, - balance: Balance, - userBalances: { - [AccountId]: Balance - }, - balanceDownloads: { - [AccountId]: ParachainState$BalanceDownload - }, - egressRoots: Array -}; diff --git a/packages/type-primitives/src/role/all.ts b/packages/type-primitives/src/role/all.ts deleted file mode 100644 index 7eb0d16a8abe..000000000000 --- a/packages/type-primitives/src/role/all.ts +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { RoleMap } from './index'; - -const all: RoleMap = { - none: 0b00000000, - full: 0b00000001, - light: 0b00000010, - authority: 0b00000100 -}; - -export default all; diff --git a/packages/type-primitives/src/role/fromId.spec.js b/packages/type-primitives/src/role/fromId.spec.js deleted file mode 100644 index a673e7085ffc..000000000000 --- a/packages/type-primitives/src/role/fromId.spec.js +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import rolesFromId from './fromId'; - -describe('rolesFromId', () => { - it('returns the role mapping an string', () => { - expect( - rolesFromId(0b00000001 | 0b00000100) - ).toEqual(['none', 'full', 'authority']); - }); -}); diff --git a/packages/type-primitives/src/role/fromId.ts b/packages/type-primitives/src/role/fromId.ts deleted file mode 100644 index 5367edea3962..000000000000 --- a/packages/type-primitives/src/role/fromId.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Role } from './index'; - -import all from './all'; - -export default function rolesFromId (roleId: number): Array { - return Object - .keys(all) - .map((key) => - key as Role - ) - .filter((key) => - (roleId & all[key]) === all[key] - ); -} diff --git a/packages/type-primitives/src/role/index.d.ts b/packages/type-primitives/src/role/index.d.ts deleted file mode 100644 index cfd56cc2ce8d..000000000000 --- a/packages/type-primitives/src/role/index.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -export type RoleMap = { - none: number, - full: number, - light: number, - authority: number -}; - -export type Role = keyof RoleMap; diff --git a/packages/type-primitives/src/role/toId.spec.js b/packages/type-primitives/src/role/toId.spec.js deleted file mode 100644 index 6c9b59cdd4d3..000000000000 --- a/packages/type-primitives/src/role/toId.spec.js +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import rolesToId from './toId'; - -describe('rolesToId', () => { - it('returns the role mapping an string', () => { - expect( - rolesToId(['authority']) - ).toEqual(0b00000100); - }); -}); diff --git a/packages/type-primitives/src/role/toId.ts b/packages/type-primitives/src/role/toId.ts deleted file mode 100644 index 3871950da9b7..000000000000 --- a/packages/type-primitives/src/role/toId.ts +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Role } from './index'; - -import all from './all'; - -export default function rolesToId (roles: Array): number { - return roles.reduce((encoded, role) => { - return encoded | all[role]; - }, 0); -} diff --git a/packages/type-primitives/src/sizes.ts b/packages/type-primitives/src/sizes.ts deleted file mode 100644 index 8c9dbccc2d49..000000000000 --- a/packages/type-primitives/src/sizes.ts +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -export type SizeType = 32 | 64 | 128; - -const Balance: SizeType = 128; -const BlockNumber: SizeType = 64; -const AccountIndex: SizeType = 32; - -export default { - AccountIndex, - Balance, - BlockNumber -}; diff --git a/packages/type-primitives/src/state.unused-ts b/packages/type-primitives/src/state.unused-ts deleted file mode 100644 index 33d617b70ff8..000000000000 --- a/packages/type-primitives/src/state.unused-ts +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2017-2018 @polkadot/primitives authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { Hash, ObjectId } from './base'; - -export type State$Storage = { - codeHash: Hash, - storageRoot: Hash -}; - -export type State = { - [index: ObjectId]: State$Storage -}; diff --git a/packages/type-storage/src/key/README.md b/packages/type-storage/src/key/README.md deleted file mode 100644 index a861f24098ee..000000000000 --- a/packages/type-storage/src/key/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Legacy - -This whole folder is legacy code, and only used because other packages (api-provider, api-format, type-params) use this folder. diff --git a/packages/type-storage/src/key/index.ts b/packages/type-storage/src/key/index.ts deleted file mode 100644 index ba095fda3bd5..000000000000 --- a/packages/type-storage/src/key/index.ts +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2017-2018 @polkadot/storage authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import { SectionItem } from '@polkadot/params/types'; -import { Storage$Key$Value } from '../types'; - -import u8aConcat from '@polkadot/util/u8a/concat'; -import u8aFromString from '@polkadot/util/u8a/fromString'; -import xxhash from '@polkadot/util-crypto/xxhash/asU8a'; - -import formatParams from './params'; - -export type Keygen = (...keyParams: Array) => Uint8Array; - -export default function bindKey ({ isUnhashed, key, params }: SectionItem): Keygen { - const prefix = u8aFromString(key); - - return (...keyParams: Array): Uint8Array => { - const postfix = keyParams.length !== 0 - ? u8aConcat.apply(null, formatParams(params, keyParams)) - : new Uint8Array([]); - const prefixedKey = u8aConcat(prefix, postfix); - - return isUnhashed - ? prefixedKey - : xxhash(prefixedKey, 128); - }; -} diff --git a/packages/type-storage/src/key/params.ts b/packages/type-storage/src/key/params.ts deleted file mode 100644 index 30ca3b3d1000..000000000000 --- a/packages/type-storage/src/key/params.ts +++ /dev/null @@ -1,90 +0,0 @@ -// Copyright 2017-2018 @polkadot/storage authors & contributors -// This software may be modified and distributed under the terms -// of the ISC license. See the LICENSE file for details. - -import BN from 'bn.js'; -import { Param, Params } from '@polkadot/params/types'; -import { Storage$Key$Value } from '../types'; - -import sizes from '@polkadot/primitives/sizes'; -import bnToU8a from '@polkadot/util/bn/toU8a'; -import u8aToU8a from '@polkadot/util/u8a/toU8a'; -import u8aFromString from '@polkadot/util/u8a/fromString'; -import addressDecode from '@polkadot/util-keyring/address/decode'; - -export default function formatParams (params: Params, values: Storage$Key$Value[] = []): Array { - const paramTypes = Object.values(params).map(({ type }: Param) => type); - - return values.map((value: any, index: number): Uint8Array => { - try { - if (Array.isArray(paramTypes[index])) { - throw new Error('Unable to handle Array type'); - } - - const type = paramTypes[index]; - - switch (type) { - case 'AccountId': - return addressDecode((value as Uint8Array)); - - case 'Balance': - return bnToU8a((value as BN), sizes.Balance, true); - - case 'u128': - return bnToU8a((value as BN), 128, true); - - case 'BlockNumber': - case 'Gas': - case 'u64': - return bnToU8a((value as BN), 64, true); - - case 'bool': - return new Uint8Array([value ? 1 : 0]); - - case 'Bytes': - case 'Call': - case 'CandidateReceipt': - case 'Code': - case 'Digest': - case 'Hash': - case 'Header': - case 'KeyValue': - case 'StorageKey': - case 'StorageKeyValue': - case 'StorageResult': - case 'StorageResultSet': - case 'MisbehaviorReport': - case 'Proposal': - case 'Signature': - return u8aToU8a((value as Uint8Array)); - - case 'AccountIndex': - return bnToU8a((value as BN), sizes.AccountIndex, true); - - case 'ParachainId': - case 'PropIndex': - case 'ReferendumIndex': - case 'SessionKey': - case 'VoteIndex': - case 'VoteThreshold': - case 'u32': - return bnToU8a((value as BN), 32, true); - - case 'String': - return u8aFromString((value as string)); - - case 'Timestamp': - return bnToU8a((value as Date).getTime(), 64, true); - - default: - // tslint:disable-next-line - (type as never); - throw new Error('Unable to find handler'); - } - } catch (error) { - console.error('formatParams', value, index, paramTypes[index], error); - - throw error; - } - }); -} diff --git a/packages/type-storage/src/types.d.ts b/packages/type-storage/src/types.d.ts index ba76c48396ff..3db29898a5ef 100644 --- a/packages/type-storage/src/types.d.ts +++ b/packages/type-storage/src/types.d.ts @@ -4,7 +4,6 @@ import { StorageFunction } from '@polkadot/api-codec/StorageKey'; import BN from 'bn.js'; -import { Section } from '@polkadot/params/types'; import * as substrate from './substrate'; @@ -18,20 +17,3 @@ export interface Storage { [key: string]: ModuleStorage; // Will hold modules returned by state_getMetadata substrate: { [key in Substrate]: StorageFunction }; } - -// TODO Below is legacy code, used in: -// - api-provider -export type Storage$Key$Value = number | BN | Uint8Array | string; -export type Storages = { - consensus: Section; - // contract: Section, - council: Section; - councilVoting: Section; - democracy: Section; - parachains: Section; - session: Section; - staking: Section; - system: Section; - timestamp: Section; -}; -export type Storage$Sections = keyof Storages;