1+ import { EthereumJSErrorWithoutCode } from './errors.ts'
2+
3+ export * from './errors.ts'
4+
15export type Input = string | number | bigint | Uint8Array | Array < Input > | null | undefined
26
37export type NestedUint8Array = Array < Uint8Array | NestedUint8Array >
@@ -13,7 +17,7 @@ export interface Decoded {
1317 */
1418function decodeLength ( v : Uint8Array ) : number {
1519 if ( v [ 0 ] === 0 ) {
16- throw new Error ( 'invalid RLP: extra zeros' )
20+ throw EthereumJSErrorWithoutCode ( 'invalid RLP: extra zeros' )
1721 }
1822 return parseHexByte ( bytesToHex ( v ) )
1923}
@@ -37,7 +41,9 @@ function encodeLength(len: number, offset: number): Uint8Array {
3741 */
3842function safeSlice ( input : Uint8Array , start : number , end : number ) {
3943 if ( end > input . length ) {
40- throw new Error ( 'invalid RLP (safeSlice): end slice of Uint8Array out-of-bounds' )
44+ throw EthereumJSErrorWithoutCode (
45+ 'invalid RLP (safeSlice): end slice of Uint8Array out-of-bounds' ,
46+ )
4147 }
4248 return input . slice ( start , end )
4349}
@@ -67,7 +73,9 @@ function _decode(input: Uint8Array): Decoded {
6773 }
6874
6975 if ( length === 2 && data [ 0 ] < 0x80 ) {
70- throw new Error ( 'invalid RLP encoding: invalid prefix, single byte < 0x80 are not prefixed' )
76+ throw EthereumJSErrorWithoutCode (
77+ 'invalid RLP encoding: invalid prefix, single byte < 0x80 are not prefixed' ,
78+ )
7179 }
7280
7381 return {
@@ -79,11 +87,11 @@ function _decode(input: Uint8Array): Decoded {
7987 // followed by the length, followed by the string
8088 lLength = firstByte - 0xb6
8189 if ( input . length - 1 < lLength ) {
82- throw new Error ( 'invalid RLP: not enough bytes for string length' )
90+ throw EthereumJSErrorWithoutCode ( 'invalid RLP: not enough bytes for string length' )
8391 }
8492 length = decodeLength ( safeSlice ( input , 1 , lLength ) )
8593 if ( length <= 55 ) {
86- throw new Error ( 'invalid RLP: expected string length to be greater than 55' )
94+ throw EthereumJSErrorWithoutCode ( 'invalid RLP: expected string length to be greater than 55' )
8795 }
8896 data = safeSlice ( input , lLength , length + lLength )
8997
@@ -110,11 +118,11 @@ function _decode(input: Uint8Array): Decoded {
110118 lLength = firstByte - 0xf6
111119 length = decodeLength ( safeSlice ( input , 1 , lLength ) )
112120 if ( length < 56 ) {
113- throw new Error ( 'invalid RLP: encoded list too short' )
121+ throw EthereumJSErrorWithoutCode ( 'invalid RLP: encoded list too short' )
114122 }
115123 const totalLength = lLength + length
116124 if ( totalLength > input . length ) {
117- throw new Error ( 'invalid RLP: total length is larger than the data' )
125+ throw EthereumJSErrorWithoutCode ( 'invalid RLP: total length is larger than the data' )
118126 }
119127
120128 innerRemainder = safeSlice ( input , lLength , totalLength )
@@ -144,7 +152,7 @@ function bytesToHex(uint8a: Uint8Array): string {
144152
145153function parseHexByte ( hexByte : string ) : number {
146154 const byte = Number . parseInt ( hexByte , 16 )
147- if ( Number . isNaN ( byte ) ) throw new Error ( 'Invalid byte sequence' )
155+ if ( Number . isNaN ( byte ) ) throw EthereumJSErrorWithoutCode ( 'Invalid byte sequence' )
148156 return byte
149157}
150158
@@ -163,17 +171,21 @@ function asciiToBase16(char: number): number | undefined {
163171 */
164172export function hexToBytes ( hex : string ) : Uint8Array {
165173 if ( hex . slice ( 0 , 2 ) === '0x' ) hex = hex . slice ( 0 , 2 )
166- if ( typeof hex !== 'string' ) throw new Error ( 'hex string expected, got ' + typeof hex )
174+ if ( typeof hex !== 'string' )
175+ throw EthereumJSErrorWithoutCode ( 'hex string expected, got ' + typeof hex )
167176 const hl = hex . length
168177 const al = hl / 2
169- if ( hl % 2 ) throw new Error ( 'padded hex string expected, got unpadded hex of length ' + hl )
178+ if ( hl % 2 )
179+ throw EthereumJSErrorWithoutCode ( 'padded hex string expected, got unpadded hex of length ' + hl )
170180 const array = new Uint8Array ( al )
171181 for ( let ai = 0 , hi = 0 ; ai < al ; ai ++ , hi += 2 ) {
172182 const n1 = asciiToBase16 ( hex . charCodeAt ( hi ) )
173183 const n2 = asciiToBase16 ( hex . charCodeAt ( hi + 1 ) )
174184 if ( n1 === undefined || n2 === undefined ) {
175185 const char = hex [ hi ] + hex [ hi + 1 ]
176- throw new Error ( 'hex string expected, got non-hex character "' + char + '" at index ' + hi )
186+ throw EthereumJSErrorWithoutCode (
187+ 'hex string expected, got non-hex character "' + char + '" at index ' + hi ,
188+ )
177189 }
178190 array [ ai ] = n1 * 16 + n2
179191 }
@@ -204,7 +216,7 @@ function utf8ToBytes(utf: string): Uint8Array {
204216/** Transform an integer into its hexadecimal value */
205217function numberToHex ( integer : number | bigint ) : string {
206218 if ( integer < 0 ) {
207- throw new Error ( 'Invalid integer as argument, must be unsigned!' )
219+ throw EthereumJSErrorWithoutCode ( 'Invalid integer as argument, must be unsigned!' )
208220 }
209221 const hex = integer . toString ( 16 )
210222 return hex . length % 2 ? `0${ hex } ` : hex
@@ -248,7 +260,7 @@ function toBytes(v: Input): Uint8Array {
248260 if ( v === null || v === undefined ) {
249261 return Uint8Array . from ( [ ] )
250262 }
251- throw new Error ( 'toBytes: received unsupported type ' + typeof v )
263+ throw EthereumJSErrorWithoutCode ( 'toBytes: received unsupported type ' + typeof v )
252264}
253265
254266/**
@@ -299,7 +311,7 @@ export function decode(input: Input, stream = false): Uint8Array | NestedUint8Ar
299311 }
300312 }
301313 if ( decoded . remainder . length !== 0 ) {
302- throw new Error ( 'invalid RLP: remainder must be zero' )
314+ throw EthereumJSErrorWithoutCode ( 'invalid RLP: remainder must be zero' )
303315 }
304316
305317 return decoded . data
0 commit comments