Skip to content

Commit

Permalink
chore: refactor rlp
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Aug 18, 2024
1 parent b2f895c commit 5e54b99
Show file tree
Hide file tree
Showing 13 changed files with 60 additions and 60 deletions.
4 changes: 2 additions & 2 deletions site/pages/api/rlp/decode.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Decodes an RLP value.

# Rlp.decode

- **Alias:** `fromRlp`
- **Alias:** `decodeRlp`

Decodes a [Recursive-Length Prefix (RLP)](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/) value into a [Bytes](/api/bytes) or [Hex](/api/hex) value.

Expand All @@ -17,7 +17,7 @@ import { Rlp } from 'ox'

// Entrypoint Imports
import * as Rlp from 'ox/Rlp'
import { fromRlp } from 'ox/Rlp'
import { decodeRlp } from 'ox/Rlp'
```

## Usage
Expand Down
4 changes: 2 additions & 2 deletions site/pages/api/rlp/encode.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ title: Encodes a value into RLP format.

# Rlp.encode

- **Alias:** `toRlp`
- **Alias:** `encodeRlp`

Encodes a [Bytes](/api/bytes) or [Hex](/api/hex) value into a [Recursive-Length Prefix (RLP)](https://ethereum.org/en/developers/docs/data-structures-and-encoding/rlp/) value.

Expand All @@ -17,7 +17,7 @@ import { Rlp } from 'ox'

// Entrypoint Imports
import * as Rlp from 'ox/Rlp'
import { toRlp } from 'ox/Rlp'
import { encodeRlp } from 'ox/Rlp'
```

