@@ -5,17 +5,17 @@
|
||||
|
||||
import type { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
import decodeListLong from './listLong';
|
||||
import decodeListShort from './listShort';
|
||||
import decodeNumber from './number';
|
||||
import decodeSingle from './single';
|
||||
import decodeString from './string';
|
||||
|
||||
type Decoder = {
|
||||
max: number,
|
||||
fn: (decode: DecodeFunc, input: Uint8Array) => DecodeOutput
|
||||
};
|
||||
|
||||
const decodeListLong = require('./listLong');
|
||||
const decodeListShort = require('./listShort');
|
||||
const decodeNumber = require('./number');
|
||||
const decodeSingle = require('./single');
|
||||
const decodeString = require('./string');
|
||||
|
||||
const decoders: Array<Decoder> = [
|
||||
{ max: 0x7f, fn: decodeSingle },
|
||||
{ max: 0xb7, fn: decodeString },
|
||||
@@ -24,11 +24,9 @@ const decoders: Array<Decoder> = [
|
||||
{ max: 0xff, fn: decodeListLong }
|
||||
];
|
||||
|
||||
function decode (input: Uint8Array): DecodeOutput {
|
||||
export default function decode (input: Uint8Array): DecodeOutput {
|
||||
return decoders
|
||||
.find(({ max }) => input[0] <= max)
|
||||
// $FlowFixMe last entry (listLong) is max uint8 value
|
||||
.fn(decode, input);
|
||||
}
|
||||
|
||||
module.exports = decode;
|
||||
|
||||
@@ -3,12 +3,12 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
import assert from '@polkadot/util/assert';
|
||||
|
||||
const internalDecode = require('./decode');
|
||||
import internalDecode from './decode';
|
||||
|
||||
// flowlint-next-line unclear-type:off
|
||||
module.exports = function decoder (input?: Uint8Array): Uint8Array | Array<any> {
|
||||
export default function decoder (input?: Uint8Array): Uint8Array | Array<any> {
|
||||
if (!input || input.length === 0) {
|
||||
return new Uint8Array([]);
|
||||
}
|
||||
@@ -18,4 +18,4 @@ module.exports = function decoder (input?: Uint8Array): Uint8Array | Array<any>
|
||||
assert(remainder.length === 0, 'invalid remainder');
|
||||
|
||||
return decoded;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
|
||||
import type { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
import assert from '@polkadot/util/assert';
|
||||
|
||||
const safeParseInt = require('./safeParseInt');
|
||||
import safeParseInt from './safeParseInt';
|
||||
|
||||
module.exports = function decodeListLong (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
export default function decodeListLong (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
const llength = input[0] - 0xf6;
|
||||
const length = safeParseInt(input.slice(1, llength));
|
||||
const totalLength = llength + length;
|
||||
@@ -32,4 +32,4 @@ module.exports = function decodeListLong (decode: DecodeFunc, input: Uint8Array)
|
||||
decoded,
|
||||
remainder: input.slice(totalLength)
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
import type { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
module.exports = function decodeListShort (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
export default function decodeListShort (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
const length = input[0] - 0xbf;
|
||||
let innerRemainder = input.slice(1, length);
|
||||
const decoded = [];
|
||||
@@ -21,4 +21,4 @@ module.exports = function decodeListShort (decode: DecodeFunc, input: Uint8Array
|
||||
decoded,
|
||||
remainder: input.slice(length)
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
|
||||
import type { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
import assert from '@polkadot/util/assert';
|
||||
|
||||
const safeParseInt = require('./safeParseInt');
|
||||
import safeParseInt from './safeParseInt';
|
||||
|
||||
module.exports = function decodeNumber (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
export default function decodeNumber (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
const llength = input[0] - 0xb6;
|
||||
const length = safeParseInt(input.slice(1, llength));
|
||||
const decoded = input.slice(llength, length + llength);
|
||||
@@ -20,4 +20,4 @@ module.exports = function decodeNumber (decode: DecodeFunc, input: Uint8Array):
|
||||
decoded,
|
||||
remainder: input.slice(length + llength)
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
import assert from '@polkadot/util/assert';
|
||||
|
||||
module.exports = function safeParseInt (input: Uint8Array): number {
|
||||
export default function safeParseInt (input: Uint8Array): number {
|
||||
assert(input[0] > 0, 'invalid RLP: extra zeros');
|
||||
|
||||
return input.reduce((result, value) => (result * 256) + value, 0);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
// of the ISC license. See the LICENSE file for details.
|
||||
// @flow
|
||||
|
||||
const safeParseInt = require('./safeParseInt');
|
||||
import safeParseInt from './safeParseInt';
|
||||
|
||||
describe('safeParseInt', () => {
|
||||
it('does not allow 0 in first position', () => {
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
|
||||
import type { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
module.exports = function decodeSingle (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
export default function decodeSingle (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
return {
|
||||
decoded: input.slice(0, 1),
|
||||
remainder: input.slice(1)
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
|
||||
import type { DecodeFunc, DecodeOutput } from './types';
|
||||
|
||||
const assert = require('@polkadot/util/assert');
|
||||
import assert from '@polkadot/util/assert';
|
||||
|
||||
module.exports = function decodeString (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
export default function decodeString (decode: DecodeFunc, input: Uint8Array): DecodeOutput {
|
||||
const firstByte = input[0];
|
||||
const length = firstByte - 0x7f;
|
||||
let decoded;
|
||||
@@ -25,4 +25,4 @@ module.exports = function decodeString (decode: DecodeFunc, input: Uint8Array):
|
||||
decoded,
|
||||
remainder: input.slice(length)
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user