diff --git a/packages/type-params/src/decode/value/bn.ts b/packages/type-params/src/decode/value/bn.ts index a30297c60..ad725a8f2 100644 --- a/packages/type-params/src/decode/value/bn.ts +++ b/packages/type-params/src/decode/value/bn.ts @@ -7,7 +7,7 @@ import { Param$Decoded } from '../../types'; import BN from 'bn.js'; import u8aToBn from '@polkadot/util/u8a/toBn'; -export default function bn (input: Uint8Array, bitLength: 32 | 64 | 128): Param$Decoded { +export default function bn (input: Uint8Array | null, bitLength: 32 | 64 | 128): Param$Decoded { const length = bitLength / 8; return { diff --git a/packages/type-params/src/decode/value/bool.ts b/packages/type-params/src/decode/value/bool.ts index cc6e98b74..20b89befa 100644 --- a/packages/type-params/src/decode/value/bool.ts +++ b/packages/type-params/src/decode/value/bool.ts @@ -4,9 +4,9 @@ import { Param$Decoded } from '../../types'; -export default function bool (input: Uint8Array): Param$Decoded { +export default function bool (input: Uint8Array | null): Param$Decoded { return { length: 1, - value: input[0] !== 0 + value: !!(input && input[0] !== 0) }; } diff --git a/packages/type-params/src/decode/value/byte.ts b/packages/type-params/src/decode/value/byte.ts index d340b0964..837734c0f 100644 --- a/packages/type-params/src/decode/value/byte.ts +++ b/packages/type-params/src/decode/value/byte.ts @@ -4,9 +4,11 @@ import { Param$Decoded } from '../../types'; -export default function byte (input: Uint8Array): Param$Decoded { +export default function byte (input: Uint8Array | null): Param$Decoded { return { length: 1, - value: input[0] + value: input + ? input[0] + : 0 }; } diff --git a/packages/type-params/src/decode/value/bytes.ts b/packages/type-params/src/decode/value/bytes.ts index de3a6d771..5f1405f79 100644 --- a/packages/type-params/src/decode/value/bytes.ts +++ b/packages/type-params/src/decode/value/bytes.ts @@ -6,11 +6,15 @@ import { Param$Decoded } from '../../types'; import u8aToBn from '@polkadot/util/u8a/toBn'; -export default function bytes (input: Uint8Array): Param$Decoded { - const length = u8aToBn(input.subarray(0, 4), true).toNumber(); +export default function bytes (input: Uint8Array | null): Param$Decoded { + const length = input + ? u8aToBn(input.subarray(0, 4), true).toNumber() + : 0; return { length: length + 4, - value: input.subarray(4, length + 4) + value: input + ? input.subarray(4, length + 4) + : new Uint8Array() }; } diff --git a/packages/type-params/src/decode/value/code.ts b/packages/type-params/src/decode/value/code.ts index 23fdf412c..d89d04092 100644 --- a/packages/type-params/src/decode/value/code.ts +++ b/packages/type-params/src/decode/value/code.ts @@ -6,9 +6,13 @@ import { Param$Decoded } from '../../types'; import u8aToBn from '@polkadot/util/u8a/toBn'; -export default function code (input: Uint8Array): Param$Decoded { - const length = u8aToBn(input.subarray(0, 4), true).toNumber() + 4; - const value = input.subarray(4, length); +export default function code (input: Uint8Array | null): Param$Decoded { + const length = input + ? u8aToBn(input.subarray(0, 4), true).toNumber() + 4 + : 0; + const value = input + ? input.subarray(4, length) + : new Uint8Array(); return { length, diff --git a/packages/type-params/src/decode/value/digest.ts b/packages/type-params/src/decode/value/digest.ts index 569c62f91..a5dbf192a 100644 --- a/packages/type-params/src/decode/value/digest.ts +++ b/packages/type-params/src/decode/value/digest.ts @@ -9,8 +9,10 @@ import u8aToBn from '@polkadot/util/u8a/toBn'; import bytes from './bytes'; -export default function digest (input: Uint8Array): Param$Decoded { - const logLength = u8aToBn(input.subarray(0, 4), true).toNumber(); +export default function digest (input: Uint8Array | null): Param$Decoded { + const logLength = input + ? u8aToBn(input.subarray(0, 4), true).toNumber() + : 0; const value: Digest = { logs: [] }; @@ -18,7 +20,12 @@ export default function digest (input: Uint8Array): Param$Decoded { let length = 4; for (let index = 0; index < logLength; index++) { - const decoded = bytes(input.subarray(length)); + const decoded = input + ? bytes(input.subarray(length)) + : { + length: 0, + value: new Uint8Array() + }; length += decoded.length; value.logs.push((decoded.value as Uint8Array)); diff --git a/packages/type-params/src/decode/value/header.ts b/packages/type-params/src/decode/value/header.ts index bd42b77fa..87e59bc2a 100644 --- a/packages/type-params/src/decode/value/header.ts +++ b/packages/type-params/src/decode/value/header.ts @@ -6,9 +6,11 @@ import { Param$Decoded } from '../../types'; import decodeHeader from '@polkadot/primitives/codec/header/decode'; -export default function header (input: Uint8Array): Param$Decoded { +export default function header (input: Uint8Array | null): Param$Decoded { // FIXME We don't have a way to determine the length atm - const length = input.length; + const length = input + ? input.length + : 0; const value = decodeHeader(input); return { diff --git a/packages/type-params/src/decode/value/keyValue.ts b/packages/type-params/src/decode/value/keyValue.ts index f07bf1675..0eb3eae9d 100644 --- a/packages/type-params/src/decode/value/keyValue.ts +++ b/packages/type-params/src/decode/value/keyValue.ts @@ -6,7 +6,17 @@ import { Param$Decoded } from '../../types'; import u8aToBn from '@polkadot/util/u8a/toBn'; -export default function bytes (input: Uint8Array): Param$Decoded { +export default function bytes (input: Uint8Array | null): Param$Decoded { + if (!input) { + return { + length: 0, + value: { + key: new Uint8Array(), + value: new Uint8Array() + } + }; + } + const keyLength = u8aToBn(input.subarray(0, 4), true).toNumber(); const key = input.subarray(4, keyLength + 4); const valLength = u8aToBn(input.subarray(keyLength + 4, keyLength + 8), true).toNumber(); diff --git a/packages/type-params/src/decode/value/string.ts b/packages/type-params/src/decode/value/string.ts index 515985a6b..96a55848b 100644 --- a/packages/type-params/src/decode/value/string.ts +++ b/packages/type-params/src/decode/value/string.ts @@ -7,13 +7,17 @@ import { Param$Decoded } from '../../types'; import u8aToBn from '@polkadot/util/u8a/toBn'; import u8aToUtf8 from '@polkadot/util/u8a/toUtf8'; -export default function string (input: Uint8Array): Param$Decoded { - const length = u8aToBn(input.subarray(0, 4), true).toNumber(); +export default function string (input: Uint8Array | null): Param$Decoded { + const length = input + ? u8aToBn(input.subarray(0, 4), true).toNumber() + : 0; return { length: length + 4, - value: u8aToUtf8( - input.subarray(4, length + 4) - ) + value: input + ? u8aToUtf8( + input.subarray(4, length + 4) + ) + : '' }; } diff --git a/packages/type-params/src/decode/value/time.ts b/packages/type-params/src/decode/value/time.ts index dcf196a7c..365b5f4aa 100644 --- a/packages/type-params/src/decode/value/time.ts +++ b/packages/type-params/src/decode/value/time.ts @@ -6,13 +6,15 @@ import { Param$Decoded } from '../../types'; import u8aToBn from '@polkadot/util/u8a/toBn'; -export default function time (input: Uint8Array): Param$Decoded { +export default function time (input: Uint8Array | null): Param$Decoded { return { length: 8, - value: new Date( - u8aToBn(input.subarray(0, 8), true) - .imuln(1000) - .toNumber() - ) + value: input + ? new Date( + u8aToBn(input.subarray(0, 8), true) + .imuln(1000) + .toNumber() + ) + : new Date(0) }; } diff --git a/packages/type-params/src/decode/value/u8a.ts b/packages/type-params/src/decode/value/u8a.ts index c05c5b61b..e58cf85bf 100644 --- a/packages/type-params/src/decode/value/u8a.ts +++ b/packages/type-params/src/decode/value/u8a.ts @@ -4,11 +4,13 @@ import { Param$Decoded } from '../../types'; -export default function u8a (input: Uint8Array, bitLength: 256 | 512, offset: number): Param$Decoded { +export default function u8a (input: Uint8Array | null, bitLength: 256 | 512, offset: number): Param$Decoded { const length = (bitLength / 8) + offset; return { length, - value: input.subarray(offset, length) + value: input + ? input.subarray(offset, length) + : new Uint8Array() }; } diff --git a/packages/type-primitives/src/codec/header/decode.ts b/packages/type-primitives/src/codec/header/decode.ts index b40c09006..859330c2d 100644 --- a/packages/type-primitives/src/codec/header/decode.ts +++ b/packages/type-primitives/src/codec/header/decode.ts @@ -4,22 +4,34 @@ import { Header } from '../../header'; -import createHeader from '../../create/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): Header { +export default function decodeHeader (u8a: Uint8Array | null): Header { // tslint:disable-next-line:variable-name - const { number, logs } = decodeRaw(u8a); + const { number, logs } = u8a + ? decodeRaw(u8a) + : { + number: new BN(0), + logs: [] + }; return createHeader({ digest: { logs }, - extrinsicsRoot: u8a.subarray(OFF_TX_ROOT, OFF_TX_ROOT + 32), + extrinsicsRoot: u8a + ? u8a.subarray(OFF_TX_ROOT, OFF_TX_ROOT + 32) + : new Uint8Array(), number, - parentHash: u8a.subarray(OFF_PARENT_HASH, OFF_PARENT_HASH + 32), - stateRoot: u8a.subarray(OFF_STATE_ROOT, OFF_STATE_ROOT + 32) + 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() }); }