@@ -191,6 +191,10 @@ export function toBeHex(_value: BigNumberish, _width?: Numeric): string {
191
191
if ( result . length % 2 ) { result = "0" + result ; }
192
192
} else {
193
193
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
+
194
198
assert ( width * 2 >= result . length , `value exceeds width (${ width } bytes)` , "NUMERIC_FAULT" , {
195
199
operation : "toBeHex" ,
196
200
fault : "overflow" ,
@@ -208,14 +212,29 @@ export function toBeHex(_value: BigNumberish, _width?: Numeric): string {
208
212
/**
209
213
* Converts %%value%% to a Big Endian Uint8Array.
210
214
*/
211
- export function toBeArray ( _value : BigNumberish ) : Uint8Array {
215
+ export function toBeArray ( _value : BigNumberish , _width ?: Numeric ) : Uint8Array {
212
216
const value = getUint ( _value , "value" ) ;
213
217
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
+ }
215
222
216
223
let hex = value . toString ( 16 ) ;
217
224
if ( hex . length % 2 ) { hex = "0" + hex ; }
218
225
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
+
219
238
const result = new Uint8Array ( hex . length / 2 ) ;
220
239
for ( let i = 0 ; i < result . length ; i ++ ) {
221
240
const offset = i * 2 ;
0 commit comments