Import type-* from common repo (#125)
* Import type-* from common repo * Update links * Fix lint links (package index)
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
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.
|
||||
@@ -0,0 +1,33 @@
|
||||
[](https://polkadot.js.org)
|
||||

|
||||
[](https://github.com/Flet/semistandard)
|
||||
[](https://www.npmjs.com/package/@polkadot/primitives)
|
||||
[](https://travis-ci.org/polkadot-js/api)
|
||||
[](https://codeclimate.com/github/polkadot-js/api/maintainability)
|
||||
[](https://coveralls.io/github/polkadot-js/api?branch=master)
|
||||
[](https://david-dm.org/polkadot-js/api?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 {
|
||||
...
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "@polkadot/primitives",
|
||||
"version": "0.28.0",
|
||||
"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 <jacogr@gmail.com>",
|
||||
"maintainers": [
|
||||
"Jaco Greeff <jacogr@gmail.com>"
|
||||
],
|
||||
"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",
|
||||
"scripts": {
|
||||
"build": "polkadot-dev-build-babel",
|
||||
"check": "eslint src && flow check",
|
||||
"test": "echo \"Tests only available from root wrapper\""
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.0.0-beta.51",
|
||||
"@polkadot/trie-hash": "^0.28.1",
|
||||
"@polkadot/util": "^0.28.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bn.js": "^4.11.8"
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
// 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;
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
// 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<Justification$Signature>
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// 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<UncheckedRaw>
|
||||
};
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// 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<Array<Array<Bytes>>>,
|
||||
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<CandidateReceipt$BalanceUpload>,
|
||||
egressQueueRoots: Array<CandidateReceipt$EgressQueueRoot>,
|
||||
fees: Balance
|
||||
};
|
||||
@@ -0,0 +1,54 @@
|
||||
// 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]
|
||||
)
|
||||
]
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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<Uint8Array>,
|
||||
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
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// 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'
|
||||
// )
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
// 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
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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<UncheckedRaw> = []): Uint8Array {
|
||||
return u8aConcat(
|
||||
header,
|
||||
encodeArray(
|
||||
extrinsics.map(uncheckedLength)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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])
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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)
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
// 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])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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>): 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);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// 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])
|
||||
})
|
||||
)
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,37 @@
|
||||
// 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()
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// 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)
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
// 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<Uint8Array>,
|
||||
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)
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// 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
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
// 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<UncheckedRaw>): Uint8Array {
|
||||
return u8aConcat(
|
||||
u8aToU8a(parentHash),
|
||||
bnToU8a(number, 64, true),
|
||||
u8aToU8a(stateRoot),
|
||||
extrinsics
|
||||
? rootRaw(extrinsics)
|
||||
: u8aToU8a(extrinsicsRoot),
|
||||
encodeArray(
|
||||
logs.map((log) =>
|
||||
encodeU8a(
|
||||
u8aToU8a(log)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,17 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,6 @@
|
||||
// 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';
|
||||
@@ -0,0 +1,82 @@
|
||||
// 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
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
// 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
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// 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
|
||||
])
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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<UncheckedRaw> = []): Uint8Array {
|
||||
return trieRootOrdered(
|
||||
extrinsics.map(uncheckedLength)
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// 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)
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
// 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<UncheckedRaw>): Header {
|
||||
return {
|
||||
digest: {
|
||||
logs
|
||||
},
|
||||
extrinsicsRoot: extrinsics
|
||||
? extrinsicsRawRoot(extrinsics)
|
||||
: (extrinsicsRoot || extrinsicsRawRoot([])),
|
||||
number: bnToBn(number || 0),
|
||||
parentHash,
|
||||
stateRoot
|
||||
};
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// 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<Digest$Log>
|
||||
},
|
||||
extrinsicsRoot?: Hash,
|
||||
parentHash?: HeaderHash,
|
||||
number?: BlockNumber | number,
|
||||
stateRoot?: Hash
|
||||
};
|
||||
|
||||
export type BlockIncomplete = {
|
||||
header?: HeaderIncomplete,
|
||||
extrinsics?: Array<UncheckedRaw>
|
||||
};
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
// 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<Digest$Log>
|
||||
};
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// 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;
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,20 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,16 @@
|
||||
// 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
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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]));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,19 @@
|
||||
// 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]));
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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';
|
||||
|
||||
export default function hashEncode (value: Hash, bitLength: number = -1): JsonHash {
|
||||
return u8aToHex(value);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,38 @@
|
||||
// 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])
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,25 @@
|
||||
// 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)
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// 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'
|
||||
})
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,23 @@
|
||||
// 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)
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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';
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// 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);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
// 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
|
||||
};
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
// 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 JsonBnType = string;
|
||||
export type JsonHash = string;
|
||||
|
||||
export type JsonBytes = string | Array<number>;
|
||||
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<JsonBytes>
|
||||
};
|
||||
|
||||
export type JsonHeader = {
|
||||
parentHash: JsonHeaderHash,
|
||||
number: JsonBlockNumber,
|
||||
stateRoot: JsonHash,
|
||||
extrinsicsRoot: JsonHash,
|
||||
digest: JsonDigest
|
||||
};
|
||||
|
||||
export type JsonBlock = {
|
||||
header: JsonHeader,
|
||||
timestamp: JsonU64,
|
||||
transactions: Array<JsonUnchecked>
|
||||
};
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// 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
|
||||
}>
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
// 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<ParachainState$EgressRoot>
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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;
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,18 @@
|
||||
// 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<Role> {
|
||||
return Object
|
||||
.keys(all)
|
||||
.map((key) =>
|
||||
key as Role
|
||||
)
|
||||
.filter((key) =>
|
||||
(roleId & all[key]) === all[key]
|
||||
);
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// 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;
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,13 @@
|
||||
// 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<Role>): number {
|
||||
return roles.reduce((encoded, role) => {
|
||||
return encoded | all[role];
|
||||
}, 0);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
// 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
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
// 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
|
||||
};
|
||||
Reference in New Issue
Block a user