From ac52059264745b63bfdeeb732c527876f5d05328 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 11 Sep 2018 15:41:50 +0200 Subject: [PATCH] Justification u8a decoding and json encoding (#146) * Decode Justifications * Json encoding for justifications * Bump deps --- package.json | 2 +- packages/api-format/package.json | 4 +- packages/api-provider/package.json | 6 +-- packages/api/package.json | 2 +- packages/type-extrinsics/package.json | 2 +- packages/type-params/package.json | 4 +- packages/type-primitives/package.json | 4 +- .../src/codec/justification/decode.ts | 30 +++++++++++ .../src/json/justification/decode.ts | 2 +- .../src/json/justification/encode.ts | 19 +++++++ packages/type-primitives/src/json/types.d.ts | 4 +- .../type-primitives/src/justification.d.ts | 18 +++++++ packages/type-storage/package.json | 6 +-- yarn.lock | 50 +++++++++---------- 14 files changed, 111 insertions(+), 42 deletions(-) create mode 100644 packages/type-primitives/src/codec/justification/decode.ts create mode 100644 packages/type-primitives/src/json/justification/encode.ts create mode 100644 packages/type-primitives/src/justification.d.ts diff --git a/package.json b/package.json index 5f46eb190b..dd1b2de867 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,6 @@ }, "devDependencies": { "@polkadot/dev": "^0.20.19", - "@polkadot/ts": "^0.1.25" + "@polkadot/ts": "^0.1.27" } } diff --git a/packages/api-format/package.json b/packages/api-format/package.json index 1d940e5cce..edb6eb8147 100644 --- a/packages/api-format/package.json +++ b/packages/api-format/package.json @@ -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" } } diff --git a/packages/api-provider/package.json b/packages/api-provider/package.json index d73333603d..eff11cd1a0 100644 --- a/packages/api-provider/package.json +++ b/packages/api-provider/package.json @@ -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", diff --git a/packages/api/package.json b/packages/api/package.json index b5fbf33433..53f24c8138 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -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" } } diff --git a/packages/type-extrinsics/package.json b/packages/type-extrinsics/package.json index 5051008ebd..1845a27f24 100644 --- a/packages/type-extrinsics/package.json +++ b/packages/type-extrinsics/package.json @@ -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" } } diff --git a/packages/type-params/package.json b/packages/type-params/package.json index b2c23bf677..752bf33a8b 100644 --- a/packages/type-params/package.json +++ b/packages/type-params/package.json @@ -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" } } diff --git a/packages/type-primitives/package.json b/packages/type-primitives/package.json index 16b8ee4c34..ae7ec71002 100644 --- a/packages/type-primitives/package.json +++ b/packages/type-primitives/package.json @@ -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" diff --git a/packages/type-primitives/src/codec/justification/decode.ts b/packages/type-primitives/src/codec/justification/decode.ts new file mode 100644 index 0000000000..bcd87c1d8a --- /dev/null +++ b/packages/type-primitives/src/codec/justification/decode.ts @@ -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 = []; + 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/json/justification/decode.ts b/packages/type-primitives/src/json/justification/decode.ts index 5cba60bcd5..8841103be5 100644 --- a/packages/type-primitives/src/json/justification/decode.ts +++ b/packages/type-primitives/src/json/justification/decode.ts @@ -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), diff --git a/packages/type-primitives/src/json/justification/encode.ts b/packages/type-primitives/src/json/justification/encode.ts new file mode 100644 index 0000000000..1f7f080033 --- /dev/null +++ b/packages/type-primitives/src/json/justification/encode.ts @@ -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) + ])) + }; +} diff --git a/packages/type-primitives/src/json/types.d.ts b/packages/type-primitives/src/json/types.d.ts index 36cc2bc19f..ca900fe3ad 100644 --- a/packages/type-primitives/src/json/types.d.ts +++ b/packages/type-primitives/src/json/types.d.ts @@ -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 }; export type JsonBlock = { diff --git a/packages/type-primitives/src/justification.d.ts b/packages/type-primitives/src/justification.d.ts new file mode 100644 index 0000000000..f0781b474d --- /dev/null +++ b/packages/type-primitives/src/justification.d.ts @@ -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 +} diff --git a/packages/type-storage/package.json b/packages/type-storage/package.json index e3323f5fd1..ac6e7fb842 100644 --- a/packages/type-storage/package.json +++ b/packages/type-storage/package.json @@ -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" } } diff --git a/yarn.lock b/yarn.lock index 6a589597ff..36f5f15659 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"