Additional null checks (#189)
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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)
|
||||
)
|
||||
: ''
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user