diff --git a/package.json b/package.json index 16f069c51..17bc8e77c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ethers", - "version": "3.0.25", + "version": "3.0.26", "description": "Ethereum wallet library.", "main": "index.js", "scripts": { diff --git a/tests/test-contract-interface.js b/tests/test-contract-interface.js index 3e51cdb7c..848fefef1 100644 --- a/tests/test-contract-interface.js +++ b/tests/test-contract-interface.js @@ -260,4 +260,15 @@ describe('Test Invalid Input', function() { return true; }, 'null bytes throws an error'); }); + + it('fails to encode fixed bytes that are out of range', function() { + assert.throws(function() { + var result = coder.encode([ 'bytes32' ], [ '0x012345678901234567890123456789012345678901234567890123456789012345' ]); + console.log('Result', result); + }, function(error) { + assert.equal(error.reason, 'invalid bytes32 value', 'got invalid bytes32'); + return true; + }, 'long bytes32 throws an error'); + }); + }); diff --git a/utils/abi-coder.js b/utils/abi-coder.js index 8c9a6571f..a029aa834 100644 --- a/utils/abi-coder.js +++ b/utils/abi-coder.js @@ -341,10 +341,11 @@ var coderNumber = function(coerceFunc, size, signed, localName) { }); } value = value.toTwos(size * 8).maskn(size * 8); - //value = value.toTwos(size * 8).maskn(size * 8); + if (signed) { value = value.fromTwos(size * 8).toTwos(256); } + return utils.padZeros(utils.arrayify(value), 32); }, decode: function(data, offset) { @@ -412,6 +413,14 @@ var coderFixedBytes = function(coerceFunc, length, localName) { encode: function(value) { try { value = utils.arrayify(value); + + // @TODO: In next major change, the value.length MUST equal the + // length, but that is a backward-incompatible change, so here + // we just check for things that can cause problems. + if (value.length > 32) { + throw new Error('too many bytes for field'); + } + } catch (error) { errors.throwError('invalid ' + name + ' value', errors.INVALID_ARGUMENT, { arg: localName, @@ -419,7 +428,8 @@ var coderFixedBytes = function(coerceFunc, length, localName) { value: error.value }); } - if (length === 32) { return value; } + + if (value.length === 32) { return value; } var result = new Uint8Array(32); result.set(value);