├── test ├── mocha.opts ├── node │ ├── app.js │ └── package.json ├── helpers │ ├── FakeQtNavigator.js │ ├── test.utils.js │ ├── FakeIpcRequest.js │ ├── FakeHttpProvider2.js │ ├── FakeXHR2.js │ ├── FakeXMLHttpRequest.js │ ├── test.method.js │ └── FakeHttpProvider.js ├── web3.db.getHex.js ├── web3.eth.getWork.js ├── web3.db.getString.js ├── web3.net.methods.js ├── web3.eth.defaultBlock.js ├── web3.db.putHex.js ├── web3.db.putString.js ├── errors.js ├── web3.personal.methods.js ├── web3.db.methods.js ├── web3.personal.newAccount.js ├── utils.toDecimal.js ├── web3.eth.getCompilers.js ├── jsonrpc.id.js ├── web3.fromICAP.js ├── iban.toAddress.js ├── web3.eth.estimateGas.js ├── utils.isFunction.js ├── web3.eth.submitWork.js ├── utils.isString.js ├── iban.createIndirect.js ├── utils.fromUtf8.js ├── web3.personal.unlockAccount.js ├── utils.toUtf8.js ├── web3.eth.getBlockUncleCount.js ├── formatters.inputDefaultBlockFormatter.js ├── utils.isJson.js ├── utils.isStrictAddress.js ├── web3.sha3.js ├── web3.eth.getBlockTransactionCount.js ├── utils.isBigNumber.js ├── web3.methods.js ├── web3.shh.post.js ├── sha3.js ├── utils.fromAscii.js ├── utils.toAscii.js ├── soldity.formatters.formatInputInt.js ├── formatters.outputPostFormatter.js ├── iban.fromAddress.js ├── formatters.inputPostFormatter.js ├── web3.eth.call.js ├── web3.eth.getCode.js ├── utils.isAddress.js ├── method.formatInput.js ├── web3.eth.protocolVersion.js ├── utils.isChecksumAddress.js ├── web3.eth.mining.js ├── web3.net.listening.js ├── web3.net.peerCount.js ├── web3.shh.methods.js ├── method.formatOutput.js ├── method.getCall.js ├── utils.fromWei.js ├── web3.eth.gasPrice.js ├── web3.personal.listAccounts.js ├── web3.eth.accounts.js ├── web3.eth.coinbase.js ├── utils.extractDisplayName.js ├── iban.isValid.js ├── web3.eth.hashRate.js ├── jsonrpc.toPayload.js ├── method.validateArgs.js ├── requestmanager.js ├── web3.eth.methods.js ├── web3.eth.getTransaction.js ├── method.extractCallback.js ├── formatters.inputAddressFormatter.js ├── utils.extractTypeName.js ├── jsonrpc.toBatchPayload.js ├── web3.shh.filter.js ├── web3.eth.sendIBANTransaction.js ├── web3.eth.getTransactionFromBlock.js ├── web3.eth.getStorageAt.js ├── httpprovider.js ├── web3.eth.getSyncing.js ├── ipcprovider.js ├── utils.toTwosComplement.js ├── utils.fromDecimal.js ├── utils.toWei.js ├── web3.eth.blockNumber.js ├── bloom.testAddress.js ├── formatters.outputLogFormatter.js ├── utils.toBigNumber.js ├── web3.eth.isSyncing.js ├── formatters.outputTransactionFormatter.js ├── bloom.testTopic.js ├── utils.toHex.js ├── async.js ├── web3.eth.getTransactionReceipt.js ├── web3.eth.getBalance.js ├── web3.extend.js └── formatters.inputTransactionFormatter.js ├── lib ├── version.json ├── utils │ ├── browser-bn.js │ ├── browser-xhr.js │ ├── sha3.js │ ├── config.js │ └── bloom.js ├── web3 │ ├── settings.js │ ├── namereg.js │ ├── extend.js │ ├── methods │ │ ├── net.js │ │ ├── db.js │ │ ├── watches.js │ │ ├── personal.js │ │ └── swarm.js │ ├── errors.js │ ├── batch.js │ ├── jsonrpc.js │ ├── transfer.js │ ├── allevents.js │ └── syncing.js ├── solidity │ ├── string.js │ ├── dynamicbytes.js │ ├── bool.js │ ├── address.js │ ├── bytes.js │ ├── int.js │ ├── real.js │ ├── uint.js │ └── ureal.js └── contracts │ ├── ICAPRegistrar.json │ └── SmartExchange.json ├── .versions ├── circle.yml ├── .bowerrc ├── .npmignore ├── index.js ├── .editorconfig ├── example ├── node-app.js ├── balance.html ├── contract.html ├── contract_array.html └── event_inc.html ├── .travis.yml ├── .jshintrc ├── .gitignore ├── package-init.js ├── LICENSE ├── package.js ├── bower.json ├── package.json └── gulpfile.js /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --reporter spec 2 | -------------------------------------------------------------------------------- /lib/version.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.20.4" 3 | } 4 | -------------------------------------------------------------------------------- /.versions: -------------------------------------------------------------------------------- 1 | ethereum:web3@0.20.2 2 | meteor@1.7.0 3 | underscore@1.0.10 4 | -------------------------------------------------------------------------------- /circle.yml: -------------------------------------------------------------------------------- 1 | test: 2 | override: 3 | - npm run test && npm run lint -------------------------------------------------------------------------------- /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "bower", 3 | "cwd": "./", 4 | "analytics": false 5 | } 6 | -------------------------------------------------------------------------------- /test/node/app.js: -------------------------------------------------------------------------------- 1 | var web3 = require('web3'); 2 | 3 | console.log(web3.version.api); 4 | 5 | 6 | -------------------------------------------------------------------------------- /lib/utils/browser-bn.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = BigNumber; // jshint ignore:line 4 | 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | example/js 2 | node_modules 3 | test 4 | .gitignore 5 | .editorconfig 6 | .travis.yml 7 | .npmignore 8 | component.json 9 | testling.html -------------------------------------------------------------------------------- /lib/web3/settings.js: -------------------------------------------------------------------------------- 1 | 2 | 3 | var Settings = function () { 4 | this.defaultBlock = 'latest'; 5 | this.defaultAccount = undefined; 6 | }; 7 | 8 | module.exports = Settings; 9 | 10 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var Web3 = require('./lib/web3'); 2 | 3 | // dont override global variable 4 | if (typeof window !== 'undefined' && typeof window.Web3 === 'undefined') { 5 | window.Web3 = Web3; 6 | } 7 | 8 | module.exports = Web3; 9 | -------------------------------------------------------------------------------- /test/helpers/FakeQtNavigator.js: -------------------------------------------------------------------------------- 1 | 2 | var navigator = { 3 | qt: { 4 | callMethod: function (payload) { 5 | return "{}"; 6 | } 7 | } 8 | }; 9 | 10 | module.exports = navigator; 11 | 12 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false -------------------------------------------------------------------------------- /lib/utils/browser-xhr.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // go env doesn't have and need XMLHttpRequest 4 | if (typeof XMLHttpRequest === 'undefined') { 5 | exports.XMLHttpRequest = {}; 6 | } else { 7 | exports.XMLHttpRequest = XMLHttpRequest; // jshint ignore:line 8 | } 9 | 10 | -------------------------------------------------------------------------------- /test/node/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "app.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC", 11 | "dependencies": { 12 | "web3": "ethereum/web3.js#master" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /test/web3.db.getHex.js: -------------------------------------------------------------------------------- 1 | var testMethod = require('./helpers/test.method.js'); 2 | 3 | var method = 'getHex'; 4 | 5 | var tests = [{ 6 | args: ['myDB', 'myKey'], 7 | formattedArgs: ['myDB', 'myKey'], 8 | result: '0xf', 9 | formattedResult: '0xf', 10 | call: 'db_'+ method 11 | }]; 12 | 13 | testMethod.runTests('db', method, tests); 14 | 15 | -------------------------------------------------------------------------------- /example/node-app.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var Web3 = require('../index.js'); 4 | var web3 = new Web3(); 5 | 6 | web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545')); 7 | 8 | var coinbase = web3.eth.coinbase; 9 | console.log(coinbase); 10 | 11 | var balance = web3.eth.getBalance(coinbase); 12 | console.log(balance.toString(10)); 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4.1" 4 | - "4.0" 5 | before_script: 6 | - npm install 7 | - npm install jshint 8 | - export DISPLAY=:99.0 9 | - sh -e /etc/init.d/xvfb start 10 | script: 11 | - "jshint *.js lib" 12 | after_script: 13 | - npm run-script build 14 | - npm run-script test-coveralls 15 | - cd test/node && npm install && node app.js 16 | 17 | -------------------------------------------------------------------------------- /test/web3.eth.getWork.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var testMethod = require('./helpers/test.method.js'); 4 | 5 | var method = 'getWork'; 6 | 7 | var tests = [{ 8 | args: [], 9 | formattedArgs: [], 10 | result: true, 11 | formattedResult: true, 12 | call: 'eth_'+ method 13 | }]; 14 | 15 | testMethod.runTests('eth', method, tests); 16 | 17 | -------------------------------------------------------------------------------- /test/web3.db.getString.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var testMethod = require('./helpers/test.method.js'); 3 | 4 | var method = 'getString'; 5 | 6 | var tests = [{ 7 | args: ['myDB', 'myKey'], 8 | formattedArgs: ['myDB', 'myKey'], 9 | result: 'myValue', 10 | formattedResult: 'myValue', 11 | call: 'db_'+ method 12 | }]; 13 | 14 | testMethod.runTests('db', method, tests); 15 | 16 | -------------------------------------------------------------------------------- /test/web3.net.methods.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index.js'); 4 | var web3 = new Web3(); 5 | var u = require('./helpers/test.utils.js'); 6 | 7 | describe('web3.net', function() { 8 | describe('methods', function() { 9 | u.propertyExists(web3.net, 'listening'); 10 | u.propertyExists(web3.net, 'peerCount'); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /test/web3.eth.defaultBlock.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | 6 | describe('web3.eth', function () { 7 | describe('defaultBlock', function () { 8 | it('should check if defaultBlock is set to proper value', function () { 9 | assert.equal(web3.eth.defaultBlock, 'latest'); 10 | }); 11 | }); 12 | }); 13 | 14 | -------------------------------------------------------------------------------- /test/web3.db.putHex.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var testMethod = require('./helpers/test.method.js'); 4 | 5 | var method = 'putHex'; 6 | 7 | var tests = [{ 8 | args: ['myDB', 'myKey', '0xb'], 9 | formattedArgs: ['myDB', 'myKey', '0xb'], 10 | result: true, 11 | formattedResult: true, 12 | call: 'db_'+ method 13 | }]; 14 | 15 | testMethod.runTests('db', method, tests); 16 | 17 | -------------------------------------------------------------------------------- /test/web3.db.putString.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var testMethod = require('./helpers/test.method.js'); 4 | 5 | var method = 'putString'; 6 | 7 | var tests = [{ 8 | args: ['myDB', 'myKey', 'myValue'], 9 | formattedArgs: ['myDB', 'myKey', 'myValue'], 10 | result: true, 11 | formattedResult: true, 12 | call: 'db_'+ method 13 | }]; 14 | 15 | testMethod.runTests('db', method, tests); 16 | 17 | -------------------------------------------------------------------------------- /test/errors.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var errors = require('../lib/web3/errors'); 4 | 5 | describe('lib/web3/method', function () { 6 | describe('getCall', function () { 7 | 8 | for(var key in errors) { 9 | it('should return and error', function () { 10 | 11 | assert.instanceOf(errors[key](), Error); 12 | }); 13 | } 14 | 15 | }); 16 | }); 17 | 18 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "browserify": true, 3 | "bitwise": true, 4 | "camelcase": true, 5 | "eqeqeq": true, 6 | "freeze": true, 7 | "funcscope": false, 8 | "maxcomplexity": 5, /* our target is 3! */ 9 | "maxdepth": 3, 10 | "maxerr": 50, 11 | /*"maxlen": 80*/ /*this should be our goal*/ 12 | /*"maxparams": 3,*/ 13 | "nonew": true, 14 | "unused": true, 15 | "undef": true, 16 | "predef": [ 17 | "console" 18 | ] 19 | } 20 | -------------------------------------------------------------------------------- /test/web3.personal.methods.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index.js'); 4 | var web3 = new Web3(); 5 | var u = require('./helpers/test.utils.js'); 6 | 7 | describe('web3.net', function() { 8 | describe('methods', function() { 9 | u.propertyExists(web3.personal, 'listAccounts'); 10 | u.methodExists(web3.personal, 'newAccount'); 11 | u.methodExists(web3.personal, 'unlockAccount'); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /test/web3.db.methods.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index.js'); 4 | var web3 = new Web3(); 5 | var u = require('./helpers/test.utils.js'); 6 | 7 | describe('web3.db', function() { 8 | describe('methods', function() { 9 | u.methodExists(web3.db, 'putHex'); 10 | u.methodExists(web3.db, 'getHex'); 11 | u.methodExists(web3.db, 'putString'); 12 | u.methodExists(web3.db, 'getString'); 13 | }); 14 | }); 15 | 16 | -------------------------------------------------------------------------------- /test/web3.personal.newAccount.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var testMethod = require('./helpers/test.method.js'); 4 | 5 | var method = 'newAccount'; 6 | 7 | var tests = [{ 8 | args: ['P@ssw0rd!'], 9 | formattedArgs: ['P@ssw0rd!'], 10 | result: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 11 | formattedResult: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 12 | call: 'personal_'+ method 13 | }]; 14 | 15 | testMethod.runTests('personal', method, tests); 16 | -------------------------------------------------------------------------------- /test/utils.toDecimal.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var utils = require('../lib/utils/utils.js'); 3 | 4 | describe('lib/utils/utils', function () { 5 | describe('toDecimal', function () { 6 | it('should return the correct value', function () { 7 | 8 | assert.equal(utils.toDecimal("0x3e8"), '1000'); 9 | // allow compatiblity 10 | assert.equal(utils.toDecimal(100000), '100000'); 11 | assert.equal(utils.toDecimal('100000'), '100000'); 12 | }); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile ~/.gitignore_global 6 | 7 | *.swp 8 | /coverage 9 | /tmp 10 | */**/*un~ 11 | *un~ 12 | .DS_Store 13 | */**/.DS_Store 14 | ethereum/ethereum 15 | ethereal/ethereal 16 | example/js 17 | node_modules 18 | bower_components 19 | npm-debug.log 20 | /bower 21 | .npm/ 22 | packages/ 23 | -------------------------------------------------------------------------------- /test/web3.eth.getCompilers.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var testMethod = require('./helpers/test.method.js'); 4 | 5 | var method = 'getCompilers'; 6 | 7 | 8 | var tests = [{ 9 | args: [], 10 | formattedArgs: [], 11 | result: ['solidity'], 12 | formattedResult: ['solidity'], 13 | call: 'eth_'+ method 14 | },{ 15 | args: [], 16 | formattedArgs: [], 17 | result: ['solidity'], 18 | formattedResult: ['solidity'], 19 | call: 'eth_'+ method 20 | }]; 21 | 22 | testMethod.runTests('eth', method, tests); 23 | 24 | -------------------------------------------------------------------------------- /test/jsonrpc.id.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Jsonrpc = require('../lib/web3/jsonrpc'); 4 | 5 | describe('lib/web3/jsonrpc', function () { 6 | describe('id', function () { 7 | it('should increment the id', function () { 8 | 9 | // given 10 | var method = 'm'; 11 | 12 | // when 13 | var p1 = Jsonrpc.toPayload(method); 14 | var p2 = Jsonrpc.toPayload(method); 15 | 16 | // then 17 | assert.equal(p2.id, p1.id + 1); 18 | }); 19 | }); 20 | }); 21 | 22 | -------------------------------------------------------------------------------- /package-init.js: -------------------------------------------------------------------------------- 1 | /* jshint ignore:start */ 2 | 3 | 4 | // Browser environment 5 | if(typeof window !== 'undefined') { 6 | Web3 = (typeof window.Web3 !== 'undefined') ? window.Web3 : require('web3'); 7 | BigNumber = (typeof window.BigNumber !== 'undefined') ? window.BigNumber : require('bignumber.js'); 8 | } 9 | 10 | 11 | // Node environment 12 | if(typeof global !== 'undefined') { 13 | Web3 = (typeof global.Web3 !== 'undefined') ? global.Web3 : require('web3'); 14 | BigNumber = (typeof global.BigNumber !== 'undefined') ? global.BigNumber : require('bignumber.js'); 15 | } 16 | 17 | /* jshint ignore:end */ -------------------------------------------------------------------------------- /test/web3.fromICAP.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Web3 = require('../index'); 3 | var web3 = new Web3(); 4 | var assert = chai.assert; 5 | 6 | var tests = [ 7 | { direct: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', address: '00c5496aee77c1ba1f0854206a26dda82a81d6d8'} 8 | ]; 9 | 10 | describe('web3', function () { 11 | describe('fromICAP', function () { 12 | tests.forEach(function (test) { 13 | it('shoud transform iban to address: ' + test.address, function () { 14 | assert.deepEqual(web3.fromICAP(test.direct), test.address); 15 | }); 16 | }); 17 | }); 18 | }); 19 | 20 | -------------------------------------------------------------------------------- /lib/solidity/string.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | var SolidityTypeString = function () { 5 | this._inputFormatter = f.formatInputString; 6 | this._outputFormatter = f.formatOutputString; 7 | }; 8 | 9 | SolidityTypeString.prototype = new SolidityType({}); 10 | SolidityTypeString.prototype.constructor = SolidityTypeString; 11 | 12 | SolidityTypeString.prototype.isType = function (name) { 13 | return !!name.match(/^string(\[([0-9]*)\])*$/); 14 | }; 15 | 16 | SolidityTypeString.prototype.isDynamicType = function () { 17 | return true; 18 | }; 19 | 20 | module.exports = SolidityTypeString; 21 | -------------------------------------------------------------------------------- /test/iban.toAddress.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Iban = require('../lib/web3/iban.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { direct: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', address: '00c5496aee77c1ba1f0854206a26dda82a81d6d8'} 7 | ]; 8 | 9 | describe('lib/web3/iban', function () { 10 | describe('toAddress', function () { 11 | tests.forEach(function (test) { 12 | it('shoud transform iban to address: ' + test.address, function () { 13 | var iban = new Iban(test.direct); 14 | assert.deepEqual(iban.address(), test.address); 15 | }); 16 | }); 17 | }); 18 | }); 19 | 20 | -------------------------------------------------------------------------------- /test/web3.eth.estimateGas.js: -------------------------------------------------------------------------------- 1 | var web3 = require('../index'); 2 | var testMethod = require('./helpers/test.method.js'); 3 | 4 | var method = 'estimateGas'; 5 | 6 | var tests = [{ 7 | args: [{ 8 | to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', 9 | data: '0x23455654', 10 | gas: 11, 11 | gasPrice: 11 12 | }], 13 | formattedArgs: [{ 14 | to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', 15 | data: '0x23455654', 16 | gas: '0xb', 17 | gasPrice: '0xb' 18 | }], 19 | result: '0x31981', 20 | formattedResult: 203137, 21 | call: 'eth_'+ method 22 | }]; 23 | 24 | testMethod.runTests('eth', method, tests); 25 | 26 | -------------------------------------------------------------------------------- /test/utils.isFunction.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { func: function () {}, is: true}, 7 | { func: new Function(), is: true}, 8 | { func: 'function', is: false}, 9 | { func: {}, is: false} 10 | ]; 11 | 12 | describe('lib/utils/utils', function () { 13 | describe('isFunction', function () { 14 | tests.forEach(function (test) { 15 | it('shoud test if value ' + test.func + ' is function: ' + test.is, function () { 16 | assert.equal(utils.isFunction(test.func), test.is); 17 | }); 18 | }); 19 | }); 20 | }); 21 | 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This file is part of web3.js. 2 | 3 | web3.js is free software: you can redistribute it and/or modify 4 | it under the terms of the GNU Lesser General Public License as published by 5 | the Free Software Foundation, either version 3 of the License, or 6 | (at your option) any later version. 7 | 8 | web3.js is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 | GNU Lesser General Public License for more details. 12 | 13 | You should have received a copy of the GNU Lesser General Public License 14 | along with web3.js. If not, see . 15 | -------------------------------------------------------------------------------- /lib/solidity/dynamicbytes.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | var SolidityTypeDynamicBytes = function () { 5 | this._inputFormatter = f.formatInputDynamicBytes; 6 | this._outputFormatter = f.formatOutputDynamicBytes; 7 | }; 8 | 9 | SolidityTypeDynamicBytes.prototype = new SolidityType({}); 10 | SolidityTypeDynamicBytes.prototype.constructor = SolidityTypeDynamicBytes; 11 | 12 | SolidityTypeDynamicBytes.prototype.isType = function (name) { 13 | return !!name.match(/^bytes(\[([0-9]*)\])*$/); 14 | }; 15 | 16 | SolidityTypeDynamicBytes.prototype.isDynamicType = function () { 17 | return true; 18 | }; 19 | 20 | module.exports = SolidityTypeDynamicBytes; 21 | -------------------------------------------------------------------------------- /test/web3.eth.submitWork.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var testMethod = require('./helpers/test.method.js'); 4 | 5 | var method = 'submitWork'; 6 | 7 | var tests = [ 8 | { 9 | args: ['0x567890abcdef5555', '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', '0xcdef1234567890abcdef1234567890abcdef0x1234567890abcf1234567890ab'], 10 | formattedArgs: ['0x567890abcdef5555', '0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef', '0xcdef1234567890abcdef1234567890abcdef0x1234567890abcf1234567890ab'], 11 | result: true, 12 | formattedResult: true, 13 | call: 'eth_'+ method 14 | }]; 15 | 16 | testMethod.runTests('eth', method, tests); 17 | 18 | -------------------------------------------------------------------------------- /test/utils.isString.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { value: function () {}, is: false}, 7 | { value: new Function(), is: false}, 8 | { value: 'function', is: true}, 9 | { value: {}, is: false}, 10 | { value: new String('hello'), is: true} 11 | ]; 12 | 13 | describe('lib/utils/utils', function () { 14 | describe('isString', function () { 15 | tests.forEach(function (test) { 16 | it('shoud test if value ' + test.func + ' is string: ' + test.is, function () { 17 | assert.equal(utils.isString(test.value), test.is); 18 | }); 19 | }); 20 | }); 21 | }); 22 | 23 | -------------------------------------------------------------------------------- /lib/solidity/bool.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | /** 5 | * SolidityTypeBool is a prootype that represents bool type 6 | * It matches: 7 | * bool 8 | * bool[] 9 | * bool[4] 10 | * bool[][] 11 | * bool[3][] 12 | * bool[][6][], ... 13 | */ 14 | var SolidityTypeBool = function () { 15 | this._inputFormatter = f.formatInputBool; 16 | this._outputFormatter = f.formatOutputBool; 17 | }; 18 | 19 | SolidityTypeBool.prototype = new SolidityType({}); 20 | SolidityTypeBool.prototype.constructor = SolidityTypeBool; 21 | 22 | SolidityTypeBool.prototype.isType = function (name) { 23 | return !!name.match(/^bool(\[([0-9]*)\])*$/); 24 | }; 25 | 26 | module.exports = SolidityTypeBool; 27 | -------------------------------------------------------------------------------- /test/iban.createIndirect.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Iban = require('../lib/web3/iban.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { institution: 'XREG', identifier: 'GAVOFYORK', expected: 'XE81ETHXREGGAVOFYORK'} 7 | ]; 8 | 9 | describe('lib/web3/iban', function () { 10 | describe('createIndirect', function () { 11 | tests.forEach(function (test) { 12 | it('shoud create indirect iban: ' + test.expected, function () { 13 | assert.deepEqual(Iban.createIndirect({ 14 | institution: test.institution, 15 | identifier: test.identifier 16 | }), new Iban(test.expected)); 17 | }); 18 | }); 19 | }); 20 | }); 21 | 22 | -------------------------------------------------------------------------------- /lib/solidity/address.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | /** 5 | * SolidityTypeAddress is a prootype that represents address type 6 | * It matches: 7 | * address 8 | * address[] 9 | * address[4] 10 | * address[][] 11 | * address[3][] 12 | * address[][6][], ... 13 | */ 14 | var SolidityTypeAddress = function () { 15 | this._inputFormatter = f.formatInputInt; 16 | this._outputFormatter = f.formatOutputAddress; 17 | }; 18 | 19 | SolidityTypeAddress.prototype = new SolidityType({}); 20 | SolidityTypeAddress.prototype.constructor = SolidityTypeAddress; 21 | 22 | SolidityTypeAddress.prototype.isType = function (name) { 23 | return !!name.match(/address(\[([0-9]*)\])?/); 24 | }; 25 | 26 | module.exports = SolidityTypeAddress; 27 | -------------------------------------------------------------------------------- /test/utils.fromUtf8.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var BigNumber = require('bignumber.js'); 3 | var utils = require('../lib/utils/utils.js'); 4 | var assert = chai.assert; 5 | 6 | var tests = [ 7 | { value: 'myString', expected: '0x6d79537472696e67'}, 8 | { value: 'myString\x00', expected: '0x6d79537472696e67'}, 9 | { value: 'expected value\u0000\u0000\u0000', expected: '0x65787065637465642076616c7565'} 10 | ]; 11 | 12 | describe('lib/utils/utils', function () { 13 | describe('fromUtf8', function () { 14 | tests.forEach(function (test) { 15 | it('should turn ' + test.value + ' to ' + test.expected, function () { 16 | assert.strictEqual(utils.fromUtf8(test.value), test.expected); 17 | }); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /test/web3.personal.unlockAccount.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var testMethod = require('./helpers/test.method.js'); 4 | 5 | var method = 'unlockAccount'; 6 | 7 | var tests = [{ 8 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'P@ssw0rd!'], 9 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'P@ssw0rd!', null], 10 | result: true, 11 | formattedResult: true, 12 | call: 'personal_'+ method 13 | },{ 14 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'P@ssw0rd!', 10], 15 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 'P@ssw0rd!', 10], 16 | result: true, 17 | formattedResult: true, 18 | call: 'personal_'+ method 19 | }]; 20 | 21 | testMethod.runTests('personal', method, tests); 22 | -------------------------------------------------------------------------------- /test/utils.toUtf8.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var BigNumber = require('bignumber.js'); 3 | var utils = require('../lib/utils/utils.js'); 4 | var assert = chai.assert; 5 | 6 | var tests = [ 7 | { value: '0x6d79537472696e67', expected: 'myString'}, 8 | { value: '0x6d79537472696e6700', expected: 'myString'}, 9 | { value: '0x65787065637465642076616c7565000000000000000000000000000000000000', expected: 'expected value'} 10 | ]; 11 | 12 | describe('lib/utils/utils', function () { 13 | describe('toUtf8', function () { 14 | tests.forEach(function (test) { 15 | it('should turn ' + test.value + ' to ' + test.expected, function () { 16 | assert.strictEqual(utils.toUtf8(test.value), test.expected); 17 | }); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /lib/solidity/bytes.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | /** 5 | * SolidityTypeBytes is a prototype that represents the bytes type. 6 | * It matches: 7 | * bytes 8 | * bytes[] 9 | * bytes[4] 10 | * bytes[][] 11 | * bytes[3][] 12 | * bytes[][6][], ... 13 | * bytes32 14 | * bytes8[4] 15 | * bytes[3][] 16 | */ 17 | var SolidityTypeBytes = function () { 18 | this._inputFormatter = f.formatInputBytes; 19 | this._outputFormatter = f.formatOutputBytes; 20 | }; 21 | 22 | SolidityTypeBytes.prototype = new SolidityType({}); 23 | SolidityTypeBytes.prototype.constructor = SolidityTypeBytes; 24 | 25 | SolidityTypeBytes.prototype.isType = function (name) { 26 | return !!name.match(/^bytes([0-9]{1,})(\[([0-9]*)\])*$/); 27 | }; 28 | 29 | module.exports = SolidityTypeBytes; 30 | -------------------------------------------------------------------------------- /lib/solidity/int.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | /** 5 | * SolidityTypeInt is a prootype that represents int type 6 | * It matches: 7 | * int 8 | * int[] 9 | * int[4] 10 | * int[][] 11 | * int[3][] 12 | * int[][6][], ... 13 | * int32 14 | * int64[] 15 | * int8[4] 16 | * int256[][] 17 | * int[3][] 18 | * int64[][6][], ... 19 | */ 20 | var SolidityTypeInt = function () { 21 | this._inputFormatter = f.formatInputInt; 22 | this._outputFormatter = f.formatOutputInt; 23 | }; 24 | 25 | SolidityTypeInt.prototype = new SolidityType({}); 26 | SolidityTypeInt.prototype.constructor = SolidityTypeInt; 27 | 28 | SolidityTypeInt.prototype.isType = function (name) { 29 | return !!name.match(/^int([0-9]*)?(\[([0-9]*)\])*$/); 30 | }; 31 | 32 | module.exports = SolidityTypeInt; 33 | -------------------------------------------------------------------------------- /test/web3.eth.getBlockUncleCount.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var method = 'getBlockUncleCount'; 4 | var testMethod = require('./helpers/test.method.js'); 5 | 6 | 7 | var tests = [{ 8 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 9 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 10 | result: '0xb', 11 | formattedResult: 11, 12 | call: 'eth_getUncleCountByBlockHash' 13 | },{ 14 | args: [436], 15 | formattedArgs: ['0x1b4'], 16 | result: '0xb', 17 | formattedResult: 11, 18 | call: 'eth_getUncleCountByBlockNumber' 19 | },{ 20 | args: ['pending'], 21 | formattedArgs: ['pending'], 22 | result: '0xb', 23 | formattedResult: 11, 24 | call: 'eth_getUncleCountByBlockNumber' 25 | }]; 26 | 27 | testMethod.runTests('eth', method, tests); 28 | -------------------------------------------------------------------------------- /test/formatters.inputDefaultBlockFormatter.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var formatters = require('../lib/web3/formatters'); 4 | 5 | var tests = [ 6 | { value: 'latest', expected: 'latest' }, 7 | { value: 'pending', expected: 'pending' }, 8 | { value: 'earliest', expected: 'earliest' }, 9 | { value: 1, expected: '0x1' }, 10 | { value: '0x1', expected: '0x1' } 11 | ]; 12 | 13 | describe('lib/web3/formatters', function () { 14 | describe('inputDefaultBlockNumberFormatter', function () { 15 | tests.forEach(function (test) { 16 | it('should turn ' + test.value + ' to ' + test.expected, function () { 17 | assert.strictEqual(formatters.inputDefaultBlockNumberFormatter(test.value), test.expected); 18 | }); 19 | }); 20 | }); 21 | }); 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /lib/solidity/real.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | /** 5 | * SolidityTypeReal is a prootype that represents real type 6 | * It matches: 7 | * real 8 | * real[] 9 | * real[4] 10 | * real[][] 11 | * real[3][] 12 | * real[][6][], ... 13 | * real32 14 | * real64[] 15 | * real8[4] 16 | * real256[][] 17 | * real[3][] 18 | * real64[][6][], ... 19 | */ 20 | var SolidityTypeReal = function () { 21 | this._inputFormatter = f.formatInputReal; 22 | this._outputFormatter = f.formatOutputReal; 23 | }; 24 | 25 | SolidityTypeReal.prototype = new SolidityType({}); 26 | SolidityTypeReal.prototype.constructor = SolidityTypeReal; 27 | 28 | SolidityTypeReal.prototype.isType = function (name) { 29 | return !!name.match(/real([0-9]*)?(\[([0-9]*)\])?/); 30 | }; 31 | 32 | module.exports = SolidityTypeReal; 33 | -------------------------------------------------------------------------------- /lib/solidity/uint.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | /** 5 | * SolidityTypeUInt is a prootype that represents uint type 6 | * It matches: 7 | * uint 8 | * uint[] 9 | * uint[4] 10 | * uint[][] 11 | * uint[3][] 12 | * uint[][6][], ... 13 | * uint32 14 | * uint64[] 15 | * uint8[4] 16 | * uint256[][] 17 | * uint[3][] 18 | * uint64[][6][], ... 19 | */ 20 | var SolidityTypeUInt = function () { 21 | this._inputFormatter = f.formatInputInt; 22 | this._outputFormatter = f.formatOutputUInt; 23 | }; 24 | 25 | SolidityTypeUInt.prototype = new SolidityType({}); 26 | SolidityTypeUInt.prototype.constructor = SolidityTypeUInt; 27 | 28 | SolidityTypeUInt.prototype.isType = function (name) { 29 | return !!name.match(/^uint([0-9]*)?(\[([0-9]*)\])*$/); 30 | }; 31 | 32 | module.exports = SolidityTypeUInt; 33 | -------------------------------------------------------------------------------- /test/utils.isJson.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { obj: function () {}, is: false}, 7 | { obj: new Function(), is: false}, 8 | { obj: 'function', is: false}, 9 | { obj: {}, is: false}, 10 | { obj: '[]', is: true}, 11 | { obj: '[1, 2]', is: true}, 12 | { obj: '{}', is: true}, 13 | { obj: '{"a": 123, "b" :3,}', is: false}, 14 | { obj: '{"c" : 2}', is: true} 15 | ]; 16 | 17 | describe('lib/utils/utils', function () { 18 | describe('isJson', function () { 19 | tests.forEach(function (test) { 20 | it('shoud test if value ' + test.obj + ' is json: ' + test.is, function () { 21 | assert.equal(utils.isJson(test.obj), test.is); 22 | }); 23 | }); 24 | }); 25 | }); 26 | 27 | -------------------------------------------------------------------------------- /test/utils.isStrictAddress.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { value: function () {}, is: false}, 7 | { value: new Function(), is: false}, 8 | { value: 'function', is: false}, 9 | { value: {}, is: false}, 10 | { value: '0xc6d9d2cd449a754c494264e1809c50e34d64562b', is: true }, 11 | { value: 'c6d9d2cd449a754c494264e1809c50e34d64562b', is: false } 12 | ]; 13 | 14 | describe('lib/utils/utils', function () { 15 | describe('isStrictAddress', function () { 16 | tests.forEach(function (test) { 17 | it('shoud test if value ' + test.value + ' is address: ' + test.is, function () { 18 | assert.equal(utils.isStrictAddress(test.value), test.is); 19 | }); 20 | }); 21 | }); 22 | }); 23 | 24 | -------------------------------------------------------------------------------- /test/web3.sha3.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var sha3 = require('../lib/utils/sha3'); 5 | var web3 = new Web3(); 6 | 7 | var method = 'sha3'; 8 | 9 | describe('web3.sha3', function () { 10 | it('should return sha3 with hex prefix', function() { 11 | test1 = web3.sha3('test123'); 12 | test2 = web3.sha3('test(int)'); 13 | test3 = web3.sha3('0x80', {encoding: 'hex'}); 14 | test4 = web3.sha3('0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', {encoding: 'hex'}); 15 | assert.deepEqual(test1, '0x' + sha3('test123')); 16 | assert.deepEqual(test2, '0x' + sha3('test(int)')); 17 | assert.deepEqual(test3, '0x' + sha3('0x80', {encoding: 'hex'})); 18 | assert.deepEqual(test4, '0x' + sha3('0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', {encoding: 'hex'})); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /lib/solidity/ureal.js: -------------------------------------------------------------------------------- 1 | var f = require('./formatters'); 2 | var SolidityType = require('./type'); 3 | 4 | /** 5 | * SolidityTypeUReal is a prootype that represents ureal type 6 | * It matches: 7 | * ureal 8 | * ureal[] 9 | * ureal[4] 10 | * ureal[][] 11 | * ureal[3][] 12 | * ureal[][6][], ... 13 | * ureal32 14 | * ureal64[] 15 | * ureal8[4] 16 | * ureal256[][] 17 | * ureal[3][] 18 | * ureal64[][6][], ... 19 | */ 20 | var SolidityTypeUReal = function () { 21 | this._inputFormatter = f.formatInputReal; 22 | this._outputFormatter = f.formatOutputUReal; 23 | }; 24 | 25 | SolidityTypeUReal.prototype = new SolidityType({}); 26 | SolidityTypeUReal.prototype.constructor = SolidityTypeUReal; 27 | 28 | SolidityTypeUReal.prototype.isType = function (name) { 29 | return !!name.match(/^ureal([0-9]*)?(\[([0-9]*)\])*$/); 30 | }; 31 | 32 | module.exports = SolidityTypeUReal; 33 | -------------------------------------------------------------------------------- /test/web3.eth.getBlockTransactionCount.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var testMethod = require('./helpers/test.method.js'); 4 | 5 | var method = 'getBlockTransactionCount'; 6 | 7 | 8 | var tests = [{ 9 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 10 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 11 | result: '0xb', 12 | formattedResult: 11, 13 | call: 'eth_getBlockTransactionCountByHash' 14 | },{ 15 | args: [436], 16 | formattedArgs: ['0x1b4'], 17 | result: '0xb', 18 | formattedResult: 11, 19 | call: 'eth_getBlockTransactionCountByNumber' 20 | },{ 21 | args: ['pending'], 22 | formattedArgs: ['pending'], 23 | result: '0xb', 24 | formattedResult: 11, 25 | call: 'eth_getBlockTransactionCountByNumber' 26 | }]; 27 | 28 | testMethod.runTests('eth', method, tests); 29 | 30 | -------------------------------------------------------------------------------- /test/utils.isBigNumber.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var BigNumber = require('bignumber.js'); 4 | var assert = chai.assert; 5 | 6 | var tests = [ 7 | { value: function () {}, is: false}, 8 | { value: new Function(), is: false}, 9 | { value: 'function', is: false}, 10 | { value: {}, is: false}, 11 | { value: new String('hello'), is: false}, 12 | { value: new BigNumber(0), is: true}, 13 | { value: 132, is: false}, 14 | { value: '0x12', is: false}, 15 | 16 | ]; 17 | 18 | describe('lib/utils/utils', function () { 19 | describe('isBigNumber', function () { 20 | tests.forEach(function (test) { 21 | it('shoud test if value ' + test.func + ' is BigNumber: ' + test.is, function () { 22 | assert.equal(utils.isBigNumber(test.value), test.is); 23 | }); 24 | }); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/web3.methods.js: -------------------------------------------------------------------------------- 1 | var Web3 = require('../index.js'); 2 | var web3 = new Web3(); 3 | var u = require('./helpers/test.utils.js'); 4 | 5 | describe('web3', function() { 6 | describe('methods', function () { 7 | u.methodExists(web3, 'sha3'); 8 | u.methodExists(web3, 'toAscii'); 9 | u.methodExists(web3, 'fromAscii'); 10 | u.methodExists(web3, 'toDecimal'); 11 | u.methodExists(web3, 'fromDecimal'); 12 | u.methodExists(web3, 'fromWei'); 13 | u.methodExists(web3, 'toWei'); 14 | u.methodExists(web3, 'toBigNumber'); 15 | u.methodExists(web3, 'isAddress'); 16 | u.methodExists(web3, 'setProvider'); 17 | u.methodExists(web3, 'reset'); 18 | 19 | u.propertyExists(web3, 'providers'); 20 | u.propertyExists(web3, 'eth'); 21 | u.propertyExists(web3, 'db'); 22 | u.propertyExists(web3, 'shh'); 23 | }); 24 | }); 25 | 26 | -------------------------------------------------------------------------------- /test/web3.shh.post.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Web3 = require('../index'); 3 | var web3 = new Web3(); 4 | var testMethod = require('./helpers/test.method.js'); 5 | 6 | var method = 'post'; 7 | 8 | var tests = [{ 9 | args: [{ 10 | symKeyID: '123123123ff', 11 | sig: '44ffdd55', 12 | topic: '0xffdd11', 13 | payload: web3.toHex('12345'), 14 | ttl: 100, 15 | minPow: 0.5, 16 | powTarget: 3, 17 | padding: '0xffdd4455' 18 | }], 19 | formattedArgs: [{ 20 | symKeyID: '123123123ff', 21 | sig: '44ffdd55', 22 | topic: '0xffdd11', 23 | payload: web3.toHex('12345'), 24 | ttl: 100, 25 | minPow: 0.5, 26 | powTarget: 3, 27 | padding: '0xffdd4455' 28 | }], 29 | result: true, 30 | formattedResult: true, 31 | call: 'shh_'+ method 32 | }]; 33 | 34 | testMethod.runTests('shh', method, tests); 35 | 36 | -------------------------------------------------------------------------------- /test/sha3.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var sha3 = require('../lib/utils/sha3'); 4 | var web3 = require('../index'); 5 | 6 | describe('lib/utils/sha3', function () { 7 | var test = function (v, e, o) { 8 | it('should encode ' + v + ' to ' + e, function () { 9 | assert.equal(sha3(v, o), e); 10 | }); 11 | }; 12 | 13 | test('test123', 'f81b517a242b218999ec8eec0ea6e2ddbef2a367a14e93f4a32a39e260f686ad'); 14 | test('test(int)', 'f4d03772bec1e62fbe8c5691e1a9101e520e8f8b5ca612123694632bf3cb51b1'); 15 | test('0x80', '56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421', { encoding: 'hex' }); 16 | test('0x80', '6b03a5eef7706e3fb52a61c19ab1122fad7237726601ac665bd4def888f0e4a0'); 17 | test('0x3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1', '82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28', { encoding: 'hex' }); 18 | }); 19 | 20 | -------------------------------------------------------------------------------- /test/helpers/test.utils.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | //var web3 = require('../../index'); 4 | 5 | var FakeHttpProvider = require('./FakeHttpProvider'); 6 | 7 | var methodExists = function (object, method) { 8 | it('should have method ' + method + ' implemented', function() { 9 | //web3.setProvider(null); 10 | assert.equal('function', typeof object[method], 'method ' + method + ' is not implemented'); 11 | }); 12 | }; 13 | 14 | var propertyExists = function (object, property) { 15 | it('should have property ' + property + ' implemented', function() { 16 | // set dummy providor, to prevent error 17 | //web3.setProvider(new FakeHttpProvider()); 18 | assert.notEqual('undefined', typeof object[property], 'property ' + property + ' is not implemented'); 19 | }); 20 | }; 21 | 22 | module.exports = { 23 | methodExists: methodExists, 24 | propertyExists: propertyExists 25 | }; 26 | 27 | -------------------------------------------------------------------------------- /test/helpers/FakeIpcRequest.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | 4 | var FakeIpcRequest = function () { 5 | this._handle = {fd: {}}; 6 | this.callbacks = []; 7 | 8 | return this 9 | }; 10 | 11 | FakeIpcRequest.prototype.connect = function (path) { 12 | assert.notEqual(path, undefined); 13 | 14 | return this; 15 | }; 16 | 17 | FakeIpcRequest.prototype.on = function(name, callback) { 18 | if(name === 'data'){ 19 | this.callbacks.push(callback); 20 | } 21 | }; 22 | 23 | FakeIpcRequest.prototype.writeSync = function (payload) { 24 | assert.equal(typeof payload, 'string'); 25 | return payload; 26 | }; 27 | 28 | FakeIpcRequest.prototype.write = function (payload) { 29 | assert.equal(typeof payload, 'string'); 30 | 31 | this.callbacks.forEach(function(cb){ 32 | setTimeout(function(){ 33 | cb(payload); 34 | }, 100); 35 | }); 36 | 37 | }; 38 | 39 | module.exports = FakeIpcRequest; 40 | 41 | -------------------------------------------------------------------------------- /test/helpers/FakeHttpProvider2.js: -------------------------------------------------------------------------------- 1 | var FakeHttpProvider = require('./FakeHttpProvider'); 2 | 3 | var FakeHttpProvider2 = function () { 4 | this.counter = 0; 5 | this.resultList = []; 6 | }; 7 | 8 | FakeHttpProvider2.prototype = new FakeHttpProvider(); 9 | FakeHttpProvider2.prototype.constructor = FakeHttpProvider2; 10 | 11 | FakeHttpProvider2.prototype.injectResultList = function (list) { 12 | this.resultList = list; 13 | }; 14 | 15 | FakeHttpProvider2.prototype.getResponse = function () { 16 | var result = this.resultList[this.counter]; 17 | this.counter++; 18 | 19 | // add fallback result value 20 | if(!result) 21 | result = { 22 | result: undefined 23 | }; 24 | 25 | if (result.type === 'batch') { 26 | this.injectBatchResults(result.result); 27 | } else { 28 | this.injectResult(result.result); 29 | } 30 | 31 | this.counter = 0; 32 | 33 | return this.response; 34 | }; 35 | 36 | module.exports = FakeHttpProvider2; 37 | 38 | -------------------------------------------------------------------------------- /package.js: -------------------------------------------------------------------------------- 1 | /* jshint ignore:start */ 2 | Package.describe({ 3 | name: 'ethereum:web3', 4 | version: '0.20.4', 5 | summary: 'Ethereum JavaScript API, middleware to talk to a ethreum node over RPC', 6 | git: 'https://github.com/ethereum/ethereum.js', 7 | // By default, Meteor will default to using README.md for documentation. 8 | // To avoid submitting documentation, set this field to null. 9 | documentation: 'README.md' 10 | }); 11 | 12 | Npm.depends({ 13 | "xmlhttprequest": "1.7.0" 14 | }); 15 | 16 | 17 | Package.onUse(function(api) { 18 | api.versionsFrom('1.0.3.2'); 19 | 20 | // api.use('3stack:bignumber@2.0.0', 'client'); 21 | 22 | api.export(['Web3', 'BigNumber'], ['client', 'server']); 23 | 24 | api.addFiles('dist/web3.js', ['client', 'server']); 25 | api.addFiles('package-init.js', ['client', 'server']); 26 | }); 27 | 28 | // Package.onTest(function(api) { 29 | // api.use('tinytest'); 30 | // api.use('test'); 31 | // api.addFiles('test-tests.js'); 32 | // }); 33 | /* jshint ignore:end */ 34 | -------------------------------------------------------------------------------- /test/utils.fromAscii.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var BigNumber = require('bignumber.js'); 3 | var utils = require('../lib/utils/utils.js'); 4 | var assert = chai.assert; 5 | 6 | var tests = [ 7 | { value: 'myString', expected: '0x6d79537472696e67'}, 8 | { value: 'myString\x00', expected: '0x6d79537472696e6700'}, 9 | { value: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ', 10 | expected: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c'} 11 | ]; 12 | 13 | describe('lib/utils/utils', function () { 14 | describe('fromAscii', function () { 15 | tests.forEach(function (test) { 16 | it('should turn ' + test.value + ' to ' + test.expected, function () { 17 | assert.strictEqual(utils.fromAscii(test.value), test.expected); 18 | }); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /test/utils.toAscii.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var BigNumber = require('bignumber.js'); 3 | var utils = require('../lib/utils/utils.js'); 4 | var assert = chai.assert; 5 | 6 | var tests = [ 7 | { value: '0x6d79537472696e67', expected: 'myString'}, 8 | { value: '0x6d79537472696e6700', expected: 'myString\u0000'}, 9 | { value: "0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c", 10 | expected: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ'} 11 | ]; 12 | 13 | describe('lib/utils/utils', function () { 14 | describe('toAscii', function () { 15 | tests.forEach(function (test) { 16 | it('should turn ' + test.value + ' to ' + test.expected, function () { 17 | assert.strictEqual(utils.toAscii(test.value), test.expected); 18 | }); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /test/soldity.formatters.formatInputInt.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var formatters = require('../lib/solidity/formatters.js'); 4 | var SolidityParam = require('../lib/solidity/param'); 5 | 6 | var tests = [ 7 | { input: 1, result: new SolidityParam('0000000000000000000000000000000000000000000000000000000000000001') }, 8 | { input: 1.1, result: new SolidityParam('0000000000000000000000000000000000000000000000000000000000000001') }, 9 | { input: -1.1, result: new SolidityParam('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') }, 10 | { input: -1, result: new SolidityParam('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff') } 11 | ]; 12 | 13 | describe('formatters', function () { 14 | describe('inputAddressFormatter', function () { 15 | tests.forEach(function(test){ 16 | it('should return the correct value', function () { 17 | assert.deepEqual(formatters.formatInputInt(test.input), test.result); 18 | }); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /test/formatters.outputPostFormatter.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var formatters = require('../lib/web3/formatters.js'); 3 | 4 | describe('formatters', function () { 5 | describe('outputPostFormatter', function () { 6 | it('should return the correct value', function () { 7 | 8 | assert.deepEqual(formatters.outputPostFormatter({ 9 | expiry: '0x3e8', 10 | sent: '0x3e8', 11 | ttl: '0x3e8', 12 | workProved: '0x3e8', 13 | payload: '0x7b2274657374223a2274657374227d', 14 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 15 | }), { 16 | expiry: 1000, 17 | sent: 1000, 18 | ttl: 1000, 19 | workProved: 1000, 20 | payload: '0x7b2274657374223a2274657374227d',//{test: 'test'}, 21 | //payloadRaw: '0x7b2274657374223a2274657374227d', 22 | topics: ['hello','mytopics'] 23 | }); 24 | }); 25 | }); 26 | }); 27 | -------------------------------------------------------------------------------- /test/iban.fromAddress.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Iban = require('../lib/web3/iban.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { address: '00c5496aee77c1ba1f0854206a26dda82a81d6d8', expected: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS'}, 7 | { address: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', expected: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS'}, 8 | { address: '0x11c5496aee77c1ba1f0854206a26dda82a81d6d8', expected: 'XE1222Q908LN1QBBU6XUQSO1OHWJIOS46OO'}, 9 | { address: '0x52dc504a422f0e2a9e7632a34a50f1a82f8224c7', expected: 'XE499OG1EH8ZZI0KXC6N83EKGT1BM97P2O7'}, 10 | { address: '0x0000a5327eab78357cbf2ae8f3d49fd9d90c7d22', expected: 'XE0600DQK33XDTYUCRI0KYM5ELAKXDWWF6'} 11 | ]; 12 | 13 | describe('lib/web3/iban', function () { 14 | describe('fromAddress', function () { 15 | tests.forEach(function (test) { 16 | it('shoud create indirect iban: ' + test.expected, function () { 17 | assert.deepEqual(Iban.fromAddress(test.address), new Iban(test.expected)); 18 | }); 19 | }); 20 | }); 21 | }); 22 | 23 | -------------------------------------------------------------------------------- /test/helpers/FakeXHR2.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var EventEmitter = require('events').EventEmitter; 4 | var util = require('util'); 5 | 6 | var FakeXHR2 = function () { 7 | this.responseText = "{}"; 8 | this.readyState = 4; 9 | this.onreadystatechange = null; 10 | this.async = true; 11 | this.headers = { 12 | 'Content-Type': 'text/plain' 13 | }; 14 | }; 15 | 16 | FakeXHR2.prototype.open = function (method, host, async) { 17 | assert.equal(method, 'POST'); 18 | assert.notEqual(host, null); 19 | assert.equal(async === true, true); 20 | this.async = async; 21 | }; 22 | 23 | FakeXHR2.prototype.setRequestHeader = function(name, value) { 24 | this.headers[name] = value; 25 | }; 26 | 27 | FakeXHR2.prototype.send = function (payload) { 28 | assert.equal(typeof payload, 'string'); 29 | if (this.async) { 30 | assert.equal(typeof this.onreadystatechange, 'function'); 31 | this.onreadystatechange(); 32 | return; 33 | } 34 | return this.responseText; 35 | }; 36 | 37 | module.exports = FakeXHR2; 38 | -------------------------------------------------------------------------------- /test/formatters.inputPostFormatter.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var formatters = require('../lib/web3/formatters.js'); 4 | 5 | describe('formatters', function () { 6 | describe('inputPostFormatter', function () { 7 | it('should return the correct value', function () { 8 | 9 | // input as strings and numbers 10 | assert.deepEqual(formatters.inputPostFormatter({ 11 | from: '0x00000', 12 | to: '0x00000', 13 | payload: '0x7b2274657374223a2274657374227d',//{test: 'test'}, 14 | ttl: 200, 15 | priority: 1000, 16 | topics: ['hello','mytopics'] 17 | }), { 18 | from: '0x00000', 19 | to: '0x00000', 20 | payload: '0x7b2274657374223a2274657374227d', 21 | ttl: '0xc8', 22 | priority: '0x3e8', 23 | topics: ['0x68656c6c6f','0x6d79746f70696373'], 24 | workToProve: '0x0' 25 | }); 26 | 27 | }); 28 | }); 29 | }); 30 | 31 | -------------------------------------------------------------------------------- /test/web3.eth.call.js: -------------------------------------------------------------------------------- 1 | var testMethod = require('./helpers/test.method.js'); 2 | 3 | var method = 'call'; 4 | 5 | var tests = [{ 6 | args: [{ 7 | to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', 8 | data: '0x23455654', 9 | gas: 11, 10 | gasPrice: 11 11 | }], 12 | formattedArgs: [{ 13 | to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', 14 | data: '0x23455654', 15 | gas: '0xb', 16 | gasPrice: '0xb' 17 | }, 'latest'], 18 | result: '0x31981', 19 | formattedResult: '0x31981', 20 | call: 'eth_'+ method 21 | },{ 22 | args: [{ 23 | to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', 24 | data: '0x23455654', 25 | gas: 11, 26 | gasPrice: 11 27 | }, 11], 28 | formattedArgs: [{ 29 | to: '0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b', 30 | data: '0x23455654', 31 | gas: '0xb', 32 | gasPrice: '0xb' 33 | }, '0xb'], 34 | result: '0x31981', 35 | formattedResult: '0x31981', 36 | call: 'eth_'+ method 37 | }]; 38 | 39 | testMethod.runTests('eth', method, tests); 40 | 41 | -------------------------------------------------------------------------------- /test/helpers/FakeXMLHttpRequest.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | 4 | var FakeXMLHttpRequest = function () { 5 | this.responseText = "{}"; 6 | this.readyState = 4; 7 | this.onreadystatechange = null; 8 | this.async = false; 9 | this.headers = { 10 | 'Content-Type': 'text/plain' 11 | }; 12 | }; 13 | 14 | FakeXMLHttpRequest.prototype.open = function (method, host, async) { 15 | assert.equal(method, 'POST'); 16 | assert.notEqual(host, null); 17 | assert.equal(async === false || async === true, true); 18 | this.async = async; 19 | }; 20 | 21 | FakeXMLHttpRequest.prototype.setRequestHeader = function(name, value) { 22 | this.headers[name] = value; 23 | }; 24 | 25 | FakeXMLHttpRequest.prototype.send = function (payload) { 26 | assert.equal(typeof payload, 'string'); 27 | if (this.async) { 28 | assert.equal(typeof this.onreadystatechange, 'function'); 29 | this.onreadystatechange(); 30 | return; 31 | } 32 | return this.responseText; 33 | }; 34 | 35 | module.exports = { 36 | XMLHttpRequest: FakeXMLHttpRequest 37 | }; 38 | 39 | -------------------------------------------------------------------------------- /test/web3.eth.getCode.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Web3 = require('../index'); 3 | var web3 = new Web3(); 4 | var testMethod = require('./helpers/test.method.js'); 5 | 6 | var method = 'getCode'; 7 | 8 | 9 | var tests = [{ 10 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 11 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', web3.eth.defaultBlock], 12 | result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 13 | formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 14 | call: 'eth_'+ method 15 | },{ 16 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 2], 17 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x2'], 18 | result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 19 | formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 20 | call: 'eth_'+ method 21 | }]; 22 | 23 | testMethod.runTests('eth', method, tests); 24 | 25 | -------------------------------------------------------------------------------- /test/utils.isAddress.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { value: function () {}, is: false}, 7 | { value: new Function(), is: false}, 8 | { value: 'function', is: false}, 9 | { value: {}, is: false}, 10 | { value: '0xc6d9d2cd449a754c494264e1809c50e34d64562b', is: true }, 11 | { value: 'c6d9d2cd449a754c494264e1809c50e34d64562b', is: true }, 12 | { value: '0xE247A45c287191d435A8a5D72A7C8dc030451E9F', is: true }, 13 | { value: '0xE247a45c287191d435A8a5D72A7C8dc030451E9F', is: false }, 14 | { value: '0xe247a45c287191d435a8a5d72a7c8dc030451e9f', is: true }, 15 | { value: '0XE247A45C287191D435A8A5D72A7C8DC030451E9F', is: false }, 16 | { value: '0xE247A45C287191D435A8A5D72A7C8DC030451E9F', is: true } 17 | ]; 18 | 19 | describe('lib/utils/utils', function () { 20 | describe('isAddress', function () { 21 | tests.forEach(function (test) { 22 | it('shoud test if value ' + test.value + ' is address: ' + test.is, function () { 23 | assert.equal(utils.isAddress(test.value), test.is); 24 | }); 25 | }); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /test/method.formatInput.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Method = require('../lib/web3/method'); 4 | 5 | describe('lib/web3/method', function () { 6 | describe('formatInput', function () { 7 | it('should format plain input', function () { 8 | 9 | // given 10 | var star = function (arg) { 11 | return arg + '*'; 12 | }; 13 | 14 | var method = new Method({ 15 | inputFormatter: [star, star, star] 16 | }); 17 | var args = ['1','2','3']; 18 | var expectedArgs = ['1*', '2*', '3*']; 19 | 20 | // when 21 | var result = method.formatInput(args); 22 | 23 | // then 24 | assert.deepEqual(result, expectedArgs); 25 | }); 26 | 27 | it('should do nothing if there is no formatter', function () { 28 | 29 | // given 30 | var method = new Method({}); 31 | var args = [1,2,3]; 32 | 33 | // when 34 | var result = method.formatInput(args); 35 | 36 | // then 37 | assert.deepEqual(result, args); 38 | }); 39 | }); 40 | }); 41 | 42 | -------------------------------------------------------------------------------- /test/web3.eth.protocolVersion.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'protocolVersion'; 8 | 9 | var tests = [{ 10 | result: ['1234'], 11 | call: 'eth_'+ method 12 | }]; 13 | 14 | describe('eth.protocolVersion', function () { 15 | describe(method, function () { 16 | tests.forEach(function (test, index) { 17 | it('property test: ' + index, function () { 18 | 19 | // given 20 | var provider = new FakeHttpProvider(); 21 | web3.setProvider(provider); 22 | provider.injectResult(test.result); 23 | provider.injectValidation(function (payload) { 24 | assert.equal(payload.jsonrpc, '2.0'); 25 | assert.equal(payload.method, test.call); 26 | assert.deepEqual(payload.params, []); 27 | }); 28 | 29 | // when 30 | var result = web3.eth[method]; 31 | 32 | // then 33 | assert.deepEqual(test.result, result); 34 | }); 35 | }); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /test/utils.isChecksumAddress.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { value: '0x52908400098527886E0F7030069857D2E4169EE7', is: true }, 7 | { value: '0x8617E340B3D01FA5F11F306F4090FD50E238070D', is: true }, 8 | { value: '0xde709f2102306220921060314715629080e2fb77', is: true }, 9 | { value: '0x27b1fdb04752bbc536007a920d24acb045561c26', is: true }, 10 | { value: '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed', is: true }, 11 | { value: '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359', is: true }, 12 | { value: '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB', is: true }, 13 | { value: '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb', is: true }, 14 | { value: '0XD1220A0CF47C7B9BE7A2E6BA89F429762E7B9ADB', is: false }, 15 | { value: '0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb', is: false } 16 | ]; 17 | 18 | describe('lib/utils/utils', function () { 19 | describe('isChecksumAddress', function () { 20 | tests.forEach(function (test) { 21 | it('shoud test if address ' + test.value + ' passes checksum: ' + test.is, function () { 22 | assert.equal(utils.isChecksumAddress(test.value), test.is); 23 | }); 24 | }); 25 | }); 26 | }); 27 | 28 | -------------------------------------------------------------------------------- /test/web3.eth.mining.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'mining'; 8 | 9 | var tests = [{ 10 | result: true, 11 | formattedResult: true, 12 | call: 'eth_'+ method 13 | }]; 14 | 15 | describe('web3.eth', function () { 16 | describe(method, function () { 17 | tests.forEach(function (test, index) { 18 | it('property test: ' + index, function () { 19 | 20 | // given 21 | var provider = new FakeHttpProvider(); 22 | web3.setProvider(provider); 23 | provider.injectResult(test.result); 24 | provider.injectValidation(function (payload) { 25 | assert.equal(payload.jsonrpc, '2.0'); 26 | assert.equal(payload.method, test.call); 27 | assert.deepEqual(payload.params, []); 28 | }); 29 | 30 | // when 31 | var result = web3.eth[method]; 32 | 33 | // then 34 | assert.deepEqual(test.formattedResult, result); 35 | }); 36 | }); 37 | }); 38 | }); 39 | 40 | -------------------------------------------------------------------------------- /test/web3.net.listening.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'listening'; 8 | 9 | var tests = [{ 10 | result: true, 11 | formattedResult: true, 12 | call: 'net_'+ method 13 | }]; 14 | 15 | describe('web3.net', function () { 16 | describe(method, function () { 17 | tests.forEach(function (test, index) { 18 | it('property test: ' + index, function () { 19 | 20 | // given 21 | var provider = new FakeHttpProvider(); 22 | web3.setProvider(provider); 23 | provider.injectResult(test.result); 24 | provider.injectValidation(function (payload) { 25 | assert.equal(payload.jsonrpc, '2.0'); 26 | assert.equal(payload.method, test.call); 27 | assert.deepEqual(payload.params, []); 28 | }); 29 | 30 | // when 31 | var result = web3.net[method]; 32 | 33 | // then 34 | assert.deepEqual(test.formattedResult, result); 35 | }); 36 | }); 37 | }); 38 | }); 39 | 40 | -------------------------------------------------------------------------------- /test/web3.net.peerCount.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'peerCount'; 8 | 9 | var tests = [{ 10 | result: '0xf', 11 | formattedResult: 15, 12 | call: 'net_'+ method 13 | }]; 14 | 15 | describe('web3.net', function () { 16 | describe(method, function () { 17 | tests.forEach(function (test, index) { 18 | it('property test: ' + index, function () { 19 | 20 | // given 21 | var provider = new FakeHttpProvider(); 22 | web3.setProvider(provider); 23 | provider.injectResult(test.result); 24 | provider.injectValidation(function (payload) { 25 | assert.equal(payload.jsonrpc, '2.0'); 26 | assert.equal(payload.method, test.call); 27 | assert.deepEqual(payload.params, []); 28 | }); 29 | 30 | // when 31 | var result = web3.net[method]; 32 | 33 | // then 34 | assert.deepEqual(test.formattedResult, result); 35 | }); 36 | }); 37 | }); 38 | }); 39 | 40 | -------------------------------------------------------------------------------- /test/web3.shh.methods.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index.js'); 4 | var web3 = new Web3(); 5 | var u = require('./helpers/test.utils.js'); 6 | 7 | describe('web3.shh', function() { 8 | describe('methods', function() { 9 | u.methodExists(web3.shh, 'version'); 10 | u.methodExists(web3.shh, 'info'); 11 | u.methodExists(web3.shh, 'setMaxMessageSize'); 12 | u.methodExists(web3.shh, 'setMinPoW'); 13 | u.methodExists(web3.shh, 'markTrustedPeer'); 14 | u.methodExists(web3.shh, 'newKeyPair'); 15 | u.methodExists(web3.shh, 'addPrivateKey'); 16 | u.methodExists(web3.shh, 'deleteKeyPair'); 17 | u.methodExists(web3.shh, 'hasKeyPair'); 18 | u.methodExists(web3.shh, 'getPublicKey'); 19 | u.methodExists(web3.shh, 'getPrivateKey'); 20 | u.methodExists(web3.shh, 'newSymKey'); 21 | u.methodExists(web3.shh, 'addSymKey'); 22 | u.methodExists(web3.shh, 'generateSymKeyFromPassword'); 23 | u.methodExists(web3.shh, 'hasSymKey'); 24 | u.methodExists(web3.shh, 'getSymKey'); 25 | u.methodExists(web3.shh, 'deleteSymKey'); 26 | u.methodExists(web3.shh, 'newMessageFilter'); 27 | u.methodExists(web3.shh, 'post'); 28 | 29 | }); 30 | }); 31 | 32 | -------------------------------------------------------------------------------- /test/method.formatOutput.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Method = require('../lib/web3/method'); 4 | 5 | describe('lib/web3/method', function () { 6 | describe('formatOutput', function () { 7 | it('should format plain output', function () { 8 | 9 | // given 10 | var formatter = function (args) { 11 | return args.map(function (arg) { 12 | return arg + '*'; 13 | }); 14 | }; 15 | 16 | var method = new Method({ 17 | outputFormatter: formatter 18 | }); 19 | var args = ['1','2','3']; 20 | var expectedArgs = ['1*', '2*', '3*']; 21 | 22 | // when 23 | var result = method.formatOutput(args); 24 | 25 | // then 26 | assert.deepEqual(result, expectedArgs); 27 | }); 28 | 29 | it('should do nothing if there is no formatter', function () { 30 | 31 | // given 32 | var method = new Method({}); 33 | var args = [1,2,3]; 34 | 35 | // when 36 | var result = method.formatOutput(args); 37 | 38 | // then 39 | assert.deepEqual(result, args); 40 | }); 41 | }); 42 | }); 43 | 44 | -------------------------------------------------------------------------------- /lib/utils/sha3.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** 18 | * @file sha3.js 19 | * @author Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | var CryptoJS = require('crypto-js'); 24 | var sha3 = require('crypto-js/sha3'); 25 | 26 | module.exports = function (value, options) { 27 | if (options && options.encoding === 'hex') { 28 | if (value.length > 2 && value.substr(0, 2) === '0x') { 29 | value = value.substr(2); 30 | } 31 | value = CryptoJS.enc.Hex.parse(value); 32 | } 33 | 34 | return sha3(value, { 35 | outputLength: 256 36 | }).toString(); 37 | }; 38 | 39 | -------------------------------------------------------------------------------- /test/method.getCall.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Method = require('../lib/web3/method'); 4 | 5 | describe('lib/web3/method', function () { 6 | describe('getCall', function () { 7 | it('should return call name', function () { 8 | 9 | // given 10 | var call = 'hello_call_world'; 11 | var method = new Method({ 12 | call: call 13 | }); 14 | 15 | // when 16 | var result = method.getCall(); 17 | 18 | // then 19 | assert.equal(call, result); 20 | }); 21 | 22 | it('should return call based on args', function () { 23 | 24 | // given 25 | var call = function (args) { 26 | return args ? args.length.toString() : '0'; 27 | }; 28 | 29 | var method = new Method({ 30 | call: call 31 | }); 32 | 33 | // when 34 | var r0 = method.getCall(); 35 | var r1 = method.getCall([1]); 36 | var r2 = method.getCall([1, 2]); 37 | 38 | // then 39 | assert.equal(r0, '0'); 40 | assert.equal(r1, '1'); 41 | assert.equal(r2, '2'); 42 | 43 | }); 44 | }); 45 | }); 46 | 47 | -------------------------------------------------------------------------------- /test/utils.fromWei.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var utils = require('../lib/utils/utils.js'); 3 | 4 | describe('lib/utils/utils', function () { 5 | describe('fromWei', function () { 6 | it('should return the correct value', function () { 7 | 8 | assert.equal(utils.fromWei(1000000000000000000, 'wei'), '1000000000000000000'); 9 | assert.equal(utils.fromWei(1000000000000000000, 'kwei'), '1000000000000000'); 10 | assert.equal(utils.fromWei(1000000000000000000, 'mwei'), '1000000000000'); 11 | assert.equal(utils.fromWei(1000000000000000000, 'gwei'), '1000000000'); 12 | assert.equal(utils.fromWei(1000000000000000000, 'szabo'), '1000000'); 13 | assert.equal(utils.fromWei(1000000000000000000, 'finney'), '1000'); 14 | assert.equal(utils.fromWei(1000000000000000000, 'ether'), '1'); 15 | assert.equal(utils.fromWei(1000000000000000000, 'kether'), '0.001'); 16 | assert.equal(utils.fromWei(1000000000000000000, 'grand'), '0.001'); 17 | assert.equal(utils.fromWei(1000000000000000000, 'mether'), '0.000001'); 18 | assert.equal(utils.fromWei(1000000000000000000, 'gether'), '0.000000001'); 19 | assert.equal(utils.fromWei(1000000000000000000, 'tether'), '0.000000000001'); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /test/web3.eth.gasPrice.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var BigNumber = require('bignumber.js'); 6 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 7 | 8 | var method = 'gasPrice'; 9 | 10 | var tests = [{ 11 | result: '0x15f90', 12 | formattedResult: new BigNumber(90000), 13 | call: 'eth_'+ method 14 | }]; 15 | 16 | describe('web3.eth', function () { 17 | describe(method, function () { 18 | tests.forEach(function (test, index) { 19 | it('property test: ' + index, function () { 20 | 21 | // given 22 | var provider = new FakeHttpProvider(); 23 | web3.setProvider(provider); 24 | provider.injectResult(test.result); 25 | provider.injectValidation(function (payload) { 26 | assert.equal(payload.jsonrpc, '2.0'); 27 | assert.equal(payload.method, test.call); 28 | assert.deepEqual(payload.params, []); 29 | }); 30 | 31 | // when 32 | var result = web3.eth[method]; 33 | 34 | // then 35 | assert.deepEqual(test.formattedResult, result); 36 | }); 37 | }); 38 | }); 39 | }); 40 | 41 | -------------------------------------------------------------------------------- /test/web3.personal.listAccounts.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'listAccounts'; 8 | 9 | var tests = [{ 10 | result: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 11 | formattedResult: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 12 | call: 'personal_'+ method 13 | }]; 14 | 15 | describe('web3.personal', function () { 16 | describe(method, function () { 17 | tests.forEach(function (test, index) { 18 | it('property test: ' + index, function () { 19 | 20 | // given 21 | var provider = new FakeHttpProvider(); 22 | web3.setProvider(provider); 23 | provider.injectResult(test.result); 24 | provider.injectValidation(function (payload) { 25 | assert.equal(payload.jsonrpc, '2.0'); 26 | assert.equal(payload.method, test.call); 27 | assert.deepEqual(payload.params, []); 28 | }); 29 | 30 | // when 31 | var result = web3.personal[method]; 32 | 33 | // then 34 | assert.deepEqual(test.formattedResult, result); 35 | }); 36 | }); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /test/web3.eth.accounts.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'accounts'; 8 | 9 | var tests = [{ 10 | result: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 11 | formattedResult: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855'], 12 | call: 'eth_'+ method 13 | }]; 14 | 15 | describe('web3.eth', function () { 16 | describe(method, function () { 17 | tests.forEach(function (test, index) { 18 | it('property test: ' + index, function () { 19 | 20 | // given 21 | var provider = new FakeHttpProvider(); 22 | web3.setProvider(provider); 23 | provider.injectResult(test.result); 24 | provider.injectValidation(function (payload) { 25 | assert.equal(payload.jsonrpc, '2.0'); 26 | assert.equal(payload.method, test.call); 27 | assert.deepEqual(payload.params, []); 28 | }); 29 | 30 | // when 31 | var result = web3.eth[method]; 32 | 33 | // then 34 | assert.deepEqual(test.formattedResult, result); 35 | }); 36 | }); 37 | }); 38 | }); 39 | 40 | -------------------------------------------------------------------------------- /test/web3.eth.coinbase.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'coinbase'; 8 | 9 | var tests = [{ 10 | result: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 11 | formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 12 | call: 'eth_'+ method 13 | }]; 14 | 15 | describe('web3.eth', function () { 16 | describe(method, function () { 17 | tests.forEach(function (test, index) { 18 | it('property test: ' + index, function () { 19 | 20 | // given 21 | var provider = new FakeHttpProvider(); 22 | web3.setProvider(provider); 23 | provider.injectResult(test.result); 24 | provider.injectValidation(function (payload) { 25 | assert.equal(payload.jsonrpc, '2.0'); 26 | assert.equal(payload.method, test.call); 27 | assert.deepEqual(payload.params, []); 28 | }); 29 | 30 | // when 31 | var result = web3.eth[method]; 32 | 33 | // then 34 | assert.deepEqual(test.formattedResult, result); 35 | }); 36 | }); 37 | }); 38 | }); 39 | 40 | -------------------------------------------------------------------------------- /test/utils.extractDisplayName.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var utils = require('../lib/utils/utils.js'); 3 | 4 | describe('lib/utils/utils', function () { 5 | describe('extractDisplayName', function () { 6 | it('should extract display name from method with no params', function () { 7 | 8 | // given 9 | var test = 'helloworld()'; 10 | 11 | // when 12 | var displayName = utils.extractDisplayName(test); 13 | 14 | // then 15 | assert.equal(displayName, 'helloworld'); 16 | }); 17 | 18 | it('should extract display name from method with one param' , function () { 19 | 20 | // given 21 | var test = 'helloworld1(int)'; 22 | 23 | // when 24 | var displayName = utils.extractDisplayName(test); 25 | 26 | // then 27 | assert.equal(displayName, 'helloworld1'); 28 | }); 29 | 30 | it('should extract display name from method with two params' , function () { 31 | 32 | // given 33 | var test = 'helloworld2(int,string)'; 34 | 35 | // when 36 | var displayName = utils.extractDisplayName(test); 37 | 38 | // then 39 | assert.equal(displayName, 'helloworld2'); 40 | }); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /test/iban.isValid.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Iban = require('../lib/web3/iban.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { obj: function () {}, is: false}, 7 | { obj: new Function(), is: false}, 8 | { obj: 'function', is: false}, 9 | { obj: {}, is: false}, 10 | { obj: '[]', is: false}, 11 | { obj: '[1, 2]', is: false}, 12 | { obj: '{}', is: false}, 13 | { obj: '{"a": 123, "b" :3,}', is: false}, 14 | { obj: '{"c" : 2}', is: false}, 15 | { obj: 'XE81ETHXREGGAVOFYORK', is: true}, 16 | { obj: 'XE82ETHXREGGAVOFYORK', is: false}, // control number is invalid 17 | { obj: 'XE81ETCXREGGAVOFYORK', is: false}, 18 | { obj: 'XE81ETHXREGGAVOFYORKD', is: false}, 19 | { obj: 'XE81ETHXREGGaVOFYORK', is: false}, 20 | { obj: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', is: true}, 21 | { obj: 'XE7438O073KYGTWWZN0F2WZ0R8PX5ZPPZS', is: false}, // control number is invalid 22 | { obj: 'XD7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', is: false}, 23 | { obj: 'XE1222Q908LN1QBBU6XUQSO1OHWJIOS46OO', is: true} 24 | ]; 25 | 26 | describe('lib/web3/iban', function () { 27 | describe('isValid', function () { 28 | tests.forEach(function (test) { 29 | it('shoud test if value ' + test.obj + ' is iban: ' + test.is, function () { 30 | assert.equal(Iban.isValid(test.obj), test.is); 31 | }); 32 | }); 33 | }); 34 | }); 35 | 36 | -------------------------------------------------------------------------------- /test/web3.eth.hashRate.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'hashrate'; 8 | 9 | var tests = [{ 10 | result: '0x788a8', 11 | formattedResult: 493736, 12 | call: 'eth_'+ method 13 | }]; 14 | 15 | describe('web3.eth', function () { 16 | describe(method, function () { 17 | tests.forEach(function (test, index) { 18 | it('property test: ' + index, function () { 19 | 20 | // given 21 | var provider = new FakeHttpProvider(); 22 | web3.setProvider(provider); 23 | provider.injectResult(test.result); 24 | provider.injectValidation(function (payload) { 25 | assert.equal(payload.jsonrpc, '2.0'); 26 | assert.equal(payload.method, test.call); 27 | assert.deepEqual(payload.params, []); 28 | }); 29 | 30 | // when 31 | var result = web3.eth[method]; 32 | 33 | // then 34 | assert.strictEqual(test.formattedResult, result); 35 | 36 | // clear the validation 37 | provider.injectValidation(function () {}); 38 | web3.reset(); 39 | }); 40 | }); 41 | }); 42 | }); 43 | 44 | -------------------------------------------------------------------------------- /lib/web3/namereg.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** 18 | * @file namereg.js 19 | * @author Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | var globalRegistrarAbi = require('../contracts/GlobalRegistrar.json'); 24 | var icapRegistrarAbi= require('../contracts/ICAPRegistrar.json'); 25 | 26 | var globalNameregAddress = '0xc6d9d2cd449a754c494264e1809c50e34d64562b'; 27 | var icapNameregAddress = '0xa1a111bc074c9cfa781f0c38e63bd51c91b8af00'; 28 | 29 | module.exports = { 30 | global: { 31 | abi: globalRegistrarAbi, 32 | address: globalNameregAddress 33 | }, 34 | icap: { 35 | abi: icapRegistrarAbi, 36 | address: icapNameregAddress 37 | } 38 | }; 39 | 40 | -------------------------------------------------------------------------------- /test/jsonrpc.toPayload.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Jsonrpc = require('../lib/web3/jsonrpc'); 4 | 5 | describe('jsonrpc', function () { 6 | describe('toPayload', function () { 7 | it('should create basic payload', function () { 8 | 9 | // given 10 | var method = 'helloworld'; 11 | 12 | // when 13 | var payload = Jsonrpc.toPayload(method); 14 | 15 | // then 16 | assert.equal(payload.jsonrpc, '2.0'); 17 | assert.equal(payload.method, method); 18 | assert.equal(Array.isArray(payload.params), true); 19 | assert.equal(payload.params.length, 0); 20 | assert.equal(typeof payload.id, 'number'); 21 | }); 22 | 23 | it('should create payload with params', function () { 24 | 25 | // given 26 | var method = 'helloworld1'; 27 | var params = [123, 'test']; 28 | 29 | // when 30 | var payload = Jsonrpc.toPayload(method, params); 31 | 32 | // then 33 | assert.equal(payload.jsonrpc, '2.0'); 34 | assert.equal(payload.method, method); 35 | assert.equal(payload.params.length, 2); 36 | assert.equal(payload.params[0], params[0]); 37 | assert.equal(payload.params[1], params[1]); 38 | assert.equal(typeof payload.id, 'number'); 39 | }); 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /example/balance.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 28 | 29 | 30 |

