Add chain_getBlock & author_pendingExtrinsics (#132)
* Add chain_getBlock & author_pendingExtrinsics * Remove sample * TSLint
This commit is contained in:
@@ -34,8 +34,8 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/runtime": "^7.0.0-beta.51",
|
||||
"@polkadot/trie-hash": "^0.28.1",
|
||||
"@polkadot/util": "^0.28.1"
|
||||
"@polkadot/trie-hash": "^0.28.2",
|
||||
"@polkadot/util": "^0.28.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"bn.js": "^4.11.8"
|
||||
|
||||
@@ -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 { Block } from '../../block';
|
||||
|
||||
import createBlock from '../../create/block';
|
||||
import decodeHeader from '../header/decode';
|
||||
import decodeRaw from './decodeRaw';
|
||||
|
||||
export default function decodeBlock (u8a: Uint8Array): Block {
|
||||
const { header, extrinsics } = decodeRaw(u8a);
|
||||
|
||||
return createBlock({
|
||||
header: decodeHeader(header),
|
||||
extrinsics
|
||||
});
|
||||
}
|
||||
@@ -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 { BlockDecoded } from '@polkadot/params/types';
|
||||
import { JsonBlock } from '../types';
|
||||
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
|
||||
import decodeExtrinsic from '../extrinsic/decode';
|
||||
import decodeHeader from '../header/decode';
|
||||
|
||||
export default function blockDecode ({ block: { extrinsics, header }, justification }: JsonBlock): BlockDecoded {
|
||||
return {
|
||||
// TODO We should decode the justifications (and change type from any)
|
||||
justification,
|
||||
header: decodeHeader(header),
|
||||
extrinsics: extrinsics.map((extrinsic) =>
|
||||
decodeExtrinsic(
|
||||
toU8a(extrinsic as any)
|
||||
)
|
||||
)
|
||||
};
|
||||
}
|
||||
@@ -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 { BlockExtrinsicDecoded, ExtrinsicDecoded } from '@polkadot/params/types';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import decoder from '@polkadot/params/decode';
|
||||
|
||||
export default function extrinsicDecode (extrinsic: Uint8Array): BlockExtrinsicDecoded {
|
||||
const addressDecoded = decoder('AccountId', extrinsic, 'latest');
|
||||
const nonceDecoded = decoder('AccountIndex', extrinsic.subarray(addressDecoded.length), 'latest');
|
||||
const extrinsicDecoded = decoder('Call', extrinsic.subarray(addressDecoded.length + nonceDecoded.length), 'latest');
|
||||
const signature = extrinsic.subarray(addressDecoded.length + nonceDecoded.length + extrinsicDecoded.length);
|
||||
|
||||
return {
|
||||
...(extrinsicDecoded.value as ExtrinsicDecoded),
|
||||
address: addressDecoded.value as string,
|
||||
nonce: nonceDecoded.value as BN,
|
||||
signature
|
||||
};
|
||||
}
|
||||
@@ -6,7 +6,10 @@ import { Hash } from '../../base';
|
||||
import { JsonHash } from '../types';
|
||||
|
||||
import u8aToHex from '@polkadot/util/u8a/toHex';
|
||||
import toU8a from '@polkadot/util/u8a/toU8a';
|
||||
|
||||
export default function hashEncode (value: Hash, bitLength: number = -1): JsonHash {
|
||||
return u8aToHex(value);
|
||||
export default function hashEncode (value: Hash | string, bitLength: number = -1): JsonHash {
|
||||
return u8aToHex(
|
||||
toU8a(value)
|
||||
);
|
||||
}
|
||||
|
||||
+9
-3
@@ -2,6 +2,8 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
|
||||
import { ExtrinsicDecoded } from '@polkadot/params/types';
|
||||
|
||||
export type JsonBnType = string;
|
||||
export type JsonHash = string;
|
||||
|
||||
@@ -44,8 +46,12 @@ export type JsonHeader = {
|
||||
digest: JsonDigest
|
||||
};
|
||||
|
||||
export type JsonJustification = {};
|
||||
|
||||
export type JsonBlock = {
|
||||
header: JsonHeader,
|
||||
timestamp: JsonU64,
|
||||
transactions: Array<JsonUnchecked>
|
||||
block: {
|
||||
extrinsics: Array<Array<JsonBytes>>,
|
||||
header: JsonHeader,
|
||||
},
|
||||
justification: JsonJustification
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user