Justification u8a decoding and json encoding (#146)
* Decode Justifications * Json encoding for justifications * Bump deps
This commit is contained in:
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
@@ -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
@@ -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>
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user