## Usage
Expand Down
4 changes: 2 additions & 2 deletions src/Rlp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import * as exports from './Rlp.js'
test('exports', () => {
expect(Object.keys(exports)).toMatchInlineSnapshot(`
[
"fromRlp",
"decodeRlp",
"decode",
"rlpToBytes",
"toBytes",
"rlpToHex",
"toHex",
"toRlp",
"encodeRlp",
"encode",
"bytesToRlp",
"fromBytes",
Expand Down
12 changes: 6 additions & 6 deletions src/Rlp.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
export {
fromRlp,
fromRlp as decode,
decodeRlp,
decodeRlp as decode,
rlpToBytes,
rlpToBytes as toBytes,
rlpToHex,
rlpToHex as toHex,
} from './internal/rlp/fromRlp.js'
} from './internal/rlp/decode.js'

export {
type RecursiveArray,
toRlp,
toRlp as encode,
encodeRlp,
encodeRlp as encode,
bytesToRlp,
bytesToRlp as fromBytes,
hexToRlp,
hexToRlp as fromHex,
} from './internal/rlp/toRlp.js'
} from './internal/rlp/encode.js'
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { RLP } from '@ethereumjs/rlp'
import { decodeRlp } from 'ethers'
import * as ethers from 'ethers'
import { Rlp } from 'ox'
import { bench, describe } from 'vitest'
import { fromRlp } from './fromRlp.js'
import { decodeRlp } from './decode.js'

const generateBytes = (length: number) => {
const bytes = new Uint8Array(length)
Expand All @@ -20,11 +20,11 @@ describe('rlp: prefix === 0xb8', () => {
const bytes = Rlp.encode(generateBytes(255))

bench('ox: `Rlp.to`', () => {
fromRlp(bytes)
decodeRlp(bytes)
})

bench('ethers: `decodeRlp`', () => {
decodeRlp(bytes as any)
ethers.decodeRlp(bytes as any)
})

bench('@ethereumjs/rlp: `RLP.decode`', () => {
Expand All @@ -36,11 +36,11 @@ describe('rlp: prefix === 0xb9', () => {
const bytes = Rlp.encode(generateBytes(65_535))

bench('ox: `Rlp.to`', () => {
fromRlp(bytes)
decodeRlp(bytes)
})

bench('ethers: `decodeRlp`', () => {
decodeRlp(bytes as any)
ethers.decodeRlp(bytes as any)
})

bench('@ethereumjs/rlp: `RLP.decode`', () => {
Expand All @@ -52,11 +52,11 @@ describe('rlp: prefix === 0xba', () => {
const bytes = Rlp.encode(generateBytes(16_777_215))

bench('ox: `Rlp.to`', () => {
fromRlp(bytes)
decodeRlp(bytes)
})

bench.skip('ethers: `decodeRlp`', () => {
decodeRlp(bytes as any)
ethers.decodeRlp(bytes as any)
})

bench('@ethereumjs/rlp: `RLP.decode`', () => {
Expand All @@ -68,11 +68,11 @@ describe('rlp list: prefix === 0xf8', () => {
const list = Rlp.encode(generateList(60))

bench('ox: `Rlp.to`', () => {
fromRlp(list)
decodeRlp(list)
})

bench('ethers: `decodeRlp`', () => {
decodeRlp(list as any)
ethers.decodeRlp(list as any)
})

bench('@ethereumjs/rlp: `RLP.decode`', () => {
Expand All @@ -95,11 +95,11 @@ describe('rlp list: prefix === 0xf8 (recursive)', () => {
])

bench('ox: `Rlp.to`', () => {
fromRlp(list)
decodeRlp(list)
})

bench('ethers: `decodeRlp`', () => {
decodeRlp(list as any)
ethers.decodeRlp(list as any)
})

bench('@ethereumjs/rlp: `RLP.decode`', () => {
Expand All @@ -120,11 +120,11 @@ describe('rlp: tx (2048kB - prefix: 0xfa)', () => {
])

bench('ox: `Rlp.to`', () => {
fromRlp(list)
decodeRlp(list)
})

bench('ethers: `decodeRlp`', () => {
decodeRlp(list as any)
ethers.decodeRlp(list as any)
})

bench('@ethereumjs/rlp: `RLP.decode`', () => {
Expand Down
File renamed without changes.
File renamed without changes.
44 changes: 22 additions & 22 deletions src/internal/rlp/fromRlp.ts → src/internal/rlp/decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import { InvalidHexLengthError } from '../errors/data.js'
import type { GlobalErrorType } from '../errors/error.js'
import { bytesToHex } from '../hex/toHex.js'
import type { Bytes, Hex } from '../types/data.js'
import type { RecursiveArray } from './toRlp.js'
import type { RecursiveArray } from './encode.js'

type To = 'hex' | 'bytes'

export declare namespace fromRlp {
export declare namespace decodeRlp {
type ReturnType<to extends To> =
| (to extends 'bytes' ? RecursiveArray<Bytes> : never)
| (to extends 'hex' ? RecursiveArray<Hex> : never)

type ErrorType =
| hexToBytes.ErrorType
| fromRlpCursor.ErrorType
| decodeRlpCursor.ErrorType
| createCursor.ErrorType
| InvalidHexLengthError
| GlobalErrorType
Expand All @@ -32,10 +32,10 @@ export declare namespace fromRlp {
* Rlp.decode('0x8b68656c6c6f20776f726c64')
* // 0x68656c6c6f20776f726c64
*/
export function fromRlp<
export function decodeRlp<
value extends Bytes | Hex,
to extends To = value extends Bytes ? 'bytes' : 'hex',
>(value: value, to_?: to | To | undefined): fromRlp.ReturnType<to> {
>(value: value, to_?: to | To | undefined): decodeRlp.ReturnType<to> {
const to = to_ ?? (typeof value === 'string' ? 'hex' : 'bytes')

const bytes = (() => {
Expand All @@ -50,14 +50,14 @@ export function fromRlp<
const cursor = createCursor(bytes, {
recursiveReadLimit: Number.POSITIVE_INFINITY,
})
const result = fromRlpCursor(cursor, to)
const result = decodeRlpCursor(cursor, to)

return result as fromRlp.ReturnType<to>
return result as decodeRlp.ReturnType<to>
}

export declare namespace rlpToBytes {
type ErrorType = fromRlp.ErrorType
type ReturnType = fromRlp.ReturnType<'bytes'>
type ErrorType = decodeRlp.ErrorType
type ReturnType = decodeRlp.ReturnType<'bytes'>
}

/**
Expand All @@ -71,12 +71,12 @@ export declare namespace rlpToBytes {
* // Uint8Array([139, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])
*/
export function rlpToBytes(value: Bytes | Hex): rlpToBytes.ReturnType {
return fromRlp(value, 'bytes')
return decodeRlp(value, 'bytes')
}

export declare namespace rlpToHex {
type ErrorType = fromRlp.ErrorType
type ReturnType = fromRlp.ReturnType<'hex'>
type ErrorType = decodeRlp.ErrorType
type ReturnType = decodeRlp.ReturnType<'hex'>
}

/**
Expand All @@ -90,29 +90,29 @@ export declare namespace rlpToHex {
* // 0x68656c6c6f20776f726c64
*/
export function rlpToHex(value: Bytes | Hex): rlpToHex.ReturnType {
return fromRlp(value, 'hex')
return decodeRlp(value, 'hex')
}

/////////////////////////////////////////////////////////////////////////////////
// Utilities
/////////////////////////////////////////////////////////////////////////////////

declare namespace fromRlpCursor {
type ReturnType<to extends To = 'hex'> = fromRlp.ReturnType<to>
declare namespace decodeRlpCursor {
type ReturnType<to extends To = 'hex'> = decodeRlp.ReturnType<to>
type ErrorType =
| bytesToHex.ErrorType
| readLength.ErrorType
| readList.ErrorType
| GlobalErrorType
}
function fromRlpCursor<to extends To = 'hex'>(
function decodeRlpCursor<to extends To = 'hex'>(
cursor: Cursor,
to: to | To | undefined = 'hex',
): fromRlpCursor.ReturnType<to> {
): decodeRlpCursor.ReturnType<to> {
if (cursor.bytes.length === 0)
return (
to === 'hex' ? bytesToHex(cursor.bytes) : cursor.bytes
) as fromRlpCursor.ReturnType<to>
) as decodeRlpCursor.ReturnType<to>

const prefix = cursor.readByte()
if (prefix < 0x80) cursor.decrementPosition(1)
Expand All @@ -123,12 +123,12 @@ function fromRlpCursor<to extends To = 'hex'>(
const bytes = cursor.readBytes(length)
return (
to === 'hex' ? bytesToHex(bytes) : bytes
) as fromRlpCursor.ReturnType<to>
) as decodeRlpCursor.ReturnType<to>
}

// list
const length = readLength(cursor, prefix, 0xc0)
return readList(cursor, length, to) as {} as fromRlpCursor.ReturnType<to>
return readList(cursor, length, to) as {} as decodeRlpCursor.ReturnType<to>
}

declare namespace readLength {
Expand All @@ -149,8 +149,8 @@ declare namespace readList {
}
function readList<to extends To>(cursor: Cursor, length: number, to: to | To) {
const position = cursor.position
const value: fromRlpCursor.ReturnType<to>[] = []
const value: decodeRlpCursor.ReturnType<to>[] = []
while (cursor.position - position < length)
value.push(fromRlpCursor(cursor, to))
value.push(decodeRlpCursor(cursor, to))
return value
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RLP } from '@ethereumjs/rlp'
import { encodeRlp } from 'ethers'
import { bench, describe } from 'vitest'
import { bytesToRlp } from './toRlp.js'
import { bytesToRlp } from './encode.js'

const generateBytes = (length: number) => {
const bytes = new Uint8Array(length)
Expand Down
File renamed without changes.
File renamed without changes.
22 changes: 11 additions & 11 deletions src/internal/rlp/toRlp.ts → src/internal/rlp/encode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Encodable = {
encode(cursor: Cursor): void
}

export declare namespace toRlp {
export declare namespace encodeRlp {
type ReturnType<to extends To> =
| (to extends 'bytes' ? Bytes : never)
| (to extends 'hex' ? Hex : never)
Expand All @@ -36,22 +36,22 @@ export declare namespace toRlp {
* Rlp.encode('0x68656c6c6f20776f726c64')
* // 0x8b68656c6c6f20776f726c64
*/
export function toRlp<
export function encodeRlp<
bytes extends RecursiveArray<Bytes> | RecursiveArray<Hex>,
to extends To = bytes extends RecursiveArray<Bytes> ? 'bytes' : 'hex',
>(bytes: bytes, to_?: to | To | undefined): toRlp.ReturnType<to> {
>(bytes: bytes, to_?: to | To | undefined): encodeRlp.ReturnType<to> {
const encodable = getEncodable(bytes)
const cursor = createCursor(new Uint8Array(encodable.length))
encodable.encode(cursor)

const to = to_ || getType(bytes)
if (to === 'hex') return bytesToHex(cursor.bytes) as toRlp.ReturnType<to>
return cursor.bytes as toRlp.ReturnType<to>
if (to === 'hex') return bytesToHex(cursor.bytes) as encodeRlp.ReturnType<to>
return cursor.bytes as encodeRlp.ReturnType<to>
}

export declare namespace bytesToRlp {
type ReturnType<to extends To> = toRlp.ReturnType<to>
type ErrorType = toRlp.ErrorType | GlobalErrorType
type ReturnType<to extends To> = encodeRlp.ReturnType<to>
type ErrorType = encodeRlp.ErrorType | GlobalErrorType
}

/**
Expand All @@ -68,12 +68,12 @@ export function bytesToRlp<to extends To = 'bytes'>(
bytes: RecursiveArray<Bytes>,
to: to | To | undefined = 'bytes',
): bytesToRlp.ReturnType<to> {
return toRlp(bytes, to)
return encodeRlp(bytes, to)
}

export declare namespace hexToRlp {
type ReturnType<to extends To> = toRlp.ReturnType<to>
type ErrorType = toRlp.ErrorType | GlobalErrorType
type ReturnType<to extends To> = encodeRlp.ReturnType<to>
type ErrorType = encodeRlp.ErrorType | GlobalErrorType
}

/**
Expand All @@ -90,7 +90,7 @@ export function hexToRlp<to extends To = 'hex'>(
hex: RecursiveArray<Hex>,
to: to | To | undefined = 'hex',
): hexToRlp.ReturnType<to> {
return toRlp(hex, to)
return encodeRlp(hex, to)
}

/////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 5e54b99

Please sign in to comment.