Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #59 from webmaster128/rlp-type
Browse files Browse the repository at this point in the history
Export types used by decode/encode
  • Loading branch information
holgerd77 authored Dec 20, 2018
2 parents ee8a7f5 + fecee84 commit beddc69
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import BN = require('bn.js')

import { RLPInput, RLPDecoded } 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
* This function takes in a data, convert it to buffer if not, and a length for recursion
* @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++) {
Expand Down Expand Up @@ -56,8 +59,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 || (<any>input).length === 0) {
return Buffer.from([])
}
Expand All @@ -80,7 +83,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 || (<any>input).length === 0) {
return Buffer.from([])
}
Expand All @@ -106,7 +109,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]
Expand Down Expand Up @@ -220,7 +223,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)) {
Expand Down
13 changes: 8 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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<RLPInput> {}
interface RLPObject {
[x: string]: RLPInput
// Use interface extension instead of type alias to
// make circular declaration possible.
export interface List extends Array<Input> {}

export interface Dictionary {
[x: string]: Input
}

export interface RLPDecoded {
export interface Decoded {
data: Buffer | Buffer[]
remainder: Buffer
}

0 comments on commit beddc69

Please sign in to comment.