added providers back to Web3.providers

This commit is contained in:
Fabian Vogelsteller 2017-03-02 18:45:33 +01:00
parent 29f91e4830
commit 4a090ff371
No known key found for this signature in database
GPG Key ID: E51EADA77F1A4124
23 changed files with 292 additions and 311 deletions

View File

@ -62,8 +62,12 @@ var packages = [{
src: './packages/web3-bzz/src/index.js' src: './packages/web3-bzz/src/index.js'
},{ },{
fileName: 'web3-eth-iban', fileName: 'web3-eth-iban',
expose: 'Iban', expose: 'EthIban',
src: './packages/web3-eth-iban/src/index.js' src: './packages/web3-eth-iban/src/index.js'
},{
fileName: 'web3-eth-abi',
expose: 'EthAbi',
src: './packages/web3-eth-abi/src/index.js'
}]; }];
var browserifyOptions = { var browserifyOptions = {

View File

@ -1,38 +1,259 @@
/* /*
This file is part of web3.js. This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify web3.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 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 the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
web3.js is distributed in the hope that it will be useful, web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details. GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>. along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/ */
/** /**
* @file contract.js * @file coder.js
* * @author Marek Kotewicz <marek@ethcore.io>
* To initialize a contract use: * @date 2015
*
* var Contract = require('web3-eth-contract');
* Contract.prototype._eth = needsAEthInstance;
* var contract = new Contract(abi, address, ...);
*
* @author Fabian Vogelsteller <fabian@frozeman.de>
* @date 2016
*/ */
var f = require('./types/formatters');
"use strict"; var SolidityTypeAddress = require('./types/address');
var SolidityTypeBool = require('./types/bool');
var SolidityTypeInt = require('./types/int');
var SolidityTypeUInt = require('./types/uint');
var SolidityTypeDynamicBytes = require('./types/dynamicbytes');
var SolidityTypeString = require('./types/string');
var SolidityTypeBytes = require('./types/bytes');
var solidity = require('./solidity/coder'); var isDynamic = function (solidityType, type) {
return solidityType.isDynamicType(type) ||
module.exports = { solidityType.isDynamicArray(type);
solidity: solidity
}; };
/**
* SolidityCoder prototype should be used to encode/decode solidity params of any type
*/
var SolidityCoder = function (types) {
this._types = types;
};
/**
* This method should be used to transform type to SolidityType
*
* @method _requireType
* @param {String} type
* @returns {SolidityType}
* @throws {Error} throws if no matching type is found
*/
SolidityCoder.prototype._requireType = function (type) {
var solidityType = this._types.filter(function (t) {
return t.isType(type);
})[0];
if (!solidityType) {
throw Error('invalid solidity type!: ' + type);
}
return solidityType;
};
/**
* Should be used to encode plain param
*
* @method encodeParam
* @param {String} type
* @param {Object} plain param
* @return {String} encoded plain param
*/
SolidityCoder.prototype.encodeParam = function (type, param) {
return this.encodeParams([type], [param]);
};
/**
* Should be used to encode list of params
*
* @method encodeParams
* @param {Array} types
* @param {Array} params
* @return {String} encoded list of params
*/
SolidityCoder.prototype.encodeParams = function (types, params) {
var solidityTypes = this.getSolidityTypes(types);
var encodeds = solidityTypes.map(function (solidityType, index) {
return solidityType.encode(params[index], types[index]);
});
var dynamicOffset = solidityTypes.reduce(function (acc, solidityType, index) {
var staticPartLength = solidityType.staticPartLength(types[index]);
var roundedStaticPartLength = Math.floor((staticPartLength + 31) / 32) * 32;
return acc + (isDynamic(solidityTypes[index], types[index]) ?
32 :
roundedStaticPartLength);
}, 0);
var result = this.encodeMultiWithOffset(types, solidityTypes, encodeds, dynamicOffset);
return result;
};
SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, encodeds, dynamicOffset) {
var result = "";
var self = this;
types.forEach(function (type, i) {
if (isDynamic(solidityTypes[i], types[i])) {
result += f.formatInputInt(dynamicOffset).encode();
var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
} else {
// don't add length to dynamicOffset. it's already counted
result += self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
}
// TODO: figure out nested arrays
});
types.forEach(function (type, i) {
if (isDynamic(solidityTypes[i], types[i])) {
var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
result += e;
}
});
return result;
};
// TODO: refactor whole encoding!
SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded, offset) {
var self = this;
if (solidityType.isDynamicArray(type)) {
return (function () {
// offset was already set
var nestedName = solidityType.nestedName(type);
var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
var result = encoded[0];
(function () {
var previousLength = 2; // in int
if (solidityType.isDynamicArray(nestedName)) {
for (var i = 1; i < encoded.length; i++) {
previousLength += +(encoded[i - 1])[0] || 0;
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
}
}
})();
// first element is length, skip it
(function () {
for (var i = 0; i < encoded.length - 1; i++) {
var additionalOffset = result / 2;
result += self.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset);
}
})();
return result;
})();
} else if (solidityType.isStaticArray(type)) {
return (function () {
var nestedName = solidityType.nestedName(type);
var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
var result = "";
if (solidityType.isDynamicArray(nestedName)) {
(function () {
var previousLength = 0; // in int
for (var i = 0; i < encoded.length; i++) {
// calculate length of previous item
previousLength += +(encoded[i - 1] || [])[0] || 0;
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
}
})();
}
(function () {
for (var i = 0; i < encoded.length; i++) {
var additionalOffset = result / 2;
result += self.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset);
}
})();
return result;
})();
}
return encoded;
};
/**
* Should be used to decode bytes to plain param
*
* @method decodeParam
* @param {String} type
* @param {String} bytes
* @return {Object} plain param
*/
SolidityCoder.prototype.decodeParam = function (type, bytes) {
return this.decodeParams([type], bytes)[0];
};
/**
* Should be used to decode list of params
*
* @method decodeParam
* @param {Array} types
* @param {String} bytes
* @return {Array} array of plain params
*/
SolidityCoder.prototype.decodeParams = function (types, bytes) {
var solidityTypes = this.getSolidityTypes(types);
var offsets = this.getOffsets(types, solidityTypes);
return solidityTypes.map(function (solidityType, index) {
return solidityType.decode(bytes, offsets[index], types[index], index);
});
};
SolidityCoder.prototype.getOffsets = function (types, solidityTypes) {
var lengths = solidityTypes.map(function (solidityType, index) {
return solidityType.staticPartLength(types[index]);
});
for (var i = 1; i < lengths.length; i++) {
// sum with length of previous element
lengths[i] += lengths[i - 1];
}
return lengths.map(function (length, index) {
// remove the current length, so the length is sum of previous elements
var staticPartLength = solidityTypes[index].staticPartLength(types[index]);
return length - staticPartLength;
});
};
SolidityCoder.prototype.getSolidityTypes = function (types) {
var self = this;
return types.map(function (type) {
return self._requireType(type);
});
};
var coder = new SolidityCoder([
new SolidityTypeAddress(),
new SolidityTypeBool(),
new SolidityTypeInt(),
new SolidityTypeUInt(),
new SolidityTypeDynamicBytes(),
new SolidityTypeBytes(),
new SolidityTypeString()
]);
module.exports = coder;

