From bf6ea2919dc424586eff00974b813bbbedcede9b Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Tue, 11 Aug 2015 17:17:20 +0200 Subject: web3: updated --- jsre/ethereum_js.go | 4409 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 2789 insertions(+), 1620 deletions(-) diff --git a/jsre/ethereum_js.go b/jsre/ethereum_js.go index 012e5af70..f33bb7c25 100644 --- a/jsre/ethereum_js.go +++ b/jsre/ethereum_js.go @@ -18,6 +18,622 @@ package jsre const Web3_JS = ` require=(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 64 || this.offset !== undefined; + return this.offset !== undefined; }; /** @@ -708,118 +1360,446 @@ SolidityParam.encodeList = function (params) { }, '')); }; -/** - * This method should be used to decode plain (static) solidity param at given index - * - * @method decodeParam - * @param {String} bytes - * @param {Number} index - * @returns {SolidityParam} - */ -SolidityParam.decodeParam = function (bytes, index) { - index = index || 0; - return new SolidityParam(bytes.substr(index * 64, 64)); -}; -/** - * This method should be called to get offset value from bytes at given index - * - * @method getOffset - * @param {String} bytes - * @param {Number} index - * @returns {Number} offset as number - */ -var getOffset = function (bytes, index) { - // we can do this cause offset is rather small - return parseInt('0x' + bytes.substr(index * 64, 64)); -}; + +module.exports = SolidityParam; + + +},{"../utils/utils":20}],12:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityType = require('./type'); /** - * This method should be called to decode solidity bytes param at given index - * - * @method decodeBytes - * @param {String} bytes - * @param {Number} index - * @returns {SolidityParam} + * SolidityTypeReal is a prootype that represents real type + * It matches: + * real + * real[] + * real[4] + * real[][] + * real[3][] + * real[][6][], ... + * real32 + * real64[] + * real8[4] + * real256[][] + * real[3][] + * real64[][6][], ... */ -SolidityParam.decodeBytes = function (bytes, index) { - index = index || 0; - - var offset = getOffset(bytes, index); +var SolidityTypeReal = function () { + this._inputFormatter = f.formatInputReal; + this._outputFormatter = f.formatOutputReal; +}; - var l = parseInt('0x' + bytes.substr(offset * 2, 64)); - l = Math.floor((l + 31) / 32); +SolidityTypeReal.prototype = new SolidityType({}); +SolidityTypeReal.prototype.constructor = SolidityTypeReal; - // (1 + l) * , cause we also parse length - return new SolidityParam(bytes.substr(offset * 2, (1 + l) * 64), 0); +SolidityTypeReal.prototype.isType = function (name) { + return !!name.match(/real([0-9]*)?(\[([0-9]*)\])?/); }; -/** - * This method should be used to decode solidity array at given index - * - * @method decodeArray - * @param {String} bytes - * @param {Number} index - * @returns {SolidityParam} - */ -SolidityParam.decodeArray = function (bytes, index) { - index = index || 0; - var offset = getOffset(bytes, index); - var length = parseInt('0x' + bytes.substr(offset * 2, 64)); - return new SolidityParam(bytes.substr(offset * 2, (length + 1) * 64), 0); +SolidityTypeReal.prototype.staticPartLength = function (name) { + return 32 * this.staticArrayLength(name); }; -module.exports = SolidityParam; +module.exports = SolidityTypeReal; +},{"./formatters":9,"./type":14}],13:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityType = require('./type'); -},{"../utils/utils":7}],4:[function(require,module,exports){ -'use strict'; +var SolidityTypeString = function () { + this._inputFormatter = f.formatInputString; + this._outputFormatter = f.formatOutputString; +}; -// go env doesn't have and need XMLHttpRequest -if (typeof XMLHttpRequest === 'undefined') { - exports.XMLHttpRequest = {}; -} else { - exports.XMLHttpRequest = XMLHttpRequest; // jshint ignore:line -} +SolidityTypeString.prototype = new SolidityType({}); +SolidityTypeString.prototype.constructor = SolidityTypeString; +SolidityTypeString.prototype.isType = function (name) { + return !!name.match(/^string(\[([0-9]*)\])*$/); +}; -},{}],5:[function(require,module,exports){ -/* - This file is part of ethereum.js. +SolidityTypeString.prototype.staticPartLength = function (name) { + return 32 * this.staticArrayLength(name); +}; - ethereum.js is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. +SolidityTypeString.prototype.isDynamicType = function () { + return true; +}; - ethereum.js is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. +module.exports = SolidityTypeString; - You should have received a copy of the GNU Lesser General Public License - along with ethereum.js. If not, see . -*/ -/** @file config.js - * @authors: - * Marek Kotewicz - * @date 2015 + +},{"./formatters":9,"./type":14}],14:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityParam = require('./param'); + +/** + * SolidityType prototype is used to encode/decode solidity params of certain type */ +var SolidityType = function (config) { + this._inputFormatter = config.inputFormatter; + this._outputFormatter = config.outputFormatter; +}; /** - * Utils - * - * @module utils + * Should be used to determine if this SolidityType do match given name + * + * @method isType + * @param {String} name + * @return {Bool} true if type match this SolidityType, otherwise false */ +SolidityType.prototype.isType = function (name) { + throw "this method should be overrwritten for type " + name; +}; /** - * Utility functions - * - * @class [utils] config - * @constructor + * Should be used to determine what is the length of static part in given type + * + * @method staticPartLength + * @param {String} name + * @return {Number} length of static part in bytes */ +SolidityType.prototype.staticPartLength = function (name) { + throw "this method should be overrwritten for type: " + name; +}; -/// required to define ETH_BIGNUMBER_ROUNDING_MODE +/** + * Should be used to determine if type is dynamic array + * eg: + * "type[]" => true + * "type[4]" => false + * + * @method isDynamicArray + * @param {String} name + * @return {Bool} true if the type is dynamic array + */ +SolidityType.prototype.isDynamicArray = function (name) { + var nestedTypes = this.nestedTypes(name); + return !!nestedTypes && !nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g); +}; + +/** + * Should be used to determine if type is static array + * eg: + * "type[]" => false + * "type[4]" => true + * + * @method isStaticArray + * @param {String} name + * @return {Bool} true if the type is static array + */ +SolidityType.prototype.isStaticArray = function (name) { + var nestedTypes = this.nestedTypes(name); + return !!nestedTypes && !!nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g); +}; + +/** + * Should return length of static array + * eg. + * "int[32]" => 32 + * "int256[14]" => 14 + * "int[2][3]" => 3 + * "int" => 1 + * "int[1]" => 1 + * "int[]" => 1 + * + * @method staticArrayLength + * @param {String} name + * @return {Number} static array length + */ +SolidityType.prototype.staticArrayLength = function (name) { + var nestedTypes = this.nestedTypes(name); + if (nestedTypes) { + return parseInt(nestedTypes[nestedTypes.length - 1].match(/[0-9]{1,}/g) || 1); + } + return 1; +}; + +/** + * Should return nested type + * eg. + * "int[32]" => "int" + * "int256[14]" => "int256" + * "int[2][3]" => "int[2]" + * "int" => "int" + * "int[]" => "int" + * + * @method nestedName + * @param {String} name + * @return {String} nested name + */ +SolidityType.prototype.nestedName = function (name) { + // remove last [] in name + var nestedTypes = this.nestedTypes(name); + if (!nestedTypes) { + return name; + } + + return name.substr(0, name.length - nestedTypes[nestedTypes.length - 1].length); +}; + +/** + * Should return true if type has dynamic size by default + * such types are "string", "bytes" + * + * @method isDynamicType + * @param {String} name + * @return {Bool} true if is dynamic, otherwise false + */ +SolidityType.prototype.isDynamicType = function () { + return false; +}; + +/** + * Should return array of nested types + * eg. + * "int[2][3][]" => ["[2]", "[3]", "[]"] + * "int[] => ["[]"] + * "int" => null + * + * @method nestedTypes + * @param {String} name + * @return {Array} array of nested types + */ +SolidityType.prototype.nestedTypes = function (name) { + // return list of strings eg. "[]", "[3]", "[]", "[2]" + return name.match(/(\[[0-9]*\])/g); +}; + +/** + * Should be used to encode the value + * + * @method encode + * @param {Object} value + * @param {String} name + * @return {String} encoded value + */ +SolidityType.prototype.encode = function (value, name) { + var self = this; + if (this.isDynamicArray(name)) { + + return (function () { + var length = value.length; // in int + var nestedName = self.nestedName(name); + + var result = []; + result.push(f.formatInputInt(length).encode()); + + value.forEach(function (v) { + result.push(self.encode(v, nestedName)); + }); + + return result; + })(); + + } else if (this.isStaticArray(name)) { + + return (function () { + var length = self.staticArrayLength(name); // in int + var nestedName = self.nestedName(name); + + var result = []; + for (var i = 0; i < length; i++) { + result.push(self.encode(value[i], nestedName)); + } + + return result; + })(); + + } + + return this._inputFormatter(value, name).encode(); +}; + +/** + * Should be used to decode value from bytes + * + * @method decode + * @param {String} bytes + * @param {Number} offset in bytes + * @param {String} name type name + * @returns {Object} decoded value + */ +SolidityType.prototype.decode = function (bytes, offset, name) { + var self = this; + + if (this.isDynamicArray(name)) { + + return (function () { + var arrayOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes + var length = parseInt('0x' + bytes.substr(arrayOffset * 2, 64)); // in int + var arrayStart = arrayOffset + 32; // array starts after length; // in bytes + + var nestedName = self.nestedName(name); + var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes + var result = []; + + for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) { + result.push(self.decode(bytes, arrayStart + i, nestedName)); + } + + return result; + })(); + + } else if (this.isStaticArray(name)) { + + return (function () { + var length = self.staticArrayLength(name); // in int + var arrayStart = offset; // in bytes + + var nestedName = self.nestedName(name); + var nestedStaticPartLength = self.staticPartLength(nestedName); // in bytes + var result = []; + + for (var i = 0; i < length * nestedStaticPartLength; i += nestedStaticPartLength) { + result.push(self.decode(bytes, arrayStart + i, nestedName)); + } + + return result; + })(); + } else if (this.isDynamicType(name)) { + + return (function () { + var dynamicOffset = parseInt('0x' + bytes.substr(offset * 2, 64)); // in bytes + var length = parseInt('0x' + bytes.substr(dynamicOffset * 2, 64)); // in bytes + var roundedLength = Math.floor((length + 31) / 32); // in int + + return self._outputFormatter(new SolidityParam(bytes.substr(dynamicOffset * 2, ( 1 + roundedLength) * 64), 0)); + })(); + } + + var length = this.staticPartLength(name); + return this._outputFormatter(new SolidityParam(bytes.substr(offset * 2, length * 2))); +}; + +module.exports = SolidityType; + +},{"./formatters":9,"./param":11}],15:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityType = require('./type'); + +/** + * SolidityTypeUInt is a prootype that represents uint type + * It matches: + * uint + * uint[] + * uint[4] + * uint[][] + * uint[3][] + * uint[][6][], ... + * uint32 + * uint64[] + * uint8[4] + * uint256[][] + * uint[3][] + * uint64[][6][], ... + */ +var SolidityTypeUInt = function () { + this._inputFormatter = f.formatInputInt; + this._outputFormatter = f.formatOutputInt; +}; + +SolidityTypeUInt.prototype = new SolidityType({}); +SolidityTypeUInt.prototype.constructor = SolidityTypeUInt; + +SolidityTypeUInt.prototype.isType = function (name) { + return !!name.match(/^uint([0-9]*)?(\[([0-9]*)\])*$/); +}; + +SolidityTypeUInt.prototype.staticPartLength = function (name) { + return 32 * this.staticArrayLength(name); +}; + +module.exports = SolidityTypeUInt; + +},{"./formatters":9,"./type":14}],16:[function(require,module,exports){ +var f = require('./formatters'); +var SolidityType = require('./type'); + +/** + * SolidityTypeUReal is a prootype that represents ureal type + * It matches: + * ureal + * ureal[] + * ureal[4] + * ureal[][] + * ureal[3][] + * ureal[][6][], ... + * ureal32 + * ureal64[] + * ureal8[4] + * ureal256[][] + * ureal[3][] + * ureal64[][6][], ... + */ +var SolidityTypeUReal = function () { + this._inputFormatter = f.formatInputReal; + this._outputFormatter = f.formatOutputUReal; +}; + +SolidityTypeUReal.prototype = new SolidityType({}); +SolidityTypeUReal.prototype.constructor = SolidityTypeUReal; + +SolidityTypeUReal.prototype.isType = function (name) { + return !!name.match(/^ureal([0-9]*)?(\[([0-9]*)\])*$/); +}; + +SolidityTypeUReal.prototype.staticPartLength = function (name) { + return 32 * this.staticArrayLength(name); +}; + +module.exports = SolidityTypeUReal; + +},{"./formatters":9,"./type":14}],17:[function(require,module,exports){ +'use strict'; + +// go env doesn't have and need XMLHttpRequest +if (typeof XMLHttpRequest === 'undefined') { + exports.XMLHttpRequest = {}; +} else { + exports.XMLHttpRequest = XMLHttpRequest; // jshint ignore:line +} + + +},{}],18:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ethereum.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ethereum.js. If not, see . +*/ +/** @file config.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +/** + * Utils + * + * @module utils + */ + +/** + * Utility functions + * + * @class [utils] config + * @constructor + */ + + +/// required to define ETH_BIGNUMBER_ROUNDING_MODE var BigNumber = require('bignumber.js'); var ETH_UNITS = [ @@ -863,7 +1843,7 @@ module.exports = { }; -},{"bignumber.js":"bignumber.js"}],6:[function(require,module,exports){ +},{"bignumber.js":"bignumber.js"}],19:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -886,6 +1866,7 @@ module.exports = { * @date 2015 */ + var utils = require('./utils'); var sha3 = require('crypto-js/sha3'); @@ -904,7 +1885,7 @@ module.exports = function (str, isNew) { }; -},{"./utils":7,"crypto-js/sha3":34}],7:[function(require,module,exports){ +},{"./utils":20,"crypto-js/sha3":47}],20:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -940,6 +1921,7 @@ module.exports = function (str, isNew) { * @constructor */ + var BigNumber = require('bignumber.js'); var unitMap = { @@ -1015,7 +1997,7 @@ var toAscii = function(hex) { str += String.fromCharCode(code); } - return decodeURIComponent(escape(str)); + return decodeURIComponent(escape(str)); // jshint ignore:line }; /** @@ -1026,7 +2008,7 @@ var toAscii = function(hex) { * @returns {String} hex representation of input string */ var toHexNative = function(str) { - str = unescape(encodeURIComponent(str)); + str = unescape(encodeURIComponent(str)); // jshint ignore:line var hex = ""; for(var i = 0; i < str.length; i++) { var n = str.charCodeAt(i).toString(16); @@ -1377,18 +2359,6 @@ var isJson = function (str) { } }; -/** - * This method should be called to check if string is valid ethereum IBAN number - * Supports direct and indirect IBANs - * - * @method isIBAN - * @param {String} - * @return {Boolean} - */ -var isIBAN = function (iban) { - return /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30})$/.test(iban); -}; - module.exports = { padLeft: padLeft, padRight: padRight, @@ -1413,17 +2383,16 @@ module.exports = { isObject: isObject, isBoolean: isBoolean, isArray: isArray, - isJson: isJson, - isIBAN: isIBAN + isJson: isJson }; -},{"bignumber.js":"bignumber.js"}],8:[function(require,module,exports){ +},{"bignumber.js":"bignumber.js"}],21:[function(require,module,exports){ module.exports={ - "version": "0.9.1" + "version": "0.12.1" } -},{}],9:[function(require,module,exports){ +},{}],22:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -1451,11 +2420,11 @@ module.exports={ */ var version = require('./version.json'); -var net = require('./web3/net'); -var eth = require('./web3/eth'); -var db = require('./web3/db'); -var shh = require('./web3/shh'); -var watches = require('./web3/watches'); +var net = require('./web3/methods/net'); +var eth = require('./web3/methods/eth'); +var db = require('./web3/methods/db'); +var shh = require('./web3/methods/shh'); +var watches = require('./web3/methods/watches'); var Filter = require('./web3/filter'); var utils = require('./utils/utils'); var formatters = require('./web3/formatters'); @@ -1600,7 +2569,7 @@ setupMethods(web3.shh, shh.methods); module.exports = web3; -},{"./utils/config":5,"./utils/sha3":6,"./utils/utils":7,"./version.json":8,"./web3/batch":11,"./web3/db":13,"./web3/eth":15,"./web3/filter":17,"./web3/formatters":18,"./web3/method":24,"./web3/net":26,"./web3/property":27,"./web3/requestmanager":28,"./web3/shh":29,"./web3/watches":31}],10:[function(require,module,exports){ +},{"./utils/config":18,"./utils/sha3":19,"./utils/utils":20,"./version.json":21,"./web3/batch":24,"./web3/filter":28,"./web3/formatters":29,"./web3/method":35,"./web3/methods/db":36,"./web3/methods/eth":37,"./web3/methods/net":38,"./web3/methods/shh":39,"./web3/methods/watches":40,"./web3/property":42,"./web3/requestmanager":43}],23:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -1628,7 +2597,7 @@ var SolidityEvent = require('./event'); var formatters = require('./formatters'); var utils = require('../utils/utils'); var Filter = require('./filter'); -var watches = require('./watches'); +var watches = require('./methods/watches'); var AllSolidityEvents = function (json, address) { this._json = json; @@ -1669,6 +2638,13 @@ AllSolidityEvents.prototype.decode = function (data) { }; AllSolidityEvents.prototype.execute = function (options, callback) { + + if (utils.isFunction(arguments[arguments.length - 1])) { + callback = arguments[arguments.length - 1]; + if(arguments.length === 1) + options = null; + } + var o = this.encode(options); var formatter = this.decode.bind(this); return new Filter(o, watches.eth(), formatter, callback); @@ -1682,7 +2658,7 @@ AllSolidityEvents.prototype.attachToContract = function (contract) { module.exports = AllSolidityEvents; -},{"../utils/sha3":6,"../utils/utils":7,"./event":16,"./filter":17,"./formatters":18,"./watches":31}],11:[function(require,module,exports){ +},{"../utils/sha3":19,"../utils/utils":20,"./event":27,"./filter":28,"./formatters":29,"./methods/watches":40}],24:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -1750,7 +2726,7 @@ Batch.prototype.execute = function () { module.exports = Batch; -},{"./errors":14,"./jsonrpc":23,"./requestmanager":28}],12:[function(require,module,exports){ +},{"./errors":26,"./jsonrpc":34,"./requestmanager":43}],25:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2029,65 +3005,7 @@ var Contract = function (abi, address) { module.exports = contract; -},{"../solidity/coder":1,"../utils/utils":7,"../web3":9,"./allevents":10,"./event":16,"./function":19}],13:[function(require,module,exports){ -/* - This file is part of ethereum.js. - - ethereum.js is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - ethereum.js is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with ethereum.js. If not, see . -*/ -/** @file db.js - * @authors: - * Marek Kotewicz - * @date 2015 - */ - -var Method = require('./method'); - -var putString = new Method({ - name: 'putString', - call: 'db_putString', - params: 3 -}); - - -var getString = new Method({ - name: 'getString', - call: 'db_getString', - params: 2 -}); - -var putHex = new Method({ - name: 'putHex', - call: 'db_putHex', - params: 3 -}); - -var getHex = new Method({ - name: 'getHex', - call: 'db_getHex', - params: 2 -}); - -var methods = [ - putString, getString, putHex, getHex -]; - -module.exports = { - methods: methods -}; - -},{"./method":24}],14:[function(require,module,exports){ +},{"../solidity/coder":7,"../utils/utils":20,"../web3":22,"./allevents":23,"./event":27,"./function":30}],26:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2127,7 +3045,7 @@ module.exports = { }; -},{}],15:[function(require,module,exports){ +},{}],27:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2144,283 +3062,199 @@ module.exports = { You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** - * @file eth.js +/** + * @file event.js * @author Marek Kotewicz - * @author Fabian Vogelsteller - * @date 2015 + * @date 2014 */ +var utils = require('../utils/utils'); +var coder = require('../solidity/coder'); +var formatters = require('./formatters'); +var sha3 = require('../utils/sha3'); +var Filter = require('./filter'); +var watches = require('./methods/watches'); + /** - * Web3 - * - * @module web3 + * This prototype should be used to create event filters */ +var SolidityEvent = function (json, address) { + this._params = json.inputs; + this._name = utils.transformToFullName(json); + this._address = address; + this._anonymous = json.anonymous; +}; /** - * Eth methods and properties - * - * An example method object can look as follows: + * Should be used to get filtered param types * - * { - * name: 'getBlock', - * call: blockCall, - * params: 2, - * outputFormatter: formatters.outputBlockFormatter, - * inputFormatter: [ // can be a formatter funciton or an array of functions. Where each item in the array will be used for one parameter - * utils.toHex, // formats paramter 1 - * function(param){ return !!param; } // formats paramter 2 - * ] - * }, - * - * @class [web3] eth - * @constructor + * @method types + * @param {Bool} decide if returned typed should be indexed + * @return {Array} array of types */ - -"use strict"; - -var formatters = require('./formatters'); -var utils = require('../utils/utils'); -var Method = require('./method'); -var Property = require('./property'); - -var blockCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber"; +SolidityEvent.prototype.types = function (indexed) { + return this._params.filter(function (i) { + return i.indexed === indexed; + }).map(function (i) { + return i.type; + }); }; -var transactionFromBlockCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex'; +/** + * Should be used to get event display name + * + * @method displayName + * @return {String} event display name + */ +SolidityEvent.prototype.displayName = function () { + return utils.extractDisplayName(this._name); }; -var uncleCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex'; +/** + * Should be used to get event type name + * + * @method typeName + * @return {String} event type name + */ +SolidityEvent.prototype.typeName = function () { + return utils.extractTypeName(this._name); }; -var getBlockTransactionCountCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber'; +/** + * Should be used to get event signature + * + * @method signature + * @return {String} event signature + */ +SolidityEvent.prototype.signature = function () { + return sha3(this._name); }; -var uncleCountCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber'; -}; +/** + * Should be used to encode indexed params and options to one final object + * + * @method encode + * @param {Object} indexed + * @param {Object} options + * @return {Object} everything combined together and encoded + */ +SolidityEvent.prototype.encode = function (indexed, options) { + indexed = indexed || {}; + options = options || {}; + var result = {}; -/// @returns an array of objects describing web3.eth api methods + ['fromBlock', 'toBlock'].filter(function (f) { + return options[f] !== undefined; + }).forEach(function (f) { + result[f] = formatters.inputBlockNumberFormatter(options[f]); + }); -var getBalance = new Method({ - name: 'getBalance', - call: 'eth_getBalance', - params: 2, - inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter], - outputFormatter: formatters.outputBigNumberFormatter -}); + result.topics = []; -var getStorageAt = new Method({ - name: 'getStorageAt', - call: 'eth_getStorageAt', - params: 3, - inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter] -}); + result.address = this._address; + if (!this._anonymous) { + result.topics.push('0x' + this.signature()); + } -var getCode = new Method({ - name: 'getCode', - call: 'eth_getCode', - params: 2, - inputFormatter: [utils.toAddress, formatters.inputDefaultBlockNumberFormatter] -}); + var indexedTopics = this._params.filter(function (i) { + return i.indexed === true; + }).map(function (i) { + var value = indexed[i.name]; + if (value === undefined || value === null) { + return null; + } + + if (utils.isArray(value)) { + return value.map(function (v) { + return '0x' + coder.encodeParam(i.type, v); + }); + } + return '0x' + coder.encodeParam(i.type, value); + }); -var getBlock = new Method({ - name: 'getBlock', - call: blockCall, - params: 2, - inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }], - outputFormatter: formatters.outputBlockFormatter -}); + result.topics = result.topics.concat(indexedTopics); -var getUncle = new Method({ - name: 'getUncle', - call: uncleCall, - params: 2, - inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], - outputFormatter: formatters.outputBlockFormatter, + return result; +}; -}); +/** + * Should be used to decode indexed params and options + * + * @method decode + * @param {Object} data + * @return {Object} result object with decoded indexed && not indexed params + */ +SolidityEvent.prototype.decode = function (data) { + + data.data = data.data || ''; + data.topics = data.topics || []; -var getCompilers = new Method({ - name: 'getCompilers', - call: 'eth_getCompilers', - params: 0 -}); + var argTopics = this._anonymous ? data.topics : data.topics.slice(1); + var indexedData = argTopics.map(function (topics) { return topics.slice(2); }).join(""); + var indexedParams = coder.decodeParams(this.types(true), indexedData); -var getBlockTransactionCount = new Method({ - name: 'getBlockTransactionCount', - call: getBlockTransactionCountCall, - params: 1, - inputFormatter: [formatters.inputBlockNumberFormatter], - outputFormatter: utils.toDecimal -}); + var notIndexedData = data.data.slice(2); + var notIndexedParams = coder.decodeParams(this.types(false), notIndexedData); + + var result = formatters.outputLogFormatter(data); + result.event = this.displayName(); + result.address = data.address; -var getBlockUncleCount = new Method({ - name: 'getBlockUncleCount', - call: uncleCountCall, - params: 1, - inputFormatter: [formatters.inputBlockNumberFormatter], - outputFormatter: utils.toDecimal -}); + result.args = this._params.reduce(function (acc, current) { + acc[current.name] = current.indexed ? indexedParams.shift() : notIndexedParams.shift(); + return acc; + }, {}); -var getTransaction = new Method({ - name: 'getTransaction', - call: 'eth_getTransactionByHash', - params: 1, - outputFormatter: formatters.outputTransactionFormatter -}); + delete result.data; + delete result.topics; -var getTransactionFromBlock = new Method({ - name: 'getTransactionFromBlock', - call: transactionFromBlockCall, - params: 2, - inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], - outputFormatter: formatters.outputTransactionFormatter -}); + return result; +}; -var getTransactionReceipt = new Method({ - name: 'getTransactionReceipt', - call: 'eth_getTransactionReceipt', - params: 1, - outputFormatter: formatters.outputTransactionReceiptFormatter -}); +/** + * Should be used to create new filter object from event + * + * @method execute + * @param {Object} indexed + * @param {Object} options + * @return {Object} filter object + */ +SolidityEvent.prototype.execute = function (indexed, options, callback) { -var getTransactionCount = new Method({ - name: 'getTransactionCount', - call: 'eth_getTransactionCount', - params: 2, - inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter], - outputFormatter: utils.toDecimal -}); + if (utils.isFunction(arguments[arguments.length - 1])) { + callback = arguments[arguments.length - 1]; + if(arguments.length === 2) + options = null; + if(arguments.length === 1) { + options = null; + indexed = {}; + } + } + + var o = this.encode(indexed, options); + var formatter = this.decode.bind(this); + return new Filter(o, watches.eth(), formatter, callback); +}; -var sendRawTransaction = new Method({ - name: 'sendRawTransaction', - call: 'eth_sendRawTransaction', - params: 1, - inputFormatter: [null] -}); +/** + * Should be used to attach event to contract object + * + * @method attachToContract + * @param {Contract} + */ +SolidityEvent.prototype.attachToContract = function (contract) { + var execute = this.execute.bind(this); + var displayName = this.displayName(); + if (!contract[displayName]) { + contract[displayName] = execute; + } + contract[displayName][this.typeName()] = this.execute.bind(this, contract); +}; -var sendTransaction = new Method({ - name: 'sendTransaction', - call: 'eth_sendTransaction', - params: 1, - inputFormatter: [formatters.inputTransactionFormatter] -}); +module.exports = SolidityEvent; -var call = new Method({ - name: 'call', - call: 'eth_call', - params: 2, - inputFormatter: [formatters.inputTransactionFormatter, formatters.inputDefaultBlockNumberFormatter] -}); - -var estimateGas = new Method({ - name: 'estimateGas', - call: 'eth_estimateGas', - params: 1, - inputFormatter: [formatters.inputTransactionFormatter], - outputFormatter: utils.toDecimal -}); - -var compileSolidity = new Method({ - name: 'compile.solidity', - call: 'eth_compileSolidity', - params: 1 -}); - -var compileLLL = new Method({ - name: 'compile.lll', - call: 'eth_compileLLL', - params: 1 -}); - -var compileSerpent = new Method({ - name: 'compile.serpent', - call: 'eth_compileSerpent', - params: 1 -}); - -var submitWork = new Method({ - name: 'submitWork', - call: 'eth_submitWork', - params: 3 -}); - -var getWork = new Method({ - name: 'getWork', - call: 'eth_getWork', - params: 0 -}); - -var methods = [ - getBalance, - getStorageAt, - getCode, - getBlock, - getUncle, - getCompilers, - getBlockTransactionCount, - getBlockUncleCount, - getTransaction, - getTransactionFromBlock, - getTransactionReceipt, - getTransactionCount, - call, - estimateGas, - sendRawTransaction, - sendTransaction, - compileSolidity, - compileLLL, - compileSerpent, - submitWork, - getWork -]; - -/// @returns an array of objects describing web3.eth api properties - - - -var properties = [ - new Property({ - name: 'coinbase', - getter: 'eth_coinbase' - }), - new Property({ - name: 'mining', - getter: 'eth_mining' - }), - new Property({ - name: 'hashrate', - getter: 'eth_hashrate', - outputFormatter: utils.toDecimal - }), - new Property({ - name: 'gasPrice', - getter: 'eth_gasPrice', - outputFormatter: formatters.outputBigNumberFormatter - }), - new Property({ - name: 'accounts', - getter: 'eth_accounts' - }), - new Property({ - name: 'blockNumber', - getter: 'eth_blockNumber', - outputFormatter: utils.toDecimal - }) -]; - -module.exports = { - methods: methods, - properties: properties -}; - -},{"../utils/utils":7,"./formatters":18,"./method":24,"./property":27}],16:[function(require,module,exports){ +},{"../solidity/coder":7,"../utils/sha3":19,"../utils/utils":20,"./filter":28,"./formatters":29,"./methods/watches":40}],28:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2437,277 +3271,68 @@ module.exports = { You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** - * @file event.js - * @author Marek Kotewicz +/** @file filter.js + * @authors: + * Jeffrey Wilcke + * Marek Kotewicz + * Marian Oancea + * Fabian Vogelsteller + * Gav Wood * @date 2014 */ -var utils = require('../utils/utils'); -var coder = require('../solidity/coder'); +var RequestManager = require('./requestmanager'); var formatters = require('./formatters'); -var sha3 = require('../utils/sha3'); -var Filter = require('./filter'); -var watches = require('./watches'); - -/** - * This prototype should be used to create event filters - */ -var SolidityEvent = function (json, address) { - this._params = json.inputs; - this._name = utils.transformToFullName(json); - this._address = address; - this._anonymous = json.anonymous; -}; +var utils = require('../utils/utils'); /** - * Should be used to get filtered param types - * - * @method types - * @param {Bool} decide if returned typed should be indexed - * @return {Array} array of types - */ -SolidityEvent.prototype.types = function (indexed) { - return this._params.filter(function (i) { - return i.indexed === indexed; - }).map(function (i) { - return i.type; - }); -}; +* Converts a given topic to a hex string, but also allows null values. +* +* @param {Mixed} value +* @return {String} +*/ +var toTopic = function(value){ -/** - * Should be used to get event display name - * - * @method displayName - * @return {String} event display name - */ -SolidityEvent.prototype.displayName = function () { - return utils.extractDisplayName(this._name); -}; + if(value === null || typeof value === 'undefined') + return null; -/** - * Should be used to get event type name - * - * @method typeName - * @return {String} event type name - */ -SolidityEvent.prototype.typeName = function () { - return utils.extractTypeName(this._name); -}; + value = String(value); -/** - * Should be used to get event signature - * - * @method signature - * @return {String} event signature - */ -SolidityEvent.prototype.signature = function () { - return sha3(this._name); + if(value.indexOf('0x') === 0) + return value; + else + return utils.fromAscii(value); }; -/** - * Should be used to encode indexed params and options to one final object - * - * @method encode - * @param {Object} indexed - * @param {Object} options - * @return {Object} everything combined together and encoded - */ -SolidityEvent.prototype.encode = function (indexed, options) { - indexed = indexed || {}; - options = options || {}; - var result = {}; - - ['fromBlock', 'toBlock'].filter(function (f) { - return options[f] !== undefined; - }).forEach(function (f) { - result[f] = formatters.inputBlockNumberFormatter(options[f]); - }); +/// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones +/// @param should be string or object +/// @returns options string or object +var getOptions = function (options) { - result.topics = []; + if (utils.isString(options)) { + return options; + } - result.address = this._address; - if (!this._anonymous) { - result.topics.push('0x' + this.signature()); - } + options = options || {}; - var indexedTopics = this._params.filter(function (i) { - return i.indexed === true; - }).map(function (i) { - var value = indexed[i.name]; - if (value === undefined || value === null) { - return null; - } - - if (utils.isArray(value)) { - return value.map(function (v) { - return '0x' + coder.encodeParam(i.type, v); - }); - } - return '0x' + coder.encodeParam(i.type, value); + // make sure topics, get converted to hex + options.topics = options.topics || []; + options.topics = options.topics.map(function(topic){ + return (utils.isArray(topic)) ? topic.map(toTopic) : toTopic(topic); }); - result.topics = result.topics.concat(indexedTopics); - - return result; + // lazy load + return { + topics: options.topics, + to: options.to, + address: options.address, + fromBlock: formatters.inputBlockNumberFormatter(options.fromBlock), + toBlock: formatters.inputBlockNumberFormatter(options.toBlock) + }; }; /** - * Should be used to decode indexed params and options - * - * @method decode - * @param {Object} data - * @return {Object} result object with decoded indexed && not indexed params - */ -SolidityEvent.prototype.decode = function (data) { - - data.data = data.data || ''; - data.topics = data.topics || []; - - var argTopics = this._anonymous ? data.topics : data.topics.slice(1); - var indexedData = argTopics.map(function (topics) { return topics.slice(2); }).join(""); - var indexedParams = coder.decodeParams(this.types(true), indexedData); - - var notIndexedData = data.data.slice(2); - var notIndexedParams = coder.decodeParams(this.types(false), notIndexedData); - - var result = formatters.outputLogFormatter(data); - result.event = this.displayName(); - result.address = data.address; - - result.args = this._params.reduce(function (acc, current) { - acc[current.name] = current.indexed ? indexedParams.shift() : notIndexedParams.shift(); - return acc; - }, {}); - - delete result.data; - delete result.topics; - - return result; -}; - -/** - * Should be used to create new filter object from event - * - * @method execute - * @param {Object} indexed - * @param {Object} options - * @return {Object} filter object - */ -SolidityEvent.prototype.execute = function (indexed, options, callback) { - - if (utils.isFunction(arguments[arguments.length - 1])) { - callback = arguments[arguments.length - 1]; - if(arguments.length === 2) - options = null; - if(arguments.length === 1) { - options = null; - indexed = {}; - } - } - - var o = this.encode(indexed, options); - var formatter = this.decode.bind(this); - return new Filter(o, watches.eth(), formatter, callback); -}; - -/** - * Should be used to attach event to contract object - * - * @method attachToContract - * @param {Contract} - */ -SolidityEvent.prototype.attachToContract = function (contract) { - var execute = this.execute.bind(this); - var displayName = this.displayName(); - if (!contract[displayName]) { - contract[displayName] = execute; - } - contract[displayName][this.typeName()] = this.execute.bind(this, contract); -}; - -module.exports = SolidityEvent; - - -},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"./filter":17,"./formatters":18,"./watches":31}],17:[function(require,module,exports){ -/* - This file is part of ethereum.js. - - ethereum.js is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - ethereum.js is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with ethereum.js. If not, see . -*/ -/** @file filter.js - * @authors: - * Jeffrey Wilcke - * Marek Kotewicz - * Marian Oancea - * Fabian Vogelsteller - * Gav Wood - * @date 2014 - */ - -var RequestManager = require('./requestmanager'); -var formatters = require('./formatters'); -var utils = require('../utils/utils'); - -/** -* Converts a given topic to a hex string, but also allows null values. -* -* @param {Mixed} value -* @return {String} -*/ -var toTopic = function(value){ - - if(value === null || typeof value === 'undefined') - return null; - - value = String(value); - - if(value.indexOf('0x') === 0) - return value; - else - return utils.fromAscii(value); -}; - -/// This method should be called on options object, to verify deprecated properties && lazy load dynamic ones -/// @param should be string or object -/// @returns options string or object -var getOptions = function (options) { - - if (utils.isString(options)) { - return options; - } - - options = options || {}; - - // make sure topics, get converted to hex - options.topics = options.topics || []; - options.topics = options.topics.map(function(topic){ - return (utils.isArray(topic)) ? topic.map(toTopic) : toTopic(topic); - }); - - // lazy load - return { - topics: options.topics, - to: options.to, - address: options.address, - fromBlock: formatters.inputBlockNumberFormatter(options.fromBlock), - toBlock: formatters.inputBlockNumberFormatter(options.toBlock) - }; -}; - -/** -Adds the callback and sets up the methods, to iterate over the results. +Adds the callback and sets up the methods, to iterate over the results. @method getLogsAtStart @param {Object} self @@ -2795,6 +3420,7 @@ var Filter = function (options, methods, formatter, callback) { } }); + return this; }; Filter.prototype.watch = function (callback) { @@ -2840,7 +3466,7 @@ Filter.prototype.get = function (callback) { module.exports = Filter; -},{"../utils/utils":7,"./formatters":18,"./requestmanager":28}],18:[function(require,module,exports){ +},{"../utils/utils":20,"./formatters":29,"./requestmanager":43}],29:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -2866,6 +3492,7 @@ module.exports = Filter; var utils = require('../utils/utils'); var config = require('../utils/config'); +var Iban = require('./iban'); /** * Should the format output to a big number @@ -2898,6 +3525,34 @@ var inputBlockNumberFormatter = function (blockNumber) { return utils.toHex(blockNumber); }; +/** + * Formats the input of a transaction and converts all values to HEX + * + * @method inputCallFormatter + * @param {Object} transaction options + * @returns object +*/ +var inputCallFormatter = function (options){ + + options.from = options.from || config.defaultAccount; + + if (options.from) { + options.from = inputAddressFormatter(options.from); + } + + if (options.to) { // it might be contract creation + options.to = inputAddressFormatter(options.to); + } + + ['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) { + return options[key] !== undefined; + }).forEach(function(key){ + options[key] = utils.fromDecimal(options[key]); + }); + + return options; +}; + /** * Formats the input of a transaction and converts all values to HEX * @@ -2908,11 +3563,10 @@ var inputBlockNumberFormatter = function (blockNumber) { var inputTransactionFormatter = function (options){ options.from = options.from || config.defaultAccount; + options.from = inputAddressFormatter(options.from); - // make code -> data - if (options.code) { - options.data = options.code; - delete options.code; + if (options.to) { // it might be contract creation + options.to = inputAddressFormatter(options.to); } ['gasPrice', 'gas', 'value', 'nonce'].filter(function (key) { @@ -3073,10 +3727,24 @@ var outputPostFormatter = function(post){ return post; }; +var inputAddressFormatter = function (address) { + var iban = new Iban(address); + if (iban.isValid() && iban.isDirect()) { + return '0x' + iban.address(); + } else if (utils.isStrictAddress(address)) { + return address; + } else if (utils.isAddress(address)) { + return '0x' + address; + } + throw 'invalid address'; +}; + module.exports = { inputDefaultBlockNumberFormatter: inputDefaultBlockNumberFormatter, inputBlockNumberFormatter: inputBlockNumberFormatter, + inputCallFormatter: inputCallFormatter, inputTransactionFormatter: inputTransactionFormatter, + inputAddressFormatter: inputAddressFormatter, inputPostFormatter: inputPostFormatter, outputBigNumberFormatter: outputBigNumberFormatter, outputTransactionFormatter: outputTransactionFormatter, @@ -3087,7 +3755,7 @@ module.exports = { }; -},{"../utils/config":5,"../utils/utils":7}],19:[function(require,module,exports){ +},{"../utils/config":18,"../utils/utils":20,"./iban":32}],30:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3324,7 +3992,7 @@ SolidityFunction.prototype.attachToContract = function (contract) { module.exports = SolidityFunction; -},{"../solidity/coder":1,"../utils/sha3":6,"../utils/utils":7,"../web3":9,"./formatters":18}],20:[function(require,module,exports){ +},{"../solidity/coder":7,"../utils/sha3":19,"../utils/utils":20,"../web3":22,"./formatters":29}],31:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3351,51 +4019,61 @@ module.exports = SolidityFunction; "use strict"; -var XMLHttpRequest = (typeof window !== 'undefined' && window.XMLHttpRequest) ? window.XMLHttpRequest : require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line var errors = require('./errors'); +// workaround to use httpprovider in different envs +var XMLHttpRequest; // jshint ignore: line + +// meteor server environment +if (typeof Meteor !== 'undefined' && Meteor.isServer) { // jshint ignore: line + XMLHttpRequest = Npm.require('xmlhttprequest').XMLHttpRequest; // jshint ignore: line + +// browser +} else if (typeof window !== 'undefined' && window.XMLHttpRequest) { + XMLHttpRequest = window.XMLHttpRequest; // jshint ignore: line + +// node +} else { + XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore: line +} + +/** + * HttpProvider should be used to send rpc calls over http + */ var HttpProvider = function (host) { this.host = host || 'http://localhost:8545'; }; -HttpProvider.prototype.isConnected = function() { +/** + * Should be called to prepare new XMLHttpRequest + * + * @method prepareRequest + * @param {Boolean} true if request should be async + * @return {XMLHttpRequest} object + */ +HttpProvider.prototype.prepareRequest = function (async) { var request = new XMLHttpRequest(); - - request.open('POST', this.host, false); - request.setRequestHeader('Content-type','application/json'); - - try { - request.send(JSON.stringify({ - id: 9999999999, - jsonrpc: '2.0', - method: 'net_listening', - params: [] - })); - return true; - } catch(e) { - return false; - } + request.open('POST', this.host, async); + request.setRequestHeader('Content-Type','application/json'); + return request; }; +/** + * Should be called to make sync request + * + * @method send + * @param {Object} payload + * @return {Object} result + */ HttpProvider.prototype.send = function (payload) { - var request = new XMLHttpRequest(); + var request = this.prepareRequest(false); - request.open('POST', this.host, false); - request.setRequestHeader('Content-type','application/json'); - try { request.send(JSON.stringify(payload)); } catch(error) { throw errors.InvalidConnection(this.host); } - - // check request.status - // TODO: throw an error here! it cannot silently fail!!! - //if (request.status !== 200) { - //return; - //} - var result = request.responseText; try { @@ -3407,8 +4085,16 @@ HttpProvider.prototype.send = function (payload) { return result; }; +/** + * Should be used to make async request + * + * @method sendAsync + * @param {Object} payload + * @param {Function} callback triggered on end with (err, result) + */ HttpProvider.prototype.sendAsync = function (payload, callback) { - var request = new XMLHttpRequest(); + var request = this.prepareRequest(true); + request.onreadystatechange = function() { if (request.readyState === 4) { var result = request.responseText; @@ -3423,9 +4109,6 @@ HttpProvider.prototype.sendAsync = function (payload, callback) { callback(error, result); } }; - - request.open('POST', this.host, true); - request.setRequestHeader('Content-type','application/json'); try { request.send(JSON.stringify(payload)); @@ -3434,10 +4117,30 @@ HttpProvider.prototype.sendAsync = function (payload, callback) { } }; +/** + * Synchronously tries to make Http request + * + * @method isConnected + * @return {Boolean} returns true if request haven't failed. Otherwise false + */ +HttpProvider.prototype.isConnected = function() { + try { + this.send({ + id: 9999999999, + jsonrpc: '2.0', + method: 'net_listening', + params: [] + }); + return true; + } catch(e) { + return false; + } +}; + module.exports = HttpProvider; -},{"./errors":14,"xmlhttprequest":4}],21:[function(require,module,exports){ +},{"./errors":26,"xmlhttprequest":17}],32:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3455,60 +4158,169 @@ module.exports = HttpProvider; along with ethereum.js. If not, see . */ /** - * @file icap.js + * @file iban.js * @author Marek Kotewicz * @date 2015 */ -var utils = require('../utils/utils'); +var BigNumber = require('bignumber.js'); + +var padLeft = function (string, bytes) { + var result = string; + while (result.length < bytes * 2) { + result = '00' + result; + } + return result; +}; /** - * This prototype should be used to extract necessary information from iban address + * Prepare an IBAN for mod 97 computation by moving the first 4 chars to the end and transforming the letters to + * numbers (A = 10, B = 11, ..., Z = 35), as specified in ISO13616. * - * @param {String} iban + * @method iso13616Prepare + * @param {String} iban the IBAN + * @returns {String} the prepared IBAN */ -var ICAP = function (iban) { - this._iban = iban; +var iso13616Prepare = function (iban) { + var A = 'A'.charCodeAt(0); + var Z = 'Z'.charCodeAt(0); + + iban = iban.toUpperCase(); + iban = iban.substr(4) + iban.substr(0,4); + + return iban.split('').map(function(n){ + var code = n.charCodeAt(0); + if (code >= A && code <= Z){ + // A = 10, B = 11, ... Z = 35 + return code - A + 10; + } else { + return n; + } + }).join(''); }; /** - * Should be called to check if icap is correct + * Calculates the MOD 97 10 of the passed IBAN as specified in ISO7064. * - * @method isValid - * @returns {Boolean} true if it is, otherwise false + * @method mod9710 + * @param {String} iban + * @returns {Number} */ -ICAP.prototype.isValid = function () { - return utils.isIBAN(this._iban); +var mod9710 = function (iban) { + var remainder = iban, + block; + + while (remainder.length > 2){ + block = remainder.slice(0, 9); + remainder = parseInt(block, 10) % 97 + remainder.slice(block.length); + } + + return parseInt(remainder, 10) % 97; }; /** - * Should be called to check if iban number is direct + * This prototype should be used to create iban object from iban correct string * - * @method isDirect - * @returns {Boolean} true if it is, otherwise false + * @param {String} iban */ -ICAP.prototype.isDirect = function () { - return this._iban.length === 34; +var Iban = function (iban) { + this._iban = iban; }; /** - * Should be called to check if iban number if indirect + * This method should be used to create iban object from ethereum address * - * @method isIndirect - * @returns {Boolean} true if it is, otherwise false + * @method fromAddress + * @param {String} address + * @return {Iban} the IBAN object */ -ICAP.prototype.isIndirect = function () { - return this._iban.length === 20; +Iban.fromAddress = function (address) { + var asBn = new BigNumber(address, 16); + var base36 = asBn.toString(36); + var padded = padLeft(base36, 15); + return Iban.fromBban(padded.toUpperCase()); }; /** - * Should be called to get iban checksum - * Uses the mod-97-10 checksumming protocol (ISO/IEC 7064:2003) + * Convert the passed BBAN to an IBAN for this country specification. + * Please note that "generation of the IBAN shall be the exclusive responsibility of the bank/branch servicing the account". + * This method implements the preferred algorithm described in http://en.wikipedia.org/wiki/International_Bank_Account_Number#Generating_IBAN_check_digits * - * @method checksum - * @returns {String} checksum + * @method fromBban + * @param {String} bban the BBAN to convert to IBAN + * @returns {Iban} the IBAN object */ -ICAP.prototype.checksum = function () { +Iban.fromBban = function (bban) { + var countryCode = 'XE'; + + var remainder = mod9710(iso13616Prepare(countryCode + '00' + bban)); + var checkDigit = ('0' + (98 - remainder)).slice(-2); + + return new Iban(countryCode + checkDigit + bban); +}; + +/** + * Should be used to create IBAN object for given institution and identifier + * + * @method createIndirect + * @param {Object} options, required options are "institution" and "identifier" + * @return {Iban} the IBAN object + */ +Iban.createIndirect = function (options) { + return Iban.fromBban('ETH' + options.institution + options.identifier); +}; + +/** + * Thos method should be used to check if given string is valid iban object + * + * @method isValid + * @param {String} iban string + * @return {Boolean} true if it is valid IBAN + */ +Iban.isValid = function (iban) { + var i = new Iban(iban); + return i.isValid(); +}; + +/** + * Should be called to check if iban is correct + * + * @method isValid + * @returns {Boolean} true if it is, otherwise false + */ +Iban.prototype.isValid = function () { + return /^XE[0-9]{2}(ETH[0-9A-Z]{13}|[0-9A-Z]{30})$/.test(this._iban) && + mod9710(iso13616Prepare(this._iban)) === 1; +}; + +/** + * Should be called to check if iban number is direct + * + * @method isDirect + * @returns {Boolean} true if it is, otherwise false + */ +Iban.prototype.isDirect = function () { + return this._iban.length === 34 || this._iban.length === 35; +}; + +/** + * Should be called to check if iban number if indirect + * + * @method isIndirect + * @returns {Boolean} true if it is, otherwise false + */ +Iban.prototype.isIndirect = function () { + return this._iban.length === 20; +}; + +/** + * Should be called to get iban checksum + * Uses the mod-97-10 checksumming protocol (ISO/IEC 7064:2003) + * + * @method checksum + * @returns {String} checksum + */ +Iban.prototype.checksum = function () { return this._iban.substr(2, 2); }; @@ -3519,7 +4331,7 @@ ICAP.prototype.checksum = function () { * @method institution * @returns {String} institution identifier */ -ICAP.prototype.institution = function () { +Iban.prototype.institution = function () { return this.isIndirect() ? this._iban.substr(7, 4) : ''; }; @@ -3530,24 +4342,872 @@ ICAP.prototype.institution = function () { * @method client * @returns {String} client identifier */ -ICAP.prototype.client = function () { +Iban.prototype.client = function () { return this.isIndirect() ? this._iban.substr(11) : ''; }; -/** - * Should be called to get client direct address - * - * @method address - * @returns {String} client direct address - */ -ICAP.prototype.address = function () { - return this.isDirect() ? this._iban.substr(4) : ''; +/** + * Should be called to get client direct address + * + * @method address + * @returns {String} client direct address + */ +Iban.prototype.address = function () { + if (this.isDirect()) { + var base36 = this._iban.substr(4); + var asBn = new BigNumber(base36, 36); + return padLeft(asBn.toString(16), 20); + } + + return ''; +}; + +Iban.prototype.toString = function () { + return this._iban; +}; + +module.exports = Iban; + + +},{"bignumber.js":"bignumber.js"}],33:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ethereum.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ethereum.js. If not, see . +*/ +/** @file ipcprovider.js + * @authors: + * Fabian Vogelsteller + * @date 2015 + */ + +"use strict"; + +var utils = require('../utils/utils'); +var errors = require('./errors'); + +var errorTimeout = function (method, id) { + var err = { + "jsonrpc": "2.0", + "error": { + "code": -32603, + "message": "IPC Request timed out for method \'" + method + "\'" + }, + "id": id + }; + return JSON.stringify(err); +}; + +var IpcProvider = function (path, net) { + var _this = this; + this.responseCallbacks = {}; + this.path = path; + + this.connection = net.connect({path: this.path}); + + this.connection.on('error', function(e){ + console.error('IPC Connection Error', e); + _this._timeout(); + }); + + this.connection.on('end', function(){ + _this._timeout(); + }); + + + // LISTEN FOR CONNECTION RESPONSES + this.connection.on('data', function(data) { + /*jshint maxcomplexity: 6 */ + + _this._parseResponse(data.toString()).forEach(function(result){ + + var id = null; + + // get the id which matches the returned id + if(utils.isArray(result)) { + result.forEach(function(load){ + if(_this.responseCallbacks[load.id]) + id = load.id; + }); + } else { + id = result.id; + } + + // fire the callback + if(_this.responseCallbacks[id]) { + _this.responseCallbacks[id](null, result); + delete _this.responseCallbacks[id]; + } + }); + }); +}; + +/** +Will parse the response and make an array out of it. + +@method _parseResponse +@param {String} data +*/ +IpcProvider.prototype._parseResponse = function(data) { + var _this = this, + returnValues = []; + + // DE-CHUNKER + var dechunkedData = data + .replace(/\}\{/g,'}|--|{') // }{ + .replace(/\}\]\[\{/g,'}]|--|[{') // }][{ + .replace(/\}\[\{/g,'}|--|[{') // }[{ + .replace(/\}\]\{/g,'}]|--|{') // }]{ + .split('|--|'); + + dechunkedData.forEach(function(data){ + + // prepend the last chunk + if(_this.lastChunk) + data = _this.lastChunk + data; + + var result = null; + + try { + result = JSON.parse(data); + + } catch(e) { + + _this.lastChunk = data; + + // start timeout to cancel all requests + clearTimeout(_this.lastChunkTimeout); + _this.lastChunkTimeout = setTimeout(function(){ + _this.timeout(); + throw errors.InvalidResponse(data); + }, 1000 * 15); + + return; + } + + // cancel timeout and set chunk to null + clearTimeout(_this.lastChunkTimeout); + _this.lastChunk = null; + + if(result) + returnValues.push(result); + }); + + return returnValues; +}; + + +/** +Get the adds a callback to the responseCallbacks object, +which will be called if a response matching the response Id will arrive. + +@method _addResponseCallback +*/ +IpcProvider.prototype._addResponseCallback = function(payload, callback) { + var id = payload.id || payload[0].id; + var method = payload.method || payload[0].method; + + this.responseCallbacks[id] = callback; + this.responseCallbacks[id].method = method; +}; + +/** +Timeout all requests when the end/error event is fired + +@method _timeout +*/ +IpcProvider.prototype._timeout = function() { + for(var key in this.responseCallbacks) { + if(this.responseCallbacks.hasOwnProperty(key)){ + this.responseCallbacks[key](errorTimeout(this.responseCallbacks[key].method, key)); + delete this.responseCallbacks[key]; + } + } +}; + + +/** +Check if the current connection is still valid. + +@method isConnected +*/ +IpcProvider.prototype.isConnected = function() { + var _this = this; + + // try reconnect, when connection is gone + if(!_this.connection.writable) + _this.connection.connect({path: _this.path}); + + return !!this.connection.writable; +}; + +IpcProvider.prototype.send = function (payload) { + + if(this.connection.writeSync) { + var result; + + // try reconnect, when connection is gone + if(!this.connection.writable) + this.connection.connect({path: this.path}); + + var data = this.connection.writeSync(JSON.stringify(payload)); + + try { + result = JSON.parse(data); + } catch(e) { + throw errors.InvalidResponse(data); + } + + return result; + + } else { + throw new Error('You tried to send "'+ payload.method +'" synchronously. Synchronous requests are not supported by the IPC provider.'); + } +}; + +IpcProvider.prototype.sendAsync = function (payload, callback) { + // try reconnect, when connection is gone + if(!this.connection.writable) + this.connection.connect({path: this.path}); + + + this.connection.write(JSON.stringify(payload)); + this._addResponseCallback(payload, callback); +}; + +module.exports = IpcProvider; + + +},{"../utils/utils":20,"./errors":26}],34:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ethereum.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ethereum.js. If not, see . +*/ +/** @file jsonrpc.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +var Jsonrpc = function () { + // singleton pattern + if (arguments.callee._singletonInstance) { + return arguments.callee._singletonInstance; + } + arguments.callee._singletonInstance = this; + + this.messageId = 1; +}; + +/** + * @return {Jsonrpc} singleton + */ +Jsonrpc.getInstance = function () { + var instance = new Jsonrpc(); + return instance; +}; + +/** + * Should be called to valid json create payload object + * + * @method toPayload + * @param {Function} method of jsonrpc call, required + * @param {Array} params, an array of method params, optional + * @returns {Object} valid jsonrpc payload object + */ +Jsonrpc.prototype.toPayload = function (method, params) { + if (!method) + console.error('jsonrpc method should be specified!'); + + return { + jsonrpc: '2.0', + method: method, + params: params || [], + id: this.messageId++ + }; +}; + +/** + * Should be called to check if jsonrpc response is valid + * + * @method isValidResponse + * @param {Object} + * @returns {Boolean} true if response is valid, otherwise false + */ +Jsonrpc.prototype.isValidResponse = function (response) { + return !!response && + !response.error && + response.jsonrpc === '2.0' && + typeof response.id === 'number' && + response.result !== undefined; // only undefined is not valid json object +}; + +/** + * Should be called to create batch payload object + * + * @method toBatchPayload + * @param {Array} messages, an array of objects with method (required) and params (optional) fields + * @returns {Array} batch payload + */ +Jsonrpc.prototype.toBatchPayload = function (messages) { + var self = this; + return messages.map(function (message) { + return self.toPayload(message.method, message.params); + }); +}; + +module.exports = Jsonrpc; + + +},{}],35:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ethereum.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ethereum.js. If not, see . +*/ +/** + * @file method.js + * @author Marek Kotewicz + * @date 2015 + */ + +var RequestManager = require('./requestmanager'); +var utils = require('../utils/utils'); +var errors = require('./errors'); + +var Method = function (options) { + this.name = options.name; + this.call = options.call; + this.params = options.params || 0; + this.inputFormatter = options.inputFormatter; + this.outputFormatter = options.outputFormatter; +}; + +/** + * Should be used to determine name of the jsonrpc method based on arguments + * + * @method getCall + * @param {Array} arguments + * @return {String} name of jsonrpc method + */ +Method.prototype.getCall = function (args) { + return utils.isFunction(this.call) ? this.call(args) : this.call; +}; + +/** + * Should be used to extract callback from array of arguments. Modifies input param + * + * @method extractCallback + * @param {Array} arguments + * @return {Function|Null} callback, if exists + */ +Method.prototype.extractCallback = function (args) { + if (utils.isFunction(args[args.length - 1])) { + return args.pop(); // modify the args array! + } +}; + +/** + * Should be called to check if the number of arguments is correct + * + * @method validateArgs + * @param {Array} arguments + * @throws {Error} if it is not + */ +Method.prototype.validateArgs = function (args) { + if (args.length !== this.params) { + throw errors.InvalidNumberOfParams(); + } +}; + +/** + * Should be called to format input args of method + * + * @method formatInput + * @param {Array} + * @return {Array} + */ +Method.prototype.formatInput = function (args) { + if (!this.inputFormatter) { + return args; + } + + return this.inputFormatter.map(function (formatter, index) { + return formatter ? formatter(args[index]) : args[index]; + }); +}; + +/** + * Should be called to format output(result) of method + * + * @method formatOutput + * @param {Object} + * @return {Object} + */ +Method.prototype.formatOutput = function (result) { + return this.outputFormatter && result ? this.outputFormatter(result) : result; +}; + +/** + * Should attach function to method + * + * @method attachToObject + * @param {Object} + * @param {Function} + */ +Method.prototype.attachToObject = function (obj) { + var func = this.send.bind(this); + func.request = this.request.bind(this); + func.call = this.call; // that's ugly. filter.js uses it + var name = this.name.split('.'); + if (name.length > 1) { + obj[name[0]] = obj[name[0]] || {}; + obj[name[0]][name[1]] = func; + } else { + obj[name[0]] = func; + } +}; + +/** + * Should create payload from given input args + * + * @method toPayload + * @param {Array} args + * @return {Object} + */ +Method.prototype.toPayload = function (args) { + var call = this.getCall(args); + var callback = this.extractCallback(args); + var params = this.formatInput(args); + this.validateArgs(params); + + return { + method: call, + params: params, + callback: callback + }; +}; + +/** + * Should be called to create pure JSONRPC request which can be used in batch request + * + * @method request + * @param {...} params + * @return {Object} jsonrpc request + */ +Method.prototype.request = function () { + var payload = this.toPayload(Array.prototype.slice.call(arguments)); + payload.format = this.formatOutput.bind(this); + return payload; +}; + +/** + * Should send request to the API + * + * @method send + * @param list of params + * @return result + */ +Method.prototype.send = function () { + var payload = this.toPayload(Array.prototype.slice.call(arguments)); + if (payload.callback) { + var self = this; + return RequestManager.getInstance().sendAsync(payload, function (err, result) { + payload.callback(err, self.formatOutput(result)); + }); + } + return this.formatOutput(RequestManager.getInstance().send(payload)); +}; + +module.exports = Method; + + +},{"../utils/utils":20,"./errors":26,"./requestmanager":43}],36:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ethereum.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ethereum.js. If not, see . +*/ +/** @file db.js + * @authors: + * Marek Kotewicz + * @date 2015 + */ + +var Method = require('../method'); + +var putString = new Method({ + name: 'putString', + call: 'db_putString', + params: 3 +}); + + +var getString = new Method({ + name: 'getString', + call: 'db_getString', + params: 2 +}); + +var putHex = new Method({ + name: 'putHex', + call: 'db_putHex', + params: 3 +}); + +var getHex = new Method({ + name: 'getHex', + call: 'db_getHex', + params: 2 +}); + +var methods = [ + putString, getString, putHex, getHex +]; + +module.exports = { + methods: methods +}; + +},{"../method":35}],37:[function(require,module,exports){ +/* + This file is part of ethereum.js. + + ethereum.js is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + ethereum.js is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with ethereum.js. If not, see . +*/ +/** + * @file eth.js + * @author Marek Kotewicz + * @author Fabian Vogelsteller + * @date 2015 + */ + +/** + * Web3 + * + * @module web3 + */ + +/** + * Eth methods and properties + * + * An example method object can look as follows: + * + * { + * name: 'getBlock', + * call: blockCall, + * params: 2, + * outputFormatter: formatters.outputBlockFormatter, + * inputFormatter: [ // can be a formatter funciton or an array of functions. Where each item in the array will be used for one parameter + * utils.toHex, // formats paramter 1 + * function(param){ return !!param; } // formats paramter 2 + * ] + * }, + * + * @class [web3] eth + * @constructor + */ + +"use strict"; + +var formatters = require('../formatters'); +var utils = require('../../utils/utils'); +var Method = require('../method'); +var Property = require('../property'); + +var blockCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? "eth_getBlockByHash" : "eth_getBlockByNumber"; +}; + +var transactionFromBlockCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex'; +}; + +var uncleCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex'; +}; + +var getBlockTransactionCountCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getBlockTransactionCountByHash' : 'eth_getBlockTransactionCountByNumber'; +}; + +var uncleCountCall = function (args) { + return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleCountByBlockHash' : 'eth_getUncleCountByBlockNumber'; +}; + +/// @returns an array of objects describing web3.eth api methods + +var getBalance = new Method({ + name: 'getBalance', + call: 'eth_getBalance', + params: 2, + inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter], + outputFormatter: formatters.outputBigNumberFormatter +}); + +var getStorageAt = new Method({ + name: 'getStorageAt', + call: 'eth_getStorageAt', + params: 3, + inputFormatter: [null, utils.toHex, formatters.inputDefaultBlockNumberFormatter] +}); + +var getCode = new Method({ + name: 'getCode', + call: 'eth_getCode', + params: 2, + inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter] +}); + +var getBlock = new Method({ + name: 'getBlock', + call: blockCall, + params: 2, + inputFormatter: [formatters.inputBlockNumberFormatter, function (val) { return !!val; }], + outputFormatter: formatters.outputBlockFormatter +}); + +var getUncle = new Method({ + name: 'getUncle', + call: uncleCall, + params: 2, + inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], + outputFormatter: formatters.outputBlockFormatter, + +}); + +var getCompilers = new Method({ + name: 'getCompilers', + call: 'eth_getCompilers', + params: 0 +}); + +var getBlockTransactionCount = new Method({ + name: 'getBlockTransactionCount', + call: getBlockTransactionCountCall, + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + outputFormatter: utils.toDecimal +}); + +var getBlockUncleCount = new Method({ + name: 'getBlockUncleCount', + call: uncleCountCall, + params: 1, + inputFormatter: [formatters.inputBlockNumberFormatter], + outputFormatter: utils.toDecimal +}); + +var getTransaction = new Method({ + name: 'getTransaction', + call: 'eth_getTransactionByHash', + params: 1, + outputFormatter: formatters.outputTransactionFormatter +}); + +var getTransactionFromBlock = new Method({ + name: 'getTransactionFromBlock', + call: transactionFromBlockCall, + params: 2, + inputFormatter: [formatters.inputBlockNumberFormatter, utils.toHex], + outputFormatter: formatters.outputTransactionFormatter +}); + +var getTransactionReceipt = new Method({ + name: 'getTransactionReceipt', + call: 'eth_getTransactionReceipt', + params: 1, + outputFormatter: formatters.outputTransactionReceiptFormatter +}); + +var getTransactionCount = new Method({ + name: 'getTransactionCount', + call: 'eth_getTransactionCount', + params: 2, + inputFormatter: [null, formatters.inputDefaultBlockNumberFormatter], + outputFormatter: utils.toDecimal +}); + +var sendRawTransaction = new Method({ + name: 'sendRawTransaction', + call: 'eth_sendRawTransaction', + params: 1, + inputFormatter: [null] +}); + +var sendTransaction = new Method({ + name: 'sendTransaction', + call: 'eth_sendTransaction', + params: 1, + inputFormatter: [formatters.inputTransactionFormatter] +}); + +var call = new Method({ + name: 'call', + call: 'eth_call', + params: 2, + inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter] +}); + +var estimateGas = new Method({ + name: 'estimateGas', + call: 'eth_estimateGas', + params: 1, + inputFormatter: [formatters.inputCallFormatter], + outputFormatter: utils.toDecimal +}); + +var compileSolidity = new Method({ + name: 'compile.solidity', + call: 'eth_compileSolidity', + params: 1 +}); + +var compileLLL = new Method({ + name: 'compile.lll', + call: 'eth_compileLLL', + params: 1 +}); + +var compileSerpent = new Method({ + name: 'compile.serpent', + call: 'eth_compileSerpent', + params: 1 +}); + +var submitWork = new Method({ + name: 'submitWork', + call: 'eth_submitWork', + params: 3 +}); + +var getWork = new Method({ + name: 'getWork', + call: 'eth_getWork', + params: 0 +}); + +var methods = [ + getBalance, + getStorageAt, + getCode, + getBlock, + getUncle, + getCompilers, + getBlockTransactionCount, + getBlockUncleCount, + getTransaction, + getTransactionFromBlock, + getTransactionReceipt, + getTransactionCount, + call, + estimateGas, + sendRawTransaction, + sendTransaction, + compileSolidity, + compileLLL, + compileSerpent, + submitWork, + getWork +]; + +/// @returns an array of objects describing web3.eth api properties + + + +var properties = [ + new Property({ + name: 'coinbase', + getter: 'eth_coinbase' + }), + new Property({ + name: 'mining', + getter: 'eth_mining' + }), + new Property({ + name: 'hashrate', + getter: 'eth_hashrate', + outputFormatter: utils.toDecimal + }), + new Property({ + name: 'gasPrice', + getter: 'eth_gasPrice', + outputFormatter: formatters.outputBigNumberFormatter + }), + new Property({ + name: 'accounts', + getter: 'eth_accounts' + }), + new Property({ + name: 'blockNumber', + getter: 'eth_blockNumber', + outputFormatter: utils.toDecimal + }) +]; + +module.exports = { + methods: methods, + properties: properties }; -module.exports = ICAP; - -},{"../utils/utils":7}],22:[function(require,module,exports){ +},{"../../utils/utils":20,"../formatters":29,"../method":35,"../property":42}],38:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3564,203 +5224,40 @@ module.exports = ICAP; You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** @file ipcprovider.js +/** @file eth.js * @authors: - * Fabian Vogelsteller + * Marek Kotewicz * @date 2015 */ -"use strict"; - -var utils = require('../utils/utils'); -var errors = require('./errors'); - -var errorTimeout = '{"jsonrpc": "2.0", "error": {"code": -32603, "message": "IPC Request timed out for method \'__method__\'"}, "id": "__id__"}'; - - -var IpcProvider = function (path, net) { - var _this = this; - this.responseCallbacks = {}; - this.path = path; - - net = net || require('net'); - - this.connection = net.connect({path: this.path}); - - this.connection.on('error', function(e){ - console.error('IPC Connection Error', e); - _this._timeout(); - }); - - this.connection.on('end', function(){ - _this._timeout(); - }); - - - // LISTEN FOR CONNECTION RESPONSES - this.connection.on('data', function(data) { - /*jshint maxcomplexity: 6 */ - - _this._parseResponse(data.toString()).forEach(function(result){ - - var id = null; - - // get the id which matches the returned id - if(utils.isArray(result)) { - result.forEach(function(load){ - if(_this.responseCallbacks[load.id]) - id = load.id; - }); - } else { - id = result.id; - } - - // fire the callback - if(_this.responseCallbacks[id]) { - _this.responseCallbacks[id](null, result); - delete _this.responseCallbacks[id]; - } - }); - }); -}; - -/** -Will parse the response and make an array out of it. - -@method _parseResponse -@param {String} data -*/ -IpcProvider.prototype._parseResponse = function(data) { - var _this = this, - returnValues = []; - - // DE-CHUNKER - var dechunkedData = data - .replace(/\}\{/g,'}|--|{') // }{ - .replace(/\}\]\[\{/g,'}]|--|[{') // }][{ - .replace(/\}\[\{/g,'}|--|[{') // }[{ - .replace(/\}\]\{/g,'}]|--|{') // }]{ - .split('|--|'); - - dechunkedData.forEach(function(data){ - - // prepend the last chunk - if(_this.lastChunk) - data = _this.lastChunk + data; - - var result = null; - - try { - result = JSON.parse(data); - - } catch(e) { - - _this.lastChunk = data; - - // start timeout to cancel all requests - clearTimeout(_this.lastChunkTimeout); - _this.lastChunkTimeout = setTimeout(function(){ - _this.timeout(); - throw errors.InvalidResponse(data); - }, 1000 * 15); - - return; - } - - // cancel timeout and set chunk to null - clearTimeout(_this.lastChunkTimeout); - _this.lastChunk = null; - - if(result) - returnValues.push(result); - }); - - return returnValues; -}; - - -/** -Get the adds a callback to the responseCallbacks object, -which will be called if a response matching the response Id will arrive. - -@method _addResponseCallback -*/ -IpcProvider.prototype._addResponseCallback = function(payload, callback) { - var id = payload.id || payload[0].id; - var method = payload.method || payload[0].method; - - this.responseCallbacks[id] = callback; - this.responseCallbacks[id].method = method; -}; - -/** -Timeout all requests when the end/error event is fired - -@method _timeout -*/ -IpcProvider.prototype._timeout = function() { - for(var key in this.responseCallbacks) { - if(this.responseCallbacks.hasOwnProperty(key)){ - this.responseCallbacks[key](errorTimeout.replace('__id__', key).replace('__method__', this.responseCallbacks[key].method)); - delete this.responseCallbacks[key]; - } - } -}; - - -/** -Check if the current connection is still valid. - -@method isConnected -*/ -IpcProvider.prototype.isConnected = function() { - var _this = this; - - // try reconnect, when connection is gone - if(!_this.connection.writable) - _this.connection.connect({path: _this.path}); - - return !!this.connection.writable; -}; - -IpcProvider.prototype.send = function (payload) { - - if(this.connection.writeSync) { - var result; - - // try reconnect, when connection is gone - if(!this.connection.writable) - this.connection.connect({path: this.path}); - - var data = this.connection.writeSync(JSON.stringify(payload)); - - try { - result = JSON.parse(data); - } catch(e) { - throw errors.InvalidResponse(data); - } - - return result; +var utils = require('../../utils/utils'); +var Property = require('../property'); - } else { - throw new Error('You tried to send "'+ payload.method +'" synchronously. Synchronous requests are not supported by the IPC provider.'); - } -}; +/// @returns an array of objects describing web3.eth api methods +var methods = [ +]; -IpcProvider.prototype.sendAsync = function (payload, callback) { - // try reconnect, when connection is gone - if(!this.connection.writable) - this.connection.connect({path: this.path}); +/// @returns an array of objects describing web3.eth api properties +var properties = [ + new Property({ + name: 'listening', + getter: 'net_listening' + }), + new Property({ + name: 'peerCount', + getter: 'net_peerCount', + outputFormatter: utils.toDecimal + }) +]; - this.connection.write(JSON.stringify(payload)); - this._addResponseCallback(payload, callback); +module.exports = { + methods: methods, + properties: properties }; -module.exports = IpcProvider; - -},{"../utils/utils":7,"./errors":14,"net":32}],23:[function(require,module,exports){ +},{"../../utils/utils":20,"../property":42}],39:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3777,83 +5274,60 @@ module.exports = IpcProvider; You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** @file jsonrpc.js +/** @file shh.js * @authors: * Marek Kotewicz * @date 2015 */ -var Jsonrpc = function () { - // singleton pattern - if (arguments.callee._singletonInstance) { - return arguments.callee._singletonInstance; - } - arguments.callee._singletonInstance = this; - - this.messageId = 1; -}; +var Method = require('../method'); +var formatters = require('../formatters'); -/** - * @return {Jsonrpc} singleton - */ -Jsonrpc.getInstance = function () { - var instance = new Jsonrpc(); - return instance; -}; +var post = new Method({ + name: 'post', + call: 'shh_post', + params: 1, + inputFormatter: [formatters.inputPostFormatter] +}); -/** - * Should be called to valid json create payload object - * - * @method toPayload - * @param {Function} method of jsonrpc call, required - * @param {Array} params, an array of method params, optional - * @returns {Object} valid jsonrpc payload object - */ -Jsonrpc.prototype.toPayload = function (method, params) { - if (!method) - console.error('jsonrpc method should be specified!'); +var newIdentity = new Method({ + name: 'newIdentity', + call: 'shh_newIdentity', + params: 0 +}); - return { - jsonrpc: '2.0', - method: method, - params: params || [], - id: this.messageId++ - }; -}; +var hasIdentity = new Method({ + name: 'hasIdentity', + call: 'shh_hasIdentity', + params: 1 +}); -/** - * Should be called to check if jsonrpc response is valid - * - * @method isValidResponse - * @param {Object} - * @returns {Boolean} true if response is valid, otherwise false - */ -Jsonrpc.prototype.isValidResponse = function (response) { - return !!response && - !response.error && - response.jsonrpc === '2.0' && - typeof response.id === 'number' && - response.result !== undefined; // only undefined is not valid json object -}; +var newGroup = new Method({ + name: 'newGroup', + call: 'shh_newGroup', + params: 0 +}); -/** - * Should be called to create batch payload object - * - * @method toBatchPayload - * @param {Array} messages, an array of objects with method (required) and params (optional) fields - * @returns {Array} batch payload - */ -Jsonrpc.prototype.toBatchPayload = function (messages) { - var self = this; - return messages.map(function (message) { - return self.toPayload(message.method, message.params); - }); -}; +var addToGroup = new Method({ + name: 'addToGroup', + call: 'shh_addToGroup', + params: 0 +}); -module.exports = Jsonrpc; +var methods = [ + post, + newIdentity, + hasIdentity, + newGroup, + addToGroup +]; + +module.exports = { + methods: methods +}; -},{}],24:[function(require,module,exports){ +},{"../formatters":29,"../method":35}],40:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -3870,164 +5344,106 @@ module.exports = Jsonrpc; You should have received a copy of the GNU Lesser General Public License along with ethereum.js. If not, see . */ -/** - * @file method.js - * @author Marek Kotewicz +/** @file watches.js + * @authors: + * Marek Kotewicz * @date 2015 */ -var RequestManager = require('./requestmanager'); -var utils = require('../utils/utils'); -var errors = require('./errors'); +var Method = require('../method'); -var Method = function (options) { - this.name = options.name; - this.call = options.call; - this.params = options.params || 0; - this.inputFormatter = options.inputFormatter; - this.outputFormatter = options.outputFormatter; -}; +/// @returns an array of objects describing web3.eth.filter api methods +var eth = function () { + var newFilterCall = function (args) { + var type = args[0]; -/** - * Should be used to determine name of the jsonrpc method based on arguments - * - * @method getCall - * @param {Array} arguments - * @return {String} name of jsonrpc method - */ -Method.prototype.getCall = function (args) { - return utils.isFunction(this.call) ? this.call(args) : this.call; -}; + switch(type) { + case 'latest': + args.shift(); + this.params = 0; + return 'eth_newBlockFilter'; + case 'pending': + args.shift(); + this.params = 0; + return 'eth_newPendingTransactionFilter'; + default: + return 'eth_newFilter'; + } + }; -/** - * Should be used to extract callback from array of arguments. Modifies input param - * - * @method extractCallback - * @param {Array} arguments - * @return {Function|Null} callback, if exists - */ -Method.prototype.extractCallback = function (args) { - if (utils.isFunction(args[args.length - 1])) { - return args.pop(); // modify the args array! - } -}; + var newFilter = new Method({ + name: 'newFilter', + call: newFilterCall, + params: 1 + }); -/** - * Should be called to check if the number of arguments is correct - * - * @method validateArgs - * @param {Array} arguments - * @throws {Error} if it is not - */ -Method.prototype.validateArgs = function (args) { - if (args.length !== this.params) { - throw errors.InvalidNumberOfParams(); - } -}; + var uninstallFilter = new Method({ + name: 'uninstallFilter', + call: 'eth_uninstallFilter', + params: 1 + }); -/** - * Should be called to format input args of method - * - * @method formatInput - * @param {Array} - * @return {Array} - */ -Method.prototype.formatInput = function (args) { - if (!this.inputFormatter) { - return args; - } + var getLogs = new Method({ + name: 'getLogs', + call: 'eth_getFilterLogs', + params: 1 + }); - return this.inputFormatter.map(function (formatter, index) { - return formatter ? formatter(args[index]) : args[index]; + var poll = new Method({ + name: 'poll', + call: 'eth_getFilterChanges', + params: 1 }); -}; -/** - * Should be called to format output(result) of method - * - * @method formatOutput - * @param {Object} - * @return {Object} - */ -Method.prototype.formatOutput = function (result) { - return this.outputFormatter && result ? this.outputFormatter(result) : result; + return [ + newFilter, + uninstallFilter, + getLogs, + poll + ]; }; -/** - * Should attach function to method - * - * @method attachToObject - * @param {Object} - * @param {Function} - */ -Method.prototype.attachToObject = function (obj) { - var func = this.send.bind(this); - func.request = this.request.bind(this); - func.call = this.call; // that's ugly. filter.js uses it - var name = this.name.split('.'); - if (name.length > 1) { - obj[name[0]] = obj[name[0]] || {}; - obj[name[0]][name[1]] = func; - } else { - obj[name[0]] = func; - } -}; +/// @returns an array of objects describing web3.shh.watch api methods +var shh = function () { + var newFilter = new Method({ + name: 'newFilter', + call: 'shh_newFilter', + params: 1 + }); -/** - * Should create payload from given input args - * - * @method toPayload - * @param {Array} args - * @return {Object} - */ -Method.prototype.toPayload = function (args) { - var call = this.getCall(args); - var callback = this.extractCallback(args); - var params = this.formatInput(args); - this.validateArgs(params); + var uninstallFilter = new Method({ + name: 'uninstallFilter', + call: 'shh_uninstallFilter', + params: 1 + }); - return { - method: call, - params: params, - callback: callback - }; -}; + var getLogs = new Method({ + name: 'getLogs', + call: 'shh_getMessages', + params: 1 + }); -/** - * Should be called to create pure JSONRPC request which can be used in batch request - * - * @method request - * @param {...} params - * @return {Object} jsonrpc request - */ -Method.prototype.request = function () { - var payload = this.toPayload(Array.prototype.slice.call(arguments)); - payload.format = this.formatOutput.bind(this); - return payload; + var poll = new Method({ + name: 'poll', + call: 'shh_getFilterChanges', + params: 1 + }); + + return [ + newFilter, + uninstallFilter, + getLogs, + poll + ]; }; -/** - * Should send request to the API - * - * @method send - * @param list of params - * @return result - */ -Method.prototype.send = function () { - var payload = this.toPayload(Array.prototype.slice.call(arguments)); - if (payload.callback) { - var self = this; - return RequestManager.getInstance().sendAsync(payload, function (err, result) { - payload.callback(err, self.formatOutput(result)); - }); - } - return this.formatOutput(RequestManager.getInstance().send(payload)); +module.exports = { + eth: eth, + shh: shh }; -module.exports = Method; - -},{"../utils/utils":7,"./errors":14,"./requestmanager":28}],25:[function(require,module,exports){ +},{"../method":35}],41:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4051,81 +5467,19 @@ module.exports = Method; */ var contract = require('./contract'); +var globalRegistrarAbi = require('../contracts/GlobalRegistrar.json'); +var icapRegistrarAbi= require('../contracts/ICAPRegistrar.json'); -var address = '0xc6d9d2cd449a754c494264e1809c50e34d64562b'; - -var abi = [ - {"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"name","outputs":[{"name":"o_name","type":"bytes32"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"owner","outputs":[{"name":"","type":"address"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"content","outputs":[{"name":"","type":"bytes32"}],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"addr","outputs":[{"name":"","type":"address"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"reserve","outputs":[],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"subRegistrar","outputs":[{"name":"o_subRegistrar","type":"address"}],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_newOwner","type":"address"}],"name":"transfer","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_registrar","type":"address"}],"name":"setSubRegistrar","outputs":[],"type":"function"}, - {"constant":false,"inputs":[],"name":"Registrar","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_a","type":"address"},{"name":"_primary","type":"bool"}],"name":"setAddress","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"},{"name":"_content","type":"bytes32"}],"name":"setContent","outputs":[],"type":"function"}, - {"constant":false,"inputs":[{"name":"_name","type":"bytes32"}],"name":"disown","outputs":[],"type":"function"}, - {"constant":true,"inputs":[{"name":"_name","type":"bytes32"}],"name":"register","outputs":[{"name":"","type":"address"}],"type":"function"}, - {"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"}],"name":"Changed","type":"event"}, - {"anonymous":false,"inputs":[{"indexed":true,"name":"name","type":"bytes32"},{"indexed":true,"name":"addr","type":"address"}],"name":"PrimaryChanged","type":"event"} -]; - -module.exports = contract(abi).at(address); - - -},{"./contract":12}],26:[function(require,module,exports){ -/* - This file is part of ethereum.js. - - ethereum.js is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - ethereum.js is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with ethereum.js. If not, see . -*/ -/** @file eth.js - * @authors: - * Marek Kotewicz - * @date 2015 - */ - -var utils = require('../utils/utils'); -var Property = require('./property'); - -/// @returns an array of objects describing web3.eth api methods -var methods = [ -]; - -/// @returns an array of objects describing web3.eth api properties -var properties = [ - new Property({ - name: 'listening', - getter: 'net_listening' - }), - new Property({ - name: 'peerCount', - getter: 'net_peerCount', - outputFormatter: utils.toDecimal - }) -]; - +var globalNameregAddress = '0xc6d9d2cd449a754c494264e1809c50e34d64562b'; +var ibanNameregAddress = '0xa1a111bc074c9cfa781f0c38e63bd51c91b8af00'; module.exports = { - methods: methods, - properties: properties + namereg: contract(globalRegistrarAbi).at(globalNameregAddress), + ibanNamereg: contract(icapRegistrarAbi).at(ibanNameregAddress) }; -},{"../utils/utils":7,"./property":27}],27:[function(require,module,exports){ +},{"../contracts/GlobalRegistrar.json":1,"../contracts/ICAPRegistrar.json":2,"./contract":25}],42:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4277,7 +5631,7 @@ Property.prototype.request = function () { module.exports = Property; -},{"../utils/utils":7,"./requestmanager":28}],28:[function(require,module,exports){ +},{"../utils/utils":20,"./requestmanager":43}],43:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4427,8 +5781,6 @@ RequestManager.prototype.setProvider = function (p) { } }; -/*jshint maxparams:4 */ - /** * Should be used to start polling * @@ -4441,9 +5793,8 @@ RequestManager.prototype.setProvider = function (p) { * @todo cleanup number of params */ RequestManager.prototype.startPolling = function (data, pollId, callback, uninstall) { - this.polls['poll_'+ pollId] = {data: data, id: pollId, callback: callback, uninstall: uninstall}; + this.polls[pollId] = {data: data, id: pollId, callback: callback, uninstall: uninstall}; }; -/*jshint maxparams:3 */ /** * Should be used to stop polling for filter with given id @@ -4452,7 +5803,7 @@ RequestManager.prototype.startPolling = function (data, pollId, callback, uninst * @param {Number} pollId */ RequestManager.prototype.stopPolling = function (pollId) { - delete this.polls['poll_'+ pollId]; + delete this.polls[pollId]; }; /** @@ -4542,77 +5893,7 @@ RequestManager.prototype.poll = function () { module.exports = RequestManager; -},{"../utils/config":5,"../utils/utils":7,"./errors":14,"./jsonrpc":23}],29:[function(require,module,exports){ -/* - This file is part of ethereum.js. - - ethereum.js is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - ethereum.js is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with ethereum.js. If not, see . -*/ -/** @file shh.js - * @authors: - * Marek Kotewicz - * @date 2015 - */ - -var Method = require('./method'); -var formatters = require('./formatters'); - -var post = new Method({ - name: 'post', - call: 'shh_post', - params: 1, - inputFormatter: [formatters.inputPostFormatter] -}); - -var newIdentity = new Method({ - name: 'newIdentity', - call: 'shh_newIdentity', - params: 0 -}); - -var hasIdentity = new Method({ - name: 'hasIdentity', - call: 'shh_hasIdentity', - params: 1 -}); - -var newGroup = new Method({ - name: 'newGroup', - call: 'shh_newGroup', - params: 0 -}); - -var addToGroup = new Method({ - name: 'addToGroup', - call: 'shh_addToGroup', - params: 0 -}); - -var methods = [ - post, - newIdentity, - hasIdentity, - newGroup, - addToGroup -]; - -module.exports = { - methods: methods -}; - - -},{"./formatters":18,"./method":24}],30:[function(require,module,exports){ +},{"../utils/config":18,"../utils/utils":20,"./errors":26,"./jsonrpc":34}],44:[function(require,module,exports){ /* This file is part of ethereum.js. @@ -4636,36 +5917,37 @@ module.exports = { */ var web3 = require('../web3'); -var ICAP = require('./icap'); -var namereg = require('./namereg'); +var Iban = require('./iban'); +var namereg = require('./namereg').ibanNamereg; var contract = require('./contract'); +var exchangeAbi = require('../contracts/SmartExchange.json'); /** - * Should be used to make ICAP transfer + * Should be used to make Iban transfer * * @method transfer - * @param {String} iban number - * @param {String} from (address) + * @param {String} from + * @param {String} to iban * @param {Value} value to be tranfered * @param {Function} callback, callback */ -var transfer = function (from, iban, value, callback) { - var icap = new ICAP(iban); - if (!icap.isValid()) { +var transfer = function (from, to, value, callback) { + var iban = new Iban(to); + if (!iban.isValid()) { throw new Error('invalid iban address'); } - if (icap.isDirect()) { - return transferToAddress(from, icap.address(), value, callback); + if (iban.isDirect()) { + return transferToAddress(from, iban.address(), value, callback); } if (!callback) { - var address = namereg.addr(icap.institution()); - return deposit(from, address, value, icap.client()); + var address = namereg.addr(iban.institution()); + return deposit(from, address, value, iban.client()); } - namereg.addr(icap.insitution(), function (err, address) { - return deposit(from, address, value, icap.client(), callback); + namereg.addr(iban.institution(), function (err, address) { + return deposit(from, address, value, iban.client(), callback); }); }; @@ -4674,14 +5956,14 @@ var transfer = function (from, iban, value, callback) { * Should be used to transfer funds to certain address * * @method transferToAddress - * @param {String} address - * @param {String} from (address) + * @param {String} from + * @param {String} to * @param {Value} value to be tranfered * @param {Function} callback, callback */ -var transferToAddress = function (from, address, value, callback) { +var transferToAddress = function (from, to, value, callback) { return web3.eth.sendTransaction({ - address: address, + address: to, from: from, value: value }, callback); @@ -4691,15 +5973,15 @@ var transferToAddress = function (from, address, value, callback) { * Should be used to deposit funds to generic Exchange contract (must implement deposit(bytes32) method!) * * @method deposit - * @param {String} address - * @param {String} from (address) - * @param {Value} value to be tranfered + * @param {String} from + * @param {String} to + * @param {Value} value to be transfered * @param {String} client unique identifier * @param {Function} callback, callback */ -var deposit = function (from, address, value, client, callback) { - var abi = [{"constant":false,"inputs":[{"name":"name","type":"bytes32"}],"name":"deposit","outputs":[],"type":"function"}]; - return contract(abi).at(address).deposit(client, { +var deposit = function (from, to, value, client, callback) { + var abi = exchangeAbi; + return contract(abi).at(to).deposit(client, { from: from, value: value }, callback); @@ -4708,125 +5990,9 @@ var deposit = function (from, address, value, client, callback) { module.exports = transfer; -},{"../web3":9,"./contract":12,"./icap":21,"./namereg":25}],31:[function(require,module,exports){ -/* - This file is part of ethereum.js. - - ethereum.js is free software: you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - ethereum.js is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public License - along with ethereum.js. If not, see . -*/ -/** @file watches.js - * @authors: - * Marek Kotewicz - * @date 2015 - */ - -var Method = require('./method'); - -/// @returns an array of objects describing web3.eth.filter api methods -var eth = function () { - var newFilterCall = function (args) { - var type = args[0]; - - switch(type) { - case 'latest': - args.shift(); - this.params = 0; - return 'eth_newBlockFilter'; - case 'pending': - args.shift(); - this.params = 0; - return 'eth_newPendingTransactionFilter'; - default: - return 'eth_newFilter'; - } - }; - - var newFilter = new Method({ - name: 'newFilter', - call: newFilterCall, - params: 1 - }); - - var uninstallFilter = new Method({ - name: 'uninstallFilter', - call: 'eth_uninstallFilter', - params: 1 - }); - - var getLogs = new Method({ - name: 'getLogs', - call: 'eth_getFilterLogs', - params: 1 - }); - - var poll = new Method({ - name: 'poll', - call: 'eth_getFilterChanges', - params: 1 - }); - - return [ - newFilter, - uninstallFilter, - getLogs, - poll - ]; -}; - -/// @returns an array of objects describing web3.shh.watch api methods -var shh = function () { - var newFilter = new Method({ - name: 'newFilter', - call: 'shh_newFilter', - params: 1 - }); - - var uninstallFilter = new Method({ - name: 'uninstallFilter', - call: 'shh_uninstallFilter', - params: 1 - }); - - var getLogs = new Method({ - name: 'getLogs', - call: 'shh_getMessages', - params: 1 - }); - - var poll = new Method({ - name: 'poll', - call: 'shh_getFilterChanges', - params: 1 - }); - - return [ - newFilter, - uninstallFilter, - getLogs, - poll - ]; -}; - -module.exports = { - eth: eth, - shh: shh -}; - - -},{"./method":24}],32:[function(require,module,exports){ +},{"../contracts/SmartExchange.json":3,"../web3":22,"./contract":25,"./iban":32,"./namereg":41}],45:[function(require,module,exports){ -},{}],33:[function(require,module,exports){ +},{}],46:[function(require,module,exports){ ;(function (root, factory) { if (typeof exports === "object") { // CommonJS @@ -5569,7 +6735,7 @@ module.exports = { return CryptoJS; })); -},{}],34:[function(require,module,exports){ +},{}],47:[function(require,module,exports){ ;(function (root, factory, undef) { if (typeof exports === "object") { // CommonJS @@ -5893,7 +7059,7 @@ module.exports = { return CryptoJS.SHA3; })); -},{"./core":33,"./x64-core":35}],35:[function(require,module,exports){ +},{"./core":46,"./x64-core":48}],48:[function(require,module,exports){ ;(function (root, factory) { if (typeof exports === "object") { // CommonJS @@ -6198,7 +7364,7 @@ module.exports = { return CryptoJS; })); -},{"./core":33}],"bignumber.js":[function(require,module,exports){ +},{"./core":46}],"bignumber.js":[function(require,module,exports){ 'use strict'; module.exports = BigNumber; // jshint ignore:line @@ -6206,13 +7372,16 @@ module.exports = BigNumber; // jshint ignore:line },{}],"web3":[function(require,module,exports){ var web3 = require('./lib/web3'); +var namereg = require('./lib/web3/namereg'); web3.providers.HttpProvider = require('./lib/web3/httpprovider'); web3.providers.IpcProvider = require('./lib/web3/ipcprovider'); web3.eth.contract = require('./lib/web3/contract'); -web3.eth.namereg = require('./lib/web3/namereg'); +web3.eth.namereg = namereg.namereg; +web3.eth.ibanNamereg = namereg.ibanNamereg; web3.eth.sendIBANTransaction = require('./lib/web3/transfer'); +web3.eth.iban = require('./lib/web3/iban'); // dont override global variable if (typeof window !== 'undefined' && typeof window.web3 === 'undefined') { @@ -6222,6 +7391,6 @@ if (typeof window !== 'undefined' && typeof window.web3 === 'undefined') { module.exports = web3; -},{"./lib/web3":9,"./lib/web3/contract":12,"./lib/web3/httpprovider":20,"./lib/web3/ipcprovider":22,"./lib/web3/namereg":25,"./lib/web3/transfer":30}]},{},["web3"]) +},{"./lib/web3":22,"./lib/web3/contract":25,"./lib/web3/httpprovider":31,"./lib/web3/iban":32,"./lib/web3/ipcprovider":33,"./lib/web3/namereg":41,"./lib/web3/transfer":44}]},{},["web3"]) //# sourceMappingURL=web3-light.js.map ` -- cgit