Justification u8a decoding and json encoding (#146)

* Decode Justifications

* Json encoding for justifications

* Bump deps
This commit is contained in:
Jaco Greeff
2018-09-11 15:41:50 +02:00
committed by GitHub
parent a07e8941a1
commit ac52059264
14 changed files with 111 additions and 42 deletions
+1 -1
View File
@@ -21,6 +21,6 @@
},
"devDependencies": {
"@polkadot/dev": "^0.20.19",
"@polkadot/ts": "^0.1.25"
"@polkadot/ts": "^0.1.27"
}
}
+2 -2
View File
@@ -32,7 +32,7 @@
"@babel/runtime": "^7.0.0",
"@polkadot/jsonrpc": "^0.28.29",
"@polkadot/primitives": "^0.28.29",
"@polkadot/util": "^0.29.33",
"@polkadot/util-keyring": "^0.29.33"
"@polkadot/util": "^0.30.1",
"@polkadot/util-keyring": "^0.30.1"
}
}
+3 -3
View File
@@ -35,9 +35,9 @@
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/storage": "^0.28.29",
"@polkadot/util": "^0.29.33",
"@polkadot/util-crypto": "^0.29.33",
"@polkadot/util-keyring": "^0.29.33",
"@polkadot/util": "^0.30.1",
"@polkadot/util-crypto": "^0.30.1",
"@polkadot/util-keyring": "^0.30.1",
"@types/nock": "^9.1.3",
"eventemitter3": "^3.1.0",
"isomorphic-fetch": "^2.2.1",
+1 -1
View File
@@ -34,6 +34,6 @@
"@polkadot/api-provider": "^0.28.29",
"@polkadot/jsonrpc": "^0.28.29",
"@polkadot/params": "^0.28.29",
"@polkadot/util": "^0.29.33"
"@polkadot/util": "^0.30.1"
}
}
+1 -1
View File
@@ -13,6 +13,6 @@
"@babel/runtime": "^7.0.0",
"@polkadot/params": "^0.28.29",
"@polkadot/primitives": "^0.28.29",
"@polkadot/util": "^0.29.33"
"@polkadot/util": "^0.30.1"
}
}
+2 -2
View File
@@ -32,7 +32,7 @@
"@polkadot/extrinsics": "^0.28.29",
"@polkadot/params": "^0.28.29",
"@polkadot/primitives": "^0.28.29",
"@polkadot/util": "^0.29.33",
"@polkadot/util-keyring": "^0.29.33"
"@polkadot/util": "^0.30.1",
"@polkadot/util-keyring": "^0.30.1"
}
}
+2 -2
View File
@@ -29,8 +29,8 @@
"homepage": "https://github.com/polkadot-js/api/tree/master/packages/type-primitives#readme",
"dependencies": {
"@babel/runtime": "^7.0.0",
"@polkadot/trie-hash": "^0.29.33",
"@polkadot/util": "^0.29.33"
"@polkadot/trie-hash": "^0.30.1",
"@polkadot/util": "^0.30.1"
},
"devDependencies": {
"bn.js": "^4.11.8"
@@ -0,0 +1,30 @@
// 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<Justification$Signature> = [];
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
};
}
@@ -9,7 +9,7 @@ import BN from 'bn.js';
import toU8a from '@polkadot/util/u8a/toU8a';
import addressEncode from '@polkadot/util-keyring/address/encode';
export default function justificcationDecode ({ hash, round_number, signatures }: JsonJustification): BlockJustificationDecoded {
export default function justificationDecode ({ hash, round_number, signatures }: JsonJustification): BlockJustificationDecoded {
return {
hash: toU8a(hash),
round: new BN(round_number),
@@ -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 { 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)
]))
};
}
+3 -1
View File
@@ -46,10 +46,12 @@ export type JsonHeader = {
digest: JsonDigest
};
export type JsonJustification$Signature = [string, string];
export type JsonJustification = {
hash: string,
round_number: number,
signatures: Array<[string, string]>
signatures: Array<JsonJustification$Signature>
};
export type JsonBlock = {
+18
View File
@@ -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 { 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<Justification$Signature>
}
+3 -3
View File
@@ -32,8 +32,8 @@
"@babel/runtime": "^7.0.0",
"@polkadot/params": "^0.28.29",
"@polkadot/primitives": "^0.28.29",
"@polkadot/util": "^0.29.33",
"@polkadot/util-crypto": "^0.29.33",
"@polkadot/util-keyring": "^0.29.33"
"@polkadot/util": "^0.30.1",
"@polkadot/util-crypto": "^0.30.1",
"@polkadot/util-keyring": "^0.30.1"
}
}
+25 -25
View File
@@ -1216,50 +1216,50 @@
typedoc-plugin-markdown "^1.1.15"
typescript "^3.0.1"
"@polkadot/trie-hash@^0.29.33":
version "0.29.33"
resolved "https://registry.yarnpkg.com/@polkadot/trie-hash/-/trie-hash-0.29.33.tgz#530757d2a080620175fb1907fe3d56a1970dc415"
"@polkadot/trie-hash@^0.30.1":
version "0.30.1"
resolved "https://registry.yarnpkg.com/@polkadot/trie-hash/-/trie-hash-0.30.1.tgz#8d288e3687b24b8ff0f6f3b2e831db2a0560f1b1"
dependencies:
"@babel/runtime" "^7.0.0"
"@polkadot/util" "^0.29.33"
"@polkadot/util-crypto" "^0.29.33"
"@polkadot/util-rlp" "^0.29.33"
"@polkadot/util" "^0.30.1"
"@polkadot/util-crypto" "^0.30.1"
"@polkadot/util-rlp" "^0.30.1"
"@polkadot/ts@^0.1.25":
version "0.1.25"
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.25.tgz#a88c5242ae02975acce7128b5a84a6df8ca9d7b7"
"@polkadot/ts@^0.1.27":
version "0.1.27"
resolved "https://registry.yarnpkg.com/@polkadot/ts/-/ts-0.1.27.tgz#88ddd9e90f4c5c98996ecd433670839eaf530d73"
"@polkadot/util-crypto@^0.29.33":
version "0.29.33"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.29.33.tgz#299136ce92bb68d616f8b2b00cb5c4630a4fee67"
"@polkadot/util-crypto@^0.30.1":
version "0.30.1"
resolved "https://registry.yarnpkg.com/@polkadot/util-crypto/-/util-crypto-0.30.1.tgz#7729cf446c9b9be1f26db90f8427a2b0ce7d30ba"
dependencies:
"@babel/runtime" "^7.0.0"
"@polkadot/util" "^0.29.33"
"@polkadot/util" "^0.30.1"
blakejs "^1.1.0"
js-sha3 "^0.8.0"
tweetnacl "^1.0.0"
xxhashjs "^0.2.2"
"@polkadot/util-keyring@^0.29.33":
version "0.29.33"
resolved "https://registry.yarnpkg.com/@polkadot/util-keyring/-/util-keyring-0.29.33.tgz#c6fd3325c08fb103cf0a4f49aa74977722ab16ee"
"@polkadot/util-keyring@^0.30.1":
version "0.30.1"
resolved "https://registry.yarnpkg.com/@polkadot/util-keyring/-/util-keyring-0.30.1.tgz#39d5f6dce24873d108d4f8283f8d8bf40b1b5fde"
dependencies:
"@babel/runtime" "^7.0.0"
"@polkadot/util" "^0.29.33"
"@polkadot/util-crypto" "^0.29.33"
"@polkadot/util" "^0.30.1"
"@polkadot/util-crypto" "^0.30.1"
"@types/bs58" "^3.0.30"
bs58 "^4.0.1"
"@polkadot/util-rlp@^0.29.33":
version "0.29.33"
resolved "https://registry.yarnpkg.com/@polkadot/util-rlp/-/util-rlp-0.29.33.tgz#0a5f9b2d954cc2f9cd92964f0efd0372a03ee808"
"@polkadot/util-rlp@^0.30.1":
version "0.30.1"
resolved "https://registry.yarnpkg.com/@polkadot/util-rlp/-/util-rlp-0.30.1.tgz#d7c0ba35f21cecb085ee719e7d874bc3063d9938"
dependencies:
"@babel/runtime" "^7.0.0"
"@polkadot/util" "^0.29.33"
"@polkadot/util" "^0.30.1"
"@polkadot/util@^0.29.33":
version "0.29.33"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.29.33.tgz#b8e59146584c4649ca9308aa636d63d0b2ea2db3"
"@polkadot/util@^0.30.1":
version "0.30.1"
resolved "https://registry.yarnpkg.com/@polkadot/util/-/util-0.30.1.tgz#986eefc66907b1d6b618035542b6bfe06e38a019"
dependencies:
"@babel/runtime" "^7.0.0"
"@types/bn.js" "^4.11.1"