coinbase balance

31 | 32 |
33 |
34 |
35 |
36 |
37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /lib/web3/extend.js: -------------------------------------------------------------------------------- 1 | var formatters = require('./formatters'); 2 | var utils = require('./../utils/utils'); 3 | var Method = require('./method'); 4 | var Property = require('./property'); 5 | 6 | // TODO: refactor, so the input params are not altered. 7 | // it's necessary to make same 'extension' work with multiple providers 8 | var extend = function (web3) { 9 | /* jshint maxcomplexity:5 */ 10 | var ex = function (extension) { 11 | 12 | var extendedObject; 13 | if (extension.property) { 14 | if (!web3[extension.property]) { 15 | web3[extension.property] = {}; 16 | } 17 | extendedObject = web3[extension.property]; 18 | } else { 19 | extendedObject = web3; 20 | } 21 | 22 | if (extension.methods) { 23 | extension.methods.forEach(function (method) { 24 | method.attachToObject(extendedObject); 25 | method.setRequestManager(web3._requestManager); 26 | }); 27 | } 28 | 29 | if (extension.properties) { 30 | extension.properties.forEach(function (property) { 31 | property.attachToObject(extendedObject); 32 | property.setRequestManager(web3._requestManager); 33 | }); 34 | } 35 | }; 36 | 37 | ex.formatters = formatters; 38 | ex.utils = utils; 39 | ex.Method = Method; 40 | ex.Property = Property; 41 | 42 | return ex; 43 | }; 44 | 45 | 46 | 47 | module.exports = extend; 48 | 49 | -------------------------------------------------------------------------------- /test/method.validateArgs.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Method = require('../lib/web3/method'); 4 | var errors = require('../lib/web3/errors'); 5 | 6 | describe('lib/web3/method', function () { 7 | describe('validateArgs', function () { 8 | it('should pass', function () { 9 | 10 | // given 11 | var method = new Method({ 12 | params: 1 13 | }); 14 | 15 | var args = [1]; 16 | var args2 = ['heloas']; 17 | 18 | // when 19 | var test = function () { method.validateArgs(args); }; 20 | var test2 = function () { method.validateArgs(args2); }; 21 | 22 | // then 23 | assert.doesNotThrow(test); 24 | assert.doesNotThrow(test2); 25 | }); 26 | 27 | it('should return call based on args', function () { 28 | 29 | // given 30 | var method = new Method({ 31 | params: 2 32 | }); 33 | 34 | var args = [1]; 35 | var args2 = ['heloas', '12', 3]; 36 | 37 | // when 38 | var test = function () { method.validateArgs(args); }; 39 | var test2 = function () { method.validateArgs(args2); }; 40 | 41 | // then 42 | assert.throws(test, errors.InvalidNumberOfRPCParams().message); 43 | assert.throws(test2, errors.InvalidNumberOfRPCParams().message); 44 | }); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /test/requestmanager.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var RequestManager = require('../lib/web3/requestmanager'); 4 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 5 | 6 | // TODO: handling errors! 7 | // TODO: validation of params! 8 | 9 | describe('lib/web3/requestmanager', function () { 10 | describe('send', function () { 11 | it('should return expected result synchronously', function () { 12 | var provider = new FakeHttpProvider(); 13 | var manager = new RequestManager(provider); 14 | var expected = 'hello_world'; 15 | provider.injectResult(expected); 16 | 17 | var result = manager.send({ 18 | method: 'test', 19 | params: [1,2,3] 20 | }); 21 | 22 | assert.equal(expected, result); 23 | }); 24 | 25 | it('should return expected result asynchronously', function (done) { 26 | var provider = new FakeHttpProvider(); 27 | var manager = new RequestManager(provider); 28 | var expected = 'hello_world'; 29 | provider.injectResult(expected); 30 | 31 | manager.sendAsync({ 32 | method: 'test', 33 | params: [1,2,3] 34 | }, function (error, result) { 35 | assert.equal(error, null); 36 | assert.equal(expected, result); 37 | done(); 38 | }); 39 | }); 40 | }); 41 | }); 42 | 43 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web3", 3 | "namespace": "ethereum", 4 | "version": "0.17.0-alpha", 5 | "description": "Ethereum Compatible JavaScript API", 6 | "main": [ 7 | "./dist/web3.js", 8 | "./dist/web3.min.js" 9 | ], 10 | "dependencies": { 11 | "bignumber.js": ">=2.0.0", 12 | "crypto-js": "~3.1.4" 13 | }, 14 | "repository": { 15 | "type": "git", 16 | "url": "https://github.com/ethereum/web3.js.git" 17 | }, 18 | "homepage": "https://github.com/ethereum/web3.js", 19 | "bugs": { 20 | "url": "https://github.com/ethereum/web3.js/issues" 21 | }, 22 | "keywords": [ 23 | "ethereum", 24 | "javascript", 25 | "API" 26 | ], 27 | "authors": [ 28 | { 29 | "name": "Marek Kotewicz", 30 | "email": "marek@ethdev.com", 31 | "homepage": "https://github.com/debris" 32 | }, 33 | { 34 | "name": "Marian Oancea", 35 | "email": "marian@ethdev.com", 36 | "homepage": "https://github.com/cubedro" 37 | }, 38 | { 39 | "name": "Fabian Vogelsteller", 40 | "email": "fabian@ethdev.com", 41 | "homepage": "https://github.com/frozeman" 42 | } 43 | ], 44 | "license": "LGPL-3.0", 45 | "ignore": [ 46 | "example", 47 | "lib", 48 | "node_modules", 49 | "package.json", 50 | "package.js", 51 | ".versions", 52 | ".bowerrc", 53 | ".editorconfig", 54 | ".gitignore", 55 | ".jshintrc", 56 | ".npmignore", 57 | ".travis.yml", 58 | "gulpfile.js", 59 | "index.js", 60 | "**/*.txt" 61 | ] 62 | } 63 | -------------------------------------------------------------------------------- /test/web3.eth.methods.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index.js'); 4 | var web3 = new Web3(); 5 | var u = require('./helpers/test.utils.js'); 6 | 7 | describe('web3.eth', function() { 8 | describe('methods', function() { 9 | u.methodExists(web3.eth, 'getBalance'); 10 | u.methodExists(web3.eth, 'getStorageAt'); 11 | u.methodExists(web3.eth, 'getTransactionCount'); 12 | u.methodExists(web3.eth, 'getCode'); 13 | u.methodExists(web3.eth, 'sendTransaction'); 14 | u.methodExists(web3.eth, 'call'); 15 | u.methodExists(web3.eth, 'getBlock'); 16 | u.methodExists(web3.eth, 'getTransaction'); 17 | u.methodExists(web3.eth, 'getUncle'); 18 | u.methodExists(web3.eth, 'getCompilers'); 19 | u.methodExists(web3.eth.compile, 'lll'); 20 | u.methodExists(web3.eth.compile, 'solidity'); 21 | u.methodExists(web3.eth.compile, 'serpent'); 22 | u.methodExists(web3.eth, 'getBlockTransactionCount'); 23 | u.methodExists(web3.eth, 'getBlockUncleCount'); 24 | u.methodExists(web3.eth, 'filter'); 25 | u.methodExists(web3.eth, 'contract'); 26 | 27 | u.propertyExists(web3.eth, 'coinbase'); 28 | u.propertyExists(web3.eth, 'mining'); 29 | u.propertyExists(web3.eth, 'gasPrice'); 30 | u.propertyExists(web3.eth, 'accounts'); 31 | u.propertyExists(web3.eth, 'defaultBlock'); 32 | u.propertyExists(web3.eth, 'blockNumber'); 33 | u.propertyExists(web3.eth, 'protocolVersion'); 34 | }); 35 | }); 36 | 37 | -------------------------------------------------------------------------------- /test/web3.eth.getTransaction.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var BigNumber = require('bignumber.js'); 4 | var testMethod = require('./helpers/test.method.js'); 5 | 6 | var method = 'getTransaction'; 7 | 8 | var txResult = { 9 | "hash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", 10 | "nonce":"0x5", 11 | "blockHash": "0x6fd9e2a26ab", 12 | "blockNumber": "0x15df", 13 | "transactionIndex": "0x1", 14 | "from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1", 15 | "to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1", 16 | "value":"0x7f110", 17 | "gas": "0x7f110", 18 | "gasPrice":"0x09184e72a000", 19 | "input":"0x603880600c6000396000f30060" 20 | }; 21 | var formattedTxResult = { 22 | "hash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", 23 | "nonce":5, 24 | "blockHash": "0x6fd9e2a26ab", 25 | "blockNumber": 5599, 26 | "transactionIndex": 1, 27 | "from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1", 28 | "to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1", 29 | "value": new BigNumber(520464), 30 | "gas": 520464, 31 | "gasPrice": new BigNumber(10000000000000), 32 | "input":"0x603880600c6000396000f30060" 33 | }; 34 | 35 | var tests = [{ 36 | args: ['0x2dbab4c0612bf9caf4c195085547dc0612bf9caf4c1950855'], 37 | formattedArgs: ['0x2dbab4c0612bf9caf4c195085547dc0612bf9caf4c1950855'], 38 | result: txResult, 39 | formattedResult: formattedTxResult, 40 | call: 'eth_'+ method + 'ByHash' 41 | }]; 42 | 43 | testMethod.runTests('eth', method, tests); 44 | 45 | -------------------------------------------------------------------------------- /test/method.extractCallback.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Method = require('../lib/web3/method'); 4 | 5 | describe('lib/web3/method', function () { 6 | describe('extractCallback', function () { 7 | it('should extract callback', function () { 8 | 9 | // given 10 | var method = new Method({}); 11 | var callback = function () { }; 12 | var args = [1, callback] 13 | 14 | // when 15 | var result = method.extractCallback(args); 16 | 17 | // then 18 | assert.equal(args.length, 1); 19 | assert.equal(callback, result); 20 | }); 21 | 22 | it('should extract callback created using newFunction', function () { 23 | 24 | // given 25 | var method = new Method({}); 26 | var callback = new Function (); 27 | var args = [1, callback] 28 | 29 | // when 30 | var result = method.extractCallback(args); 31 | 32 | // then 33 | assert.equal(args.length, 1); 34 | assert.equal(callback, result); 35 | }); 36 | 37 | it('should not extract the callback', function () { 38 | 39 | // given 40 | var method = new Method({}); 41 | var args = [1, 2] 42 | 43 | // when 44 | var result = method.extractCallback(args); 45 | 46 | // then 47 | assert.equal(args.length, 2); 48 | assert.equal(result, null); 49 | }); 50 | }); 51 | }); 52 | 53 | -------------------------------------------------------------------------------- /test/formatters.inputAddressFormatter.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var formatters = require('../lib/web3/formatters.js'); 4 | var BigNumber = require('bignumber.js'); 5 | 6 | var tests = [ 7 | { input: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', result: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8' }, 8 | { input: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', result: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8'}, 9 | { input: '00c5496aee77c1ba1f0854206a26dda82a81d6d8', result: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8'}, 10 | { input: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae', result: '0x11f4d0a3c12e86b4b5f39b213f7e19d048276dae' } 11 | ]; 12 | 13 | var errorTests = [ 14 | '0x0c5496aee77c1ba1f0854206a26dda82a81d6d8', 15 | '0x0c5496aee77c1ba1f0854206a26dda82a81d6d8', 16 | '00c5496aee77c1ba1f0854206a26dda82a81d6d', 17 | 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZE', 18 | '0x' 19 | ] 20 | 21 | describe('formatters', function () { 22 | describe('inputAddressFormatter', function () { 23 | tests.forEach(function(test){ 24 | it('should return the correct value', function () { 25 | assert.deepEqual(formatters.inputAddressFormatter(test.input), test.result); 26 | }); 27 | }); 28 | }); 29 | }); 30 | 31 | 32 | describe('formatters', function () { 33 | describe('inputAddressFormatter', function () { 34 | errorTests.forEach(function(test){ 35 | it('should throw an exception', function () { 36 | assert.throws(function () { 37 | formatters.inputAddressFormatter(test); 38 | }); 39 | }); 40 | }); 41 | }); 42 | }); 43 | -------------------------------------------------------------------------------- /lib/web3/methods/net.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** @file eth.js 18 | * @authors: 19 | * Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | var utils = require('../../utils/utils'); 24 | var Property = require('../property'); 25 | 26 | var Net = function (web3) { 27 | this._requestManager = web3._requestManager; 28 | 29 | var self = this; 30 | 31 | properties().forEach(function(p) { 32 | p.attachToObject(self); 33 | p.setRequestManager(web3._requestManager); 34 | }); 35 | }; 36 | 37 | /// @returns an array of objects describing web3.eth api properties 38 | var properties = function () { 39 | return [ 40 | new Property({ 41 | name: 'listening', 42 | getter: 'net_listening' 43 | }), 44 | new Property({ 45 | name: 'peerCount', 46 | getter: 'net_peerCount', 47 | outputFormatter: utils.toDecimal 48 | }) 49 | ]; 50 | }; 51 | 52 | module.exports = Net; 53 | -------------------------------------------------------------------------------- /test/utils.extractTypeName.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var utils = require('../lib/utils/utils.js'); 3 | 4 | describe('lib/utils/utils', function () { 5 | describe('extractTypeName', function () { 6 | it('should extract type name from method with no params', function () { 7 | 8 | // given 9 | var test = 'helloworld()'; 10 | 11 | // when 12 | var typeName = utils.extractTypeName(test); 13 | 14 | // then 15 | assert.equal(typeName, ''); 16 | }); 17 | 18 | it('should extract type name from method with one param', function () { 19 | 20 | // given 21 | var test = 'helloworld1(int)'; 22 | 23 | // when 24 | var typeName = utils.extractTypeName(test); 25 | 26 | // then 27 | assert.equal(typeName, 'int'); 28 | }); 29 | 30 | it('should extract type name from method with two params', function () { 31 | 32 | // given 33 | var test = 'helloworld2(int,string)'; 34 | 35 | // when 36 | var typeName = utils.extractTypeName(test); 37 | 38 | // then 39 | assert.equal(typeName, 'int,string'); 40 | }); 41 | 42 | it('should extract type name from method with spaces between params', function () { 43 | 44 | // given 45 | var test = 'helloworld3(int, string)'; 46 | 47 | // when 48 | var typeName = utils.extractTypeName(test); 49 | 50 | // then 51 | assert.equal(typeName, 'int,string'); 52 | }); 53 | 54 | }); 55 | }); 56 | -------------------------------------------------------------------------------- /test/jsonrpc.toBatchPayload.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var Jsonrpc = require('../lib/web3/jsonrpc'); 3 | 4 | describe('jsonrpc', function () { 5 | describe('toBatchPayload', function () { 6 | it('should create basic batch payload', function () { 7 | 8 | // given 9 | var messages = [{ 10 | method: 'helloworld' 11 | }, { 12 | method: 'test2', 13 | params: [1] 14 | }]; 15 | 16 | // when 17 | var payload = Jsonrpc.toBatchPayload(messages); 18 | 19 | // then 20 | assert.equal(Array.isArray(payload), true); 21 | assert.equal(payload.length, 2); 22 | assert.equal(payload[0].jsonrpc, '2.0'); 23 | assert.equal(payload[1].jsonrpc, '2.0'); 24 | assert.equal(payload[0].method, 'helloworld'); 25 | assert.equal(payload[1].method, 'test2'); 26 | assert.equal(Array.isArray(payload[0].params), true); 27 | assert.equal(payload[1].params.length, 1); 28 | assert.equal(payload[1].params[0], 1); 29 | assert.equal(typeof payload[0].id, 'number'); 30 | assert.equal(typeof payload[1].id, 'number'); 31 | assert.equal(payload[0].id + 1, payload[1].id); 32 | }); 33 | 34 | it('should create batch payload for empty input array', function () { 35 | 36 | // given 37 | var messages = []; 38 | 39 | // when 40 | var payload = Jsonrpc.toBatchPayload(messages); 41 | 42 | // then 43 | assert.equal(Array.isArray(payload), true); 44 | assert.equal(payload.length, 0); 45 | }); 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /test/web3.shh.filter.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Web3 = require('../index'); 3 | var web3 = new Web3(); 4 | var assert = chai.assert; 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'newMessageFilter'; 8 | 9 | var tests = [{ 10 | args: [{ 11 | symKeyID: '47d33b27bb249a2dbab4c0612bf9caf4c1950855', 12 | sig: '0x55dd47d33b27bb249a2dbab4c0612bf9caf4c1950855', 13 | minPow: 0.5, 14 | topics: ['0x32dd4f54', '0x564b4566'], 15 | allowP2P: false 16 | }], 17 | formattedArgs: [{ 18 | symKeyID: '47d33b27bb249a2dbab4c0612bf9caf4c1950855', 19 | sig: '0x55dd47d33b27bb249a2dbab4c0612bf9caf4c1950855', 20 | minPow: 0.5, 21 | topics: ['0x32dd4f54', '0x564b4566'], 22 | allowP2P: false 23 | }], 24 | result: '0xf', 25 | formattedResult: '0xf', 26 | call: 'shh_newMessageFilter' 27 | }]; 28 | 29 | describe('shh', function () { 30 | describe(method, function () { 31 | tests.forEach(function (test, index) { 32 | it('property test: ' + index, function () { 33 | 34 | // given 35 | var provider = new FakeHttpProvider(); 36 | web3.setProvider(provider); 37 | web3.reset(); 38 | provider.injectResult(test.result); 39 | provider.injectValidation(function (payload) { 40 | assert.equal(payload.jsonrpc, '2.0'); 41 | assert.equal(payload.method, test.call); 42 | assert.deepEqual(payload.params, test.formattedArgs); 43 | }); 44 | 45 | // call 46 | web3.shh[method].apply(web3.shh, test.args); 47 | 48 | }); 49 | }); 50 | }); 51 | }); 52 | 53 | -------------------------------------------------------------------------------- /test/web3.eth.sendIBANTransaction.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider2 = require('./helpers/FakeHttpProvider2'); 6 | 7 | describe('web3.eth.sendIBANTransaction', function () { 8 | it('should send transaction', function () { 9 | 10 | var iban = 'XE81ETHXREGGAVOFYORK'; 11 | var address = '0x1234567890123456789012345678901234500000'; 12 | var exAddress = '0x1234567890123456789012345678901234567890' 13 | 14 | var provider = new FakeHttpProvider2(); 15 | web3.setProvider(provider); 16 | web3.reset(); 17 | 18 | provider.injectResultList([{ 19 | result: exAddress 20 | }, { 21 | result: '' 22 | }]); 23 | 24 | var step = 0; 25 | provider.injectValidation(function (payload) { 26 | if (step === 0) { 27 | step++; 28 | assert.equal(payload.method, 'eth_call'); 29 | assert.deepEqual(payload.params, [{ 30 | data: "0x3b3b57de5852454700000000000000000000000000000000000000000000000000000000", 31 | to: web3.eth.icapNamereg().address 32 | }, "latest"]); 33 | 34 | return; 35 | } 36 | assert.equal(payload.method, 'eth_sendTransaction'); 37 | assert.deepEqual(payload.params, [{ 38 | data: '0xb214faa54741564f46594f524b0000000000000000000000000000000000000000000000', 39 | from: address, 40 | to: exAddress, 41 | value: payload.params[0].value // don't check this 42 | }]); 43 | }); 44 | 45 | web3.eth.sendIBANTransaction(address, iban, 10000); 46 | 47 | }); 48 | }); 49 | 50 | -------------------------------------------------------------------------------- /lib/web3/errors.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** 18 | * @file errors.js 19 | * @author Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | module.exports = { 24 | InvalidNumberOfSolidityArgs: function () { 25 | return new Error('Invalid number of arguments to Solidity function'); 26 | }, 27 | InvalidNumberOfRPCParams: function () { 28 | return new Error('Invalid number of input parameters to RPC method'); 29 | }, 30 | InvalidConnection: function (host){ 31 | return new Error('CONNECTION ERROR: Couldn\'t connect to node '+ host +'.'); 32 | }, 33 | InvalidProvider: function () { 34 | return new Error('Provider not set or invalid'); 35 | }, 36 | InvalidResponse: function (result){ 37 | var message = !!result && !!result.error && !!result.error.message ? result.error.message : 'Invalid JSON RPC response: ' + JSON.stringify(result); 38 | return new Error(message); 39 | }, 40 | ConnectionTimeout: function (ms){ 41 | return new Error('CONNECTION TIMEOUT: timeout of ' + ms + ' ms achived'); 42 | } 43 | }; 44 | -------------------------------------------------------------------------------- /test/web3.eth.getTransactionFromBlock.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var BigNumber = require('bignumber.js'); 4 | var testMethod = require('./helpers/test.method.js'); 5 | 6 | var method = 'getTransactionFromBlock'; 7 | 8 | var txResult = { 9 | "status": "mined", 10 | "hash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", 11 | "nonce":"0xb", 12 | "blockHash": "0x6fd9e2a26ab", 13 | "blockNumber": "0x15df", 14 | "transactionIndex": "0x1", 15 | "from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1", 16 | "to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1", 17 | "value":"0x7f110", 18 | "gas": "0x7f110", 19 | "gasPrice":"0x09184e72a000", 20 | "input":"0x603880600c6000396000f30060" 21 | }; 22 | var formattedTxResult = { 23 | "status": "mined", 24 | "hash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", 25 | "nonce":11, 26 | "blockHash": "0x6fd9e2a26ab", 27 | "blockNumber": 5599, 28 | "transactionIndex": 1, 29 | "from":"0x407d73d8a49eeb85d32cf465507dd71d507100c1", 30 | "to":"0x85h43d8a49eeb85d32cf465507dd71d507100c1", 31 | "value": new BigNumber(520464), 32 | "gas": 520464, 33 | "gasPrice": new BigNumber(10000000000000), 34 | "input":"0x603880600c6000396000f30060" 35 | }; 36 | 37 | var tests = [{ 38 | args: ['0x2dbab4c0612bf9caf4c195085547dc0612bf9caf4c1950855', 2], 39 | formattedArgs: ['0x2dbab4c0612bf9caf4c195085547dc0612bf9caf4c1950855', '0x2'], 40 | result: txResult, 41 | formattedResult: formattedTxResult, 42 | call: 'eth_getTransactionByBlockHashAndIndex' 43 | },{ 44 | args: [436, 11], 45 | formattedArgs: ['0x1b4', '0xb'], 46 | result: txResult, 47 | formattedResult: formattedTxResult, 48 | call: 'eth_getTransactionByBlockNumberAndIndex' 49 | }]; 50 | 51 | testMethod.runTests('eth', method, tests); 52 | 53 | -------------------------------------------------------------------------------- /test/web3.eth.getStorageAt.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Web3 = require('../index'); 3 | var web3 = new Web3(); 4 | var testMethod = require('./helpers/test.method.js'); 5 | 6 | var method = 'getStorageAt'; 7 | 8 | 9 | var tests = [{ 10 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 2], 11 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x2', web3.eth.defaultBlock], 12 | result: '0x47d33b2', 13 | formattedResult: '0x47d33b2', 14 | call: 'eth_'+ method 15 | },{ 16 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 2, 0], 17 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0x2', '0x0'], 18 | result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 19 | formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 20 | call: 'eth_'+ method 21 | },{ 22 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 0xb, 0x0], 23 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0xb', '0x0'], 24 | result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 25 | formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 26 | call: 'eth_'+ method 27 | }, { 28 | args: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', 0xb, 'latest'], 29 | formattedArgs: ['0x47d33b27bb249a2dbab4c0612bf9caf4c1950855', '0xb', 'latest'], 30 | result: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 31 | formattedResult: '0x47d33b27bb249a2dbab4c0612bf9caf4747d33b27bb249a2dbab4c0612bf9cafd33b27bb249a2dbab4c0612bf9caf4c1950855', 32 | call: 'eth_'+ method 33 | }]; 34 | 35 | testMethod.runTests('eth', method, tests); 36 | 37 | -------------------------------------------------------------------------------- /test/httpprovider.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var SandboxedModule = require('sandboxed-module'); 4 | 5 | SandboxedModule.registerBuiltInSourceTransformer('istanbul'); 6 | var HttpProvider = SandboxedModule.require('../lib/web3/httpprovider', { 7 | requires: { 8 | 'xhr2': require('./helpers/FakeXHR2'), 9 | 'xmlhttprequest': require('./helpers/FakeXMLHttpRequest') 10 | }, 11 | singleOnly: true 12 | }); 13 | 14 | describe('lib/web3/httpprovider', function () { 15 | describe('prepareRequest', function () { 16 | it('should set request header', function () { 17 | var provider = new HttpProvider('http://localhost:8545', 0 , null, null, [{name: 'Access-Control-Allow-Origin', value: '*'}]); 18 | var result = provider.prepareRequest(true); 19 | 20 | assert.equal(typeof result, 'object'); 21 | assert.equal(result.headers['Access-Control-Allow-Origin'], '*'); 22 | }); 23 | }); 24 | 25 | describe('send', function () { 26 | it('should send basic request', function () { 27 | var provider = new HttpProvider(); 28 | var result = provider.send({}); 29 | 30 | assert.equal(typeof result, 'object'); 31 | }); 32 | }); 33 | 34 | describe('sendAsync', function () { 35 | it('should send basic async request', function (done) { 36 | var provider = new HttpProvider(); 37 | 38 | provider.sendAsync({}, function (err, result) { 39 | assert.equal(typeof result, 'object'); 40 | done(); 41 | }); 42 | }); 43 | }); 44 | 45 | describe('isConnected', function () { 46 | it('should return a boolean', function () { 47 | var provider = new HttpProvider(); 48 | 49 | assert.isBoolean(provider.isConnected()); 50 | }); 51 | }); 52 | }); 53 | -------------------------------------------------------------------------------- /test/web3.eth.getSyncing.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | var chai = require('chai'); 4 | var Web3 = require('../index'); 5 | var assert = chai.assert; 6 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 7 | 8 | describe('eth', function () { 9 | describe('getSyncing', function () { 10 | it('syncing object', function (done) { 11 | // given 12 | var provider = new FakeHttpProvider(); 13 | var web3 = new Web3(provider); 14 | provider.injectResult({ 15 | startingBlock: '0xb', 16 | currentBlock: '0xb', 17 | highestBlock: '0xb' 18 | }); 19 | provider.injectValidation(function(payload) { 20 | assert.equal(payload.jsonrpc, '2.0', 'failed'); 21 | assert.equal(payload.method, 'eth_syncing'); 22 | }); 23 | 24 | // call 25 | web3.eth.getSyncing(function(err, res){ 26 | assert.deepEqual(res, { 27 | startingBlock: 11, 28 | currentBlock: 11, 29 | highestBlock: 11 30 | }); 31 | done(); 32 | }); 33 | }); 34 | 35 | it('false', function (done) { 36 | // given 37 | var provider = new FakeHttpProvider(); 38 | var web3 = new Web3(provider); 39 | provider.injectResult(false); 40 | provider.injectValidation(function(payload) { 41 | assert.equal(payload.jsonrpc, '2.0', 'failed'); 42 | assert.equal(payload.method, 'eth_syncing'); 43 | }); 44 | 45 | // call 46 | web3.eth.getSyncing(function(err, res){ 47 | console.log('err', err, 'res', res) 48 | assert.strictEqual(res, false); 49 | done(); 50 | }); 51 | }); 52 | }); 53 | }); 54 | 55 | -------------------------------------------------------------------------------- /lib/web3/methods/db.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** @file db.js 18 | * @authors: 19 | * Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | var Method = require('../method'); 24 | 25 | var DB = function (web3) { 26 | this._requestManager = web3._requestManager; 27 | 28 | var self = this; 29 | 30 | methods().forEach(function(method) { 31 | method.attachToObject(self); 32 | method.setRequestManager(web3._requestManager); 33 | }); 34 | }; 35 | 36 | var methods = function () { 37 | var putString = new Method({ 38 | name: 'putString', 39 | call: 'db_putString', 40 | params: 3 41 | }); 42 | 43 | var getString = new Method({ 44 | name: 'getString', 45 | call: 'db_getString', 46 | params: 2 47 | }); 48 | 49 | var putHex = new Method({ 50 | name: 'putHex', 51 | call: 'db_putHex', 52 | params: 3 53 | }); 54 | 55 | var getHex = new Method({ 56 | name: 'getHex', 57 | call: 'db_getHex', 58 | params: 2 59 | }); 60 | 61 | return [ 62 | putString, getString, putHex, getHex 63 | ]; 64 | }; 65 | 66 | module.exports = DB; 67 | -------------------------------------------------------------------------------- /lib/utils/config.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** @file config.js 18 | * @authors: 19 | * Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | /** 24 | * Utils 25 | * 26 | * @module utils 27 | */ 28 | 29 | /** 30 | * Utility functions 31 | * 32 | * @class [utils] config 33 | * @constructor 34 | */ 35 | 36 | 37 | /// required to define ETH_BIGNUMBER_ROUNDING_MODE 38 | var BigNumber = require('bignumber.js'); 39 | 40 | var ETH_UNITS = [ 41 | 'wei', 42 | 'kwei', 43 | 'Mwei', 44 | 'Gwei', 45 | 'szabo', 46 | 'finney', 47 | 'femtoether', 48 | 'picoether', 49 | 'nanoether', 50 | 'microether', 51 | 'milliether', 52 | 'nano', 53 | 'micro', 54 | 'milli', 55 | 'ether', 56 | 'grand', 57 | 'Mether', 58 | 'Gether', 59 | 'Tether', 60 | 'Pether', 61 | 'Eether', 62 | 'Zether', 63 | 'Yether', 64 | 'Nether', 65 | 'Dether', 66 | 'Vether', 67 | 'Uether' 68 | ]; 69 | 70 | module.exports = { 71 | ETH_PADDING: 32, 72 | ETH_SIGNATURE_LENGTH: 4, 73 | ETH_UNITS: ETH_UNITS, 74 | ETH_BIGNUMBER_ROUNDING_MODE: { ROUNDING_MODE: BigNumber.ROUND_DOWN }, 75 | ETH_POLLING_TIMEOUT: 1000/2, 76 | defaultBlock: 'latest', 77 | defaultAccount: undefined 78 | }; 79 | 80 | -------------------------------------------------------------------------------- /test/ipcprovider.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var SandboxedModule = require('sandboxed-module'); 4 | var FakeIpcRequest = require('./helpers/FakeIpcRequest') 5 | var net = new FakeIpcRequest(); 6 | 7 | SandboxedModule.registerBuiltInSourceTransformer('istanbul'); 8 | var IpcProvider = SandboxedModule.require('../lib/web3/ipcprovider', { 9 | requires: { 10 | 'bignumber.js': require('bignumber.js'), 11 | }, 12 | singleOnly: true 13 | }); 14 | 15 | describe('lib/web3/ipcprovider', function () { 16 | describe('send', function () { 17 | it('should send basic request', function () { 18 | var provider = new IpcProvider('', net); 19 | var result = provider.send({id: 1, method: 'eth_test'}); 20 | 21 | assert.isObject(result); 22 | }); 23 | }); 24 | 25 | describe('sendAsync', function () { 26 | it('should send basic async request', function (done) { 27 | var provider = new IpcProvider('', net); 28 | provider.sendAsync({id: 1, method: 'eth_test'}, function (err, result) { 29 | assert.isObject(result); 30 | done(); 31 | }); 32 | }); 33 | }); 34 | 35 | describe('isConnected', function () { 36 | it('should return a boolean', function () { 37 | var provider = new IpcProvider('', net); 38 | 39 | assert.isBoolean(provider.isConnected()); 40 | }); 41 | 42 | it('should return false', function () { 43 | var provider = new IpcProvider('', net); 44 | 45 | provider.connection.writable = false; 46 | 47 | assert.isFalse(provider.isConnected()); 48 | }); 49 | 50 | it('should return true, when a net handle is set', function () { 51 | var provider = new IpcProvider('', net); 52 | 53 | provider.connection.writable = true; 54 | 55 | assert.isTrue(provider.isConnected()); 56 | }); 57 | }); 58 | }); 59 | 60 | -------------------------------------------------------------------------------- /test/utils.toTwosComplement.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by danielbruce on 2017-09-25. 3 | */ 4 | 5 | /** 6 | * Created by danielbruce on 2017-09-25. 7 | */ 8 | var chai = require('chai'); 9 | var utils = require('../lib/utils/utils'); 10 | var BigNumber = require('bignumber.js'); 11 | var assert = chai.assert; 12 | 13 | var tests = [ 14 | { value: 1, expected: new BigNumber(1).round() }, 15 | { value: '1', expected: new BigNumber(1) }, 16 | { value: '0x1', expected: new BigNumber(1) }, 17 | { value: '15', expected: new BigNumber(15)}, 18 | { value: '0xf', expected: new BigNumber(15)}, 19 | { value: -1, expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-1)).plus(1)}, 20 | { value: '-1', expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-1)).plus(1)}, 21 | { value: '-0x1', expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-1)).plus(1)}, 22 | { value: '-15', expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-15)).plus(1)}, 23 | { value: '-0xf', expected: new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).plus(new BigNumber(-15)).plus(1)}, 24 | { value: 0, expected: new BigNumber(0)}, 25 | { value: '0', expected: new BigNumber(0)}, 26 | { value: '0x0', expected: new BigNumber(0)}, 27 | { value: -0, expected: new BigNumber(0)}, 28 | { value: '-0', expected: new BigNumber(0)}, 29 | { value: '-0x0', expected: new BigNumber(0)}, 30 | { value: new BigNumber(15), expected: new BigNumber(15)} 31 | ]; 32 | 33 | describe('lib/utils/utils', function () { 34 | describe('toTwosComplement', function () { 35 | tests.forEach(function (test) { 36 | it('printing ' + test.value, function () { 37 | assert(test.expected.equals(utils.toTwosComplement(test.value))); 38 | }); 39 | }); 40 | }); 41 | }); 42 | -------------------------------------------------------------------------------- /test/utils.fromDecimal.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var assert = chai.assert; 4 | 5 | var tests = [ 6 | { value: 1, expected: '0x1' }, 7 | { value: '1', expected: '0x1' }, 8 | { value: '0x1', expected: '0x1'}, 9 | { value: '0x01', expected: '0x1'}, 10 | { value: 15, expected: '0xf'}, 11 | { value: '15', expected: '0xf'}, 12 | { value: '0xf', expected: '0xf'}, 13 | { value: '0x0f', expected: '0xf'}, 14 | { value: -1, expected: '-0x1'}, 15 | { value: '-1', expected: '-0x1'}, 16 | { value: '-0x1', expected: '-0x1'}, 17 | { value: '-0x01', expected: '-0x1'}, 18 | { value: -15, expected: '-0xf'}, 19 | { value: '-15', expected: '-0xf'}, 20 | { value: '-0xf', expected: '-0xf'}, 21 | { value: '-0x0f', expected: '-0xf'}, 22 | { value: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}, 23 | { value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'}, 24 | { value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}, 25 | { value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'}, 26 | { value: 0, expected: '0x0'}, 27 | { value: '0', expected: '0x0'}, 28 | { value: '0x0', expected: '0x0'}, 29 | { value: -0, expected: '0x0'}, 30 | { value: '-0', expected: '0x0'}, 31 | { value: '-0x0', expected: '0x0'} 32 | ]; 33 | 34 | describe('lib/utils/utils', function () { 35 | describe('fromDecimal', function () { 36 | tests.forEach(function (test) { 37 | it('should turn ' + test.value + ' to ' + test.expected, function () { 38 | assert.equal(utils.fromDecimal(test.value), test.expected); 39 | }); 40 | }); 41 | }); 42 | }); 43 | 44 | -------------------------------------------------------------------------------- /test/utils.toWei.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils'); 3 | var assert = chai.assert; 4 | 5 | describe('lib/utils/utils', function () { 6 | describe('toWei', function () { 7 | it('should return the correct value', function () { 8 | 9 | assert.equal(utils.toWei(1, 'wei'), '1'); 10 | assert.equal(utils.toWei(1, 'kwei'), '1000'); 11 | assert.equal(utils.toWei(1, 'Kwei'), '1000'); 12 | assert.equal(utils.toWei(1, 'babbage'), '1000'); 13 | assert.equal(utils.toWei(1, 'mwei'), '1000000'); 14 | assert.equal(utils.toWei(1, 'Mwei'), '1000000'); 15 | assert.equal(utils.toWei(1, 'lovelace'), '1000000'); 16 | assert.equal(utils.toWei(1, 'gwei'), '1000000000'); 17 | assert.equal(utils.toWei(1, 'Gwei'), '1000000000'); 18 | assert.equal(utils.toWei(1, 'shannon'), '1000000000'); 19 | assert.equal(utils.toWei(1, 'szabo'), '1000000000000'); 20 | assert.equal(utils.toWei(1, 'finney'), '1000000000000000'); 21 | assert.equal(utils.toWei(1, 'ether'), '1000000000000000000'); 22 | assert.equal(utils.toWei(1, 'kether'), '1000000000000000000000'); 23 | assert.equal(utils.toWei(1, 'grand'), '1000000000000000000000'); 24 | assert.equal(utils.toWei(1, 'mether'), '1000000000000000000000000'); 25 | assert.equal(utils.toWei(1, 'gether'), '1000000000000000000000000000'); 26 | assert.equal(utils.toWei(1, 'tether'), '1000000000000000000000000000000'); 27 | 28 | assert.equal(utils.toWei(1, 'kwei'), utils.toWei(1, 'femtoether')); 29 | assert.equal(utils.toWei(1, 'szabo'), utils.toWei(1, 'microether')); 30 | assert.equal(utils.toWei(1, 'finney'), utils.toWei(1, 'milliether')); 31 | assert.equal(utils.toWei(1, 'milli'), utils.toWei(1, 'milliether')); 32 | assert.equal(utils.toWei(1, 'milli'), utils.toWei(1000, 'micro')); 33 | 34 | assert.throws(function () {utils.toWei(1, 'wei1');}, Error); 35 | }); 36 | }); 37 | }); 38 | -------------------------------------------------------------------------------- /test/web3.eth.blockNumber.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | var method = 'blockNumber'; 8 | 9 | var tests = [{ 10 | result: '0xb', 11 | formattedResult: 11, 12 | call: 'eth_'+ method 13 | }]; 14 | 15 | describe('web3.eth', function () { 16 | describe(method, function () { 17 | tests.forEach(function (test, index) { 18 | it('property test: ' + index, function () { 19 | 20 | // given 21 | var provider = new FakeHttpProvider(); 22 | web3.setProvider(provider); 23 | provider.injectResult(test.result); 24 | provider.injectValidation(function (payload) { 25 | assert.equal(payload.jsonrpc, '2.0'); 26 | assert.equal(payload.method, test.call); 27 | assert.deepEqual(payload.params, []); 28 | }); 29 | 30 | // when 31 | var result = web3.eth[method]; 32 | 33 | // then 34 | assert.strictEqual(test.formattedResult, result); 35 | }); 36 | 37 | it('async get property test: ' + index, function (done) { 38 | 39 | // given 40 | var provider = new FakeHttpProvider(); 41 | web3.setProvider(provider); 42 | provider.injectResult(test.result); 43 | provider.injectValidation(function (payload) { 44 | assert.equal(payload.jsonrpc, '2.0'); 45 | assert.equal(payload.method, test.call); 46 | assert.deepEqual(payload.params, []); 47 | }); 48 | 49 | // when 50 | web3.eth.getBlockNumber(function (err, result) { 51 | assert.strictEqual(test.formattedResult, result); 52 | done(); 53 | }); 54 | 55 | }); 56 | }); 57 | }); 58 | }); 59 | 60 | -------------------------------------------------------------------------------- /test/bloom.testAddress.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var bloom = require('../lib/utils/bloom.js') 4 | var assert = chai.assert; 5 | 6 | var addressTests = [ 7 | {bloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', address: '0xb794f5ea0ba39494ce839613fffba74279579268', result: false}, 8 | {bloom: '0x0020008400000010000000000400000200000008000000000010000000002000000080000020000000080004000000010000000000000040000000000000000000000001000200008000000D000000000010000400000400000100000000000001400008220000000000004000040802004000200000000000000010000041000000020100008000000000000000000000000010000000080000000000800900000000000000000000000000100000800000000000000C28000000000000010000000002000040002000000080000000000000000000000020120020000020200000000040000000000000040000000400000000000000000000020000000000', address: '0x54D2C22A7E422DFC71D09D1557D4EBA6C9997D12', result: true}, 9 | {bloom: '0x0020008400000010000000000400000200000008000000000010000000002000000080000020000000080004000000010000000000000040000000000000000000000001000200008000000D000000000010000400000400000100000000000001400008220000000000004000040802004000200000000000000010000041000000020100008000000000000000000000000010000000080000000000800900000000000000000000000000100000800000000000000C28000000000000010000000002000040002000000080000000000000000000000020120020000020200000000040000000000000040000000400000000000000000000020000000000', address: '0x54d2c22a7e422dfc71d09d1557d4eba6c9997d13', result: false} 10 | ]; 11 | 12 | describe('lib/utils/bloom', function() { 13 | describe('testAddress', function() { 14 | addressTests.forEach(function(test) { 15 | assert.equal(bloom.testAddress(test.bloom, test.address), test.result); 16 | }); 17 | }); 18 | }); -------------------------------------------------------------------------------- /lib/web3/batch.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** 18 | * @file batch.js 19 | * @author Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | var Jsonrpc = require('./jsonrpc'); 24 | var errors = require('./errors'); 25 | 26 | var Batch = function (web3) { 27 | this.requestManager = web3._requestManager; 28 | this.requests = []; 29 | }; 30 | 31 | /** 32 | * Should be called to add create new request to batch request 33 | * 34 | * @method add 35 | * @param {Object} jsonrpc requet object 36 | */ 37 | Batch.prototype.add = function (request) { 38 | this.requests.push(request); 39 | }; 40 | 41 | /** 42 | * Should be called to execute batch request 43 | * 44 | * @method execute 45 | */ 46 | Batch.prototype.execute = function () { 47 | var requests = this.requests; 48 | this.requestManager.sendBatch(requests, function (err, results) { 49 | results = results || []; 50 | requests.map(function (request, index) { 51 | return results[index] || {}; 52 | }).forEach(function (result, index) { 53 | if (requests[index].callback) { 54 | 55 | if (!Jsonrpc.isValidResponse(result)) { 56 | return requests[index].callback(errors.InvalidResponse(result)); 57 | } 58 | 59 | requests[index].callback(null, (requests[index].format ? requests[index].format(result.result) : result.result)); 60 | } 61 | }); 62 | }); 63 | }; 64 | 65 | module.exports = Batch; 66 | 67 | -------------------------------------------------------------------------------- /lib/contracts/ICAPRegistrar.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "constant": true, 4 | "inputs": [ 5 | { 6 | "name": "_name", 7 | "type": "bytes32" 8 | } 9 | ], 10 | "name": "owner", 11 | "outputs": [ 12 | { 13 | "name": "", 14 | "type": "address" 15 | } 16 | ], 17 | "type": "function" 18 | }, 19 | { 20 | "constant": false, 21 | "inputs": [ 22 | { 23 | "name": "_name", 24 | "type": "bytes32" 25 | }, 26 | { 27 | "name": "_refund", 28 | "type": "address" 29 | } 30 | ], 31 | "name": "disown", 32 | "outputs": [], 33 | "type": "function" 34 | }, 35 | { 36 | "constant": true, 37 | "inputs": [ 38 | { 39 | "name": "_name", 40 | "type": "bytes32" 41 | } 42 | ], 43 | "name": "addr", 44 | "outputs": [ 45 | { 46 | "name": "", 47 | "type": "address" 48 | } 49 | ], 50 | "type": "function" 51 | }, 52 | { 53 | "constant": false, 54 | "inputs": [ 55 | { 56 | "name": "_name", 57 | "type": "bytes32" 58 | } 59 | ], 60 | "name": "reserve", 61 | "outputs": [], 62 | "type": "function" 63 | }, 64 | { 65 | "constant": false, 66 | "inputs": [ 67 | { 68 | "name": "_name", 69 | "type": "bytes32" 70 | }, 71 | { 72 | "name": "_newOwner", 73 | "type": "address" 74 | } 75 | ], 76 | "name": "transfer", 77 | "outputs": [], 78 | "type": "function" 79 | }, 80 | { 81 | "constant": false, 82 | "inputs": [ 83 | { 84 | "name": "_name", 85 | "type": "bytes32" 86 | }, 87 | { 88 | "name": "_a", 89 | "type": "address" 90 | } 91 | ], 92 | "name": "setAddr", 93 | "outputs": [], 94 | "type": "function" 95 | }, 96 | { 97 | "anonymous": false, 98 | "inputs": [ 99 | { 100 | "indexed": true, 101 | "name": "name", 102 | "type": "bytes32" 103 | } 104 | ], 105 | "name": "Changed", 106 | "type": "event" 107 | } 108 | ] 109 | -------------------------------------------------------------------------------- /test/formatters.outputLogFormatter.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var formatters = require('../lib/web3/formatters.js'); 3 | 4 | describe('formatters', function () { 5 | describe('outputLogFormatter', function () { 6 | it('should return the correct value', function () { 7 | 8 | assert.deepEqual(formatters.outputLogFormatter({ 9 | transactionIndex: '0x3e8', 10 | logIndex: '0x3e8', 11 | blockNumber: '0x3e8', 12 | transactionHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', 13 | blockHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', 14 | data: '0x7b2274657374223a2274657374227', 15 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 16 | }), { 17 | transactionIndex: 1000, 18 | logIndex: 1000, 19 | blockNumber: 1000, 20 | transactionHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', 21 | blockHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', 22 | data: '0x7b2274657374223a2274657374227', 23 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 24 | }); 25 | }); 26 | it('should return the correct value, when null values are present', function () { 27 | 28 | assert.deepEqual(formatters.outputLogFormatter({ 29 | transactionIndex: null, 30 | logIndex: null, 31 | blockNumber: null, 32 | transactionHash: null, 33 | blockHash: null, 34 | data: '0x7b2274657374223a2274657374227', 35 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 36 | }), { 37 | transactionIndex: null, 38 | logIndex: null, 39 | blockNumber: null, 40 | transactionHash: null, 41 | blockHash: null, 42 | data: '0x7b2274657374223a2274657374227', 43 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 44 | }); 45 | }); 46 | }); 47 | }); 48 | -------------------------------------------------------------------------------- /test/utils.toBigNumber.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var BigNumber = require('bignumber.js'); 4 | var assert = chai.assert; 5 | 6 | var tests = [ 7 | { value: 1, expected: '1' }, 8 | { value: '1', expected: '1' }, 9 | { value: '0x1', expected: '1'}, 10 | { value: '0x01', expected: '1'}, 11 | { value: 15, expected: '15'}, 12 | { value: '15', expected: '15'}, 13 | { value: '0xf', expected: '15'}, 14 | { value: '0x0f', expected: '15'}, 15 | { value: new BigNumber('f', 16), expected: '15'}, 16 | { value: -1, expected: '-1'}, 17 | { value: '-1', expected: '-1'}, 18 | { value: '-0x1', expected: '-1'}, 19 | { value: '-0x01', expected: '-1'}, 20 | { value: -15, expected: '-15'}, 21 | { value: '-15', expected: '-15'}, 22 | { value: '-0xf', expected: '-15'}, 23 | { value: '-0x0f', expected: '-15'}, 24 | { value: '0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639935'}, 25 | { value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '115792089237316195423570985008687907853269984665640564039457584007913129639933'}, 26 | { value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639935'}, 27 | { value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-115792089237316195423570985008687907853269984665640564039457584007913129639933'}, 28 | { value: 0, expected: '0'}, 29 | { value: '0', expected: '0'}, 30 | { value: '0x0', expected: '0'}, 31 | { value: -0, expected: '0'}, 32 | { value: '-0', expected: '0'}, 33 | { value: '-0x0', expected: '0'}, 34 | { value: new BigNumber(0), expected: '0'} 35 | ]; 36 | 37 | describe('lib/utils/utils', function () { 38 | describe('toBigNumber', function () { 39 | tests.forEach(function (test) { 40 | it('should turn ' + test.value + ' to ' + test.expected, function () { 41 | assert.equal(utils.toBigNumber(test.value).toString(10), test.expected); 42 | }); 43 | }); 44 | }); 45 | }); 46 | -------------------------------------------------------------------------------- /test/web3.eth.isSyncing.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var Web3 = require('../index'); 3 | var assert = chai.assert; 4 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 5 | 6 | var method = 'isSyncing'; 7 | 8 | var tests = [{ 9 | args: [], 10 | formattedArgs: [], 11 | result: [{ 12 | startingBlock: '0xb', 13 | currentBlock: '0xb', 14 | highestBlock: '0xb' 15 | }], 16 | formattedResult: { 17 | startingBlock: 11, 18 | currentBlock: 11, 19 | highestBlock: 11 20 | }, 21 | call: 'eth_syncing' 22 | }, { 23 | args: [], 24 | formattedArgs: [], 25 | result: [{ 26 | startingBlock: '0xb', 27 | currentBlock: '0xb', 28 | highestBlock: '0xb', 29 | knownStates: '0xb', 30 | pulledStates: '0xb' 31 | }], 32 | formattedResult: { 33 | startingBlock: 11, 34 | currentBlock: 11, 35 | highestBlock: 11, 36 | knownStates: 11, 37 | pulledStates: 11 38 | }, 39 | call: 'eth_syncing' 40 | }]; 41 | 42 | describe('eth', function () { 43 | describe(method, function () { 44 | tests.forEach(function (test, index) { 45 | it('property test: ' + index, function (done) { 46 | // given 47 | var provider = new FakeHttpProvider(); 48 | var web3 = new Web3(provider); 49 | provider.injectBatchResults(test.result); 50 | provider.injectValidation(function(payload) { 51 | assert.equal(payload[0].jsonrpc, '2.0', 'failed'); 52 | assert.equal(payload[0].method, test.call); 53 | assert.deepEqual(payload[0].params, test.formattedArgs); 54 | }); 55 | 56 | var count = 1; 57 | 58 | // TODO results seem to be overwritten 59 | 60 | 61 | // call 62 | var syncing = web3.eth[method](function(e, res){ 63 | if(count === 1) { 64 | assert.isTrue(res); 65 | count++; 66 | } else { 67 | assert.deepEqual(res, test.formattedResult); 68 | syncing.stopWatching(); 69 | done(); 70 | } 71 | }); 72 | 73 | }); 74 | }); 75 | }); 76 | }); 77 | 78 | -------------------------------------------------------------------------------- /test/formatters.outputTransactionFormatter.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | var formatters = require('../lib/web3/formatters.js'); 3 | var BigNumber = require('bignumber.js'); 4 | 5 | describe('formatters', function () { 6 | describe('outputTransactionFormatter', function () { 7 | it('should return the correct value', function () { 8 | 9 | assert.deepEqual(formatters.outputTransactionFormatter({ 10 | input: '0x3454645634534', 11 | from: '0x00000', 12 | to: '0x00000', 13 | value: '0x3e8', 14 | gas: '0x3e8', 15 | gasPrice: '0x3e8', 16 | nonce: '0xb', 17 | transactionIndex: '0x1', 18 | blockNumber: '0x3e8', 19 | blockHash: '0xc9b9cdc2092a9d6589d96662b1fd6949611163fb3910cf8a173cd060f17702f9' 20 | }), { 21 | input: '0x3454645634534', 22 | from: '0x00000', 23 | to: '0x00000', 24 | value: new BigNumber(1000), 25 | gas: 1000, 26 | gasPrice: new BigNumber(1000), 27 | nonce: 11, 28 | blockNumber: 1000, 29 | blockHash: '0xc9b9cdc2092a9d6589d96662b1fd6949611163fb3910cf8a173cd060f17702f9', 30 | transactionIndex: 1 31 | }); 32 | }); 33 | 34 | it('should return the correct value, when null values are present', function () { 35 | 36 | assert.deepEqual(formatters.outputTransactionFormatter({ 37 | input: '0x3454645634534', 38 | from: '0x00000', 39 | to: null, 40 | value: '0x3e8', 41 | gas: '0x3e8', 42 | gasPrice: '0x3e8', 43 | nonce: '0xb', 44 | transactionIndex: null, 45 | blockNumber: null, 46 | blockHash: null 47 | }), { 48 | input: '0x3454645634534', 49 | from: '0x00000', 50 | to: null, 51 | value: new BigNumber(1000), 52 | gas: 1000, 53 | gasPrice: new BigNumber(1000), 54 | nonce: 11, 55 | blockNumber: null, 56 | blockHash: null, 57 | transactionIndex: null 58 | }); 59 | }); 60 | }); 61 | }); 62 | -------------------------------------------------------------------------------- /test/bloom.testTopic.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils.js'); 3 | var bloom = require('../lib/utils/bloom.js') 4 | var assert = chai.assert; 5 | 6 | var topicTests = [ 7 | {bloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', topics: ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"], result: false}, 8 | {bloom: '0x0020008400000010000000000400000200000008000000000010000000002000000080000020000000080004000000010000000000000040000000000000000000000001000200008000000d000000000010000400000400000100000000000001400008220000000000004000040802004000200000000000000010000041000000020100008000000000000000000000000010000000080000000000800900000000000000000000000000100000800000000000000c28000000000000010000000002000040002000000080000000000000000000000020120020000020200000000040000000000000040000000400000000000000000000020000000000', topics: ['0xDDF252AD1BE2C89B69C2B068FC378DAA952BA7F163C4A11628F55A4DF523B3EF', '0x000000000000000000000000b3bb037d2f2341a1c2775d51909a3d944597987d', '0x00000000000000000000000041f106fe815079086c73e24c5da97a4afec1c8c3'], result: true}, 9 | {bloom: '0x0020008400000010000000000400000200000008000000000010000000002000000080000020000000080004000000010000000000000040000000000000000000000001000200008000000d000000000010000400000400000100000000000001400008220000000000004000040802004000200000000000000010000041000000020100008000000000000000000000000010000000080000000000800900000000000000000000000000100000800000000000000c28000000000000010000000002000040002000000080000000000000000000000020120020000020200000000040000000000000040000000400000000000000000000020000000000', topics: ['0x00000000000000000000000041f106fe815079086c73e24c5da97a4afec1c8c4'], result: false} 10 | ]; 11 | 12 | describe('lib/utils/bloom', function() { 13 | describe('hasTopic', function() { 14 | topicTests.forEach(function(test) { 15 | test.topics.forEach(function(topic) { 16 | assert.equal(bloom.testTopic(test.bloom, topic), test.result); 17 | }); 18 | }); 19 | }); 20 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "web3", 3 | "namespace": "ethereum", 4 | "version": "0.20.4", 5 | "description": "Ethereum JavaScript API, middleware to talk to a ethereum node over RPC", 6 | "main": "./index.js", 7 | "directories": { 8 | "lib": "./lib" 9 | }, 10 | "dependencies": { 11 | "bignumber.js": "git+https://github.com/frozeman/bignumber.js-nolookahead.git", 12 | "crypto-js": "^3.1.4", 13 | "utf8": "^2.1.1", 14 | "xhr2": "*", 15 | "xmlhttprequest": "*" 16 | }, 17 | "browser": { 18 | "xmlhttprequest": "./lib/utils/browser-xhr.js" 19 | }, 20 | "devDependencies": { 21 | "bower": ">=1.4.1", 22 | "browserify": ">=10.0", 23 | "chai": "^3.0.0", 24 | "coveralls": "^2.11.2", 25 | "del": ">=2.0.2", 26 | "exorcist": "^0.4.0", 27 | "gulp": ">=3.9.0", 28 | "gulp-jshint": ">=1.5.0", 29 | "gulp-rename": ">=1.2.0", 30 | "gulp-replace": "^0.5.3", 31 | "gulp-streamify": "0.0.5", 32 | "gulp-uglify": ">=1.2.0", 33 | "istanbul": "^0.4.4", 34 | "jshint": ">=2.5.0", 35 | "mocha": ">=2.3.3", 36 | "sandboxed-module": "^2.0.2", 37 | "vinyl-source-stream": "^1.1.0" 38 | }, 39 | "scripts": { 40 | "build": "gulp", 41 | "watch": "gulp watch", 42 | "lint": "jshint *.js lib", 43 | "test": "mocha; jshint *.js lib", 44 | "test-coveralls": "istanbul cover _mocha -- -R spec && cat coverage/lcov.info | coveralls --verbose" 45 | }, 46 | "repository": { 47 | "type": "git", 48 | "url": "https://github.com/ethereum/web3.js.git" 49 | }, 50 | "homepage": "https://github.com/ethereum/web3.js", 51 | "bugs": { 52 | "url": "https://github.com/ethereum/web3.js/issues" 53 | }, 54 | "keywords": [ 55 | "ethereum", 56 | "javascript", 57 | "API" 58 | ], 59 | "author": "ethdev.com", 60 | "authors": [ 61 | { 62 | "name": "Marek Kotewicz", 63 | "email": "marek@ethdev.com", 64 | "url": "https://github.com/debris" 65 | }, 66 | { 67 | "name": "Fabian Vogelsteller", 68 | "email": "fabian@ethdev.com", 69 | "homepage": "http://frozeman.de" 70 | }, 71 | { 72 | "name": "Marian Oancea", 73 | "email": "marian@ethdev.com", 74 | "url": "https://github.com/cubedro" 75 | }, 76 | { 77 | "name": "Gav Wood", 78 | "email": "g@ethdev.com", 79 | "homepage": "http://gavwood.com" 80 | }, 81 | { 82 | "name": "Jeffery Wilcke", 83 | "email": "jeff@ethdev.com", 84 | "url": "https://github.com/obscuren" 85 | } 86 | ], 87 | 88 | "license": "LGPL-3.0" 89 | } 90 | -------------------------------------------------------------------------------- /test/utils.toHex.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var utils = require('../lib/utils/utils'); 3 | var BigNumber = require('bignumber.js'); 4 | var assert = chai.assert; 5 | 6 | var tests = [ 7 | { value: 1, expected: '0x1' }, 8 | { value: '1', expected: '0x1' }, 9 | { value: '0x1', expected: '0x1'}, 10 | { value: '15', expected: '0xf'}, 11 | { value: '0xf', expected: '0xf'}, 12 | { value: -1, expected: '-0x1'}, 13 | { value: '-1', expected: '-0x1'}, 14 | { value: '-0x1', expected: '-0x1'}, 15 | { value: '-15', expected: '-0xf'}, 16 | { value: '-0xf', expected: '-0xf'}, 17 | { value: '0x657468657265756d', expected: '0x657468657265756d'}, 18 | { value: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'}, 19 | { value: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', expected: '-0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff'}, 20 | { value: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd', expected: '-0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffd'}, 21 | { value: 0, expected: '0x0'}, 22 | { value: '0', expected: '0x0'}, 23 | { value: '0x0', expected: '0x0'}, 24 | { value: -0, expected: '0x0'}, 25 | { value: '-0', expected: '0x0'}, 26 | { value: '-0x0', expected: '0x0'}, 27 | { value: [1,2,3,{test: 'data'}], expected: '0x5b312c322c332c7b2274657374223a2264617461227d5d'}, 28 | { value: {test: 'test'}, expected: '0x7b2274657374223a2274657374227d'}, 29 | { value: '{"test": "test"}', expected: '0x7b2274657374223a202274657374227d'}, 30 | { value: 'myString', expected: '0x6d79537472696e67'}, 31 | { value: new BigNumber(15), expected: '0xf'}, 32 | { value: true, expected: '0x1'}, 33 | { value: false, expected: '0x0'}, 34 | { value: '\u0003\u0000\u0000\u00005èÆÕL]\u0012|Î¾ž\u001a7«›\u00052\u0011(ЗY\n<\u0010\u0000\u0000\u0000\u0000\u0000\u0000e!ßd/ñõì\f:z¦Î¦±ç·÷Í¢Ëß\u00076*…\bŽ—ñžùC1ÉUÀé2\u001aӆBŒ', 35 | expected: '0x0300000035e8c6d54c5d127c9dcebe9e1a37ab9b05321128d097590a3c100000000000006521df642ff1f5ec0c3a7aa6cea6b1e7b7f7cda2cbdf07362a85088e97f19ef94331c955c0e9321ad386428c'} 36 | ]; 37 | 38 | describe('lib/utils/utils', function () { 39 | describe('toHex', function () { 40 | tests.forEach(function (test) { 41 | it('should turn ' + test.value + ' to ' + test.expected, function () { 42 | assert.strictEqual(utils.toHex(test.value), test.expected); 43 | }); 44 | }); 45 | }); 46 | }); 47 | -------------------------------------------------------------------------------- /test/async.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../index'); 4 | var web3 = new Web3(); 5 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 6 | 7 | // use sendTransaction as dummy 8 | var method = 'sendTransaction'; 9 | 10 | var tests = [{ 11 | input: { 12 | 'from': 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', 13 | 'to': 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS' 14 | }, 15 | formattedInput: { 16 | 'from': '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 17 | 'to': '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8' 18 | }, 19 | result: '0xb', 20 | formattedResult: '0xb', 21 | call: 'eth_'+ method 22 | }]; 23 | 24 | describe('async', function () { 25 | tests.forEach(function (test, index) { 26 | it('test: ' + index, function (done) { 27 | 28 | // given 29 | var provider = new FakeHttpProvider(); 30 | web3.setProvider(provider); 31 | provider.injectResult(test.result); 32 | provider.injectValidation(function (payload) { 33 | assert.equal(payload.jsonrpc, '2.0'); 34 | assert.equal(payload.method, test.call); 35 | assert.deepEqual(payload.params, [test.formattedInput]); 36 | }); 37 | 38 | // when 39 | web3.eth[method](test.input, function(error, result){ 40 | 41 | // then 42 | assert.isNull(error); 43 | assert.strictEqual(test.formattedResult, result); 44 | 45 | done(); 46 | }); 47 | 48 | }); 49 | 50 | it('error test: ' + index, function (done) { 51 | 52 | // given 53 | var provider = new FakeHttpProvider(); 54 | web3.setProvider(provider); 55 | provider.injectError({ 56 | message: test.result, 57 | code: -32603 58 | }); 59 | provider.injectValidation(function (payload) { 60 | assert.equal(payload.jsonrpc, '2.0'); 61 | assert.equal(payload.method, test.call); 62 | assert.deepEqual(payload.params, [test.formattedInput]); 63 | }); 64 | 65 | // when 66 | web3.eth[method](test.input, function(error, result){ 67 | 68 | // then 69 | assert.isUndefined(result); 70 | assert.strictEqual(test.formattedResult, error.message); 71 | 72 | done(); 73 | }); 74 | 75 | }); 76 | }); 77 | }); 78 | 79 | -------------------------------------------------------------------------------- /example/contract.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 64 | 65 | 66 |

