From 1767b36d140d7bbb44bd678c9bbe1afbd41d22d6 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 19 Dec 2018 16:37:40 +0100 Subject: [PATCH 1/4] Add missing export of RLPObject --- src/types.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index a7c4053..77145c7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3,7 +3,8 @@ import BN = require('bn.js') export type RLPInput = Buffer | string | number | Uint8Array | BN | RLPObject | RLPArray | null export interface RLPArray extends Array {} -interface RLPObject { + +export interface RLPObject { [x: string]: RLPInput } From 57db38f9ec7114019b9fbd486997d332ffea0c1c Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 19 Dec 2018 16:41:32 +0100 Subject: [PATCH 2/4] Un-prefix types --- src/index.ts | 14 +++++++------- src/types.ts | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/index.ts b/src/index.ts index fd46237..7b33ce0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import BN = require('bn.js') -import { RLPInput, RLPDecoded } from './types' +import { Decoded, Input } from './types' /** * RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP @@ -8,7 +8,7 @@ import { RLPInput, RLPDecoded } from './types' * @param input - will be converted to buffer * @returns returns buffer of encoded data **/ -export function encode(input: RLPInput): Buffer { +export function encode(input: Input): Buffer { if (input instanceof Array) { const output: Buffer[] = [] for (let i = 0; i < input.length; i++) { @@ -56,8 +56,8 @@ function encodeLength(len: number, offset: number): Buffer { **/ export function decode(input: Buffer, stream?: boolean): Buffer export function decode(input: Buffer[], stream?: boolean): Buffer[] -export function decode(input: RLPInput, stream?: boolean): Buffer[] | Buffer | RLPDecoded -export function decode(input: RLPInput, stream: boolean = false): Buffer[] | Buffer | RLPDecoded { +export function decode(input: Input, stream?: boolean): Buffer[] | Buffer | Decoded +export function decode(input: Input, stream: boolean = false): Buffer[] | Buffer | Decoded { if (!input || (input).length === 0) { return Buffer.from([]) } @@ -80,7 +80,7 @@ export function decode(input: RLPInput, stream: boolean = false): Buffer[] | Buf * @param input * @returns The length of the input or an empty Buffer if no input */ -export function getLength(input: RLPInput): Buffer | number { +export function getLength(input: Input): Buffer | number { if (!input || (input).length === 0) { return Buffer.from([]) } @@ -106,7 +106,7 @@ export function getLength(input: RLPInput): Buffer | number { } /** Decode an input with RLP */ -function _decode(input: Buffer): RLPDecoded { +function _decode(input: Buffer): Decoded { let length, llength, data, innerRemainder, d const decoded = [] const firstByte = input[0] @@ -220,7 +220,7 @@ function intToBuffer(integer: number): Buffer { } /** Transform anything into a Buffer */ -function toBuffer(v: RLPInput): Buffer { +function toBuffer(v: Input): Buffer { if (!Buffer.isBuffer(v)) { if (typeof v === 'string') { if (isHexPrefixed(v)) { diff --git a/src/types.ts b/src/types.ts index 77145c7..6827103 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,14 +1,14 @@ import BN = require('bn.js') -export type RLPInput = Buffer | string | number | Uint8Array | BN | RLPObject | RLPArray | null +export type Input = Buffer | string | number | Uint8Array | BN | Dictionary | List | null -export interface RLPArray extends Array {} +export interface List extends Array {} -export interface RLPObject { - [x: string]: RLPInput +export interface Dictionary { + [x: string]: Input } -export interface RLPDecoded { +export interface Decoded { data: Buffer | Buffer[] remainder: Buffer } From 5db9bbe45b2ebe5f2dfcf9ab019fd99eade1ffd6 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 19 Dec 2018 16:53:34 +0100 Subject: [PATCH 3/4] Add comment on List type --- src/types.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/types.ts b/src/types.ts index 6827103..177b5f7 100644 --- a/src/types.ts +++ b/src/types.ts @@ -2,6 +2,8 @@ import BN = require('bn.js') export type Input = Buffer | string | number | Uint8Array | BN | Dictionary | List | null +// Use interface extension instead of type alias to +// make circular declaration possible. export interface List extends Array {} export interface Dictionary { From fecee84a403b97a5cf53605bffd721a78f712291 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 19 Dec 2018 17:44:35 +0100 Subject: [PATCH 4/4] Export types Decoded, Dictionary, Input, List --- src/index.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 7b33ce0..9110321 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,9 @@ import BN = require('bn.js') -import { Decoded, Input } from './types' +import { Decoded, Dictionary, Input, List } from './types' + +// Types exported outside of this package +export { Decoded, Dictionary, Input, List } /** * RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP