diff --git a/src.ts/utils/bytes.ts b/src.ts/utils/bytes.ts index 824bf7c11..1cc1286e2 100644 --- a/src.ts/utils/bytes.ts +++ b/src.ts/utils/bytes.ts @@ -168,6 +168,15 @@ export function hexlify(value: Arrayish | Hexable | number): string { errors.throwError('cannot hexlify negative value', errors.INVALID_ARGUMENT, { arg: 'value', value: value }); } + // @TODO: Roll this into the above error as a numeric fault (overflow); next version, not backward compatible + // We can about (value == MAX_INT) to as well, since that may indicate we underflowed already + if (value >= 9007199254740991) { + errors.throwError("out-of-range", errors.NUMERIC_FAULT, { + operartion: "hexlify", + fault: "out-of-safe-range" + }); + } + var hex = ''; while (value) { hex = HexCharacters[value & 0x0f] + hex; diff --git a/tests/test-utils.js b/tests/test-utils.js index b1dfb08cb..076b19599 100644 --- a/tests/test-utils.js +++ b/tests/test-utils.js @@ -348,3 +348,20 @@ describe('Test BigNumber', function() { ].forEach(testAbs); }); }); + +describe("Hexlify", function() { + it("hexlify on string of unsafe number", function() { + assert(ethers.utils.hexlify(ethers.utils.bigNumberify("9985956830000000000")), "0x8a953ed43a892c00", "hexlify on large BigNumber"); + }); + + [9007199254740991, 9985956830000000000].forEach((value) => { + it('hexlify fails on unsafe number - ' + value, function() { + assert.throws(function() { + var result = ethers.utils.hexlify(value); + console.log('Result', result); + }, function(error) { + return (error.code === "NUMERIC_FAULT" && error.fault === "out-of-safe-range"); + }, "hexlify throws on out-of-range value - " + value); + }); + }); +});