contract

67 |
68 |
69 |
70 | 71 |
72 | 75 |
76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /test/web3.eth.getTransactionReceipt.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var web3 = require('../index'); 3 | var BigNumber = require('bignumber.js'); 4 | var testMethod = require('./helpers/test.method.js'); 5 | 6 | var method = 'getTransactionReceipt'; 7 | 8 | var txResult = { 9 | "blockHash": "0x6fd9e2a26ab", 10 | "blockNumber": "0x15df", 11 | "transactionHash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", 12 | "transactionIndex": "0x1", 13 | "contractAddress":"0x407d73d8a49eeb85d32cf465507dd71d507100c1", 14 | "cumulativeGasUsed":"0x7f110", 15 | "gasUsed": "0x7f110", 16 | "logs": [{ 17 | transactionIndex: '0x3e8', 18 | logIndex: '0x3e8', 19 | blockNumber: '0x3e8', 20 | transactionHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', 21 | blockHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', 22 | data: '0x7b2274657374223a2274657374227', 23 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 24 | },{ 25 | transactionIndex: null, 26 | logIndex: null, 27 | blockNumber: null, 28 | transactionHash: null, 29 | blockHash: null, 30 | data: '0x7b2274657374223a2274657374227', 31 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 32 | }] 33 | }; 34 | var formattedTxResult = { 35 | "blockHash": "0x6fd9e2a26ab", 36 | "blockNumber": 5599, 37 | "transactionHash":"0xc6ef2fc5426d6ad6fd9e2a26abeab0aa2411b7ab17f30a99d3cb96aed1d1055b", 38 | "transactionIndex": 1, 39 | "contractAddress":"0x407d73d8a49eeb85d32cf465507dd71d507100c1", 40 | "cumulativeGasUsed": 520464, 41 | "gasUsed": 520464, 42 | "logs": [{ 43 | transactionIndex: 1000, 44 | logIndex: 1000, 45 | blockNumber: 1000, 46 | transactionHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', 47 | blockHash: '0xd6960376d6c6dea93647383ffb245cfced97ccc5c7525397a543a72fdaea5265', 48 | data: '0x7b2274657374223a2274657374227', 49 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 50 | },{ 51 | transactionIndex: null, 52 | logIndex: null, 53 | blockNumber: null, 54 | transactionHash: null, 55 | blockHash: null, 56 | data: '0x7b2274657374223a2274657374227', 57 | topics: ['0x68656c6c6f','0x6d79746f70696373'] 58 | }] 59 | }; 60 | 61 | var tests = [{ 62 | args: ['0x2dbab4c0612bf9caf4c195085547dc0612bf9caf4c1950855'], 63 | formattedArgs: ['0x2dbab4c0612bf9caf4c195085547dc0612bf9caf4c1950855'], 64 | result: txResult, 65 | formattedResult: formattedTxResult, 66 | call: 'eth_'+ method 67 | }]; 68 | 69 | testMethod.runTests('eth', method, tests); 70 | 71 | -------------------------------------------------------------------------------- /lib/web3/jsonrpc.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** @file jsonrpc.js 18 | * @authors: 19 | * Marek Kotewicz 20 | * Aaron Kumavis 21 | * @date 2015 22 | */ 23 | 24 | // Initialize Jsonrpc as a simple object with utility functions. 25 | var Jsonrpc = { 26 | messageId: 0 27 | }; 28 | 29 | /** 30 | * Should be called to valid json create payload object 31 | * 32 | * @method toPayload 33 | * @param {Function} method of jsonrpc call, required 34 | * @param {Array} params, an array of method params, optional 35 | * @returns {Object} valid jsonrpc payload object 36 | */ 37 | Jsonrpc.toPayload = function (method, params) { 38 | if (!method) 39 | console.error('jsonrpc method should be specified!'); 40 | 41 | // advance message ID 42 | Jsonrpc.messageId++; 43 | 44 | return { 45 | jsonrpc: '2.0', 46 | id: Jsonrpc.messageId, 47 | method: method, 48 | params: params || [] 49 | }; 50 | }; 51 | 52 | /** 53 | * Should be called to check if jsonrpc response is valid 54 | * 55 | * @method isValidResponse 56 | * @param {Object} 57 | * @returns {Boolean} true if response is valid, otherwise false 58 | */ 59 | Jsonrpc.isValidResponse = function (response) { 60 | return Array.isArray(response) ? response.every(validateSingleMessage) : validateSingleMessage(response); 61 | 62 | function validateSingleMessage(message){ 63 | return !!message && 64 | !message.error && 65 | message.jsonrpc === '2.0' && 66 | typeof message.id === 'number' && 67 | message.result !== undefined; // only undefined is not valid json object 68 | } 69 | }; 70 | 71 | /** 72 | * Should be called to create batch payload object 73 | * 74 | * @method toBatchPayload 75 | * @param {Array} messages, an array of objects with method (required) and params (optional) fields 76 | * @returns {Array} batch payload 77 | */ 78 | Jsonrpc.toBatchPayload = function (messages) { 79 | return messages.map(function (message) { 80 | return Jsonrpc.toPayload(message.method, message.params); 81 | }); 82 | }; 83 | 84 | module.exports = Jsonrpc; 85 | 86 | -------------------------------------------------------------------------------- /example/contract_array.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 65 | 66 | 67 |

contract

68 |
69 |
70 |
71 | 72 |
73 | 80 |
81 | 82 | 83 | 84 | -------------------------------------------------------------------------------- /test/web3.eth.getBalance.js: -------------------------------------------------------------------------------- 1 | var BigNumber = require('bignumber.js'); 2 | var Web3 = require('../index'); 3 | var web3 = new Web3(); 4 | var testMethod = require('./helpers/test.method.js'); 5 | 6 | var method = 'getBalance'; 7 | 8 | var tests = [{ 9 | args: ['0x000000000000000000000000000000000000012d', 2], 10 | formattedArgs: ['0x000000000000000000000000000000000000012d', '0x2'], 11 | result: '0x31981', 12 | formattedResult: new BigNumber('0x31981', 16), 13 | call: 'eth_'+ method 14 | },{ 15 | args: ['0x000000000000000000000000000000000000012d', '0x1'], 16 | formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'], 17 | result: '0x31981', 18 | formattedResult: new BigNumber('0x31981', 16), 19 | call: 'eth_'+ method 20 | }, { 21 | args: ['0x000000000000000000000000000000000000012d', 0x1], 22 | formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'], 23 | result: '0x31981', 24 | formattedResult: new BigNumber('0x31981', 16), 25 | call: 'eth_'+ method 26 | }, { 27 | args: ['0x000000000000000000000000000000000000012d'], 28 | formattedArgs: ['0x000000000000000000000000000000000000012d', web3.eth.defaultBlock], 29 | result: '0x31981', 30 | formattedResult: new BigNumber('0x31981', 16), 31 | call: 'eth_'+ method 32 | }, { 33 | args: ['0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', 0x1], 34 | formattedArgs: ['0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', '0x1'], 35 | result: '0x31981', 36 | formattedResult: new BigNumber('0x31981', 16), 37 | call: 'eth_'+ method 38 | }, { 39 | args: ['dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', 0x1], 40 | formattedArgs: ['0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', '0x1'], 41 | result: '0x31981', 42 | formattedResult: new BigNumber('0x31981', 16), 43 | call: 'eth_'+ method 44 | }, { 45 | args: ['0x000000000000000000000000000000000000012d', 0x1], 46 | formattedArgs: ['0x000000000000000000000000000000000000012d', '0x1'], 47 | result: '0x31981', 48 | formattedResult: new BigNumber('0x31981', 16), 49 | call: 'eth_'+ method 50 | }, { 51 | args: ['0x000000000000000000000000000000000000012d'], 52 | formattedArgs: ['0x000000000000000000000000000000000000012d', 'latest'], 53 | result: '0x31981', 54 | formattedResult: new BigNumber('0x31981', 16), 55 | call: 'eth_'+ method 56 | }, { 57 | args: ['000000000000000000000000000000000000012d'], 58 | formattedArgs: ['0x000000000000000000000000000000000000012d', 'latest'], 59 | result: '0x31981', 60 | formattedResult: new BigNumber('0x31981', 16), 61 | call: 'eth_'+ method 62 | }, { 63 | args: ['XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS'], 64 | formattedArgs: ['0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 'latest'], 65 | result: '0x31981', 66 | formattedResult: new BigNumber('0x31981', 16), 67 | call: 'eth_'+ method 68 | }]; 69 | 70 | testMethod.runTests('eth', method, tests); 71 | 72 | -------------------------------------------------------------------------------- /lib/web3/transfer.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** 18 | * @file transfer.js 19 | * @author Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | var Iban = require('./iban'); 24 | var exchangeAbi = require('../contracts/SmartExchange.json'); 25 | 26 | /** 27 | * Should be used to make Iban transfer 28 | * 29 | * @method transfer 30 | * @param {String} from 31 | * @param {String} to iban 32 | * @param {Value} value to be tranfered 33 | * @param {Function} callback, callback 34 | */ 35 | var transfer = function (eth, from, to, value, callback) { 36 | var iban = new Iban(to); 37 | if (!iban.isValid()) { 38 | throw new Error('invalid iban address'); 39 | } 40 | 41 | if (iban.isDirect()) { 42 | return transferToAddress(eth, from, iban.address(), value, callback); 43 | } 44 | 45 | if (!callback) { 46 | var address = eth.icapNamereg().addr(iban.institution()); 47 | return deposit(eth, from, address, value, iban.client()); 48 | } 49 | 50 | eth.icapNamereg().addr(iban.institution(), function (err, address) { 51 | return deposit(eth, from, address, value, iban.client(), callback); 52 | }); 53 | 54 | }; 55 | 56 | /** 57 | * Should be used to transfer funds to certain address 58 | * 59 | * @method transferToAddress 60 | * @param {String} from 61 | * @param {String} to 62 | * @param {Value} value to be tranfered 63 | * @param {Function} callback, callback 64 | */ 65 | var transferToAddress = function (eth, from, to, value, callback) { 66 | return eth.sendTransaction({ 67 | address: to, 68 | from: from, 69 | value: value 70 | }, callback); 71 | }; 72 | 73 | /** 74 | * Should be used to deposit funds to generic Exchange contract (must implement deposit(bytes32) method!) 75 | * 76 | * @method deposit 77 | * @param {String} from 78 | * @param {String} to 79 | * @param {Value} value to be transfered 80 | * @param {String} client unique identifier 81 | * @param {Function} callback, callback 82 | */ 83 | var deposit = function (eth, from, to, value, client, callback) { 84 | var abi = exchangeAbi; 85 | return eth.contract(abi).at(to).deposit(client, { 86 | from: from, 87 | value: value 88 | }, callback); 89 | }; 90 | 91 | module.exports = transfer; 92 | 93 | -------------------------------------------------------------------------------- /lib/web3/allevents.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** 18 | * @file allevents.js 19 | * @author Marek Kotewicz 20 | * @date 2014 21 | */ 22 | 23 | var sha3 = require('../utils/sha3'); 24 | var SolidityEvent = require('./event'); 25 | var formatters = require('./formatters'); 26 | var utils = require('../utils/utils'); 27 | var Filter = require('./filter'); 28 | var watches = require('./methods/watches'); 29 | 30 | var AllSolidityEvents = function (requestManager, json, address) { 31 | this._requestManager = requestManager; 32 | this._json = json; 33 | this._address = address; 34 | }; 35 | 36 | AllSolidityEvents.prototype.encode = function (options) { 37 | options = options || {}; 38 | var result = {}; 39 | 40 | ['fromBlock', 'toBlock'].filter(function (f) { 41 | return options[f] !== undefined; 42 | }).forEach(function (f) { 43 | result[f] = formatters.inputBlockNumberFormatter(options[f]); 44 | }); 45 | 46 | result.address = this._address; 47 | 48 | return result; 49 | }; 50 | 51 | AllSolidityEvents.prototype.decode = function (data) { 52 | data.data = data.data || ''; 53 | data.topics = data.topics || []; 54 | 55 | var eventTopic = data.topics[0].slice(2); 56 | var match = this._json.filter(function (j) { 57 | return eventTopic === sha3(utils.transformToFullName(j)); 58 | })[0]; 59 | 60 | if (!match) { // cannot find matching event? 61 | console.warn('cannot find event for log'); 62 | return data; 63 | } 64 | 65 | var event = new SolidityEvent(this._requestManager, match, this._address); 66 | return event.decode(data); 67 | }; 68 | 69 | AllSolidityEvents.prototype.execute = function (options, callback) { 70 | 71 | if (utils.isFunction(arguments[arguments.length - 1])) { 72 | callback = arguments[arguments.length - 1]; 73 | if(arguments.length === 1) 74 | options = null; 75 | } 76 | 77 | var o = this.encode(options); 78 | var formatter = this.decode.bind(this); 79 | return new Filter(o, 'eth', this._requestManager, watches.eth(), formatter, callback); 80 | }; 81 | 82 | AllSolidityEvents.prototype.attachToContract = function (contract) { 83 | var execute = this.execute.bind(this); 84 | contract.allEvents = execute; 85 | }; 86 | 87 | module.exports = AllSolidityEvents; 88 | 89 | -------------------------------------------------------------------------------- /lib/web3/syncing.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** @file syncing.js 18 | * @authors: 19 | * Fabian Vogelsteller 20 | * @date 2015 21 | */ 22 | 23 | var formatters = require('./formatters'); 24 | var utils = require('../utils/utils'); 25 | 26 | var count = 1; 27 | 28 | /** 29 | Adds the callback and sets up the methods, to iterate over the results. 30 | 31 | @method pollSyncing 32 | @param {Object} self 33 | */ 34 | var pollSyncing = function(self) { 35 | 36 | var onMessage = function (error, sync) { 37 | if (error) { 38 | return self.callbacks.forEach(function (callback) { 39 | callback(error); 40 | }); 41 | } 42 | 43 | if(utils.isObject(sync) && sync.startingBlock) 44 | sync = formatters.outputSyncingFormatter(sync); 45 | 46 | self.callbacks.forEach(function (callback) { 47 | if (self.lastSyncState !== sync) { 48 | 49 | // call the callback with true first so the app can stop anything, before receiving the sync data 50 | if(!self.lastSyncState && utils.isObject(sync)) 51 | callback(null, true); 52 | 53 | // call on the next CPU cycle, so the actions of the sync stop can be processes first 54 | setTimeout(function() { 55 | callback(null, sync); 56 | }, 0); 57 | 58 | self.lastSyncState = sync; 59 | } 60 | }); 61 | }; 62 | 63 | self.requestManager.startPolling({ 64 | method: 'eth_syncing', 65 | params: [], 66 | }, self.pollId, onMessage, self.stopWatching.bind(self)); 67 | 68 | }; 69 | 70 | var IsSyncing = function (requestManager, callback) { 71 | this.requestManager = requestManager; 72 | this.pollId = 'syncPoll_'+ count++; 73 | this.callbacks = []; 74 | this.addCallback(callback); 75 | this.lastSyncState = false; 76 | pollSyncing(this); 77 | 78 | return this; 79 | }; 80 | 81 | IsSyncing.prototype.addCallback = function (callback) { 82 | if(callback) 83 | this.callbacks.push(callback); 84 | return this; 85 | }; 86 | 87 | IsSyncing.prototype.stopWatching = function () { 88 | this.requestManager.stopPolling(this.pollId); 89 | this.callbacks = []; 90 | }; 91 | 92 | module.exports = IsSyncing; 93 | 94 | -------------------------------------------------------------------------------- /test/web3.extend.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var FakeHttpProvider = require('./helpers/FakeHttpProvider'); 4 | var Web3 = require('../lib/web3'); 5 | var web3 = new Web3(); 6 | 7 | 8 | var tests = [{ 9 | properties: [new web3._extend.Property({ 10 | name: 'gasPrice', 11 | getter: 'eth_gasPrice', 12 | outputFormatter: web3._extend.formatters.outputBigNumberFormatter 13 | })] 14 | },{ 15 | methods: [new web3._extend.Method({ 16 | name: 'getBalance', 17 | call: 'eth_getBalance', 18 | params: 2, 19 | inputFormatter: [web3._extend.utils.toAddress, web3._extend.formatters.inputDefaultBlockNumberFormatter], 20 | outputFormatter: web3._extend.formatters.outputBigNumberFormatter 21 | })] 22 | },{ 23 | property: 'admin', 24 | properties: [new web3._extend.Property({ 25 | name: 'gasPrice', 26 | getter: 'eth_gasPrice', 27 | outputFormatter: web3._extend.formatters.outputBigNumberFormatter 28 | })], 29 | methods: [new web3._extend.Method({ 30 | name: 'getBalance', 31 | call: 'eth_getBalance', 32 | params: 2, 33 | inputFormatter: [web3._extend.utils.toAddress, web3._extend.formatters.inputDefaultBlockNumberFormatter], 34 | outputFormatter: web3._extend.formatters.outputBigNumberFormatter 35 | })] 36 | }]; 37 | 38 | describe('web3', function () { 39 | describe('_extend', function () { 40 | tests.forEach(function (test, index) { 41 | it('test no: ' + index, function () { 42 | web3._extend(test); 43 | 44 | 45 | if(test.properties) 46 | test.properties.forEach(function(property){ 47 | 48 | var provider = new FakeHttpProvider(); 49 | web3.setProvider(provider); 50 | provider.injectResult(''); 51 | provider.injectValidation(function (payload) { 52 | assert.equal(payload.jsonrpc, '2.0'); 53 | assert.equal(payload.method, property.getter); 54 | }); 55 | 56 | if(test.property) { 57 | assert.isObject(web3[test.property][property.name]); 58 | assert.isFunction(web3[test.property]['get'+ property.name.charAt(0).toUpperCase() + property.name.slice(1)]); 59 | } else { 60 | assert.isObject(web3[property.name]); 61 | assert.isFunction(web3['get'+ property.name.charAt(0).toUpperCase() + property.name.slice(1)]); 62 | } 63 | }); 64 | 65 | if(test.methods) 66 | test.methods.forEach(function(property){ 67 | if(test.property) 68 | assert.isFunction(web3[test.property][property.name]); 69 | else 70 | assert.isFunction(web3[property.name]); 71 | }); 72 | 73 | }); 74 | }); 75 | }); 76 | }); 77 | 78 | -------------------------------------------------------------------------------- /lib/web3/methods/watches.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** @file watches.js 18 | * @authors: 19 | * Marek Kotewicz 20 | * @date 2015 21 | */ 22 | 23 | var Method = require('../method'); 24 | 25 | /// @returns an array of objects describing web3.eth.filter api methods 26 | var eth = function () { 27 | var newFilterCall = function (args) { 28 | var type = args[0]; 29 | 30 | switch(type) { 31 | case 'latest': 32 | args.shift(); 33 | this.params = 0; 34 | return 'eth_newBlockFilter'; 35 | case 'pending': 36 | args.shift(); 37 | this.params = 0; 38 | return 'eth_newPendingTransactionFilter'; 39 | default: 40 | return 'eth_newFilter'; 41 | } 42 | }; 43 | 44 | var newFilter = new Method({ 45 | name: 'newFilter', 46 | call: newFilterCall, 47 | params: 1 48 | }); 49 | 50 | var uninstallFilter = new Method({ 51 | name: 'uninstallFilter', 52 | call: 'eth_uninstallFilter', 53 | params: 1 54 | }); 55 | 56 | var getLogs = new Method({ 57 | name: 'getLogs', 58 | call: 'eth_getFilterLogs', 59 | params: 1 60 | }); 61 | 62 | var poll = new Method({ 63 | name: 'poll', 64 | call: 'eth_getFilterChanges', 65 | params: 1 66 | }); 67 | 68 | return [ 69 | newFilter, 70 | uninstallFilter, 71 | getLogs, 72 | poll 73 | ]; 74 | }; 75 | 76 | /// @returns an array of objects describing web3.shh.watch api methods 77 | var shh = function () { 78 | 79 | return [ 80 | new Method({ 81 | name: 'newFilter', 82 | call: 'shh_newMessageFilter', 83 | params: 1 84 | }), 85 | new Method({ 86 | name: 'uninstallFilter', 87 | call: 'shh_deleteMessageFilter', 88 | params: 1 89 | }), 90 | new Method({ 91 | name: 'getLogs', 92 | call: 'shh_getFilterMessages', 93 | params: 1 94 | }), 95 | new Method({ 96 | name: 'poll', 97 | call: 'shh_getFilterMessages', 98 | params: 1 99 | }) 100 | ]; 101 | }; 102 | 103 | module.exports = { 104 | eth: eth, 105 | shh: shh 106 | }; 107 | 108 | -------------------------------------------------------------------------------- /test/helpers/test.method.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var Web3 = require('../../index'); 4 | 5 | var FakeHttpProvider = require('./FakeHttpProvider'); 6 | var clone = function (object) { return JSON.parse(JSON.stringify(object)); }; 7 | 8 | var runTests = function (obj, method, tests) { 9 | 10 | var testName = obj ? 'web3.' + obj : 'web'; 11 | 12 | describe(testName, function () { 13 | describe(method, function () { 14 | tests.forEach(function (test, index) { 15 | it('sync test: ' + index, function () { 16 | 17 | // given 18 | var provider = new FakeHttpProvider(); 19 | var web3 = new Web3(provider); 20 | provider.injectResult(test.result); 21 | provider.injectValidation(function (payload) { 22 | assert.equal(payload.jsonrpc, '2.0'); 23 | assert.equal(payload.method, test.call); 24 | assert.deepEqual(payload.params, test.formattedArgs); 25 | }); 26 | 27 | var args = clone(test.args) 28 | 29 | // when 30 | if (obj) { 31 | var result = web3[obj][method].apply(web3[obj], args); 32 | } else { 33 | var result = web3[method].apply(web3, args); 34 | } 35 | // when 36 | //var result = (obj) 37 | //? web3[obj][method].apply(null, test.args.slice(0)) 38 | //: web3[method].apply(null, test.args.slice(0)); 39 | 40 | // then 41 | assert.deepEqual(test.formattedResult, result); 42 | }); 43 | 44 | it('async test: ' + index, function (done) { 45 | 46 | // given 47 | var provider = new FakeHttpProvider(); 48 | var web3 = new Web3(provider); 49 | provider.injectResult(test.result); 50 | provider.injectValidation(function (payload) { 51 | assert.equal(payload.jsonrpc, '2.0'); 52 | assert.equal(payload.method, test.call); 53 | assert.deepEqual(payload.params, test.formattedArgs); 54 | }); 55 | 56 | var args = clone(test.args); 57 | 58 | // add callback 59 | args.push(function (err, result) { 60 | assert.deepEqual(test.formattedResult, result); 61 | done(); 62 | }); 63 | 64 | // when 65 | if (obj) { 66 | web3[obj][method].apply(web3[obj], args); 67 | } else { 68 | web3[method].apply(web3, args); 69 | } 70 | }); 71 | }); 72 | }); 73 | }); 74 | 75 | }; 76 | 77 | module.exports = { 78 | runTests: runTests 79 | } 80 | 81 | -------------------------------------------------------------------------------- /lib/contracts/SmartExchange.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "constant": false, 4 | "inputs": [ 5 | { 6 | "name": "from", 7 | "type": "bytes32" 8 | }, 9 | { 10 | "name": "to", 11 | "type": "address" 12 | }, 13 | { 14 | "name": "value", 15 | "type": "uint256" 16 | } 17 | ], 18 | "name": "transfer", 19 | "outputs": [], 20 | "type": "function" 21 | }, 22 | { 23 | "constant": false, 24 | "inputs": [ 25 | { 26 | "name": "from", 27 | "type": "bytes32" 28 | }, 29 | { 30 | "name": "to", 31 | "type": "address" 32 | }, 33 | { 34 | "name": "indirectId", 35 | "type": "bytes32" 36 | }, 37 | { 38 | "name": "value", 39 | "type": "uint256" 40 | } 41 | ], 42 | "name": "icapTransfer", 43 | "outputs": [], 44 | "type": "function" 45 | }, 46 | { 47 | "constant": false, 48 | "inputs": [ 49 | { 50 | "name": "to", 51 | "type": "bytes32" 52 | } 53 | ], 54 | "name": "deposit", 55 | "outputs": [], 56 | "payable": true, 57 | "type": "function" 58 | }, 59 | { 60 | "anonymous": false, 61 | "inputs": [ 62 | { 63 | "indexed": true, 64 | "name": "from", 65 | "type": "address" 66 | }, 67 | { 68 | "indexed": false, 69 | "name": "value", 70 | "type": "uint256" 71 | } 72 | ], 73 | "name": "AnonymousDeposit", 74 | "type": "event" 75 | }, 76 | { 77 | "anonymous": false, 78 | "inputs": [ 79 | { 80 | "indexed": true, 81 | "name": "from", 82 | "type": "address" 83 | }, 84 | { 85 | "indexed": true, 86 | "name": "to", 87 | "type": "bytes32" 88 | }, 89 | { 90 | "indexed": false, 91 | "name": "value", 92 | "type": "uint256" 93 | } 94 | ], 95 | "name": "Deposit", 96 | "type": "event" 97 | }, 98 | { 99 | "anonymous": false, 100 | "inputs": [ 101 | { 102 | "indexed": true, 103 | "name": "from", 104 | "type": "bytes32" 105 | }, 106 | { 107 | "indexed": true, 108 | "name": "to", 109 | "type": "address" 110 | }, 111 | { 112 | "indexed": false, 113 | "name": "value", 114 | "type": "uint256" 115 | } 116 | ], 117 | "name": "Transfer", 118 | "type": "event" 119 | }, 120 | { 121 | "anonymous": false, 122 | "inputs": [ 123 | { 124 | "indexed": true, 125 | "name": "from", 126 | "type": "bytes32" 127 | }, 128 | { 129 | "indexed": true, 130 | "name": "to", 131 | "type": "address" 132 | }, 133 | { 134 | "indexed": false, 135 | "name": "indirectId", 136 | "type": "bytes32" 137 | }, 138 | { 139 | "indexed": false, 140 | "name": "value", 141 | "type": "uint256" 142 | } 143 | ], 144 | "name": "IcapTransfer", 145 | "type": "event" 146 | } 147 | ] 148 | -------------------------------------------------------------------------------- /test/formatters.inputTransactionFormatter.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = chai.assert; 3 | var formatters = require('../lib/web3/formatters.js'); 4 | var BigNumber = require('bignumber.js'); 5 | 6 | var tests = [{ 7 | input: { 8 | data: '0x34234bf23bf4234', 9 | value: new BigNumber(100), 10 | from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 11 | to: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 12 | nonce: 1000, 13 | gas: 1000, 14 | gasPrice: new BigNumber(1000) 15 | }, 16 | result: { 17 | data: '0x34234bf23bf4234', 18 | value: '0x64', 19 | from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 20 | to: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 21 | nonce: '0x3e8', 22 | gas: '0x3e8', 23 | gasPrice: '0x3e8' 24 | } 25 | },{ 26 | input: { 27 | data: '0x34234bf23bf4234', 28 | value: new BigNumber(100), 29 | from: '00c5496aee77c1ba1f0854206a26dda82a81d6d8', 30 | to: '00c5496aee77c1ba1f0854206a26dda82a81d6d8', 31 | }, 32 | result: { 33 | data: '0x34234bf23bf4234', 34 | value: '0x64', 35 | from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 36 | to: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 37 | } 38 | },{ 39 | input: { 40 | data: '0x34234bf23bf4234', 41 | value: new BigNumber(100), 42 | from: '00c5496aee77c1ba1f0854206a26dda82a81d6d8', 43 | to: '00c5496aee77c1ba1f0854206a26dda82a81d6d8', 44 | gas: '1000', 45 | gasPrice: new BigNumber(1000) 46 | }, 47 | result: { 48 | data: '0x34234bf23bf4234', 49 | value: '0x64', 50 | from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 51 | to: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 52 | gas: '0x3e8', 53 | gasPrice: '0x3e8' 54 | }, 55 | }, { 56 | input: { 57 | data: '0x34234bf23bf4234', 58 | value: new BigNumber(100), 59 | from: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', 60 | to: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', 61 | gas: '1000', 62 | gasPrice: new BigNumber(1000) 63 | }, 64 | result: { 65 | data: '0x34234bf23bf4234', 66 | value: '0x64', 67 | from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 68 | to: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 69 | gas: '0x3e8', 70 | gasPrice: '0x3e8' 71 | }, 72 | }, { 73 | input: { 74 | data: '0x34234bf23bf4234', 75 | value: new BigNumber(100), 76 | from: 'XE7338O073KYGTWWZN0F2WZ0R8PX5ZPPZS', 77 | gas: '1000', 78 | gasPrice: new BigNumber(1000) 79 | }, 80 | result: { 81 | data: '0x34234bf23bf4234', 82 | value: '0x64', 83 | from: '0x00c5496aee77c1ba1f0854206a26dda82a81d6d8', 84 | gas: '0x3e8', 85 | gasPrice: '0x3e8' 86 | } 87 | }]; 88 | 89 | describe('formatters', function () { 90 | describe('inputTransactionFormatter', function () { 91 | tests.forEach(function(test){ 92 | it('should return the correct value', function () { 93 | assert.deepEqual(formatters.inputTransactionFormatter(test.input), test.result); 94 | }); 95 | }); 96 | }); 97 | }); 98 | -------------------------------------------------------------------------------- /test/helpers/FakeHttpProvider.js: -------------------------------------------------------------------------------- 1 | var chai = require('chai'); 2 | var assert = require('assert'); 3 | var utils = require('../../lib/utils/utils'); 4 | 5 | countId = 1; 6 | 7 | var getResponseStub = function () { 8 | return { 9 | jsonrpc: '2.0', 10 | id: countId++, 11 | result: null 12 | }; 13 | }; 14 | 15 | var getErrorStub = function () { 16 | return { 17 | jsonrpc: '2.0', 18 | countId: countId++, 19 | error: { 20 | code: 1234, 21 | message: 'Stub error' 22 | } 23 | }; 24 | }; 25 | 26 | var FakeHttpProvider = function () { 27 | this.response = getResponseStub(); 28 | this.error = null; 29 | this.validation = null; 30 | }; 31 | 32 | FakeHttpProvider.prototype.send = function (payload) { 33 | assert.equal(utils.isArray(payload) || utils.isObject(payload), true); 34 | // TODO: validate jsonrpc request 35 | if (this.error) { 36 | throw this.error; 37 | } 38 | if (this.validation) { 39 | // imitate plain json object 40 | this.validation(JSON.parse(JSON.stringify(payload))); 41 | } 42 | 43 | return this.getResponse(payload); 44 | }; 45 | 46 | FakeHttpProvider.prototype.sendAsync = function (payload, callback) { 47 | assert.equal(utils.isArray(payload) || utils.isObject(payload), true); 48 | assert.equal(utils.isFunction(callback), true); 49 | if (this.validation) { 50 | // imitate plain json object 51 | this.validation(JSON.parse(JSON.stringify(payload)), callback); 52 | } 53 | 54 | var response = this.getResponse(payload); 55 | var error = this.error; 56 | setTimeout(function(){ 57 | callback(error, response); 58 | },1); 59 | }; 60 | 61 | FakeHttpProvider.prototype.injectResponse = function (response) { 62 | this.response = response; 63 | }; 64 | 65 | FakeHttpProvider.prototype.injectResult = function (result) { 66 | this.response = getResponseStub(); 67 | this.response.result = result; 68 | }; 69 | 70 | FakeHttpProvider.prototype.injectBatchResults = function (results, error) { 71 | this.response = results.map(function (r) { 72 | if(error) { 73 | var response = getErrorStub(); 74 | response.error.message = r; 75 | } else { 76 | var response = getResponseStub(); 77 | response.result = r; 78 | } 79 | return response; 80 | }); 81 | }; 82 | 83 | FakeHttpProvider.prototype.getResponse = function (payload) { 84 | 85 | if(this.response) { 86 | if(utils.isArray(this.response)) { 87 | this.response = this.response.map(function(response, index) { 88 | response.id = payload[index] ? payload[index].id : countId++; 89 | return response; 90 | }); 91 | } else 92 | this.response.id = payload.id; 93 | } 94 | 95 | return this.response; 96 | }; 97 | 98 | FakeHttpProvider.prototype.injectError = function (error) { 99 | this.error = error; 100 | }; 101 | 102 | FakeHttpProvider.prototype.injectValidation = function (callback) { 103 | this.validation = callback; 104 | }; 105 | 106 | module.exports = FakeHttpProvider; 107 | 108 | -------------------------------------------------------------------------------- /example/event_inc.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 75 | 76 | 77 | 78 |
79 |
80 | 81 |
82 |
83 | 84 |
85 |
86 |
87 |
88 | 89 | 90 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | 'use strict'; 4 | 5 | var version = require('./lib/version.json'); 6 | var path = require('path'); 7 | 8 | var del = require('del'); 9 | var gulp = require('gulp'); 10 | var browserify = require('browserify'); 11 | var jshint = require('gulp-jshint'); 12 | var uglify = require('gulp-uglify'); 13 | var rename = require('gulp-rename'); 14 | var source = require('vinyl-source-stream'); 15 | var exorcist = require('exorcist'); 16 | var bower = require('bower'); 17 | var streamify = require('gulp-streamify'); 18 | var replace = require('gulp-replace'); 19 | 20 | var DEST = path.join(__dirname, 'dist/'); 21 | var src = 'index'; 22 | var dst = 'web3'; 23 | var lightDst = 'web3-light'; 24 | 25 | var browserifyOptions = { 26 | debug: true, 27 | insert_global_vars: false, // jshint ignore:line 28 | detectGlobals: false, 29 | bundleExternal: true 30 | }; 31 | 32 | gulp.task('version', function(){ 33 | gulp.src(['./package.json']) 34 | .pipe(replace(/\"version\"\: \"([\.0-9]*)\"/, '"version": "'+ version.version + '"')) 35 | .pipe(gulp.dest('./')); 36 | gulp.src(['./bower.json']) 37 | .pipe(replace(/\"version\"\: \"([\.0-9]*)\"/, '"version": "'+ version.version + '"')) 38 | .pipe(gulp.dest('./')); 39 | gulp.src(['./package.js']) 40 | .pipe(replace(/version\: \'([\.0-9]*)\'/, "version: '"+ version.version + "'")) 41 | .pipe(gulp.dest('./')); 42 | }); 43 | 44 | gulp.task('bower', ['version'], function(cb){ 45 | bower.commands.install().on('end', function (installed){ 46 | console.log(installed); 47 | cb(); 48 | }); 49 | }); 50 | 51 | gulp.task('lint', [], function(){ 52 | return gulp.src(['./*.js', './lib/*.js']) 53 | .pipe(jshint()) 54 | .pipe(jshint.reporter('default')); 55 | }); 56 | 57 | gulp.task('clean', ['lint'], function(cb) { 58 | del([ DEST ]).then(cb.bind(null, null)); 59 | }); 60 | 61 | gulp.task('light', ['clean'], function () { 62 | return browserify(browserifyOptions) 63 | .require('./' + src + '.js', {expose: 'web3'}) 64 | .ignore('bignumber.js') 65 | .require('./lib/utils/browser-bn.js', {expose: 'bignumber.js'}) // fake bignumber.js 66 | .add('./' + src + '.js') 67 | .bundle() 68 | .pipe(exorcist(path.join( DEST, lightDst + '.js.map'))) 69 | .pipe(source(lightDst + '.js')) 70 | .pipe(gulp.dest( DEST )) 71 | .pipe(streamify(uglify())) 72 | .pipe(rename(lightDst + '.min.js')) 73 | .pipe(gulp.dest( DEST )); 74 | }); 75 | 76 | gulp.task('standalone', ['clean'], function () { 77 | return browserify(browserifyOptions) 78 | .require('./' + src + '.js', {expose: 'web3'}) 79 | .require('bignumber.js') // expose it to dapp users 80 | .add('./' + src + '.js') 81 | .ignore('crypto') 82 | .bundle() 83 | .pipe(exorcist(path.join( DEST, dst + '.js.map'))) 84 | .pipe(source(dst + '.js')) 85 | .pipe(gulp.dest( DEST )) 86 | .pipe(streamify(uglify())) 87 | .pipe(rename(dst + '.min.js')) 88 | .pipe(gulp.dest( DEST )); 89 | }); 90 | 91 | gulp.task('watch', function() { 92 | gulp.watch(['./lib/*.js'], ['lint', 'build']); 93 | }); 94 | 95 | gulp.task('default', ['version', 'lint', 'clean', 'light', 'standalone']); 96 | 97 | -------------------------------------------------------------------------------- /lib/utils/bloom.js: -------------------------------------------------------------------------------- 1 | /*jshint bitwise: false*/ 2 | 3 | /* 4 | This file is part of web3.js. 5 | 6 | web3.js is free software: you can redistribute it and/or modify 7 | it under the terms of the GNU Lesser General Public License as published by 8 | the Free Software Foundation, either version 3 of the License, or 9 | (at your option) any later version. 10 | 11 | web3.js is distributed in the hope that it will be useful, 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | GNU Lesser General Public License for more details. 15 | 16 | You should have received a copy of the GNU Lesser General Public License 17 | along with web3.js. If not, see . 18 | */ 19 | 20 | /** 21 | * @file bloom.js 22 | * @author Bas van Kervel 23 | * @date 2017 24 | */ 25 | 26 | /** 27 | * Ethereum bloom filter support. 28 | * 29 | * @module bloom 30 | * @class [bloom] bloom 31 | */ 32 | 33 | var utils = require("./utils.js"); 34 | var sha3 = require("./sha3.js"); 35 | 36 | function codePointToInt(codePoint) { 37 | if (codePoint >= 48 && codePoint <= 57) { /*['0'..'9'] -> [0..9]*/ 38 | return codePoint-48; 39 | } 40 | 41 | if (codePoint >= 65 && codePoint <= 70) { /*['A'..'F'] -> [10..15]*/ 42 | return codePoint-55; 43 | } 44 | 45 | if (codePoint >= 97 && codePoint <= 102) { /*['a'..'f'] -> [10..15]*/ 46 | return codePoint-87; 47 | } 48 | 49 | throw "invalid bloom"; 50 | } 51 | 52 | function testBytes(bloom, bytes) { 53 | var hash = sha3(bytes, {encoding: "hex"}); 54 | 55 | for (var i=0; i < 12; i+=4) { 56 | // calculate bit position in bloom fiter that must be active 57 | var bitpos = ((parseInt(hash.substr(i, 2), 16) << 8) + parseInt(hash.substr((i+2), 2), 16)) & 2047; 58 | 59 | // test if bitpos in bloom is active 60 | var code = codePointToInt(bloom.charCodeAt(bloom.length-1-Math.floor(bitpos/4))); 61 | var offset = 1 << (bitpos % 4); 62 | 63 | if ((code&offset) !== offset) { 64 | return false; 65 | } 66 | } 67 | 68 | return true; 69 | } 70 | 71 | /** 72 | * Returns true if address is part of the given bloom. 73 | * note: false positives are possible. 74 | * 75 | * @method testAddress 76 | * @param {String} hex encoded bloom 77 | * @param {String} address in hex notation 78 | * @returns {Boolean} topic is (probably) part of the block 79 | */ 80 | var testAddress = function(bloom, address) { 81 | if (!utils.isBloom(bloom)) throw "invalid bloom"; 82 | if (!utils.isAddress(address)) throw "invalid address"; 83 | 84 | return testBytes(bloom, address); 85 | }; 86 | 87 | /** 88 | * Returns true if the topic is part of the given bloom. 89 | * note: false positives are possible. 90 | * 91 | * @method hasTopic 92 | * @param {String} hex encoded bloom 93 | * @param {String} address in hex notation 94 | * @returns {Boolean} topic is (probably) part of the block 95 | */ 96 | var testTopic = function(bloom, topic) { 97 | if (!utils.isBloom(bloom)) throw "invalid bloom"; 98 | if (!utils.isTopic(topic)) throw "invalid topic"; 99 | 100 | return testBytes(bloom, topic); 101 | }; 102 | 103 | module.exports = { 104 | testAddress: testAddress, 105 | testTopic: testTopic, 106 | }; 107 | -------------------------------------------------------------------------------- /lib/web3/methods/personal.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** 18 | * @file eth.js 19 | * @author Marek Kotewicz 20 | * @author Fabian Vogelsteller 21 | * @date 2015 22 | */ 23 | 24 | "use strict"; 25 | 26 | var Method = require('../method'); 27 | var Property = require('../property'); 28 | var formatters = require('../formatters'); 29 | 30 | function Personal(web3) { 31 | this._requestManager = web3._requestManager; 32 | 33 | var self = this; 34 | 35 | methods().forEach(function(method) { 36 | method.attachToObject(self); 37 | method.setRequestManager(self._requestManager); 38 | }); 39 | 40 | properties().forEach(function(p) { 41 | p.attachToObject(self); 42 | p.setRequestManager(self._requestManager); 43 | }); 44 | } 45 | 46 | var methods = function () { 47 | var newAccount = new Method({ 48 | name: 'newAccount', 49 | call: 'personal_newAccount', 50 | params: 1, 51 | inputFormatter: [null] 52 | }); 53 | 54 | var importRawKey = new Method({ 55 | name: 'importRawKey', 56 | call: 'personal_importRawKey', 57 | params: 2 58 | }); 59 | 60 | var sign = new Method({ 61 | name: 'sign', 62 | call: 'personal_sign', 63 | params: 3, 64 | inputFormatter: [null, formatters.inputAddressFormatter, null] 65 | }); 66 | 67 | var ecRecover = new Method({ 68 | name: 'ecRecover', 69 | call: 'personal_ecRecover', 70 | params: 2 71 | }); 72 | 73 | var unlockAccount = new Method({ 74 | name: 'unlockAccount', 75 | call: 'personal_unlockAccount', 76 | params: 3, 77 | inputFormatter: [formatters.inputAddressFormatter, null, null] 78 | }); 79 | 80 | var sendTransaction = new Method({ 81 | name: 'sendTransaction', 82 | call: 'personal_sendTransaction', 83 | params: 2, 84 | inputFormatter: [formatters.inputTransactionFormatter, null] 85 | }); 86 | 87 | var lockAccount = new Method({ 88 | name: 'lockAccount', 89 | call: 'personal_lockAccount', 90 | params: 1, 91 | inputFormatter: [formatters.inputAddressFormatter] 92 | }); 93 | 94 | return [ 95 | newAccount, 96 | importRawKey, 97 | unlockAccount, 98 | ecRecover, 99 | sign, 100 | sendTransaction, 101 | lockAccount 102 | ]; 103 | }; 104 | 105 | var properties = function () { 106 | return [ 107 | new Property({ 108 | name: 'listAccounts', 109 | getter: 'personal_listAccounts' 110 | }) 111 | ]; 112 | }; 113 | 114 | 115 | module.exports = Personal; 116 | -------------------------------------------------------------------------------- /lib/web3/methods/swarm.js: -------------------------------------------------------------------------------- 1 | /* 2 | This file is part of web3.js. 3 | 4 | web3.js is free software: you can redistribute it and/or modify 5 | it under the terms of the GNU Lesser General Public License as published by 6 | the Free Software Foundation, either version 3 of the License, or 7 | (at your option) any later version. 8 | 9 | web3.js is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 | GNU Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public License 15 | along with web3.js. If not, see . 16 | */ 17 | /** 18 | * @file bzz.js 19 | * @author Alex Beregszaszi 20 | * @date 2016 21 | * 22 | * Reference: https://github.com/ethereum/go-ethereum/blob/swarm/internal/web3ext/web3ext.go#L33 23 | */ 24 | 25 | "use strict"; 26 | 27 | var Method = require('../method'); 28 | var Property = require('../property'); 29 | 30 | function Swarm(web3) { 31 | this._requestManager = web3._requestManager; 32 | 33 | var self = this; 34 | 35 | methods().forEach(function(method) { 36 | method.attachToObject(self); 37 | method.setRequestManager(self._requestManager); 38 | }); 39 | 40 | properties().forEach(function(p) { 41 | p.attachToObject(self); 42 | p.setRequestManager(self._requestManager); 43 | }); 44 | } 45 | 46 | var methods = function () { 47 | var blockNetworkRead = new Method({ 48 | name: 'blockNetworkRead', 49 | call: 'bzz_blockNetworkRead', 50 | params: 1, 51 | inputFormatter: [null] 52 | }); 53 | 54 | var syncEnabled = new Method({ 55 | name: 'syncEnabled', 56 | call: 'bzz_syncEnabled', 57 | params: 1, 58 | inputFormatter: [null] 59 | }); 60 | 61 | var swapEnabled = new Method({ 62 | name: 'swapEnabled', 63 | call: 'bzz_swapEnabled', 64 | params: 1, 65 | inputFormatter: [null] 66 | }); 67 | 68 | var download = new Method({ 69 | name: 'download', 70 | call: 'bzz_download', 71 | params: 2, 72 | inputFormatter: [null, null] 73 | }); 74 | 75 | var upload = new Method({ 76 | name: 'upload', 77 | call: 'bzz_upload', 78 | params: 2, 79 | inputFormatter: [null, null] 80 | }); 81 | 82 | var retrieve = new Method({ 83 | name: 'retrieve', 84 | call: 'bzz_retrieve', 85 | params: 1, 86 | inputFormatter: [null] 87 | }); 88 | 89 | var store = new Method({ 90 | name: 'store', 91 | call: 'bzz_store', 92 | params: 2, 93 | inputFormatter: [null, null] 94 | }); 95 | 96 | var get = new Method({ 97 | name: 'get', 98 | call: 'bzz_get', 99 | params: 1, 100 | inputFormatter: [null] 101 | }); 102 | 103 | var put = new Method({ 104 | name: 'put', 105 | call: 'bzz_put', 106 | params: 2, 107 | inputFormatter: [null, null] 108 | }); 109 | 110 | var modify = new Method({ 111 | name: 'modify', 112 | call: 'bzz_modify', 113 | params: 4, 114 | inputFormatter: [null, null, null, null] 115 | }); 116 | 117 | return [ 118 | blockNetworkRead, 119 | syncEnabled, 120 | swapEnabled, 121 | download, 122 | upload, 123 | retrieve, 124 | store, 125 | get, 126 | put, 127 | modify 128 | ]; 129 | }; 130 | 131 | var properties = function () { 132 | return [ 133 | new Property({ 134 | name: 'hive', 135 | getter: 'bzz_hive' 136 | }), 137 | new Property({ 138 | name: 'info', 139 | getter: 'bzz_info' 140 | }) 141 | ]; 142 | }; 143 | 144 | 145 | module.exports = Swarm; 146 | --------------------------------------------------------------------------------