View File

@ -1,259 +0,0 @@
/*
This file is part of web3.js.
web3.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.
web3.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 web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file coder.js
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
var f = require('./formatters');
var SolidityTypeAddress = require('./address');
var SolidityTypeBool = require('./bool');
var SolidityTypeInt = require('./int');
var SolidityTypeUInt = require('./uint');
var SolidityTypeDynamicBytes = require('./dynamicbytes');
var SolidityTypeString = require('./string');
var SolidityTypeBytes = require('./bytes');
var isDynamic = function (solidityType, type) {
return solidityType.isDynamicType(type) ||
solidityType.isDynamicArray(type);
};
/**
* SolidityCoder prototype should be used to encode/decode solidity params of any type
*/
var SolidityCoder = function (types) {
this._types = types;
};
/**
* This method should be used to transform type to SolidityType
*
* @method _requireType
* @param {String} type
* @returns {SolidityType}
* @throws {Error} throws if no matching type is found
*/
SolidityCoder.prototype._requireType = function (type) {
var solidityType = this._types.filter(function (t) {
return t.isType(type);
})[0];
if (!solidityType) {
throw Error('invalid solidity type!: ' + type);
}
return solidityType;
};
/**
* Should be used to encode plain param
*
* @method encodeParam
* @param {String} type
* @param {Object} plain param
* @return {String} encoded plain param
*/
SolidityCoder.prototype.encodeParam = function (type, param) {
return this.encodeParams([type], [param]);
};
/**
* Should be used to encode list of params
*
* @method encodeParams
* @param {Array} types
* @param {Array} params
* @return {String} encoded list of params
*/
SolidityCoder.prototype.encodeParams = function (types, params) {
var solidityTypes = this.getSolidityTypes(types);
var encodeds = solidityTypes.map(function (solidityType, index) {
return solidityType.encode(params[index], types[index]);
});
var dynamicOffset = solidityTypes.reduce(function (acc, solidityType, index) {
var staticPartLength = solidityType.staticPartLength(types[index]);
var roundedStaticPartLength = Math.floor((staticPartLength + 31) / 32) * 32;
return acc + (isDynamic(solidityTypes[index], types[index]) ?
32 :
roundedStaticPartLength);
}, 0);
var result = this.encodeMultiWithOffset(types, solidityTypes, encodeds, dynamicOffset);
return result;
};
SolidityCoder.prototype.encodeMultiWithOffset = function (types, solidityTypes, encodeds, dynamicOffset) {
var result = "";
var self = this;
types.forEach(function (type, i) {
if (isDynamic(solidityTypes[i], types[i])) {
result += f.formatInputInt(dynamicOffset).encode();
var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
} else {
// don't add length to dynamicOffset. it's already counted
result += self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
}
// TODO: figure out nested arrays
});
types.forEach(function (type, i) {
if (isDynamic(solidityTypes[i], types[i])) {
var e = self.encodeWithOffset(types[i], solidityTypes[i], encodeds[i], dynamicOffset);
dynamicOffset += e.length / 2;
result += e;
}
});
return result;
};
// TODO: refactor whole encoding!
SolidityCoder.prototype.encodeWithOffset = function (type, solidityType, encoded, offset) {
var self = this;
if (solidityType.isDynamicArray(type)) {
return (function () {
// offset was already set
var nestedName = solidityType.nestedName(type);
var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
var result = encoded[0];
(function () {
var previousLength = 2; // in int
if (solidityType.isDynamicArray(nestedName)) {
for (var i = 1; i < encoded.length; i++) {
previousLength += +(encoded[i - 1])[0] || 0;
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
}
}
})();
// first element is length, skip it
(function () {
for (var i = 0; i < encoded.length - 1; i++) {
var additionalOffset = result / 2;
result += self.encodeWithOffset(nestedName, solidityType, encoded[i + 1], offset + additionalOffset);
}
})();
return result;
})();
} else if (solidityType.isStaticArray(type)) {
return (function () {
var nestedName = solidityType.nestedName(type);
var nestedStaticPartLength = solidityType.staticPartLength(nestedName);
var result = "";
if (solidityType.isDynamicArray(nestedName)) {
(function () {
var previousLength = 0; // in int
for (var i = 0; i < encoded.length; i++) {
// calculate length of previous item
previousLength += +(encoded[i - 1] || [])[0] || 0;
result += f.formatInputInt(offset + i * nestedStaticPartLength + previousLength * 32).encode();
}
})();
}
(function () {
for (var i = 0; i < encoded.length; i++) {
var additionalOffset = result / 2;
result += self.encodeWithOffset(nestedName, solidityType, encoded[i], offset + additionalOffset);
}
})();
return result;
})();
}
return encoded;
};
/**
* Should be used to decode bytes to plain param
*
* @method decodeParam
* @param {String} type
* @param {String} bytes
* @return {Object} plain param
*/
SolidityCoder.prototype.decodeParam = function (type, bytes) {
return this.decodeParams([type], bytes)[0];
};
/**
* Should be used to decode list of params
*
* @method decodeParam
* @param {Array} types
* @param {String} bytes
* @return {Array} array of plain params
*/
SolidityCoder.prototype.decodeParams = function (types, bytes) {
var solidityTypes = this.getSolidityTypes(types);
var offsets = this.getOffsets(types, solidityTypes);
return solidityTypes.map(function (solidityType, index) {
return solidityType.decode(bytes, offsets[index], types[index], index);
});
};
SolidityCoder.prototype.getOffsets = function (types, solidityTypes) {
var lengths = solidityTypes.map(function (solidityType, index) {
return solidityType.staticPartLength(types[index]);
});
for (var i = 1; i < lengths.length; i++) {
// sum with length of previous element
lengths[i] += lengths[i - 1];
}
return lengths.map(function (length, index) {
// remove the current length, so the length is sum of previous elements
var staticPartLength = solidityTypes[index].staticPartLength(types[index]);
return length - staticPartLength;
});
};
SolidityCoder.prototype.getSolidityTypes = function (types) {
var self = this;
return types.map(function (type) {
return self._requireType(type);
});
};
var coder = new SolidityCoder([
new SolidityTypeAddress(),
new SolidityTypeBool(),
new SolidityTypeInt(),
new SolidityTypeUInt(),
new SolidityTypeDynamicBytes(),
new SolidityTypeBytes(),
new SolidityTypeString()
]);
module.exports = coder;

