Prevent odd-length values from being passed in as bytesXX (#281).
This commit is contained in:
parent
f02f4bc0c0
commit
9d04f2c1eb
@ -271,4 +271,17 @@ describe('Test Invalid Input', function() {
|
||||
}, 'long bytes32 throws an error');
|
||||
});
|
||||
|
||||
// See: https://github.com/ethers-io/ethers.js/issues/281
|
||||
it('fails to encode byteXX with odd input', function() {
|
||||
assert.throws(function() {
|
||||
var coder = ethers.utils.AbiCoder.defaultCoder;
|
||||
var result = coder.encode([ 'bytes32' ], [ '0x1' ]);
|
||||
console.log(result);
|
||||
}, function(error) {
|
||||
assert.equal(error.message, 'hex string cannot be odd-length', 'got odd bytes');
|
||||
return true;
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
@ -17,6 +17,7 @@ var utils = (function() {
|
||||
getAddress: require('../utils/address').getAddress,
|
||||
|
||||
concat: convert.concat,
|
||||
isHexString: convert.isHexString,
|
||||
|
||||
toUtf8Bytes: utf8.toUtf8Bytes,
|
||||
toUtf8String: utf8.toUtf8String,
|
||||
@ -411,6 +412,9 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
|
||||
name: name,
|
||||
type: name,
|
||||
encode: function(value) {
|
||||
if (utils.isHexString(value) && (value.length % 2) !== 0) {
|
||||
throw new Error('hex string cannot be odd-length');
|
||||
}
|
||||
try {
|
||||
value = utils.arrayify(value);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user