1- import * as ethjsUtil from 'ethjs-util'
21import * as BN from 'bn.js'
2+ import { intToBuffer , stripHexPrefix , padToEven , isHexString , isHexPrefixed } from 'ethjs-util'
3+ import { TransformableToArray , TransformableToBuffer } from './types'
34import { assertIsBuffer , assertIsArray , assertIsHexString } from './helpers'
45
56/**
@@ -86,7 +87,7 @@ export const unpadArray = function(a: number[]): number[] {
8687 */
8788export const unpadHexString = function ( a : string ) : string {
8889 assertIsHexString ( a )
89- a = ethjsUtil . stripHexPrefix ( a )
90+ a = stripHexPrefix ( a )
9091 return stripZeros ( a ) as string
9192}
9293
@@ -105,35 +106,62 @@ const stripZeros = function(a: any): Buffer | number[] | string {
105106}
106107
107108/**
108- * Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` method.
109+ * Attempts to turn a value into a `Buffer`.
110+ * Inputs supported: `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` or `toBuffer()` method.
109111 * @param v the value
110112 */
111- export const toBuffer = function ( v : any ) : Buffer {
112- if ( ! Buffer . isBuffer ( v ) ) {
113- if ( Array . isArray ( v ) || v instanceof Uint8Array ) {
114- v = Buffer . from ( v as Uint8Array )
115- } else if ( typeof v === 'string' ) {
116- if ( ethjsUtil . isHexString ( v ) ) {
117- v = Buffer . from ( ethjsUtil . padToEven ( ethjsUtil . stripHexPrefix ( v ) ) , 'hex' )
118- } else {
119- throw new Error (
120- `Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${ v } ` ,
121- )
122- }
123- } else if ( typeof v === 'number' ) {
124- v = ethjsUtil . intToBuffer ( v )
125- } else if ( v === null || v === undefined ) {
126- v = Buffer . allocUnsafe ( 0 )
127- } else if ( BN . isBN ( v ) ) {
128- v = v . toArrayLike ( Buffer )
129- } else if ( v . toArray ) {
130- // converts a BN to a Buffer
131- v = Buffer . from ( v . toArray ( ) )
132- } else {
133- throw new Error ( 'invalid type' )
113+ export const toBuffer = function (
114+ v :
115+ | string
116+ | number
117+ | BN
118+ | Buffer
119+ | Uint8Array
120+ | number [ ]
121+ | TransformableToArray
122+ | TransformableToBuffer
123+ | null
124+ | undefined ,
125+ ) : Buffer {
126+ if ( v === null || v === undefined ) {
127+ return Buffer . allocUnsafe ( 0 )
128+ }
129+
130+ if ( Buffer . isBuffer ( v ) ) {
131+ return Buffer . from ( v )
132+ }
133+
134+ if ( Array . isArray ( v ) || v instanceof Uint8Array ) {
135+ return Buffer . from ( v as Uint8Array )
136+ }
137+
138+ if ( typeof v === 'string' ) {
139+ if ( ! isHexString ( v ) ) {
140+ throw new Error (
141+ `Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${ v } ` ,
142+ )
134143 }
144+ return Buffer . from ( padToEven ( stripHexPrefix ( v ) ) , 'hex' )
145+ }
146+
147+ if ( typeof v === 'number' ) {
148+ return intToBuffer ( v )
135149 }
136- return v
150+
151+ if ( BN . isBN ( v ) ) {
152+ return v . toArrayLike ( Buffer )
153+ }
154+
155+ if ( v . toArray ) {
156+ // converts a BN to a Buffer
157+ return Buffer . from ( v . toArray ( ) )
158+ }
159+
160+ if ( v . toBuffer ) {
161+ return Buffer . from ( v . toBuffer ( ) )
162+ }
163+
164+ throw new Error ( 'invalid type' )
137165}
138166
139167/**
@@ -178,7 +206,7 @@ export const addHexPrefix = function(str: string): string {
178206 return str
179207 }
180208
181- return ethjsUtil . isHexPrefixed ( str ) ? str : '0x' + str
209+ return isHexPrefixed ( str ) ? str : '0x' + str
182210}
183211
184212/**
0 commit comments