Better error messages for contracts.

This commit is contained in:
Richard Moore 2018-04-16 21:42:17 -04:00
parent 72bf73f931
commit cebf2aab29
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295
16 changed files with 1291 additions and 267 deletions

@ -14,6 +14,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('../utils/errors');
var allowedTransactionKeys = { var allowedTransactionKeys = {
data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
} }
@ -63,12 +65,8 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
// If 1 extra parameter was passed in, it contains overrides // If 1 extra parameter was passed in, it contains overrides
if (params.length == method.inputs.types.length + 1) { if (params.length === method.inputs.types.length + 1 && typeof(params[params.length - 1]) === 'object') {
transaction = params.pop(); transaction = copyObject(params.pop());
if (typeof(transaction) !== 'object') {
throw new Error('invalid transaction overrides');
}
transaction = copyObject(transaction);
// Check for unexpected keys (e.g. using "gas" instead of "gasLimit") // Check for unexpected keys (e.g. using "gas" instead of "gasLimit")
for (var key in transaction) { for (var key in transaction) {
@ -76,10 +74,6 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
throw new Error('unknown transaction override ' + key); throw new Error('unknown transaction override ' + key);
} }
} }
} else if (params.length > method.inputs.types.length) {
throw new Error('too many parameters');
} else if (params.length < method.inputs.types.length) {
throw new Error('too few parameters');
} }
// Check overrides make sense // Check overrides make sense
@ -129,7 +123,18 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
return provider.call(transaction); return provider.call(transaction);
}).then(function(value) { }).then(function(value) {
try {
var result = call.parse(value); var result = call.parse(value);
} catch (error) {
if (value === '0x' && method.inputs.types.length > 0) {
errors.throwError('call exception', errors.CALL_EXCEPTION, {
address: addressOrName,
method: call.signature,
value: params
});
}
throw error;
}
if (method.outputs.types.length === 1) { if (method.outputs.types.length === 1) {
result = result[0]; result = result[0];
} }

@ -2,8 +2,6 @@
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var throwError = require('../utils/throw-error');
var utils = (function() { var utils = (function() {
var convert = require('../utils/convert'); var convert = require('../utils/convert');
var properties = require('../utils/properties'); var properties = require('../utils/properties');
@ -25,6 +23,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('../utils/errors');
function parseParams(params) { function parseParams(params) {
var names = []; var names = [];
var types = []; var types = [];
@ -103,7 +103,11 @@ function Interface(abi) {
try { try {
abi = JSON.parse(abi); abi = JSON.parse(abi);
} catch (error) { } catch (error) {
throwError('invalid abi', { input: abi }); errors.throwError('could not parse ABI JSON', errors.INVALID_ARGUMENT, {
arg: 'abi',
errorMessage: error.message,
value: abi
});
} }
} }
@ -123,18 +127,39 @@ function Interface(abi) {
var func = function(bytecode) { var func = function(bytecode) {
if (!utils.isHexString(bytecode)) { if (!utils.isHexString(bytecode)) {
throwError('invalid bytecode', { input: bytecode }); errors.throwError('invalid contract bytecode', errors.INVALID_ARGUMENT, {
arg: 'bytecode',
type: typeof(bytecode),
value: bytecode
});
} }
var params = Array.prototype.slice.call(arguments, 1); var params = Array.prototype.slice.call(arguments, 1);
if (params.length < inputParams.types.length) { if (params.length < inputParams.types.length) {
throwError('missing parameter'); errors.throwError('missing constructor argument', errors.MISSING_ARGUMENT, {
arg: (inputParams.names[params.length] || 'unknown'),
count: params.length,
expectedCount: inputParams.types.length
});
} else if (params.length > inputParams.types.length) { } else if (params.length > inputParams.types.length) {
throwError('too many parameters'); errors.throwError('too many constructor arguments', errors.UNEXPECTED_ARGUMENT, {
count: params.length,
expectedCount: inputParams.types.length
});
}
try {
var encodedParams = utils.coder.encode(inputParams.names, inputParams.types, params)
} catch (error) {
errors.throwError('invalid constructor argument', errors.INVALID_ARGUMENT, {
arg: error.arg,
reason: error.reason,
value: error.value
});
} }
var result = { var result = {
bytecode: bytecode + utils.coder.encode(inputParams.names, inputParams.types, params).substring(2), bytecode: bytecode + encodedParams.substring(2),
type: 'deploy' type: 'deploy'
} }
@ -161,11 +186,21 @@ function Interface(abi) {
signature = method.name + signature; signature = method.name + signature;
var parse = function(data) { var parse = function(data) {
try {
return utils.coder.decode( return utils.coder.decode(
outputParams.names, outputParams.names,
outputParams.types, outputParams.types,
utils.arrayify(data) utils.arrayify(data)
); );
} catch(error) {
errors.throwError('invalid data for function output', errors.INVALID_ARGUMENT, {
arg: 'data',
errorArg: error.arg,
errorValue: error.value,
value: data,
reason: error.reason
});
}
}; };
var sighash = utils.keccak256(utils.toUtf8Bytes(signature)).substring(0, 10); var sighash = utils.keccak256(utils.toUtf8Bytes(signature)).substring(0, 10);
@ -180,12 +215,30 @@ function Interface(abi) {
var params = Array.prototype.slice.call(arguments, 0); var params = Array.prototype.slice.call(arguments, 0);
if (params.length < inputParams.types.length) { if (params.length < inputParams.types.length) {
throwError('missing parameter'); errors.throwError('missing input argument', errors.MISSING_ARGUMENT, {
arg: (inputParams.names[params.length] || 'unknown'),
count: params.length,
expectedCount: inputParams.types.length,
name: method.name
});
} else if (params.length > inputParams.types.length) { } else if (params.length > inputParams.types.length) {
throwError('too many parameters'); errors.throwError('too many input arguments', errors.UNEXPECTED_ARGUMENT, {
count: params.length,
expectedCount: inputParams.types.length
});
} }
result.data = sighash + utils.coder.encode(inputParams.names, inputParams.types, params).substring(2); try {
var encodedParams = utils.coder.encode(inputParams.names, inputParams.types, params);
} catch (error) {
errors.throwError('invalid input argument', errors.INVALID_ARGUMENT, {
arg: error.arg,
reason: error.reason,
value: error.value
});
}
result.data = sighash + encodedParams.substring(2);
result.parse = parse; result.parse = parse;
return populateDescription(new FunctionDescription(), result); return populateDescription(new FunctionDescription(), result);

@ -1,4 +1,4 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
'use strict'; 'use strict';
var Interface = require('./interface.js'); var Interface = require('./interface.js');
@ -15,6 +15,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('../utils/errors');
var allowedTransactionKeys = { var allowedTransactionKeys = {
data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
} }
@ -64,12 +66,8 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
// If 1 extra parameter was passed in, it contains overrides // If 1 extra parameter was passed in, it contains overrides
if (params.length == method.inputs.types.length + 1) { if (params.length === method.inputs.types.length + 1 && typeof(params[params.length - 1]) === 'object') {
transaction = params.pop(); transaction = copyObject(params.pop());
if (typeof(transaction) !== 'object') {
throw new Error('invalid transaction overrides');
}
transaction = copyObject(transaction);
// Check for unexpected keys (e.g. using "gas" instead of "gasLimit") // Check for unexpected keys (e.g. using "gas" instead of "gasLimit")
for (var key in transaction) { for (var key in transaction) {
@ -77,10 +75,6 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
throw new Error('unknown transaction override ' + key); throw new Error('unknown transaction override ' + key);
} }
} }
} else if (params.length > method.inputs.types.length) {
throw new Error('too many parameters');
} else if (params.length < method.inputs.types.length) {
throw new Error('too few parameters');
} }
// Check overrides make sense // Check overrides make sense
@ -130,7 +124,18 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
return provider.call(transaction); return provider.call(transaction);
}).then(function(value) { }).then(function(value) {
try {
var result = call.parse(value); var result = call.parse(value);
} catch (error) {
if (value === '0x' && method.inputs.types.length > 0) {
errors.throwError('call exception', errors.CALL_EXCEPTION, {
address: addressOrName,
method: call.signature,
value: params
});
}
throw error;
}
if (method.outputs.types.length === 1) { if (method.outputs.types.length === 1) {
result = result[0]; result = result[0];
} }
@ -317,7 +322,7 @@ utils.defineProperty(Contract, 'getDeployTransaction', function(bytecode, contra
module.exports = Contract; module.exports = Contract;
},{"../utils/address.js":9,"../utils/bignumber.js":10,"../utils/convert.js":11,"../utils/properties.js":14,"./interface.js":3}],2:[function(require,module,exports){ },{"../utils/address.js":9,"../utils/bignumber.js":10,"../utils/convert.js":11,"../utils/errors":12,"../utils/properties.js":14,"./interface.js":3}],2:[function(require,module,exports){
'use strict'; 'use strict';
var Contract = require('./contract.js'); var Contract = require('./contract.js');
@ -334,8 +339,6 @@ module.exports = {
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var throwError = require('../utils/throw-error');
var utils = (function() { var utils = (function() {
var convert = require('../utils/convert'); var convert = require('../utils/convert');
var properties = require('../utils/properties'); var properties = require('../utils/properties');
@ -357,6 +360,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('../utils/errors');
function parseParams(params) { function parseParams(params) {
var names = []; var names = [];
var types = []; var types = [];
@ -435,7 +440,11 @@ function Interface(abi) {
try { try {
abi = JSON.parse(abi); abi = JSON.parse(abi);
} catch (error) { } catch (error) {
throwError('invalid abi', { input: abi }); errors.throwError('could not parse ABI JSON', errors.INVALID_ARGUMENT, {
arg: 'abi',
errorMessage: error.message,
value: abi
});
} }
} }
@ -455,18 +464,39 @@ function Interface(abi) {
var func = function(bytecode) { var func = function(bytecode) {
if (!utils.isHexString(bytecode)) { if (!utils.isHexString(bytecode)) {
throwError('invalid bytecode', { input: bytecode }); errors.throwError('invalid contract bytecode', errors.INVALID_ARGUMENT, {
arg: 'bytecode',
type: typeof(bytecode),
value: bytecode
});
} }
var params = Array.prototype.slice.call(arguments, 1); var params = Array.prototype.slice.call(arguments, 1);
if (params.length < inputParams.types.length) { if (params.length < inputParams.types.length) {
throwError('missing parameter'); errors.throwError('missing constructor argument', errors.MISSING_ARGUMENT, {
arg: (inputParams.names[params.length] || 'unknown'),
count: params.length,
expectedCount: inputParams.types.length
});
} else if (params.length > inputParams.types.length) { } else if (params.length > inputParams.types.length) {
throwError('too many parameters'); errors.throwError('too many constructor arguments', errors.UNEXPECTED_ARGUMENT, {
count: params.length,
expectedCount: inputParams.types.length
});
}
try {
var encodedParams = utils.coder.encode(inputParams.names, inputParams.types, params)
} catch (error) {
errors.throwError('invalid constructor argument', errors.INVALID_ARGUMENT, {
arg: error.arg,
reason: error.reason,
value: error.value
});
} }
var result = { var result = {
bytecode: bytecode + utils.coder.encode(inputParams.names, inputParams.types, params).substring(2), bytecode: bytecode + encodedParams.substring(2),
type: 'deploy' type: 'deploy'
} }
@ -493,11 +523,21 @@ function Interface(abi) {
signature = method.name + signature; signature = method.name + signature;
var parse = function(data) { var parse = function(data) {
try {
return utils.coder.decode( return utils.coder.decode(
outputParams.names, outputParams.names,
outputParams.types, outputParams.types,
utils.arrayify(data) utils.arrayify(data)
); );
} catch(error) {
errors.throwError('invalid data for function output', errors.INVALID_ARGUMENT, {
arg: 'data',
errorArg: error.arg,
errorValue: error.value,
value: data,
reason: error.reason
});
}
}; };
var sighash = utils.keccak256(utils.toUtf8Bytes(signature)).substring(0, 10); var sighash = utils.keccak256(utils.toUtf8Bytes(signature)).substring(0, 10);
@ -512,12 +552,30 @@ function Interface(abi) {
var params = Array.prototype.slice.call(arguments, 0); var params = Array.prototype.slice.call(arguments, 0);
if (params.length < inputParams.types.length) { if (params.length < inputParams.types.length) {
throwError('missing parameter'); errors.throwError('missing input argument', errors.MISSING_ARGUMENT, {
arg: (inputParams.names[params.length] || 'unknown'),
count: params.length,
expectedCount: inputParams.types.length,
name: method.name
});
} else if (params.length > inputParams.types.length) { } else if (params.length > inputParams.types.length) {
throwError('too many parameters'); errors.throwError('too many input arguments', errors.UNEXPECTED_ARGUMENT, {
count: params.length,
expectedCount: inputParams.types.length
});
} }
result.data = sighash + utils.coder.encode(inputParams.names, inputParams.types, params).substring(2); try {
var encodedParams = utils.coder.encode(inputParams.names, inputParams.types, params);
} catch (error) {
errors.throwError('invalid input argument', errors.INVALID_ARGUMENT, {
arg: error.arg,
reason: error.reason,
value: error.value
});
}
result.data = sighash + encodedParams.substring(2);
result.parse = parse; result.parse = parse;
return populateDescription(new FunctionDescription(), result); return populateDescription(new FunctionDescription(), result);
@ -672,7 +730,7 @@ function Interface(abi) {
module.exports = Interface; module.exports = Interface;
},{"../utils/abi-coder":8,"../utils/convert":11,"../utils/keccak256":13,"../utils/properties":14,"../utils/throw-error":15,"../utils/utf8":16}],4:[function(require,module,exports){ },{"../utils/abi-coder":8,"../utils/convert":11,"../utils/errors":12,"../utils/keccak256":13,"../utils/properties":14,"../utils/utf8":16}],4:[function(require,module,exports){
(function (module, exports) { (function (module, exports) {
'use strict'; 'use strict';
@ -4589,8 +4647,6 @@ module.exports = undefined;
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var throwError = require('../utils/throw-error');
var utils = (function() { var utils = (function() {
var convert = require('../utils/convert.js'); var convert = require('../utils/convert.js');
var utf8 = require('../utils/utf8.js'); var utf8 = require('../utils/utf8.js');
@ -4614,6 +4670,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('./errors');
var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/); var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/); var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/); var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/);
@ -4649,7 +4707,16 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
value = utils.bigNumberify(value).toTwos(size * 8).maskn(size * 8); try {
value = utils.bigNumberify(value)
} catch (error) {
errors.throwError('invalid number value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
value = value.toTwos(size * 8).maskn(size * 8);
//value = value.toTwos(size * 8).maskn(size * 8); //value = value.toTwos(size * 8).maskn(size * 8);
if (signed) { if (signed) {
value = value.fromTwos(size * 8).toTwos(256); value = value.fromTwos(size * 8).toTwos(256);
@ -4657,7 +4724,13 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
return utils.padZeros(utils.arrayify(value), 32); return utils.padZeros(utils.arrayify(value), 32);
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid ' + name); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var junkLength = 32 - size; var junkLength = 32 - size;
var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32)); var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32));
if (signed) { if (signed) {
@ -4683,14 +4756,18 @@ var coderBoolean = function(coerceFunc, localName) {
name: 'boolean', name: 'boolean',
type: 'boolean', type: 'boolean',
encode: function(value) { encode: function(value) {
return uint256Coder.encode(value ? 1: 0); return uint256Coder.encode(!!value ? 1: 0);
}, },
decode: function(data, offset) { decode: function(data, offset) {
try { try {
var result = uint256Coder.decode(data, offset); var result = uint256Coder.decode(data, offset);
} catch (error) { } catch (error) {
if (error.message === 'invalid uint256') { if (error.reason === 'insufficient data for uint256 type') {
throwError('invalid bool'); errors.throwError('insufficient data for boolean type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'boolean',
value: error.value
});
} }
throw error; throw error;
} }
@ -4709,7 +4786,15 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(value); value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid ' + name + ' value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
if (length === 32) { return value; } if (length === 32) { return value; }
var result = new Uint8Array(32); var result = new Uint8Array(32);
@ -4717,7 +4802,13 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid bytes' + length); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
@ -4733,13 +4824,27 @@ var coderAddress = function(coerceFunc, localName) {
name: 'address', name: 'address',
type: 'address', type: 'address',
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(utils.getAddress(value)); value = utils.arrayify(utils.getAddress(value));
} catch (error) {
errors.throwError('invalid address', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
var result = new Uint8Array(32); var result = new Uint8Array(32);
result.set(value, 12); result.set(value, 12);
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid address'); } if (data.length < offset + 32) {
errors.throwError('insufficuent data for address type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'address',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32)))) value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32))))
@ -4759,12 +4864,33 @@ function _encodeDynamicBytes(value) {
]); ]);
} }
function _decodeDynamicBytes(data, offset) { function _decodeDynamicBytes(data, offset, localName) {
if (data.length < offset + 32) { throwError('invalid bytes'); } if (data.length < offset + 32) {
errors.throwError('insufficient data for dynamicBytes length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var length = uint256Coder.decode(data, offset).value; var length = uint256Coder.decode(data, offset).value;
try {
length = length.toNumber(); length = length.toNumber();
if (data.length < offset + 32 + length) { throwError('invalid bytes'); } } catch (error) {
errors.throwError('dynamic bytes count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: length.toString()
});
}
if (data.length < offset + 32 + length) {
errors.throwError('insufficient data for dynamicBytes type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32 + length))
});
}
return { return {
consumed: parseInt(32 + 32 * Math.ceil(length / 32)), consumed: parseInt(32 + 32 * Math.ceil(length / 32)),
@ -4778,10 +4904,19 @@ var coderDynamicBytes = function(coerceFunc, localName) {
name: 'bytes', name: 'bytes',
type: 'bytes', type: 'bytes',
encode: function(value) { encode: function(value) {
return _encodeDynamicBytes(utils.arrayify(value)); try {
value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid bytes value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
return _encodeDynamicBytes(value);
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('bytes', utils.hexlify(result.value)); result.value = coerceFunc('bytes', utils.hexlify(result.value));
return result; return result;
}, },
@ -4795,10 +4930,17 @@ var coderString = function(coerceFunc, localName) {
name: 'string', name: 'string',
type: 'string', type: 'string',
encode: function(value) { encode: function(value) {
if (typeof(value) !== 'string') {
errors.throwError('invalid string value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
return _encodeDynamicBytes(utils.toUtf8Bytes(value)); return _encodeDynamicBytes(utils.toUtf8Bytes(value));
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('string', utils.toUtf8String(result.value)); result.value = coerceFunc('string', utils.toUtf8String(result.value));
return result; return result;
}, },
@ -4811,10 +4953,9 @@ function alignSize(size) {
} }
function pack(coders, values) { function pack(coders, values) {
if (Array.isArray(values)) { if (Array.isArray(values)) {
if (coders.length !== values.length) { // do nothing
throwError('types/values mismatch', { type: type, values: values });
}
} else if (values && typeof(values) === 'object') { } else if (values && typeof(values) === 'object') {
var arrayValues = []; var arrayValues = [];
@ -4824,7 +4965,18 @@ function pack(coders, values) {
values = arrayValues; values = arrayValues;
} else { } else {
throwError('invalid value', { type: 'tuple', values: values }); errors.throwError('invalid tuple value', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
type: typeof(values),
value: values
});
}
if (coders.length !== values.length) {
errors.throwError('types/value length mismatch', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
value: values
});
} }
var parts = []; var parts = [];
@ -4919,7 +5071,14 @@ function coderArray(coerceFunc, coder, length, localName) {
name: 'array', name: 'array',
type: type, type: type,
encode: function(value) { encode: function(value) {
if (!Array.isArray(value)) { throwError('invalid array'); } if (!Array.isArray(value)) {
errors.throwError('expected array value', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
type: typeof(value),
value: value
});
}
var count = length; var count = length;
@ -4929,7 +5088,15 @@ function coderArray(coerceFunc, coder, length, localName) {
result = uint256Coder.encode(count); result = uint256Coder.encode(count);
} }
if (count !== value.length) { throwError('size mismatch'); } if (count !== value.length) {
error.throwError('array value length mismatch', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
count: value.length,
expectedCount: count,
value: value
});
}
var coders = []; var coders = [];
value.forEach(function(value) { coders.push(coder); }); value.forEach(function(value) { coders.push(coder); });
@ -4945,8 +5112,24 @@ function coderArray(coerceFunc, coder, length, localName) {
var count = length; var count = length;
if (count === -1) { if (count === -1) {
try {
var decodedLength = uint256Coder.decode(data, offset); var decodedLength = uint256Coder.decode(data, offset);
} catch (error) {
errors.throwError('insufficient data for dynamic array length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: error.value
});
}
try {
count = decodedLength.value.toNumber(); count = decodedLength.value.toNumber();
} catch (error) {
errors.throwError('array count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: decodedLength.value.toString()
});
}
consumed += decodedLength.consumed; consumed += decodedLength.consumed;
offset += decodedLength.consumed; offset += decodedLength.consumed;
} }
@ -5037,7 +5220,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[2] || 256); var size = parseInt(match[2] || 256);
if (size === 0 || size > 256 || (size % 8) !== 0) { if (size === 0 || size > 256 || (size % 8) !== 0) {
throwError('invalid type', { type: type }); errors.throwError('invalid ' + match[1] + ' bit length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName); return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName);
} }
@ -5046,7 +5232,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[1]); var size = parseInt(match[1]);
if (size === 0 || size > 32) { if (size === 0 || size > 32) {
throwError('invalid type ' + type); errors.throwError('invalid bytes length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderFixedBytes(coerceFunc, size, localName); return coderFixedBytes(coerceFunc, size, localName);
} }
@ -5074,7 +5263,10 @@ function getParamCoder(coerceFunc, type, localName) {
return coderNull(coerceFunc); return coderNull(coerceFunc);
} }
throwError('invalid type', { type: type }); errors.throwError('invalid type', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
function Coder(coerceFunc) { function Coder(coerceFunc) {
@ -5092,7 +5284,19 @@ utils.defineProperty(Coder.prototype, 'encode', function(names, types, values) {
names = null; names = null;
} }
if (types.length !== values.length) { throwError('types/values mismatch', {types: types, values: values}); } if (types.length !== values.length) {
errors.throwError('types/values length mismatch', errors.INVALID_ARGUMENT, {
count: { types: types.length, values: values.length },
value: { types: types, values: values }
});
}
if (names && names.length != types.length) {
errors.throwError('names/types length mismatch', errors.INVALID_ARGUMENT, {
count: { names: names.length, types: types.length },
value: { names: names, types: types }
});
}
var coders = []; var coders = [];
types.forEach(function(type, index) { types.forEach(function(type, index) {
@ -5126,7 +5330,7 @@ utils.defineProperty(Coder, 'defaultCoder', new Coder());
module.exports = Coder module.exports = Coder
},{"../utils/address":9,"../utils/bignumber.js":10,"../utils/convert.js":11,"../utils/properties.js":14,"../utils/throw-error":15,"../utils/utf8.js":16}],9:[function(require,module,exports){ },{"../utils/address":9,"../utils/bignumber.js":10,"../utils/convert.js":11,"../utils/properties.js":14,"../utils/utf8.js":16,"./errors":12}],9:[function(require,module,exports){
var BN = require('bn.js'); var BN = require('bn.js');
@ -5648,15 +5852,31 @@ var codes = { };
'MISSING_NEW', 'MISSING_NEW',
// Call exception
'CALL_EXCEPTION',
// Response from a server was invalid
// - response: The body of the response
//'BAD_RESPONSE',
// Invalid argument (e.g. type) to a function: // Invalid argument (e.g. type) to a function:
// - arg: The argument name that was invalid // - arg: The argument name that was invalid
// - value: The value of the argument
// - type: The type of the argument
// - expected: What was expected
'INVALID_ARGUMENT', 'INVALID_ARGUMENT',
// Missing argument to a function: // Missing argument to a function:
// - arg: The argument name that is required // - arg: The argument name that is required
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'MISSING_ARGUMENT', 'MISSING_ARGUMENT',
// Too many arguments // Too many arguments
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'UNEXPECTED_ARGUMENT', 'UNEXPECTED_ARGUMENT',
@ -5676,7 +5896,11 @@ defineProperty(codes, 'throwError', function(message, code, params) {
var messageDetails = []; var messageDetails = [];
Object.keys(params).forEach(function(key) { Object.keys(params).forEach(function(key) {
try {
messageDetails.push(key + '=' + JSON.stringify(params[key])); messageDetails.push(key + '=' + JSON.stringify(params[key]));
} catch (error) {
messageDetails.push(key + '=' + JSON.stringify(params[key].toString()));
}
}); });
var reason = message; var reason = message;
if (messageDetails.length) { if (messageDetails.length) {

File diff suppressed because one or more lines are too long

@ -1,4 +1,4 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
(function (module, exports) { (function (module, exports) {
'use strict'; 'use strict';
@ -6582,15 +6582,31 @@ var codes = { };
'MISSING_NEW', 'MISSING_NEW',
// Call exception
'CALL_EXCEPTION',
// Response from a server was invalid
// - response: The body of the response
//'BAD_RESPONSE',
// Invalid argument (e.g. type) to a function: // Invalid argument (e.g. type) to a function:
// - arg: The argument name that was invalid // - arg: The argument name that was invalid
// - value: The value of the argument
// - type: The type of the argument
// - expected: What was expected
'INVALID_ARGUMENT', 'INVALID_ARGUMENT',
// Missing argument to a function: // Missing argument to a function:
// - arg: The argument name that is required // - arg: The argument name that is required
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'MISSING_ARGUMENT', 'MISSING_ARGUMENT',
// Too many arguments // Too many arguments
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'UNEXPECTED_ARGUMENT', 'UNEXPECTED_ARGUMENT',
@ -6610,7 +6626,11 @@ defineProperty(codes, 'throwError', function(message, code, params) {
var messageDetails = []; var messageDetails = [];
Object.keys(params).forEach(function(key) { Object.keys(params).forEach(function(key) {
try {
messageDetails.push(key + '=' + JSON.stringify(params[key])); messageDetails.push(key + '=' + JSON.stringify(params[key]));
} catch (error) {
messageDetails.push(key + '=' + JSON.stringify(params[key].toString()));
}
}); });
var reason = message; var reason = message;
if (messageDetails.length) { if (messageDetails.length) {

File diff suppressed because one or more lines are too long

220
dist/ethers-utils.js vendored

@ -1,4 +1,4 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
(function (module, exports) { (function (module, exports) {
'use strict'; 'use strict';
@ -4875,8 +4875,6 @@ module.exports = undefined;
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var throwError = require('../utils/throw-error');
var utils = (function() { var utils = (function() {
var convert = require('../utils/convert.js'); var convert = require('../utils/convert.js');
var utf8 = require('../utils/utf8.js'); var utf8 = require('../utils/utf8.js');
@ -4900,6 +4898,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('./errors');
var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/); var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/); var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/); var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/);
@ -4935,7 +4935,16 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
value = utils.bigNumberify(value).toTwos(size * 8).maskn(size * 8); try {
value = utils.bigNumberify(value)
} catch (error) {
errors.throwError('invalid number value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
value = value.toTwos(size * 8).maskn(size * 8);
//value = value.toTwos(size * 8).maskn(size * 8); //value = value.toTwos(size * 8).maskn(size * 8);
if (signed) { if (signed) {
value = value.fromTwos(size * 8).toTwos(256); value = value.fromTwos(size * 8).toTwos(256);
@ -4943,7 +4952,13 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
return utils.padZeros(utils.arrayify(value), 32); return utils.padZeros(utils.arrayify(value), 32);
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid ' + name); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var junkLength = 32 - size; var junkLength = 32 - size;
var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32)); var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32));
if (signed) { if (signed) {
@ -4969,14 +4984,18 @@ var coderBoolean = function(coerceFunc, localName) {
name: 'boolean', name: 'boolean',
type: 'boolean', type: 'boolean',
encode: function(value) { encode: function(value) {
return uint256Coder.encode(value ? 1: 0); return uint256Coder.encode(!!value ? 1: 0);
}, },
decode: function(data, offset) { decode: function(data, offset) {
try { try {
var result = uint256Coder.decode(data, offset); var result = uint256Coder.decode(data, offset);
} catch (error) { } catch (error) {
if (error.message === 'invalid uint256') { if (error.reason === 'insufficient data for uint256 type') {
throwError('invalid bool'); errors.throwError('insufficient data for boolean type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'boolean',
value: error.value
});
} }
throw error; throw error;
} }
@ -4995,7 +5014,15 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(value); value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid ' + name + ' value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
if (length === 32) { return value; } if (length === 32) { return value; }
var result = new Uint8Array(32); var result = new Uint8Array(32);
@ -5003,7 +5030,13 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid bytes' + length); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
@ -5019,13 +5052,27 @@ var coderAddress = function(coerceFunc, localName) {
name: 'address', name: 'address',
type: 'address', type: 'address',
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(utils.getAddress(value)); value = utils.arrayify(utils.getAddress(value));
} catch (error) {
errors.throwError('invalid address', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
var result = new Uint8Array(32); var result = new Uint8Array(32);
result.set(value, 12); result.set(value, 12);
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid address'); } if (data.length < offset + 32) {
errors.throwError('insufficuent data for address type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'address',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32)))) value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32))))
@ -5045,12 +5092,33 @@ function _encodeDynamicBytes(value) {
]); ]);
} }
function _decodeDynamicBytes(data, offset) { function _decodeDynamicBytes(data, offset, localName) {
if (data.length < offset + 32) { throwError('invalid bytes'); } if (data.length < offset + 32) {
errors.throwError('insufficient data for dynamicBytes length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var length = uint256Coder.decode(data, offset).value; var length = uint256Coder.decode(data, offset).value;
try {
length = length.toNumber(); length = length.toNumber();
if (data.length < offset + 32 + length) { throwError('invalid bytes'); } } catch (error) {
errors.throwError('dynamic bytes count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: length.toString()
});
}
if (data.length < offset + 32 + length) {
errors.throwError('insufficient data for dynamicBytes type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32 + length))
});
}
return { return {
consumed: parseInt(32 + 32 * Math.ceil(length / 32)), consumed: parseInt(32 + 32 * Math.ceil(length / 32)),
@ -5064,10 +5132,19 @@ var coderDynamicBytes = function(coerceFunc, localName) {
name: 'bytes', name: 'bytes',
type: 'bytes', type: 'bytes',
encode: function(value) { encode: function(value) {
return _encodeDynamicBytes(utils.arrayify(value)); try {
value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid bytes value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
return _encodeDynamicBytes(value);
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('bytes', utils.hexlify(result.value)); result.value = coerceFunc('bytes', utils.hexlify(result.value));
return result; return result;
}, },
@ -5081,10 +5158,17 @@ var coderString = function(coerceFunc, localName) {
name: 'string', name: 'string',
type: 'string', type: 'string',
encode: function(value) { encode: function(value) {
if (typeof(value) !== 'string') {
errors.throwError('invalid string value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
return _encodeDynamicBytes(utils.toUtf8Bytes(value)); return _encodeDynamicBytes(utils.toUtf8Bytes(value));
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('string', utils.toUtf8String(result.value)); result.value = coerceFunc('string', utils.toUtf8String(result.value));
return result; return result;
}, },
@ -5097,10 +5181,9 @@ function alignSize(size) {
} }
function pack(coders, values) { function pack(coders, values) {
if (Array.isArray(values)) { if (Array.isArray(values)) {
if (coders.length !== values.length) { // do nothing
throwError('types/values mismatch', { type: type, values: values });
}
} else if (values && typeof(values) === 'object') { } else if (values && typeof(values) === 'object') {
var arrayValues = []; var arrayValues = [];
@ -5110,7 +5193,18 @@ function pack(coders, values) {
values = arrayValues; values = arrayValues;
} else { } else {
throwError('invalid value', { type: 'tuple', values: values }); errors.throwError('invalid tuple value', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
type: typeof(values),
value: values
});
}
if (coders.length !== values.length) {
errors.throwError('types/value length mismatch', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
value: values
});
} }
var parts = []; var parts = [];
@ -5205,7 +5299,14 @@ function coderArray(coerceFunc, coder, length, localName) {
name: 'array', name: 'array',
type: type, type: type,
encode: function(value) { encode: function(value) {
if (!Array.isArray(value)) { throwError('invalid array'); } if (!Array.isArray(value)) {
errors.throwError('expected array value', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
type: typeof(value),
value: value
});
}
var count = length; var count = length;
@ -5215,7 +5316,15 @@ function coderArray(coerceFunc, coder, length, localName) {
result = uint256Coder.encode(count); result = uint256Coder.encode(count);
} }
if (count !== value.length) { throwError('size mismatch'); } if (count !== value.length) {
error.throwError('array value length mismatch', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
count: value.length,
expectedCount: count,
value: value
});
}
var coders = []; var coders = [];
value.forEach(function(value) { coders.push(coder); }); value.forEach(function(value) { coders.push(coder); });
@ -5231,8 +5340,24 @@ function coderArray(coerceFunc, coder, length, localName) {
var count = length; var count = length;
if (count === -1) { if (count === -1) {
try {
var decodedLength = uint256Coder.decode(data, offset); var decodedLength = uint256Coder.decode(data, offset);
} catch (error) {
errors.throwError('insufficient data for dynamic array length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: error.value
});
}
try {
count = decodedLength.value.toNumber(); count = decodedLength.value.toNumber();
} catch (error) {
errors.throwError('array count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: decodedLength.value.toString()
});
}
consumed += decodedLength.consumed; consumed += decodedLength.consumed;
offset += decodedLength.consumed; offset += decodedLength.consumed;
} }
@ -5323,7 +5448,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[2] || 256); var size = parseInt(match[2] || 256);
if (size === 0 || size > 256 || (size % 8) !== 0) { if (size === 0 || size > 256 || (size % 8) !== 0) {
throwError('invalid type', { type: type }); errors.throwError('invalid ' + match[1] + ' bit length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName); return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName);
} }
@ -5332,7 +5460,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[1]); var size = parseInt(match[1]);
if (size === 0 || size > 32) { if (size === 0 || size > 32) {
throwError('invalid type ' + type); errors.throwError('invalid bytes length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderFixedBytes(coerceFunc, size, localName); return coderFixedBytes(coerceFunc, size, localName);
} }
@ -5360,7 +5491,10 @@ function getParamCoder(coerceFunc, type, localName) {
return coderNull(coerceFunc); return coderNull(coerceFunc);
} }
throwError('invalid type', { type: type }); errors.throwError('invalid type', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
function Coder(coerceFunc) { function Coder(coerceFunc) {
@ -5378,7 +5512,19 @@ utils.defineProperty(Coder.prototype, 'encode', function(names, types, values) {
names = null; names = null;
} }
if (types.length !== values.length) { throwError('types/values mismatch', {types: types, values: values}); } if (types.length !== values.length) {
errors.throwError('types/values length mismatch', errors.INVALID_ARGUMENT, {
count: { types: types.length, values: values.length },
value: { types: types, values: values }
});
}
if (names && names.length != types.length) {
errors.throwError('names/types length mismatch', errors.INVALID_ARGUMENT, {
count: { names: names.length, types: types.length },
value: { names: names, types: types }
});
}
var coders = []; var coders = [];
types.forEach(function(type, index) { types.forEach(function(type, index) {
@ -5412,7 +5558,7 @@ utils.defineProperty(Coder, 'defaultCoder', new Coder());
module.exports = Coder module.exports = Coder
},{"../utils/address":20,"../utils/bignumber.js":21,"../utils/convert.js":25,"../utils/properties.js":31,"../utils/throw-error":35,"../utils/utf8.js":37}],20:[function(require,module,exports){ },{"../utils/address":20,"../utils/bignumber.js":21,"../utils/convert.js":25,"../utils/properties.js":31,"../utils/utf8.js":37,"./errors":26}],20:[function(require,module,exports){
var BN = require('bn.js'); var BN = require('bn.js');
@ -6029,15 +6175,31 @@ var codes = { };
'MISSING_NEW', 'MISSING_NEW',
// Call exception
'CALL_EXCEPTION',
// Response from a server was invalid
// - response: The body of the response
//'BAD_RESPONSE',
// Invalid argument (e.g. type) to a function: // Invalid argument (e.g. type) to a function:
// - arg: The argument name that was invalid // - arg: The argument name that was invalid
// - value: The value of the argument
// - type: The type of the argument
// - expected: What was expected
'INVALID_ARGUMENT', 'INVALID_ARGUMENT',
// Missing argument to a function: // Missing argument to a function:
// - arg: The argument name that is required // - arg: The argument name that is required
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'MISSING_ARGUMENT', 'MISSING_ARGUMENT',
// Too many arguments // Too many arguments
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'UNEXPECTED_ARGUMENT', 'UNEXPECTED_ARGUMENT',
@ -6057,7 +6219,11 @@ defineProperty(codes, 'throwError', function(message, code, params) {
var messageDetails = []; var messageDetails = [];
Object.keys(params).forEach(function(key) { Object.keys(params).forEach(function(key) {
try {
messageDetails.push(key + '=' + JSON.stringify(params[key])); messageDetails.push(key + '=' + JSON.stringify(params[key]));
} catch (error) {
messageDetails.push(key + '=' + JSON.stringify(params[key].toString()));
}
}); });
var reason = message; var reason = message;
if (messageDetails.length) { if (messageDetails.length) {

File diff suppressed because one or more lines are too long

220
dist/ethers-wallet.js vendored

@ -1,4 +1,4 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
"use strict"; "use strict";
(function(root) { (function(root) {
@ -8879,8 +8879,6 @@ module.exports = uuid;
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var throwError = require('../utils/throw-error');
var utils = (function() { var utils = (function() {
var convert = require('../utils/convert.js'); var convert = require('../utils/convert.js');
var utf8 = require('../utils/utf8.js'); var utf8 = require('../utils/utf8.js');
@ -8904,6 +8902,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('./errors');
var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/); var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/); var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/); var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/);
@ -8939,7 +8939,16 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
value = utils.bigNumberify(value).toTwos(size * 8).maskn(size * 8); try {
value = utils.bigNumberify(value)
} catch (error) {
errors.throwError('invalid number value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
value = value.toTwos(size * 8).maskn(size * 8);
//value = value.toTwos(size * 8).maskn(size * 8); //value = value.toTwos(size * 8).maskn(size * 8);
if (signed) { if (signed) {
value = value.fromTwos(size * 8).toTwos(256); value = value.fromTwos(size * 8).toTwos(256);
@ -8947,7 +8956,13 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
return utils.padZeros(utils.arrayify(value), 32); return utils.padZeros(utils.arrayify(value), 32);
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid ' + name); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var junkLength = 32 - size; var junkLength = 32 - size;
var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32)); var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32));
if (signed) { if (signed) {
@ -8973,14 +8988,18 @@ var coderBoolean = function(coerceFunc, localName) {
name: 'boolean', name: 'boolean',
type: 'boolean', type: 'boolean',
encode: function(value) { encode: function(value) {
return uint256Coder.encode(value ? 1: 0); return uint256Coder.encode(!!value ? 1: 0);
}, },
decode: function(data, offset) { decode: function(data, offset) {
try { try {
var result = uint256Coder.decode(data, offset); var result = uint256Coder.decode(data, offset);
} catch (error) { } catch (error) {
if (error.message === 'invalid uint256') { if (error.reason === 'insufficient data for uint256 type') {
throwError('invalid bool'); errors.throwError('insufficient data for boolean type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'boolean',
value: error.value
});
} }
throw error; throw error;
} }
@ -8999,7 +9018,15 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(value); value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid ' + name + ' value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
if (length === 32) { return value; } if (length === 32) { return value; }
var result = new Uint8Array(32); var result = new Uint8Array(32);
@ -9007,7 +9034,13 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid bytes' + length); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
@ -9023,13 +9056,27 @@ var coderAddress = function(coerceFunc, localName) {
name: 'address', name: 'address',
type: 'address', type: 'address',
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(utils.getAddress(value)); value = utils.arrayify(utils.getAddress(value));
} catch (error) {
errors.throwError('invalid address', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
var result = new Uint8Array(32); var result = new Uint8Array(32);
result.set(value, 12); result.set(value, 12);
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid address'); } if (data.length < offset + 32) {
errors.throwError('insufficuent data for address type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'address',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32)))) value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32))))
@ -9049,12 +9096,33 @@ function _encodeDynamicBytes(value) {
]); ]);
} }
function _decodeDynamicBytes(data, offset) { function _decodeDynamicBytes(data, offset, localName) {
if (data.length < offset + 32) { throwError('invalid bytes'); } if (data.length < offset + 32) {
errors.throwError('insufficient data for dynamicBytes length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var length = uint256Coder.decode(data, offset).value; var length = uint256Coder.decode(data, offset).value;
try {
length = length.toNumber(); length = length.toNumber();
if (data.length < offset + 32 + length) { throwError('invalid bytes'); } } catch (error) {
errors.throwError('dynamic bytes count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: length.toString()
});
}
if (data.length < offset + 32 + length) {
errors.throwError('insufficient data for dynamicBytes type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32 + length))
});
}
return { return {
consumed: parseInt(32 + 32 * Math.ceil(length / 32)), consumed: parseInt(32 + 32 * Math.ceil(length / 32)),
@ -9068,10 +9136,19 @@ var coderDynamicBytes = function(coerceFunc, localName) {
name: 'bytes', name: 'bytes',
type: 'bytes', type: 'bytes',
encode: function(value) { encode: function(value) {
return _encodeDynamicBytes(utils.arrayify(value)); try {
value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid bytes value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
return _encodeDynamicBytes(value);
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('bytes', utils.hexlify(result.value)); result.value = coerceFunc('bytes', utils.hexlify(result.value));
return result; return result;
}, },
@ -9085,10 +9162,17 @@ var coderString = function(coerceFunc, localName) {
name: 'string', name: 'string',
type: 'string', type: 'string',
encode: function(value) { encode: function(value) {
if (typeof(value) !== 'string') {
errors.throwError('invalid string value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
return _encodeDynamicBytes(utils.toUtf8Bytes(value)); return _encodeDynamicBytes(utils.toUtf8Bytes(value));
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('string', utils.toUtf8String(result.value)); result.value = coerceFunc('string', utils.toUtf8String(result.value));
return result; return result;
}, },
@ -9101,10 +9185,9 @@ function alignSize(size) {
} }
function pack(coders, values) { function pack(coders, values) {
if (Array.isArray(values)) { if (Array.isArray(values)) {
if (coders.length !== values.length) { // do nothing
throwError('types/values mismatch', { type: type, values: values });
}
} else if (values && typeof(values) === 'object') { } else if (values && typeof(values) === 'object') {
var arrayValues = []; var arrayValues = [];
@ -9114,7 +9197,18 @@ function pack(coders, values) {
values = arrayValues; values = arrayValues;
} else { } else {
throwError('invalid value', { type: 'tuple', values: values }); errors.throwError('invalid tuple value', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
type: typeof(values),
value: values
});
}
if (coders.length !== values.length) {
errors.throwError('types/value length mismatch', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
value: values
});
} }
var parts = []; var parts = [];
@ -9209,7 +9303,14 @@ function coderArray(coerceFunc, coder, length, localName) {
name: 'array', name: 'array',
type: type, type: type,
encode: function(value) { encode: function(value) {
if (!Array.isArray(value)) { throwError('invalid array'); } if (!Array.isArray(value)) {
errors.throwError('expected array value', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
type: typeof(value),
value: value
});
}
var count = length; var count = length;
@ -9219,7 +9320,15 @@ function coderArray(coerceFunc, coder, length, localName) {
result = uint256Coder.encode(count); result = uint256Coder.encode(count);
} }
if (count !== value.length) { throwError('size mismatch'); } if (count !== value.length) {
error.throwError('array value length mismatch', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
count: value.length,
expectedCount: count,
value: value
});
}
var coders = []; var coders = [];
value.forEach(function(value) { coders.push(coder); }); value.forEach(function(value) { coders.push(coder); });
@ -9235,8 +9344,24 @@ function coderArray(coerceFunc, coder, length, localName) {
var count = length; var count = length;
if (count === -1) { if (count === -1) {
try {
var decodedLength = uint256Coder.decode(data, offset); var decodedLength = uint256Coder.decode(data, offset);
} catch (error) {
errors.throwError('insufficient data for dynamic array length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: error.value
});
}
try {
count = decodedLength.value.toNumber(); count = decodedLength.value.toNumber();
} catch (error) {
errors.throwError('array count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: decodedLength.value.toString()
});
}
consumed += decodedLength.consumed; consumed += decodedLength.consumed;
offset += decodedLength.consumed; offset += decodedLength.consumed;
} }
@ -9327,7 +9452,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[2] || 256); var size = parseInt(match[2] || 256);
if (size === 0 || size > 256 || (size % 8) !== 0) { if (size === 0 || size > 256 || (size % 8) !== 0) {
throwError('invalid type', { type: type }); errors.throwError('invalid ' + match[1] + ' bit length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName); return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName);
} }
@ -9336,7 +9464,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[1]); var size = parseInt(match[1]);
if (size === 0 || size > 32) { if (size === 0 || size > 32) {
throwError('invalid type ' + type); errors.throwError('invalid bytes length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderFixedBytes(coerceFunc, size, localName); return coderFixedBytes(coerceFunc, size, localName);
} }
@ -9364,7 +9495,10 @@ function getParamCoder(coerceFunc, type, localName) {
return coderNull(coerceFunc); return coderNull(coerceFunc);
} }
throwError('invalid type', { type: type }); errors.throwError('invalid type', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
function Coder(coerceFunc) { function Coder(coerceFunc) {
@ -9382,7 +9516,19 @@ utils.defineProperty(Coder.prototype, 'encode', function(names, types, values) {
names = null; names = null;
} }
if (types.length !== values.length) { throwError('types/values mismatch', {types: types, values: values}); } if (types.length !== values.length) {
errors.throwError('types/values length mismatch', errors.INVALID_ARGUMENT, {
count: { types: types.length, values: values.length },
value: { types: types, values: values }
});
}
if (names && names.length != types.length) {
errors.throwError('names/types length mismatch', errors.INVALID_ARGUMENT, {
count: { names: names.length, types: types.length },
value: { names: names, types: types }
});
}
var coders = []; var coders = [];
types.forEach(function(type, index) { types.forEach(function(type, index) {
@ -9416,7 +9562,7 @@ utils.defineProperty(Coder, 'defaultCoder', new Coder());
module.exports = Coder module.exports = Coder
},{"../utils/address":42,"../utils/bignumber.js":43,"../utils/convert.js":47,"../utils/properties.js":55,"../utils/throw-error":59,"../utils/utf8.js":61}],42:[function(require,module,exports){ },{"../utils/address":42,"../utils/bignumber.js":43,"../utils/convert.js":47,"../utils/properties.js":55,"../utils/utf8.js":61,"./errors":48}],42:[function(require,module,exports){
var BN = require('bn.js'); var BN = require('bn.js');
@ -10033,15 +10179,31 @@ var codes = { };
'MISSING_NEW', 'MISSING_NEW',
// Call exception
'CALL_EXCEPTION',
// Response from a server was invalid
// - response: The body of the response
//'BAD_RESPONSE',
// Invalid argument (e.g. type) to a function: // Invalid argument (e.g. type) to a function:
// - arg: The argument name that was invalid // - arg: The argument name that was invalid
// - value: The value of the argument
// - type: The type of the argument
// - expected: What was expected
'INVALID_ARGUMENT', 'INVALID_ARGUMENT',
// Missing argument to a function: // Missing argument to a function:
// - arg: The argument name that is required // - arg: The argument name that is required
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'MISSING_ARGUMENT', 'MISSING_ARGUMENT',
// Too many arguments // Too many arguments
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'UNEXPECTED_ARGUMENT', 'UNEXPECTED_ARGUMENT',
@ -10061,7 +10223,11 @@ defineProperty(codes, 'throwError', function(message, code, params) {
var messageDetails = []; var messageDetails = [];
Object.keys(params).forEach(function(key) { Object.keys(params).forEach(function(key) {
try {
messageDetails.push(key + '=' + JSON.stringify(params[key])); messageDetails.push(key + '=' + JSON.stringify(params[key]));
} catch (error) {
messageDetails.push(key + '=' + JSON.stringify(params[key].toString()));
}
}); });
var reason = message; var reason = message;
if (messageDetails.length) { if (messageDetails.length) {

File diff suppressed because one or more lines are too long

322
dist/ethers.js vendored

@ -1,4 +1,4 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ethers = f()}})(function(){var define,module,exports;return (function(){function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}return e})()({1:[function(require,module,exports){
'use strict'; 'use strict';
var Interface = require('./interface.js'); var Interface = require('./interface.js');
@ -15,6 +15,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('../utils/errors');
var allowedTransactionKeys = { var allowedTransactionKeys = {
data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true data: true, from: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
} }
@ -64,12 +66,8 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
var params = Array.prototype.slice.call(arguments); var params = Array.prototype.slice.call(arguments);
// If 1 extra parameter was passed in, it contains overrides // If 1 extra parameter was passed in, it contains overrides
if (params.length == method.inputs.types.length + 1) { if (params.length === method.inputs.types.length + 1 && typeof(params[params.length - 1]) === 'object') {
transaction = params.pop(); transaction = copyObject(params.pop());
if (typeof(transaction) !== 'object') {
throw new Error('invalid transaction overrides');
}
transaction = copyObject(transaction);
// Check for unexpected keys (e.g. using "gas" instead of "gasLimit") // Check for unexpected keys (e.g. using "gas" instead of "gasLimit")
for (var key in transaction) { for (var key in transaction) {
@ -77,10 +75,6 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
throw new Error('unknown transaction override ' + key); throw new Error('unknown transaction override ' + key);
} }
} }
} else if (params.length > method.inputs.types.length) {
throw new Error('too many parameters');
} else if (params.length < method.inputs.types.length) {
throw new Error('too few parameters');
} }
// Check overrides make sense // Check overrides make sense
@ -130,7 +124,18 @@ function Contract(addressOrName, contractInterface, signerOrProvider) {
return provider.call(transaction); return provider.call(transaction);
}).then(function(value) { }).then(function(value) {
try {
var result = call.parse(value); var result = call.parse(value);
} catch (error) {
if (value === '0x' && method.inputs.types.length > 0) {
errors.throwError('call exception', errors.CALL_EXCEPTION, {
address: addressOrName,
method: call.signature,
value: params
});
}
throw error;
}
if (method.outputs.types.length === 1) { if (method.outputs.types.length === 1) {
result = result[0]; result = result[0];
} }
@ -317,7 +322,7 @@ utils.defineProperty(Contract, 'getDeployTransaction', function(bytecode, contra
module.exports = Contract; module.exports = Contract;
},{"../utils/address.js":56,"../utils/bignumber.js":57,"../utils/convert.js":61,"../utils/properties.js":70,"./interface.js":3}],2:[function(require,module,exports){ },{"../utils/address.js":56,"../utils/bignumber.js":57,"../utils/convert.js":61,"../utils/errors":63,"../utils/properties.js":70,"./interface.js":3}],2:[function(require,module,exports){
'use strict'; 'use strict';
var Contract = require('./contract.js'); var Contract = require('./contract.js');
@ -334,8 +339,6 @@ module.exports = {
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var throwError = require('../utils/throw-error');
var utils = (function() { var utils = (function() {
var convert = require('../utils/convert'); var convert = require('../utils/convert');
var properties = require('../utils/properties'); var properties = require('../utils/properties');
@ -357,6 +360,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('../utils/errors');
function parseParams(params) { function parseParams(params) {
var names = []; var names = [];
var types = []; var types = [];
@ -435,7 +440,11 @@ function Interface(abi) {
try { try {
abi = JSON.parse(abi); abi = JSON.parse(abi);
} catch (error) { } catch (error) {
throwError('invalid abi', { input: abi }); errors.throwError('could not parse ABI JSON', errors.INVALID_ARGUMENT, {
arg: 'abi',
errorMessage: error.message,
value: abi
});
} }
} }
@ -455,18 +464,39 @@ function Interface(abi) {
var func = function(bytecode) { var func = function(bytecode) {
if (!utils.isHexString(bytecode)) { if (!utils.isHexString(bytecode)) {
throwError('invalid bytecode', { input: bytecode }); errors.throwError('invalid contract bytecode', errors.INVALID_ARGUMENT, {
arg: 'bytecode',
type: typeof(bytecode),
value: bytecode
});
} }
var params = Array.prototype.slice.call(arguments, 1); var params = Array.prototype.slice.call(arguments, 1);
if (params.length < inputParams.types.length) { if (params.length < inputParams.types.length) {
throwError('missing parameter'); errors.throwError('missing constructor argument', errors.MISSING_ARGUMENT, {
arg: (inputParams.names[params.length] || 'unknown'),
count: params.length,
expectedCount: inputParams.types.length
});
} else if (params.length > inputParams.types.length) { } else if (params.length > inputParams.types.length) {
throwError('too many parameters'); errors.throwError('too many constructor arguments', errors.UNEXPECTED_ARGUMENT, {
count: params.length,
expectedCount: inputParams.types.length
});
}
try {
var encodedParams = utils.coder.encode(inputParams.names, inputParams.types, params)
} catch (error) {
errors.throwError('invalid constructor argument', errors.INVALID_ARGUMENT, {
arg: error.arg,
reason: error.reason,
value: error.value
});
} }
var result = { var result = {
bytecode: bytecode + utils.coder.encode(inputParams.names, inputParams.types, params).substring(2), bytecode: bytecode + encodedParams.substring(2),
type: 'deploy' type: 'deploy'
} }
@ -493,11 +523,21 @@ function Interface(abi) {
signature = method.name + signature; signature = method.name + signature;
var parse = function(data) { var parse = function(data) {
try {
return utils.coder.decode( return utils.coder.decode(
outputParams.names, outputParams.names,
outputParams.types, outputParams.types,
utils.arrayify(data) utils.arrayify(data)
); );
} catch(error) {
errors.throwError('invalid data for function output', errors.INVALID_ARGUMENT, {
arg: 'data',
errorArg: error.arg,
errorValue: error.value,
value: data,
reason: error.reason
});
}
}; };
var sighash = utils.keccak256(utils.toUtf8Bytes(signature)).substring(0, 10); var sighash = utils.keccak256(utils.toUtf8Bytes(signature)).substring(0, 10);
@ -512,12 +552,30 @@ function Interface(abi) {
var params = Array.prototype.slice.call(arguments, 0); var params = Array.prototype.slice.call(arguments, 0);
if (params.length < inputParams.types.length) { if (params.length < inputParams.types.length) {
throwError('missing parameter'); errors.throwError('missing input argument', errors.MISSING_ARGUMENT, {
arg: (inputParams.names[params.length] || 'unknown'),
count: params.length,
expectedCount: inputParams.types.length,
name: method.name
});
} else if (params.length > inputParams.types.length) { } else if (params.length > inputParams.types.length) {
throwError('too many parameters'); errors.throwError('too many input arguments', errors.UNEXPECTED_ARGUMENT, {
count: params.length,
expectedCount: inputParams.types.length
});
} }
result.data = sighash + utils.coder.encode(inputParams.names, inputParams.types, params).substring(2); try {
var encodedParams = utils.coder.encode(inputParams.names, inputParams.types, params);
} catch (error) {
errors.throwError('invalid input argument', errors.INVALID_ARGUMENT, {
arg: error.arg,
reason: error.reason,
value: error.value
});
}
result.data = sighash + encodedParams.substring(2);
result.parse = parse; result.parse = parse;
return populateDescription(new FunctionDescription(), result); return populateDescription(new FunctionDescription(), result);
@ -672,7 +730,7 @@ function Interface(abi) {
module.exports = Interface; module.exports = Interface;
},{"../utils/abi-coder":55,"../utils/convert":61,"../utils/keccak256":67,"../utils/properties":70,"../utils/throw-error":74,"../utils/utf8":76}],4:[function(require,module,exports){ },{"../utils/abi-coder":55,"../utils/convert":61,"../utils/errors":63,"../utils/keccak256":67,"../utils/properties":70,"../utils/utf8":76}],4:[function(require,module,exports){
'use strict'; 'use strict';
var version = require('./package.json').version; var version = require('./package.json').version;
@ -11658,8 +11716,6 @@ module.exports = Web3Provider;
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var throwError = require('../utils/throw-error');
var utils = (function() { var utils = (function() {
var convert = require('../utils/convert.js'); var convert = require('../utils/convert.js');
var utf8 = require('../utils/utf8.js'); var utf8 = require('../utils/utf8.js');
@ -11683,6 +11739,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('./errors');
var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/); var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/); var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/); var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/);
@ -11718,7 +11776,16 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
value = utils.bigNumberify(value).toTwos(size * 8).maskn(size * 8); try {
value = utils.bigNumberify(value)
} catch (error) {
errors.throwError('invalid number value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
value = value.toTwos(size * 8).maskn(size * 8);
//value = value.toTwos(size * 8).maskn(size * 8); //value = value.toTwos(size * 8).maskn(size * 8);
if (signed) { if (signed) {
value = value.fromTwos(size * 8).toTwos(256); value = value.fromTwos(size * 8).toTwos(256);
@ -11726,7 +11793,13 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
return utils.padZeros(utils.arrayify(value), 32); return utils.padZeros(utils.arrayify(value), 32);
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid ' + name); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var junkLength = 32 - size; var junkLength = 32 - size;
var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32)); var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32));
if (signed) { if (signed) {
@ -11752,14 +11825,18 @@ var coderBoolean = function(coerceFunc, localName) {
name: 'boolean', name: 'boolean',
type: 'boolean', type: 'boolean',
encode: function(value) { encode: function(value) {
return uint256Coder.encode(value ? 1: 0); return uint256Coder.encode(!!value ? 1: 0);
}, },
decode: function(data, offset) { decode: function(data, offset) {
try { try {
var result = uint256Coder.decode(data, offset); var result = uint256Coder.decode(data, offset);
} catch (error) { } catch (error) {
if (error.message === 'invalid uint256') { if (error.reason === 'insufficient data for uint256 type') {
throwError('invalid bool'); errors.throwError('insufficient data for boolean type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'boolean',
value: error.value
});
} }
throw error; throw error;
} }
@ -11778,7 +11855,15 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(value); value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid ' + name + ' value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
if (length === 32) { return value; } if (length === 32) { return value; }
var result = new Uint8Array(32); var result = new Uint8Array(32);
@ -11786,7 +11871,13 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid bytes' + length); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
@ -11802,13 +11893,27 @@ var coderAddress = function(coerceFunc, localName) {
name: 'address', name: 'address',
type: 'address', type: 'address',
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(utils.getAddress(value)); value = utils.arrayify(utils.getAddress(value));
} catch (error) {
errors.throwError('invalid address', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
var result = new Uint8Array(32); var result = new Uint8Array(32);
result.set(value, 12); result.set(value, 12);
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid address'); } if (data.length < offset + 32) {
errors.throwError('insufficuent data for address type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'address',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32)))) value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32))))
@ -11828,12 +11933,33 @@ function _encodeDynamicBytes(value) {
]); ]);
} }
function _decodeDynamicBytes(data, offset) { function _decodeDynamicBytes(data, offset, localName) {
if (data.length < offset + 32) { throwError('invalid bytes'); } if (data.length < offset + 32) {
errors.throwError('insufficient data for dynamicBytes length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var length = uint256Coder.decode(data, offset).value; var length = uint256Coder.decode(data, offset).value;
try {
length = length.toNumber(); length = length.toNumber();
if (data.length < offset + 32 + length) { throwError('invalid bytes'); } } catch (error) {
errors.throwError('dynamic bytes count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: length.toString()
});
}
if (data.length < offset + 32 + length) {
errors.throwError('insufficient data for dynamicBytes type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32 + length))
});
}
return { return {
consumed: parseInt(32 + 32 * Math.ceil(length / 32)), consumed: parseInt(32 + 32 * Math.ceil(length / 32)),
@ -11847,10 +11973,19 @@ var coderDynamicBytes = function(coerceFunc, localName) {
name: 'bytes', name: 'bytes',
type: 'bytes', type: 'bytes',
encode: function(value) { encode: function(value) {
return _encodeDynamicBytes(utils.arrayify(value)); try {
value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid bytes value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
return _encodeDynamicBytes(value);
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('bytes', utils.hexlify(result.value)); result.value = coerceFunc('bytes', utils.hexlify(result.value));
return result; return result;
}, },
@ -11864,10 +11999,17 @@ var coderString = function(coerceFunc, localName) {
name: 'string', name: 'string',
type: 'string', type: 'string',
encode: function(value) { encode: function(value) {
if (typeof(value) !== 'string') {
errors.throwError('invalid string value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
return _encodeDynamicBytes(utils.toUtf8Bytes(value)); return _encodeDynamicBytes(utils.toUtf8Bytes(value));
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('string', utils.toUtf8String(result.value)); result.value = coerceFunc('string', utils.toUtf8String(result.value));
return result; return result;
}, },
@ -11880,10 +12022,9 @@ function alignSize(size) {
} }
function pack(coders, values) { function pack(coders, values) {
if (Array.isArray(values)) { if (Array.isArray(values)) {
if (coders.length !== values.length) { // do nothing
throwError('types/values mismatch', { type: type, values: values });
}
} else if (values && typeof(values) === 'object') { } else if (values && typeof(values) === 'object') {
var arrayValues = []; var arrayValues = [];
@ -11893,7 +12034,18 @@ function pack(coders, values) {
values = arrayValues; values = arrayValues;
} else { } else {
throwError('invalid value', { type: 'tuple', values: values }); errors.throwError('invalid tuple value', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
type: typeof(values),
value: values
});
}
if (coders.length !== values.length) {
errors.throwError('types/value length mismatch', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
value: values
});
} }
var parts = []; var parts = [];
@ -11988,7 +12140,14 @@ function coderArray(coerceFunc, coder, length, localName) {
name: 'array', name: 'array',
type: type, type: type,
encode: function(value) { encode: function(value) {
if (!Array.isArray(value)) { throwError('invalid array'); } if (!Array.isArray(value)) {
errors.throwError('expected array value', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
type: typeof(value),
value: value
});
}
var count = length; var count = length;
@ -11998,7 +12157,15 @@ function coderArray(coerceFunc, coder, length, localName) {
result = uint256Coder.encode(count); result = uint256Coder.encode(count);
} }
if (count !== value.length) { throwError('size mismatch'); } if (count !== value.length) {
error.throwError('array value length mismatch', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
count: value.length,
expectedCount: count,
value: value
});
}
var coders = []; var coders = [];
value.forEach(function(value) { coders.push(coder); }); value.forEach(function(value) { coders.push(coder); });
@ -12014,8 +12181,24 @@ function coderArray(coerceFunc, coder, length, localName) {
var count = length; var count = length;
if (count === -1) { if (count === -1) {
try {
var decodedLength = uint256Coder.decode(data, offset); var decodedLength = uint256Coder.decode(data, offset);
} catch (error) {
errors.throwError('insufficient data for dynamic array length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: error.value
});
}
try {
count = decodedLength.value.toNumber(); count = decodedLength.value.toNumber();
} catch (error) {
errors.throwError('array count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: decodedLength.value.toString()
});
}
consumed += decodedLength.consumed; consumed += decodedLength.consumed;
offset += decodedLength.consumed; offset += decodedLength.consumed;
} }
@ -12106,7 +12289,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[2] || 256); var size = parseInt(match[2] || 256);
if (size === 0 || size > 256 || (size % 8) !== 0) { if (size === 0 || size > 256 || (size % 8) !== 0) {
throwError('invalid type', { type: type }); errors.throwError('invalid ' + match[1] + ' bit length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName); return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName);
} }
@ -12115,7 +12301,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[1]); var size = parseInt(match[1]);
if (size === 0 || size > 32) { if (size === 0 || size > 32) {
throwError('invalid type ' + type); errors.throwError('invalid bytes length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderFixedBytes(coerceFunc, size, localName); return coderFixedBytes(coerceFunc, size, localName);
} }
@ -12143,7 +12332,10 @@ function getParamCoder(coerceFunc, type, localName) {
return coderNull(coerceFunc); return coderNull(coerceFunc);
} }
throwError('invalid type', { type: type }); errors.throwError('invalid type', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
function Coder(coerceFunc) { function Coder(coerceFunc) {
@ -12161,7 +12353,19 @@ utils.defineProperty(Coder.prototype, 'encode', function(names, types, values) {
names = null; names = null;
} }
if (types.length !== values.length) { throwError('types/values mismatch', {types: types, values: values}); } if (types.length !== values.length) {
errors.throwError('types/values length mismatch', errors.INVALID_ARGUMENT, {
count: { types: types.length, values: values.length },
value: { types: types, values: values }
});
}
if (names && names.length != types.length) {
errors.throwError('names/types length mismatch', errors.INVALID_ARGUMENT, {
count: { names: names.length, types: types.length },
value: { names: names, types: types }
});
}
var coders = []; var coders = [];
types.forEach(function(type, index) { types.forEach(function(type, index) {
@ -12195,7 +12399,7 @@ utils.defineProperty(Coder, 'defaultCoder', new Coder());
module.exports = Coder module.exports = Coder
},{"../utils/address":56,"../utils/bignumber.js":57,"../utils/convert.js":61,"../utils/properties.js":70,"../utils/throw-error":74,"../utils/utf8.js":76}],56:[function(require,module,exports){ },{"../utils/address":56,"../utils/bignumber.js":57,"../utils/convert.js":61,"../utils/properties.js":70,"../utils/utf8.js":76,"./errors":63}],56:[function(require,module,exports){
var BN = require('bn.js'); var BN = require('bn.js');
@ -12815,15 +13019,31 @@ var codes = { };
'MISSING_NEW', 'MISSING_NEW',
// Call exception
'CALL_EXCEPTION',
// Response from a server was invalid
// - response: The body of the response
//'BAD_RESPONSE',
// Invalid argument (e.g. type) to a function: // Invalid argument (e.g. type) to a function:
// - arg: The argument name that was invalid // - arg: The argument name that was invalid
// - value: The value of the argument
// - type: The type of the argument
// - expected: What was expected
'INVALID_ARGUMENT', 'INVALID_ARGUMENT',
// Missing argument to a function: // Missing argument to a function:
// - arg: The argument name that is required // - arg: The argument name that is required
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'MISSING_ARGUMENT', 'MISSING_ARGUMENT',
// Too many arguments // Too many arguments
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'UNEXPECTED_ARGUMENT', 'UNEXPECTED_ARGUMENT',
@ -12843,7 +13063,11 @@ defineProperty(codes, 'throwError', function(message, code, params) {
var messageDetails = []; var messageDetails = [];
Object.keys(params).forEach(function(key) { Object.keys(params).forEach(function(key) {
try {
messageDetails.push(key + '=' + JSON.stringify(params[key])); messageDetails.push(key + '=' + JSON.stringify(params[key]));
} catch (error) {
messageDetails.push(key + '=' + JSON.stringify(params[key].toString()));
}
}); });
var reason = message; var reason = message;
if (messageDetails.length) { if (messageDetails.length) {

6
dist/ethers.min.js vendored

File diff suppressed because one or more lines are too long

@ -1,6 +1,6 @@
{ {
"name": "ethers", "name": "ethers",
"version": "3.0.14", "version": "3.0.15",
"description": "Ethereum wallet library.", "description": "Ethereum wallet library.",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {

@ -236,7 +236,7 @@ describe('Test Invalid Input', function() {
var result = coder.decode([ 'bool' ], '0x'); var result = coder.decode([ 'bool' ], '0x');
console.log(result); console.log(result);
}, function(error) { }, function(error) {
assert.equal(error.message, 'invalid bool', 'got invalid bool'); assert.equal(error.reason, 'insufficient data for boolean type', 'got invalid bool');
return true; return true;
}, 'null bytes throws an error'); }, 'null bytes throws an error');
}); });

@ -2,8 +2,6 @@
// See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI // See: https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI
var throwError = require('../utils/throw-error');
var utils = (function() { var utils = (function() {
var convert = require('../utils/convert.js'); var convert = require('../utils/convert.js');
var utf8 = require('../utils/utf8.js'); var utf8 = require('../utils/utf8.js');
@ -27,6 +25,8 @@ var utils = (function() {
}; };
})(); })();
var errors = require('./errors');
var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/); var paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/); var paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/); var paramTypeArray = new RegExp(/^(.*)\[([0-9]*)\]$/);
@ -62,7 +62,16 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
value = utils.bigNumberify(value).toTwos(size * 8).maskn(size * 8); try {
value = utils.bigNumberify(value)
} catch (error) {
errors.throwError('invalid number value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
value = value.toTwos(size * 8).maskn(size * 8);
//value = value.toTwos(size * 8).maskn(size * 8); //value = value.toTwos(size * 8).maskn(size * 8);
if (signed) { if (signed) {
value = value.fromTwos(size * 8).toTwos(256); value = value.fromTwos(size * 8).toTwos(256);
@ -70,7 +79,13 @@ var coderNumber = function(coerceFunc, size, signed, localName) {
return utils.padZeros(utils.arrayify(value), 32); return utils.padZeros(utils.arrayify(value), 32);
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid ' + name); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var junkLength = 32 - size; var junkLength = 32 - size;
var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32)); var value = utils.bigNumberify(data.slice(offset + junkLength, offset + 32));
if (signed) { if (signed) {
@ -96,14 +111,18 @@ var coderBoolean = function(coerceFunc, localName) {
name: 'boolean', name: 'boolean',
type: 'boolean', type: 'boolean',
encode: function(value) { encode: function(value) {
return uint256Coder.encode(value ? 1: 0); return uint256Coder.encode(!!value ? 1: 0);
}, },
decode: function(data, offset) { decode: function(data, offset) {
try { try {
var result = uint256Coder.decode(data, offset); var result = uint256Coder.decode(data, offset);
} catch (error) { } catch (error) {
if (error.message === 'invalid uint256') { if (error.reason === 'insufficient data for uint256 type') {
throwError('invalid bool'); errors.throwError('insufficient data for boolean type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'boolean',
value: error.value
});
} }
throw error; throw error;
} }
@ -122,7 +141,15 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
name: name, name: name,
type: name, type: name,
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(value); value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid ' + name + ' value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
if (length === 32) { return value; } if (length === 32) { return value; }
var result = new Uint8Array(32); var result = new Uint8Array(32);
@ -130,7 +157,13 @@ var coderFixedBytes = function(coerceFunc, length, localName) {
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid bytes' + length); } if (data.length < offset + 32) {
errors.throwError('insufficient data for ' + name + ' type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: name,
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
@ -146,13 +179,27 @@ var coderAddress = function(coerceFunc, localName) {
name: 'address', name: 'address',
type: 'address', type: 'address',
encode: function(value) { encode: function(value) {
try {
value = utils.arrayify(utils.getAddress(value)); value = utils.arrayify(utils.getAddress(value));
} catch (error) {
errors.throwError('invalid address', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
var result = new Uint8Array(32); var result = new Uint8Array(32);
result.set(value, 12); result.set(value, 12);
return result; return result;
}, },
decode: function(data, offset) { decode: function(data, offset) {
if (data.length < offset + 32) { throwError('invalid address'); } if (data.length < offset + 32) {
errors.throwError('insufficuent data for address type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'address',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
return { return {
consumed: 32, consumed: 32,
value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32)))) value: coerceFunc('address', utils.getAddress(utils.hexlify(data.slice(offset + 12, offset + 32))))
@ -172,12 +219,33 @@ function _encodeDynamicBytes(value) {
]); ]);
} }
function _decodeDynamicBytes(data, offset) { function _decodeDynamicBytes(data, offset, localName) {
if (data.length < offset + 32) { throwError('invalid bytes'); } if (data.length < offset + 32) {
errors.throwError('insufficient data for dynamicBytes length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32))
});
}
var length = uint256Coder.decode(data, offset).value; var length = uint256Coder.decode(data, offset).value;
try {
length = length.toNumber(); length = length.toNumber();
if (data.length < offset + 32 + length) { throwError('invalid bytes'); } } catch (error) {
errors.throwError('dynamic bytes count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: length.toString()
});
}
if (data.length < offset + 32 + length) {
errors.throwError('insufficient data for dynamicBytes type', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'dynamicBytes',
value: utils.hexlify(data.slice(offset, offset + 32 + length))
});
}
return { return {
consumed: parseInt(32 + 32 * Math.ceil(length / 32)), consumed: parseInt(32 + 32 * Math.ceil(length / 32)),
@ -191,10 +259,19 @@ var coderDynamicBytes = function(coerceFunc, localName) {
name: 'bytes', name: 'bytes',
type: 'bytes', type: 'bytes',
encode: function(value) { encode: function(value) {
return _encodeDynamicBytes(utils.arrayify(value)); try {
value = utils.arrayify(value);
} catch (error) {
errors.throwError('invalid bytes value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: error.value
});
}
return _encodeDynamicBytes(value);
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('bytes', utils.hexlify(result.value)); result.value = coerceFunc('bytes', utils.hexlify(result.value));
return result; return result;
}, },
@ -208,10 +285,17 @@ var coderString = function(coerceFunc, localName) {
name: 'string', name: 'string',
type: 'string', type: 'string',
encode: function(value) { encode: function(value) {
if (typeof(value) !== 'string') {
errors.throwError('invalid string value', errors.INVALID_ARGUMENT, {
arg: localName,
type: typeof(value),
value: value
});
}
return _encodeDynamicBytes(utils.toUtf8Bytes(value)); return _encodeDynamicBytes(utils.toUtf8Bytes(value));
}, },
decode: function(data, offset) { decode: function(data, offset) {
var result = _decodeDynamicBytes(data, offset); var result = _decodeDynamicBytes(data, offset, localName);
result.value = coerceFunc('string', utils.toUtf8String(result.value)); result.value = coerceFunc('string', utils.toUtf8String(result.value));
return result; return result;
}, },
@ -224,10 +308,9 @@ function alignSize(size) {
} }
function pack(coders, values) { function pack(coders, values) {
if (Array.isArray(values)) { if (Array.isArray(values)) {
if (coders.length !== values.length) { // do nothing
throwError('types/values mismatch', { type: type, values: values });
}
} else if (values && typeof(values) === 'object') { } else if (values && typeof(values) === 'object') {
var arrayValues = []; var arrayValues = [];
@ -237,7 +320,18 @@ function pack(coders, values) {
values = arrayValues; values = arrayValues;
} else { } else {
throwError('invalid value', { type: 'tuple', values: values }); errors.throwError('invalid tuple value', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
type: typeof(values),
value: values
});
}
if (coders.length !== values.length) {
errors.throwError('types/value length mismatch', errors.INVALID_ARGUMENT, {
coderType: 'tuple',
value: values
});
} }
var parts = []; var parts = [];
@ -332,7 +426,14 @@ function coderArray(coerceFunc, coder, length, localName) {
name: 'array', name: 'array',
type: type, type: type,
encode: function(value) { encode: function(value) {
if (!Array.isArray(value)) { throwError('invalid array'); } if (!Array.isArray(value)) {
errors.throwError('expected array value', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
type: typeof(value),
value: value
});
}
var count = length; var count = length;
@ -342,7 +443,15 @@ function coderArray(coerceFunc, coder, length, localName) {
result = uint256Coder.encode(count); result = uint256Coder.encode(count);
} }
if (count !== value.length) { throwError('size mismatch'); } if (count !== value.length) {
error.throwError('array value length mismatch', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
count: value.length,
expectedCount: count,
value: value
});
}
var coders = []; var coders = [];
value.forEach(function(value) { coders.push(coder); }); value.forEach(function(value) { coders.push(coder); });
@ -358,8 +467,24 @@ function coderArray(coerceFunc, coder, length, localName) {
var count = length; var count = length;
if (count === -1) { if (count === -1) {
try {
var decodedLength = uint256Coder.decode(data, offset); var decodedLength = uint256Coder.decode(data, offset);
} catch (error) {
errors.throwError('insufficient data for dynamic array length', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: error.value
});
}
try {
count = decodedLength.value.toNumber(); count = decodedLength.value.toNumber();
} catch (error) {
errors.throwError('array count too large', errors.INVALID_ARGUMENT, {
arg: localName,
coderType: 'array',
value: decodedLength.value.toString()
});
}
consumed += decodedLength.consumed; consumed += decodedLength.consumed;
offset += decodedLength.consumed; offset += decodedLength.consumed;
} }
@ -450,7 +575,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[2] || 256); var size = parseInt(match[2] || 256);
if (size === 0 || size > 256 || (size % 8) !== 0) { if (size === 0 || size > 256 || (size % 8) !== 0) {
throwError('invalid type', { type: type }); errors.throwError('invalid ' + match[1] + ' bit length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName); return coderNumber(coerceFunc, size / 8, (match[1] === 'int'), localName);
} }
@ -459,7 +587,10 @@ function getParamCoder(coerceFunc, type, localName) {
if (match) { if (match) {
var size = parseInt(match[1]); var size = parseInt(match[1]);
if (size === 0 || size > 32) { if (size === 0 || size > 32) {
throwError('invalid type ' + type); errors.throwError('invalid bytes length', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
return coderFixedBytes(coerceFunc, size, localName); return coderFixedBytes(coerceFunc, size, localName);
} }
@ -487,7 +618,10 @@ function getParamCoder(coerceFunc, type, localName) {
return coderNull(coerceFunc); return coderNull(coerceFunc);
} }
throwError('invalid type', { type: type }); errors.throwError('invalid type', errors.INVALID_ARGUMENT, {
arg: 'type',
value: type
});
} }
function Coder(coerceFunc) { function Coder(coerceFunc) {
@ -505,7 +639,19 @@ utils.defineProperty(Coder.prototype, 'encode', function(names, types, values) {
names = null; names = null;
} }
if (types.length !== values.length) { throwError('types/values mismatch', {types: types, values: values}); } if (types.length !== values.length) {
errors.throwError('types/values length mismatch', errors.INVALID_ARGUMENT, {
count: { types: types.length, values: values.length },
value: { types: types, values: values }
});
}
if (names && names.length != types.length) {
errors.throwError('names/types length mismatch', errors.INVALID_ARGUMENT, {
count: { names: names.length, types: types.length },
value: { names: names, types: types }
});
}
var coders = []; var coders = [];
types.forEach(function(type, index) { types.forEach(function(type, index) {

@ -16,15 +16,31 @@ var codes = { };
'MISSING_NEW', 'MISSING_NEW',
// Call exception
'CALL_EXCEPTION',
// Response from a server was invalid
// - response: The body of the response
//'BAD_RESPONSE',
// Invalid argument (e.g. type) to a function: // Invalid argument (e.g. type) to a function:
// - arg: The argument name that was invalid // - arg: The argument name that was invalid
// - value: The value of the argument
// - type: The type of the argument
// - expected: What was expected
'INVALID_ARGUMENT', 'INVALID_ARGUMENT',
// Missing argument to a function: // Missing argument to a function:
// - arg: The argument name that is required // - arg: The argument name that is required
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'MISSING_ARGUMENT', 'MISSING_ARGUMENT',
// Too many arguments // Too many arguments
// - count: The number of arguments received
// - expectedCount: The number of arguments expected
'UNEXPECTED_ARGUMENT', 'UNEXPECTED_ARGUMENT',
@ -44,7 +60,11 @@ defineProperty(codes, 'throwError', function(message, code, params) {
var messageDetails = []; var messageDetails = [];
Object.keys(params).forEach(function(key) { Object.keys(params).forEach(function(key) {
try {
messageDetails.push(key + '=' + JSON.stringify(params[key])); messageDetails.push(key + '=' + JSON.stringify(params[key]));
} catch (error) {
messageDetails.push(key + '=' + JSON.stringify(params[key].toString()));
}
}); });
var reason = message; var reason = message;
if (messageDetails.length) { if (messageDetails.length) {