View File

@ -37,7 +37,7 @@ var utils = require('web3-utils');
var Subscription = require('web3-core-subscriptions').subscription; var Subscription = require('web3-core-subscriptions').subscription;
var formatters = require('web3-core-helpers').formatters; var formatters = require('web3-core-helpers').formatters;
var promiEvent = require('web3-core-promiEvent'); var promiEvent = require('web3-core-promiEvent');
var coder = require('web3-eth-abi').solidity; var coder = require('web3-eth-abi');
/** /**

View File

@ -11,6 +11,7 @@
"web3-core-subscriptions": "^1.0.0", "web3-core-subscriptions": "^1.0.0",
"web3-core-method": "^1.0.0", "web3-core-method": "^1.0.0",
"web3-eth-iban": "^1.0.0", "web3-eth-iban": "^1.0.0",
"web3-eth-abi": "^1.0.0",
"web3-eth-contract": "^1.0.0", "web3-eth-contract": "^1.0.0",
"web3-utils": "^1.0.0", "web3-utils": "^1.0.0",
"underscore": "^1.8.3" "underscore": "^1.8.3"

View File

@ -28,6 +28,7 @@ var helpers = require('web3-core-helpers');
var Subscriptions = require('web3-core-subscriptions').subscriptions; var Subscriptions = require('web3-core-subscriptions').subscriptions;
var utils = require('web3-utils'); var utils = require('web3-utils');
var Method = require('web3-core-method'); var Method = require('web3-core-method');
var abi = require('web3-eth-abi');
var Contract = require('web3-eth-contract'); var Contract = require('web3-eth-contract');
var Iban = require('web3-eth-iban'); var Iban = require('web3-eth-iban');
@ -77,6 +78,9 @@ var Eth = function Eth() {
// add IBAN // add IBAN
this.Iban = Iban; this.Iban = Iban;
// add ABI
this.abi = abi;
// add guess chain // add guess chain
this.net.getNetworkType = getNetworkType.bind(this); this.net.getNetworkType = getNetworkType.bind(this);

View File

@ -37,6 +37,12 @@ var Bzz = require('../packages/web3-bzz');
var utils = require('../packages/web3-utils'); var utils = require('../packages/web3-utils');
// providers
var providers = {
WebsocketProvider: require('../packages/web3-providers-ws'),
HttpProvider: require('../packages/web3-providers-http'),
IpcProvider: require('../packages/web3-providers-ipc')
}
var Web3 = function Web3() { var Web3 = function Web3() {
@ -45,6 +51,9 @@ var Web3 = function Web3() {
core.packageInit(this, arguments); core.packageInit(this, arguments);
this.providers = providers;
this.eth = new Eth(this); this.eth = new Eth(this);
this.personal = new Personal(this); // move to -> web3.eth.accounts this.personal = new Personal(this); // move to -> web3.eth.accounts
this.shh = new Shh(this); this.shh = new Shh(this);
@ -63,6 +72,7 @@ var Web3 = function Web3() {
}; };
Web3.providers = providers;
core.addProviders(Web3); core.addProviders(Web3);

View File

@ -1,6 +1,6 @@
var chai = require('chai'); var chai = require('chai');
var assert = chai.assert; var assert = chai.assert;
var coder = require('../packages/web3-eth-abi').solidity; var coder = require('../packages/web3-eth-abi');
// TODO check line 108 again! // TODO check line 108 again!

View File

@ -1,6 +1,6 @@
var chai = require('chai'); var chai = require('chai');
var assert = chai.assert; var assert = chai.assert;
var coder = require('../packages/web3-eth-abi').solidity; var coder = require('../packages/web3-eth-abi');
describe('lib/solidity/coder', function () { describe('lib/solidity/coder', function () {

View File

@ -0,0 +1,21 @@
var chai = require('chai');
var assert = chai.assert;
var formatters = require('../packages/web3-eth-abi/src/types/formatters.js');
var Param = require('../packages/web3-eth-abi/src/types/param');
var tests = [
{ input: 1, result: new Param('0000000000000000000000000000000000000000000000000000000000000001') },
{ input: 1.1, result: new Param('0000000000000000000000000000000000000000000000000000000000000001') },
{ input: -1.1, result: new Param('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') },
{ input: -1, result: new Param('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') }
];
describe('formatters', function () {
describe('formatInputInt', function () {
tests.forEach(function(test){
it('should return the correct value', function () {
assert.deepEqual(formatters.formatInputInt(test.input), test.result);
});
});
});
});

View File

@ -1,21 +0,0 @@
var chai = require('chai');
var assert = chai.assert;
var formatters = require('../packages/web3-eth-abi/src/solidity/formatters.js');
var SolidityParam = require('../packages/web3-eth-abi/src/solidity/param');
var tests = [
{ input: 1, result: new SolidityParam('0000000000000000000000000000000000000000000000000000000000000001') },
{ input: 1.1, result: new SolidityParam('0000000000000000000000000000000000000000000000000000000000000001') },
{ input: -1.1, result: new SolidityParam('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') },
{ input: -1, result: new SolidityParam('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') }
];
describe('formatters', function () {
describe('formatInputInt', function () {
tests.forEach(function(test){
it('should return the correct value', function () {
assert.deepEqual(formatters.formatInputInt(test.input), test.result);
});
});
});
});