@@ -191,6 +191,10 @@ export function toBeHex(_value: BigNumberish, _width?: Numeric): string {
191191 if ( result . length % 2 ) { result = "0" + result ; }
192192 } else {
193193 const width = getNumber ( _width , "width" ) ;
194+
195+ // Special case when both value and width are 0 (see: #5025)
196+ if ( width === 0 && value === BN_0 ) { return "0x" ; }
197+
194198 assert ( width * 2 >= result . length , `value exceeds width (${ width } bytes)` , "NUMERIC_FAULT" , {
195199 operation : "toBeHex" ,
196200 fault : "overflow" ,
@@ -208,14 +212,29 @@ export function toBeHex(_value: BigNumberish, _width?: Numeric): string {
208212/**
209213 * Converts %%value%% to a Big Endian Uint8Array.
210214 */
211- export function toBeArray ( _value : BigNumberish ) : Uint8Array {
215+ export function toBeArray ( _value : BigNumberish , _width ?: Numeric ) : Uint8Array {
212216 const value = getUint ( _value , "value" ) ;
213217
214- if ( value === BN_0 ) { return new Uint8Array ( [ ] ) ; }
218+ if ( value === BN_0 ) {
219+ const width = ( _width != null ) ? getNumber ( _width , "width" ) : 0 ;
220+ return new Uint8Array ( width ) ;
221+ }
215222
216223 let hex = value . toString ( 16 ) ;
217224 if ( hex . length % 2 ) { hex = "0" + hex ; }
218225
226+ if ( _width != null ) {
227+ const width = getNumber ( _width , "width" ) ;
228+
229+ while ( hex . length < ( width * 2 ) ) { hex = "00" + hex ; }
230+
231+ assert ( ( width * 2 ) === hex . length , `value exceeds width (${ width } bytes)` , "NUMERIC_FAULT" , {
232+ operation : "toBeArray" ,
233+ fault : "overflow" ,
234+ value : _value
235+ } ) ;
236+ }
237+
219238 const result = new Uint8Array ( hex . length / 2 ) ;
220239 for ( let i = 0 ; i < result . length ; i ++ ) {
221240 const offset = i * 2 ;
0 commit comments