From 5c34c26fe47b8f9219e8757dc931a142e4ac26a4 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 17 Oct 2019 11:51:36 +0200 Subject: [PATCH 01/12] signTransaction updated, getId method in net module, and e2e signTransaction method updated --- packages/web3-eth-accounts/src/index.js | 23 +++++++- packages/web3-net/src/index.js | 2 +- test/e2e.method.signing.js | 77 ++++++++++++++----------- 3 files changed, 66 insertions(+), 36 deletions(-) diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 8eb8954eae8..527e481ea88 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -118,6 +118,7 @@ Accounts.prototype.privateKeyToAccount = function privateKeyToAccount(privateKey Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, callback) { var _this = this, error = false, + transactionOptions = {}, result; callback = callback || function () {}; @@ -131,6 +132,10 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca function signed (tx) { + if (tx.common && (tx.chain && tx.hardfork)) { + error = new Error('Please provide the ethereumjs-common object or the chain and hardfork property but not both.') + } + if (!tx.gas && !tx.gasLimit) { error = new Error('"gas" is missing'); } @@ -154,11 +159,27 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca transaction.value = transaction.value || '0x'; transaction.chainId = utils.numberToHex(transaction.chainId); + if (transaction.common) { + transactionOptions['common'] = transaction.common; + delete transaction.common; + } + + if (transaction.chain) { + transactionOptions['chain'] = transaction.chain; + delete transaction.chain; + } + + if (transaction.hardfork) { + transactionOptions['hardfork'] = transaction.hardfork; + delete transaction.hardfork; + } + if (privateKey.startsWith('0x')) { privateKey = privateKey.substring(2); } - var ethTx = new Transaction(transaction); + var ethTx = new Transaction(transaction, transactionOptions); + ethTx.sign(Buffer.from(privateKey, 'hex')); var validationResult = ethTx.validate(true); diff --git a/packages/web3-net/src/index.js b/packages/web3-net/src/index.js index ebf1b456bf5..cdca9d6f980 100644 --- a/packages/web3-net/src/index.js +++ b/packages/web3-net/src/index.js @@ -39,7 +39,7 @@ var Net = function () { name: 'getId', call: 'net_version', params: 0, - outputFormatter: utils.hexToNumber + outputFormatter: parseInt }), new Method({ name: 'isListening', diff --git a/test/e2e.method.signing.js b/test/e2e.method.signing.js index 1bad2428a12..b37d2b25ec2 100644 --- a/test/e2e.method.signing.js +++ b/test/e2e.method.signing.js @@ -1,17 +1,12 @@ -var assert = require('assert'); -var EJSCommon = require('ethereumjs-common'); -var EJSTx = require('ethereumjs-tx'); -var Web3 = require('../packages/web3'); -var Basic = require('./sources/Basic'); -var utils = require('./helpers/test.utils'); -var util = require('util') +let assert = require('assert'); +let EJSCommon = require('ethereumjs-common'); +let Web3 = require('../packages/web3'); describe('transaction and message signing [ @E2E ]', function() { - var web3; - var accounts; - var wallet; - var Transaction = EJSTx.Transaction; - var Common = EJSCommon.default; + let web3; + let accounts; + let wallet; + let Common = EJSCommon.default; before(async function(){ web3 = new Web3('http://localhost:8545'); @@ -25,59 +20,73 @@ describe('transaction and message signing [ @E2E ]', function() { to: wallet[0].address, value: web3.utils.toWei('50', 'ether'), }); - }) + }); it('sendSignedTransaction (with eth.signTransaction)', async function(){ // ganache does not support eth_signTransaction - if (process.env.GANACHE) return + if (process.env.GANACHE) return; - var destination = wallet[1].address; - var source = accounts[0] // Unlocked geth-dev account + const destination = wallet[1].address; + const source = accounts[0]; // Unlocked geth-dev account const txCount = await web3.eth.getTransactionCount(source); - var rawTx = { + const rawTx = { nonce: web3.utils.toHex(txCount), to: destination, from: source, value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')), gasLimit: web3.utils.toHex(21000), gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')) - } + }; - var signed = await web3.eth.signTransaction(rawTx) - var receipt = await web3.eth.sendSignedTransaction(signed.raw); + const signed = await web3.eth.signTransaction(rawTx); + const receipt = await web3.eth.sendSignedTransaction(signed.raw); assert(receipt.status === true); }); - it.skip('sendSignedTransaction (with eth.accounts.signTransaction)', async function(){ - var source = wallet[0].address - var destination = wallet[1].address; + it('sendSignedTransaction (with eth.accounts.signTransaction)', async function(){ + const source = wallet[0].address; + const destination = wallet[1].address; const txCount = await web3.eth.getTransactionCount(source); + const networkId = await web3.eth.net.getId(); + const chainId = await web3.eth.getChainId(); + + + const customCommon = Common.forCustomChain( + 'mainnet', + { + name: 'my-network', + networkId: networkId, + chainId: chainId, + }, + 'petersburg', + ); - var txObject = { + const txObject = { nonce: web3.utils.toHex(txCount), to: destination, value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')), gasLimit: web3.utils.toHex(21000), - gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')) - } + gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), + common: customCommon + }; - var signed = await web3.eth.accounts.signTransaction(txObject, wallet[0].privateKey); - var receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); + const signed = await web3.eth.accounts.signTransaction(txObject, wallet[0].privateKey); + const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); assert(receipt.status === true); }); it('eth.personal.sign', async function(){ // ganache does not support eth_sign - if (process.env.GANACHE) return + if (process.env.GANACHE) return; - var message = 'hello'; + const message = 'hello'; - var signature = await web3.eth.personal.sign( + const signature = await web3.eth.personal.sign( message, accounts[1], // Unlocked geth-dev acct "left-hand-of-darkness" // Default password at geth-dev @@ -89,11 +98,11 @@ describe('transaction and message signing [ @E2E ]', function() { it('eth.accounts.sign', async function(){ // ganache does not support eth_sign - if (process.env.GANACHE) return + if (process.env.GANACHE) return; - var message = 'hello'; + const message = 'hello'; - var signed = web3.eth.accounts.sign(message, wallet[0].privateKey); + const signed = web3.eth.accounts.sign(message, wallet[0].privateKey); const recovered = await web3.eth.personal.ecRecover(message, signed.signature); assert.equal(wallet[0].address.toLowerCase(), recovered.toLowerCase()); }) From 6af9a41205991fea0254d8ef45d36d2e1ae65707 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 17 Oct 2019 12:26:35 +0200 Subject: [PATCH 02/12] types and documentation for accounts.signTransaction updated --- docs/web3-eth-accounts.rst | 3 +++ packages/web3-core/types/index.d.ts | 3 +++ 2 files changed, 6 insertions(+) diff --git a/docs/web3-eth-accounts.rst b/docs/web3-eth-accounts.rst index 65bd4cbeb9e..2086bade063 100644 --- a/docs/web3-eth-accounts.rst +++ b/docs/web3-eth-accounts.rst @@ -143,6 +143,9 @@ Parameters ---------- 1. ``tx`` - ``Object``: The transaction object as follows: + - ``chain`` - ``String``: (optional) Defaults to ``mainnet``. + - ``hardfork`` - ``String``: (optional) Defaults to ``petersburg``. + - ``common`` - ``String``: (optional) The initiated common object of the `ethereumjs-common `_ package. - ``nonce`` - ``String``: (optional) The nonce to use when signing this transaction. Default will use :ref:`web3.eth.getTransactionCount() `. - ``chainId`` - ``String``: (optional) The chain id to use when signing this transaction. Default will use :ref:`web3.eth.net.getId() `. - ``to`` - ``String``: (optional) The recevier of the transaction, can be empty when deploying a contract. diff --git a/packages/web3-core/types/index.d.ts b/packages/web3-core/types/index.d.ts index 93a98cd183a..fec26c6f02d 100644 --- a/packages/web3-core/types/index.d.ts +++ b/packages/web3-core/types/index.d.ts @@ -124,6 +124,9 @@ export interface TransactionConfig { data?: string; nonce?: number; chainId?: number; + common?: any; // ethereumjs-common + chain?: string; + hardfork?: string; } export interface RLPEncodedTransaction { From 4c5a609eab367f548dc6176814e872aeff8cc308 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 17 Oct 2019 12:44:54 +0200 Subject: [PATCH 03/12] example for custom common added --- docs/web3-eth-accounts.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/web3-eth-accounts.rst b/docs/web3-eth-accounts.rst index 2086bade063..dbfc7c262cf 100644 --- a/docs/web3-eth-accounts.rst +++ b/docs/web3-eth-accounts.rst @@ -209,6 +209,29 @@ Example transactionHash: '0xd8f64a42b57be0d565f385378db2f6bf324ce14a594afc05de90436e9ce01f60' } + // or with a common + + var Common = require('ethereumjs-common').default; + + var customCommon = Common.forCustomChain( + 'mainnet', + { + name: 'my-network', + networkId: 1, + chainId: 1337, + }, + 'petersburg', + ); + + web3.eth.accounts.signTransaction({ + to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', + value: '1000000000', + gas: 2000000 + common: customCommon + }, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318') + .then(console.log); + + ------------------------------------------------------------------------------ From 1d26e0aad8748174ab9ddad2bc5ff47efb6c6792 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 17 Oct 2019 12:59:24 +0200 Subject: [PATCH 04/12] CHANGELOG.md updated and url added to accounts.signTransaction documentation --- CHANGELOG.md | 1 + docs/web3-eth-accounts.rst | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89b50feb070..6e5535a628e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,6 +58,7 @@ Released with 1.0.0-beta.37 code base. - Add `eth.getChainId` method (#3113) - Minified file added to web3 package (#3131) - The transaction confirmation workflow can now be configured (#3130) +- Additional parameters for accounts.signTransaction added [(docs)](https://web3js.readthedocs.io/en/v1.2.2/web3-eth-accounts.html#signtransaction) (#3141) ### Fixed diff --git a/docs/web3-eth-accounts.rst b/docs/web3-eth-accounts.rst index dbfc7c262cf..54d2b4f44c0 100644 --- a/docs/web3-eth-accounts.rst +++ b/docs/web3-eth-accounts.rst @@ -145,7 +145,7 @@ Parameters 1. ``tx`` - ``Object``: The transaction object as follows: - ``chain`` - ``String``: (optional) Defaults to ``mainnet``. - ``hardfork`` - ``String``: (optional) Defaults to ``petersburg``. - - ``common`` - ``String``: (optional) The initiated common object of the `ethereumjs-common `_ package. + - ``common`` - ``String``: (optional) The initiated common object of the `ethereumjs-common `_ package. `(Example) `_ - ``nonce`` - ``String``: (optional) The nonce to use when signing this transaction. Default will use :ref:`web3.eth.getTransactionCount() `. - ``chainId`` - ``String``: (optional) The chain id to use when signing this transaction. Default will use :ref:`web3.eth.net.getId() `. - ``to`` - ``String``: (optional) The recevier of the transaction, can be empty when deploying a contract. From 77eb8fed4143eee0b9fa9f434c18870cb54523d1 Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 17 Oct 2019 13:08:51 +0200 Subject: [PATCH 05/12] error msg improved --- packages/web3-eth-accounts/src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 527e481ea88..78744dfd9c0 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -133,7 +133,9 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca function signed (tx) { if (tx.common && (tx.chain && tx.hardfork)) { - error = new Error('Please provide the ethereumjs-common object or the chain and hardfork property but not both.') + error = new Error( + 'Please provide the ethereumjs-common object or the chain and hardfork property but not all together.' + ); } if (!tx.gas && !tx.gasLimit) { From aa2ce3e1cb69128d07394c5de5b3d549d46dcebb Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 17 Oct 2019 13:28:27 +0200 Subject: [PATCH 06/12] web3-eth sendTransaction documentation updated --- docs/web3-eth.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index b38bc8adcae..0d2daddcc50 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -1056,9 +1056,13 @@ Parameters - ``gasPrice`` - ``Number|String|BN|BigNumber``: (optional) The price of gas for this transaction in :ref:`wei `, defaults to :ref:`web3.eth.gasPrice `. - ``data`` - ``String``: (optional) Either a `ABI byte string `_ containing the data of the function call on a contract, or in the case of a contract-creation transaction the initialisation code. - ``nonce`` - ``Number``: (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce. + - ``chain`` - ``String``: (optional) Defaults to ``mainnet``. + - ``hardfork`` - ``String``: (optional) Defaults to ``petersburg``. + - ``common`` - ``String``: (optional) The initiated common object of the `ethereumjs-common `_ package. `(Example) `_ + 2. ``callback`` - ``Function``: (optional) Optional callback, returns an error object as first parameter and the result as second. -.. note:: The ``from`` property can also be an address or index from the :ref:`web3.eth.accounts.wallet `. It will then sign locally using the private key of that account, and send the transaction via :ref:`web3.eth.sendSignedTransaction() `. +.. note:: The ``from`` property can also be an address or index from the :ref:`web3.eth.accounts.wallet `. It will then sign locally using the private key of that account, and send the transaction via :ref:`web3.eth.sendSignedTransaction() `. The properties ``chain`` and ``hardfork`` or ``common`` are required if you are not connected to the ``mainnet``. .. _eth-sendtransaction-return: From 3f38254c1a17e33f176f87d6a54407bd4dc83a7d Mon Sep 17 00:00:00 2001 From: nivida Date: Thu, 17 Oct 2019 16:13:19 +0200 Subject: [PATCH 07/12] parameter validation updated for accounts.signTransaction method --- packages/web3-eth-accounts/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 78744dfd9c0..de1191b962a 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -132,7 +132,7 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca function signed (tx) { - if (tx.common && (tx.chain && tx.hardfork)) { + if (tx.common && (tx.chain || tx.hardfork)) { error = new Error( 'Please provide the ethereumjs-common object or the chain and hardfork property but not all together.' ); From f5dd9ada991fab77e78bb8709603a272a73d2551 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Mon, 21 Oct 2019 06:16:41 -0700 Subject: [PATCH 08/12] Auto fetch tx signing option values when not set by user (#3143) * Auto fetch tx signing option values when not set by user * public api simplified if someone wants to pass custom chain configuration (ethereumjs-common) * default properties added (chain, hardfork, and common) and related types updated --- docs/web3-eth-accounts.rst | 23 ++- docs/web3-eth.rst | 3 +- package-lock.json | 120 +-------------- package.json | 2 - packages/web3-core-method/src/index.js | 19 ++- packages/web3-core/types/index.d.ts | 32 +++- packages/web3-eth-accounts/package-lock.json | 8 +- packages/web3-eth-accounts/package.json | 5 +- packages/web3-eth-accounts/src/index.js | 143 ++++++++++++------ packages/web3-eth-contract/src/index.js | 3 + packages/web3-eth-contract/types/index.d.ts | 10 +- .../types/tests/contract-test.ts | 27 +++- packages/web3-eth-contract/types/tslint.json | 3 +- packages/web3-eth/src/index.js | 52 +++++++ packages/web3-eth/types/index.d.ts | 11 +- packages/web3-eth/types/tests/eth.tests.ts | 24 +++ packages/web3-utils/package-lock.json | 97 +++--------- packages/web3/angular-patch.js | 4 +- test/contract.js | 4 + test/e2e.method.signing.js | 107 +++++++++++-- test/eth.accounts.signTransaction.js | 118 ++++++++++++--- test/eth.sendTransaction.js | 12 +- 22 files changed, 521 insertions(+), 306 deletions(-) diff --git a/docs/web3-eth-accounts.rst b/docs/web3-eth-accounts.rst index 54d2b4f44c0..9d9dd03589e 100644 --- a/docs/web3-eth-accounts.rst +++ b/docs/web3-eth-accounts.rst @@ -210,24 +210,19 @@ Example } // or with a common - - var Common = require('ethereumjs-common').default; - - var customCommon = Common.forCustomChain( - 'mainnet', - { - name: 'my-network', - networkId: 1, - chainId: 1337, - }, - 'petersburg', - ); - web3.eth.accounts.signTransaction({ to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', value: '1000000000', gas: 2000000 - common: customCommon + common: { + baseChain: 'mainnet', + hardfork: 'petersburg', + customChain: { + name: 'custom-chain', + chainId: 1, + networkId: 1 + } + } }, '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318') .then(console.log); diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 0d2daddcc50..9a7edf01108 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -1062,7 +1062,8 @@ Parameters 2. ``callback`` - ``Function``: (optional) Optional callback, returns an error object as first parameter and the result as second. -.. note:: The ``from`` property can also be an address or index from the :ref:`web3.eth.accounts.wallet `. It will then sign locally using the private key of that account, and send the transaction via :ref:`web3.eth.sendSignedTransaction() `. The properties ``chain`` and ``hardfork`` or ``common`` are required if you are not connected to the ``mainnet``. +.. note:: The ``from`` property can also be an address or index from the :ref:`web3.eth.accounts.wallet `. It will then sign locally using the private key of that account, and send the transaction via :ref:`web3.eth.sendSignedTransaction() `. If the properties ``chain`` and ``hardfork`` or ``common`` are not set, Web3 will try to set appropriate values by +querying the network for its chainId and networkId. .. _eth-sendtransaction-return: diff --git a/package-lock.json b/package-lock.json index e7ad0f0578e..ff6b33f2fc4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1640,24 +1640,6 @@ "integrity": "sha512-xVNN69YGDghOqCCtA6FI7avYrr02mTJjOgB0/f1VPD3pJC8QEvjTKWc4epDx8AqxxA75NI0QpVM2gPJXUbE4Tg==", "dev": true }, - "bindings": { - "version": "1.5.0", - "resolved": "https://registry.npmjs.org/bindings/-/bindings-1.5.0.tgz", - "integrity": "sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==", - "dev": true, - "requires": { - "file-uri-to-path": "1.0.0" - } - }, - "bip66": { - "version": "1.1.5", - "resolved": "https://registry.npmjs.org/bip66/-/bip66-1.1.5.tgz", - "integrity": "sha1-AfqHSHhcpwlV1QESF9GzE5lpyiI=", - "dev": true, - "requires": { - "safe-buffer": "^5.0.1" - } - }, "bl": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/bl/-/bl-1.2.2.tgz", @@ -3527,17 +3509,6 @@ "is-obj": "^1.0.0" } }, - "drbg.js": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/drbg.js/-/drbg.js-1.0.1.tgz", - "integrity": "sha1-Pja2xCs3BDgjzbwzLVjzHiRFSAs=", - "dev": true, - "requires": { - "browserify-aes": "^1.0.6", - "create-hash": "^1.1.2", - "create-hmac": "^1.1.4" - } - }, "duplexer": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", @@ -3829,37 +3800,6 @@ "xhr-request-promise": "^0.1.2" } }, - "ethereumjs-common": { - "version": "1.3.2", - "resolved": "https://registry.npmjs.org/ethereumjs-common/-/ethereumjs-common-1.3.2.tgz", - "integrity": "sha512-GkltYRIqBLzaZLmF/K3E+g9lZ4O4FL+TtpisAlD3N+UVlR+mrtoG+TvxavqVa6PwOY4nKIEMe5pl6MrTio3Lww==", - "dev": true - }, - "ethereumjs-tx": { - "version": "2.1.1", - "resolved": "https://registry.npmjs.org/ethereumjs-tx/-/ethereumjs-tx-2.1.1.tgz", - "integrity": "sha512-QtVriNqowCFA19X9BCRPMgdVNJ0/gMBS91TQb1DfrhsbR748g4STwxZptFAwfqehMyrF8rDwB23w87PQwru0wA==", - "dev": true, - "requires": { - "ethereumjs-common": "^1.3.1", - "ethereumjs-util": "^6.0.0" - } - }, - "ethereumjs-util": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/ethereumjs-util/-/ethereumjs-util-6.1.0.tgz", - "integrity": "sha512-URESKMFbDeJxnAxPppnk2fN6Y3BIatn9fwn76Lm8bQlt+s52TpG8dN9M66MLPuRAiAOIqL3dfwqWJf0sd0fL0Q==", - "dev": true, - "requires": { - "bn.js": "^4.11.0", - "create-hash": "^1.1.2", - "ethjs-util": "0.1.6", - "keccak": "^1.0.2", - "rlp": "^2.0.0", - "safe-buffer": "^5.1.1", - "secp256k1": "^3.0.1" - } - }, "ethers": { "version": "4.0.33", "resolved": "https://registry.npmjs.org/ethers/-/ethers-4.0.33.tgz", @@ -3981,16 +3921,6 @@ } } }, - "ethjs-util": { - "version": "0.1.6", - "resolved": "https://registry.npmjs.org/ethjs-util/-/ethjs-util-0.1.6.tgz", - "integrity": "sha512-CUnVOQq7gSpDHZVVrQW8ExxUETWrnrvXYvYz55wOU8Uj4VCgw56XC2B/fVqQN+f7gmrnRHSLVnFAwsCuNwji8w==", - "dev": true, - "requires": { - "is-hex-prefixed": "1.0.0", - "strip-hex-prefix": "1.0.0" - } - }, "eventemitter3": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/eventemitter3/-/eventemitter3-3.1.2.tgz", @@ -4307,12 +4237,6 @@ "integrity": "sha1-LdvqfHP/42No365J3DOMBYwritY=", "dev": true }, - "file-uri-to-path": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz", - "integrity": "sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==", - "dev": true - }, "fileset": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/fileset/-/fileset-2.0.3.tgz", @@ -7904,18 +7828,6 @@ "integrity": "sha1-h/zPrv/AtozRnVX2cilD+SnqNeo=", "dev": true }, - "keccak": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/keccak/-/keccak-1.4.0.tgz", - "integrity": "sha512-eZVaCpblK5formjPjeTBik7TAg+pqnDrMHIffSvi9Lh7PQgM1+hSzakUeZFCk9DVVG0dacZJuaz2ntwlzZUIBw==", - "dev": true, - "requires": { - "bindings": "^1.2.1", - "inherits": "^2.0.3", - "nan": "^2.2.1", - "safe-buffer": "^5.1.0" - } - }, "keccakjs": { "version": "0.2.3", "resolved": "https://registry.npmjs.org/keccakjs/-/keccakjs-0.2.3.tgz", @@ -10554,16 +10466,6 @@ "inherits": "^2.0.1" } }, - "rlp": { - "version": "2.2.3", - "resolved": "https://registry.npmjs.org/rlp/-/rlp-2.2.3.tgz", - "integrity": "sha512-l6YVrI7+d2vpW6D6rS05x2Xrmq8oW7v3pieZOJKBEdjuTF4Kz/iwk55Zyh1Zaz+KOB2kC8+2jZlp2u9L4tTzCQ==", - "dev": true, - "requires": { - "bn.js": "^4.11.1", - "safe-buffer": "^5.1.1" - } - }, "run-async": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/run-async/-/run-async-2.3.0.tgz", @@ -10639,22 +10541,6 @@ "integrity": "sha512-1CdSqHQowJBnMAFyPEBRfqag/YP9OF394FV+4YREIJX4ljD7OxvQRDayyoyyCk+senRjSkP6VnUNQmVQqB6g7w==", "dev": true }, - "secp256k1": { - "version": "3.7.1", - "resolved": "https://registry.npmjs.org/secp256k1/-/secp256k1-3.7.1.tgz", - "integrity": "sha512-1cf8sbnRreXrQFdH6qsg2H71Xw91fCCS9Yp021GnUNJzWJS/py96fS4lHbnTnouLp08Xj6jBoBB6V78Tdbdu5g==", - "dev": true, - "requires": { - "bindings": "^1.5.0", - "bip66": "^1.1.5", - "bn.js": "^4.11.8", - "create-hash": "^1.2.0", - "drbg.js": "^1.0.1", - "elliptic": "^6.4.1", - "nan": "^2.14.0", - "safe-buffer": "^5.1.2" - } - }, "seek-bzip": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/seek-bzip/-/seek-bzip-1.0.5.tgz", @@ -11759,9 +11645,9 @@ } }, "typescript": { - "version": "3.7.0-dev.20191017", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.0-dev.20191017.tgz", - "integrity": "sha512-Yi0lCPEN0cn9Gp8TEEkPpgKNR5SWAmx9Hmzzz+oEuivw6amURqRGynaLyFZkMA9iMsvYG5LLqhdlFO3uu5ZT/w==", + "version": "3.7.0-dev.20191018", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.0-dev.20191018.tgz", + "integrity": "sha512-Z8KpsytbY5lBMp5cc08VFoO8CgHC6IcbgyiA5vjh7fitkoG0qcem9C354YuiWV4O2+i2gdC7vF8tNUYqO/vUkQ==", "dev": true }, "uglify-js": { diff --git a/package.json b/package.json index 606ab46ca23..acc1d93fb1a 100644 --- a/package.json +++ b/package.json @@ -87,8 +87,6 @@ "coveralls": "^3.0.7", "crypto-js": "^3.1.9-1", "del": "^4.1.1", - "ethereumjs-common": "^1.3.2", - "ethereumjs-tx": "^2.1.1", "ethers": "4.0.33", "ethjs-signer": "^0.1.1", "exorcist": "^1.0.1", diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index c93a16effa4..f9a31144f30 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -54,6 +54,9 @@ var Method = function Method(options) { this.transactionBlockTimeout = options.transactionBlockTimeout || 50; this.transactionConfirmationBlocks = options.transactionConfirmationBlocks || 24; this.transactionPollingTimeout = options.transactionPollingTimeout || 750; + this.defaultCommon = options.defaultCommon; + this.defaultChain = options.defaultChain; + this.defaultHardfork = options.defaultHardfork; }; Method.prototype.setRequestManager = function(requestManager, accounts) { @@ -597,7 +600,21 @@ Method.prototype.buildCall = function() { // If wallet was found, sign tx, and send using sendRawTransaction if (wallet && wallet.privateKey) { - return method.accounts.signTransaction(_.omit(tx, 'from'), wallet.privateKey) + var txOptions = _.omit(tx, 'from'); + + if (method.defaultChain) { + txOptions.chain = method.defaultChain; + } + + if (method.defaultHardfork) { + txOptions.hardfork = method.defaultHardfork; + } + + if (method.defaultCommon) { + txOptions.common = method.defaultCommon; + } + + return method.accounts.signTransaction(txOptions, wallet.privateKey) .then(sendSignedTx) .catch(function(err) { if (_.isFunction(defer.eventEmitter.listeners) && defer.eventEmitter.listeners('error').length) { diff --git a/packages/web3-core/types/index.d.ts b/packages/web3-core/types/index.d.ts index fec26c6f02d..f7c57b6095c 100644 --- a/packages/web3-core/types/index.d.ts +++ b/packages/web3-core/types/index.d.ts @@ -124,11 +124,41 @@ export interface TransactionConfig { data?: string; nonce?: number; chainId?: number; - common?: any; // ethereumjs-common + common?: Common; chain?: string; hardfork?: string; } +export type chain = + | 'mainnet' + | 'goerli' + | 'kovan' + | 'rinkeby' + | 'ropsten'; + +export type hardfork = + | 'chainstart' + | 'homestead' + | 'dao' + | 'tangerineWhistle' + | 'spuriousDragon' + | 'byzantium' + | 'constantinople' + | 'petersburg' + | 'istanbul'; + +export interface Common { + customChain: CustomChainParams; + baseChain?: chain; + hardfork?: hardfork; +} + +export interface CustomChainParams { + name?: string; + networkId: number; + chainId: number; +} + export interface RLPEncodedTransaction { raw: string; tx: { diff --git a/packages/web3-eth-accounts/package-lock.json b/packages/web3-eth-accounts/package-lock.json index 0221d7b9c19..6857548d2ac 100644 --- a/packages/web3-eth-accounts/package-lock.json +++ b/packages/web3-eth-accounts/package-lock.json @@ -340,7 +340,7 @@ "fs-extra": "^6.0.1", "strip-json-comments": "^2.0.1", "tslint": "^5.12.0", - "typescript": "^3.7.0-dev.20191015" + "typescript": "^3.7.0-dev.20191018" }, "dependencies": { "definitelytyped-header-parser": { @@ -1004,9 +1004,9 @@ } }, "typescript": { - "version": "3.7.0-dev.20191015", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.0-dev.20191015.tgz", - "integrity": "sha512-Cpfj1n4pEUVKL+jtS0mkZodJffyMmf3Wk/UjyZMGX4fsjK5KBPJf3NUlyXij8I8p1E2CAomdS5NPFrAR+z8pKw==" + "version": "3.7.0-dev.20191018", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.0-dev.20191018.tgz", + "integrity": "sha512-Z8KpsytbY5lBMp5cc08VFoO8CgHC6IcbgyiA5vjh7fitkoG0qcem9C354YuiWV4O2+i2gdC7vF8tNUYqO/vUkQ==" }, "underscore": { "version": "1.9.1", diff --git a/packages/web3-eth-accounts/package.json b/packages/web3-eth-accounts/package.json index 043ce47243f..40c9c4c705a 100644 --- a/packages/web3-eth-accounts/package.json +++ b/packages/web3-eth-accounts/package.json @@ -16,14 +16,15 @@ "any-promise": "1.3.0", "crypto-browserify": "3.12.0", "eth-lib": "0.2.7", + "ethereumjs-common": "^1.3.2", + "ethereumjs-tx": "^2.1.1", "scrypt-shim": "github:web3-js/scrypt-shim", "underscore": "1.9.1", "uuid": "3.3.2", "web3-core": "1.2.1", "web3-core-helpers": "1.2.1", "web3-core-method": "1.2.1", - "web3-utils": "1.2.1", - "ethereumjs-tx": "^2.1.1" + "web3-utils": "1.2.1" }, "devDependencies": { "definitelytyped-header-parser": "^1.0.1", diff --git a/packages/web3-eth-accounts/src/index.js b/packages/web3-eth-accounts/src/index.js index 78744dfd9c0..41a186c2337 100644 --- a/packages/web3-eth-accounts/src/index.js +++ b/packages/web3-eth-accounts/src/index.js @@ -36,9 +36,10 @@ var uuid = require('uuid'); var utils = require('web3-utils'); var helpers = require('web3-core-helpers'); var Transaction = require('ethereumjs-tx').Transaction; +var Common = require('ethereumjs-common').default; -var isNot = function(value) { +var isNot = function (value) { return (_.isUndefined(value) || _.isNull(value)); }; @@ -54,7 +55,13 @@ var Accounts = function Accounts() { var _ethereumCall = [ new Method({ - name: 'getId', + name: 'getNetworkId', + call: 'net_version', + params: 0, + outputFormatter: parseInt + }), + new Method({ + name: 'getChainId', call: 'eth_chainId', params: 0, outputFormatter: utils.hexToNumber @@ -72,9 +79,11 @@ var Accounts = function Accounts() { if (utils.isAddress(address)) { return address; } else { - throw new Error('Address '+ address +' is not a valid address to get the "transactionCount".'); + throw new Error('Address ' + address + ' is not a valid address to get the "transactionCount".'); } - }, function () { return 'latest'; }] + }, function () { + return 'latest'; + }] }) ]; // attach methods to this._ethereumCall @@ -119,9 +128,11 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca var _this = this, error = false, transactionOptions = {}, - result; + result, + hasTxSigningOptions = !!((tx.chain && tx.hardfork) || tx.common); - callback = callback || function () {}; + callback = callback || function () { + }; if (!tx) { error = new Error('No transaction object given!'); @@ -130,22 +141,28 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca return Promise.reject(error); } - function signed (tx) { - + function signed(tx) { if (tx.common && (tx.chain && tx.hardfork)) { error = new Error( 'Please provide the ethereumjs-common object or the chain and hardfork property but not all together.' ); } + if ((tx.chain && !tx.hardfork) || (tx.hardfork && !tx.chain)) { + error = new Error( + 'When specifying chain and hardfork, both values must be defined. ' + + 'Received "chain": ' + tx.chain + ', "hardfork": ' + tx.hardfork + ); + } + if (!tx.gas && !tx.gasLimit) { error = new Error('"gas" is missing'); } - if (tx.nonce < 0 || - tx.gas < 0 || - tx.gasPrice < 0 || - tx.chainId < 0) { + if (tx.nonce < 0 || + tx.gas < 0 || + tx.gasPrice < 0 || + tx.chainId < 0) { error = new Error('Gas, gasPrice, nonce or chainId is lower than 0'); } @@ -161,19 +178,43 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca transaction.value = transaction.value || '0x'; transaction.chainId = utils.numberToHex(transaction.chainId); - if (transaction.common) { - transactionOptions['common'] = transaction.common; - delete transaction.common; - } + // Because tx has no ethereumjs-tx signing options we use fetched vals. + if (!hasTxSigningOptions) { + transactionOptions.common = Common.forCustomChain( + 'mainnet', + { + name: 'custom-network', + networkId: transaction.networkId, + chainId: transaction.chainId, + }, + 'petersburg' + ); + + delete transaction.networkId; + } else { + if (transaction.common) { + transactionOptions.common = Common.forCustomChain( + transaction.common.baseChain || 'mainnet', + { + name: transaction.common.customChain.name || 'custom-network', + networkId: transaction.common.customChain.networkId, + chainId: transaction.common.customChain.chainId, + }, + transaction.common.hardfork || 'petersburg' + ); + + delete transaction.common; + } - if (transaction.chain) { - transactionOptions['chain'] = transaction.chain; - delete transaction.chain; - } + if (transaction.chain) { + transactionOptions.chain = transaction.chain; + delete transaction.chain; + } - if (transaction.hardfork) { - transactionOptions['hardfork'] = transaction.hardfork; - delete transaction.hardfork; + if (transaction.hardfork) { + transactionOptions.hardfork = transaction.hardfork; + delete transaction.hardfork; + } } if (privateKey.startsWith('0x')) { @@ -203,7 +244,7 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca transactionHash: transactionHash }; - } catch(e) { + } catch (e) { callback(e); return Promise.reject(e); } @@ -212,32 +253,33 @@ Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, ca return result; } - // Resolve immediately if nonce, chainId and price are provided - if (tx.nonce !== undefined && tx.chainId !== undefined && tx.gasPrice !== undefined) { + + // Resolve immediately if nonce, chainId, price and signing options are provided + if (tx.nonce !== undefined && tx.chainId !== undefined && tx.gasPrice !== undefined && hasTxSigningOptions) { return Promise.resolve(signed(tx)); } - // Otherwise, get the missing info from the Ethereum Node return Promise.all([ - isNot(tx.chainId) ? _this._ethereumCall.getId() : tx.chainId, + isNot(tx.chainId) ? _this._ethereumCall.getChainId() : tx.chainId, isNot(tx.gasPrice) ? _this._ethereumCall.getGasPrice() : tx.gasPrice, - isNot(tx.nonce) ? _this._ethereumCall.getTransactionCount(_this.privateKeyToAccount(privateKey).address) : tx.nonce + isNot(tx.nonce) ? _this._ethereumCall.getTransactionCount(_this.privateKeyToAccount(privateKey).address) : tx.nonce, + isNot(hasTxSigningOptions) ? _this._ethereumCall.getNetworkId() : 1, ]).then(function (args) { - if (isNot(args[0]) || isNot(args[1]) || isNot(args[2])) { - throw new Error('One of the values "chainId", "gasPrice", or "nonce" couldn\'t be fetched: '+ JSON.stringify(args)); + if (isNot(args[0]) || isNot(args[1]) || isNot(args[2]) || isNot(args[3])) { + throw new Error('One of the values "chainId", "networkId", "gasPrice", or "nonce" couldn\'t be fetched: ' + JSON.stringify(args)); } - return signed(_.extend(tx, {chainId: args[0], gasPrice: args[1], nonce: args[2]})); + return signed(_.extend(tx, {chainId: args[0], gasPrice: args[1], nonce: args[2], networkId: args[3]})); }); }; /* jshint ignore:start */ Accounts.prototype.recoverTransaction = function recoverTransaction(rawTx) { var values = RLP.decode(rawTx); - var signature = Account.encodeSignature(values.slice(6,9)); + var signature = Account.encodeSignature(values.slice(6, 9)); var recovery = Bytes.toNumber(values[6]); var extraData = recovery < 35 ? [] : [Bytes.fromNumber((recovery - 35) >> 1), "0x", "0x"]; - var signingData = values.slice(0,6).concat(extraData); + var signingData = values.slice(0, 6).concat(extraData); var signingDataHex = RLP.encode(signingData); return Account.recover(Hash.keccak256(signingDataHex), signature); }; @@ -291,7 +333,7 @@ Accounts.prototype.recover = function recover(message, signature, preFixed) { Accounts.prototype.decrypt = function (v3Keystore, password, nonStrict) { /* jshint maxcomplexity: 10 */ - if(!_.isString(password)) { + if (!_.isString(password)) { throw new Error('No password given.'); } @@ -322,13 +364,13 @@ Accounts.prototype.decrypt = function (v3Keystore, password, nonStrict) { var ciphertext = Buffer.from(json.crypto.ciphertext, 'hex'); - var mac = utils.sha3(Buffer.concat([ derivedKey.slice(16, 32), ciphertext ])).replace('0x',''); + var mac = utils.sha3(Buffer.concat([derivedKey.slice(16, 32), ciphertext])).replace('0x', ''); if (mac !== json.crypto.mac) { throw new Error('Key derivation failed - possibly wrong password'); } var decipher = cryp.createDecipheriv(json.crypto.cipher, derivedKey.slice(0, 16), Buffer.from(json.crypto.cipherparams.iv, 'hex')); - var seed = '0x'+ Buffer.concat([ decipher.update(ciphertext), decipher.final() ]).toString('hex'); + var seed = '0x' + Buffer.concat([decipher.update(ciphertext), decipher.final()]).toString('hex'); return this.privateKeyToAccount(seed); }; @@ -367,14 +409,14 @@ Accounts.prototype.encrypt = function (privateKey, password, options) { throw new Error('Unsupported cipher'); } - var ciphertext = Buffer.concat([ cipher.update(Buffer.from(account.privateKey.replace('0x',''), 'hex')), cipher.final() ]); + var ciphertext = Buffer.concat([cipher.update(Buffer.from(account.privateKey.replace('0x', ''), 'hex')), cipher.final()]); - var mac = utils.sha3(Buffer.concat([ derivedKey.slice(16, 32), Buffer.from(ciphertext, 'hex') ])).replace('0x',''); + var mac = utils.sha3(Buffer.concat([derivedKey.slice(16, 32), Buffer.from(ciphertext, 'hex')])).replace('0x', ''); return { version: 3, - id: uuid.v4({ random: options.uuid || cryp.randomBytes(16) }), - address: account.address.toLowerCase().replace('0x',''), + id: uuid.v4({random: options.uuid || cryp.randomBytes(16)}), + address: account.address.toLowerCase().replace('0x', ''), crypto: { ciphertext: ciphertext.toString('hex'), cipherparams: { @@ -410,8 +452,12 @@ Wallet.prototype._findSafeIndex = function (pointer) { Wallet.prototype._currentIndexes = function () { var keys = Object.keys(this); var indexes = keys - .map(function(key) { return parseInt(key); }) - .filter(function(n) { return (n < 9e20); }); + .map(function (key) { + return parseInt(key); + }) + .filter(function (n) { + return (n < 9e20); + }); return indexes; }; @@ -470,7 +516,7 @@ Wallet.prototype.clear = function () { var _this = this; var indexes = this._currentIndexes(); - indexes.forEach(function(index) { + indexes.forEach(function (index) { _this.remove(index); }); @@ -481,7 +527,7 @@ Wallet.prototype.encrypt = function (password, options) { var _this = this; var indexes = this._currentIndexes(); - var accounts = indexes.map(function(index) { + var accounts = indexes.map(function (index) { return _this[index].encrypt(password, options); }); @@ -517,7 +563,7 @@ Wallet.prototype.load = function (password, keyName) { if (keystore) { try { keystore = JSON.parse(keystore); - } catch(e) { + } catch (e) { } } @@ -547,10 +593,9 @@ function storageAvailable(type) { storage.setItem(x, x); storage.removeItem(x); return true; - } - catch(e) { + } catch (e) { return e && ( - // everything except Firefox + // everything except Firefox e.code === 22 || // Firefox e.code === 1014 || diff --git a/packages/web3-eth-contract/src/index.js b/packages/web3-eth-contract/src/index.js index d85aa0e6fe8..1c51f0bd6f0 100644 --- a/packages/web3-eth-contract/src/index.js +++ b/packages/web3-eth-contract/src/index.js @@ -183,6 +183,9 @@ var Contract = function Contract(jsonInterface, address, options) { this.transactionBlockTimeout = this.constructor.transactionBlockTimeout; this.transactionConfirmationBlocks = this.constructor.transactionConfirmationBlocks; this.transactionPollingTimeout = this.constructor.transactionPollingTimeout; + this.defaultChain = this.constructor.defaultChain; + this.defaultHardfork = this.constructor.defaultHardfork; + this.defaultCommon = this.constructor.defaultCommon; Object.defineProperty(this, 'defaultAccount', { get: function () { diff --git a/packages/web3-eth-contract/types/index.d.ts b/packages/web3-eth-contract/types/index.d.ts index 4f5f90213a6..a29aeb5714b 100644 --- a/packages/web3-eth-contract/types/index.d.ts +++ b/packages/web3-eth-contract/types/index.d.ts @@ -18,7 +18,7 @@ */ import BN = require('bn.js'); -import {PromiEvent, provider} from 'web3-core'; +import {Common, PromiEvent, provider, hardfork, chain} from 'web3-core'; import {AbiItem} from 'web3-utils'; export class Contract { @@ -31,6 +31,14 @@ export class Contract { private _address: string; private _jsonInterface: AbiItem[]; + defaultAccount: string | null; + defaultBlock: string | number; + defaultCommon: Common; + defaultHardfork: hardfork; + defaultChain: chain; + transactionPollingTimeout: number; + transactionConfirmationBlocks: number; + transactionBlockTimeout: number; options: Options; diff --git a/packages/web3-eth-contract/types/tests/contract-test.ts b/packages/web3-eth-contract/types/tests/contract-test.ts index 51c2d22bdc5..9bc767d75dd 100644 --- a/packages/web3-eth-contract/types/tests/contract-test.ts +++ b/packages/web3-eth-contract/types/tests/contract-test.ts @@ -13,7 +13,8 @@ */ /** * @file contract-tests.ts - * @author Josh Stevens , Samuel Furter + * @author Josh Stevens + * @author Samuel Furter * @date 2018 */ @@ -21,6 +22,30 @@ import { Contract } from 'web3-eth-contract'; const contract = new Contract('http://localhost:500', []); +// $ExpectType string | null +contract.defaultAccount; + +// $ExpectType string | number +contract.defaultBlock; + +// $ExpectType Common +contract.defaultCommon; + +// $ExpectType hardfork +contract.defaultHardfork; + +// $ExpectType chain +contract.defaultChain; + +// $ExpectType number +contract.transactionPollingTimeout; + +// $ExpectType number +contract.transactionConfirmationBlocks; + +// $ExpectType number +contract.transactionBlockTimeout; + // $ExpectType string contract.options.address; diff --git a/packages/web3-eth-contract/types/tslint.json b/packages/web3-eth-contract/types/tslint.json index 30e95b7bca5..383b159dc72 100644 --- a/packages/web3-eth-contract/types/tslint.json +++ b/packages/web3-eth-contract/types/tslint.json @@ -7,6 +7,7 @@ "whitespace": false, "no-unnecessary-class": false, "no-empty-interface": false, - "unified-signatures": false + "unified-signatures": false, + "no-redundant-jsdoc": false } } diff --git a/packages/web3-eth/src/index.js b/packages/web3-eth/src/index.js index d2cd4608989..90663836e34 100644 --- a/packages/web3-eth/src/index.js +++ b/packages/web3-eth/src/index.js @@ -84,7 +84,59 @@ var Eth = function Eth() { var transactionBlockTimeout = 50; var transactionConfirmationBlocks = 24; var transactionPollingTimeout = 750; + var defaultChain, defaultHardfork, defaultCommon; + Object.defineProperty(this, '≈', { + get: function () { + return defaultCommon; + }, + set: function (val) { + defaultCommon = val; + + // also set on the Contract object + _this.Contract.defaultCommon = defaultCommon; + + // update defaultBlock + methods.forEach(function(method) { + method.defaultCommon = defaultCommon; + }); + }, + enumerable: true + }); + Object.defineProperty(this, 'defaultHardfork', { + get: function () { + return defaultHardfork; + }, + set: function (val) { + defaultHardfork = val; + + // also set on the Contract object + _this.Contract.defaultHardfork = defaultHardfork; + + // update defaultBlock + methods.forEach(function(method) { + method.defaultHardfork = defaultHardfork; + }); + }, + enumerable: true + }); + Object.defineProperty(this, 'defaultChain', { + get: function () { + return defaultChain; + }, + set: function (val) { + defaultChain = val; + + // also set on the Contract object + _this.Contract.defaultChain = defaultChain; + + // update defaultBlock + methods.forEach(function(method) { + method.defaultChain = defaultChain; + }); + }, + enumerable: true + }); Object.defineProperty(this, 'transactionPollingTimeout', { get: function () { return transactionPollingTimeout; diff --git a/packages/web3-eth/types/index.d.ts b/packages/web3-eth/types/index.d.ts index a15132dc9d4..b42d386741f 100644 --- a/packages/web3-eth/types/index.d.ts +++ b/packages/web3-eth/types/index.d.ts @@ -29,7 +29,10 @@ import { RLPEncodedTransaction, Transaction, TransactionConfig, - TransactionReceipt + TransactionReceipt, + Common, + hardfork, + chain } from 'web3-core'; import { Subscription } from 'web3-core-subscriptions'; import { AbiCoder } from 'web3-eth-abi'; @@ -59,6 +62,12 @@ export class Eth { readonly givenProvider: any; defaultAccount: string | null; defaultBlock: string | number; + defaultCommon: Common; + defaultHardfork: hardfork; + defaultChain: chain; + transactionPollingTimeout: number; + transactionConfirmationBlocks: number; + transactionBlockTimeout: number; readonly currentProvider: provider; setProvider(provider: provider): boolean; BatchRequest: new () => BatchRequest; diff --git a/packages/web3-eth/types/tests/eth.tests.ts b/packages/web3-eth/types/tests/eth.tests.ts index d06c388c996..395bd7ee0a2 100644 --- a/packages/web3-eth/types/tests/eth.tests.ts +++ b/packages/web3-eth/types/tests/eth.tests.ts @@ -28,6 +28,30 @@ import { Block, BlockHeader, Eth, GetProof, Syncing } from 'web3-eth'; const eth = new Eth('http://localhost:8545'); +// $ExpectType string | null +eth.defaultAccount; + +// $ExpectType string | number +eth.defaultBlock; + +// $ExpectType Common +eth.defaultCommon; + +// $ExpectType hardfork +eth.defaultHardfork; + +// $ExpectType chain +eth.defaultChain; + +// $ExpectType number +eth.transactionPollingTimeout; + +// $ExpectType number +eth.transactionConfirmationBlocks; + +// $ExpectType number +eth.transactionBlockTimeout; + // $ExpectType new (jsonInterface: AbiItem | AbiItem[], address?: string | undefined, options?: ContractOptions | undefined) => Contract eth.Contract; diff --git a/packages/web3-utils/package-lock.json b/packages/web3-utils/package-lock.json index 8446e08b573..a7d5d6f9953 100644 --- a/packages/web3-utils/package-lock.json +++ b/packages/web3-utils/package-lock.json @@ -1,14 +1,11 @@ { - "name": "web3-utils", - "version": "1.2.1", - "lockfileVersion": 1, "requires": true, + "lockfileVersion": 1, "dependencies": { "@babel/code-frame": { "version": "7.5.5", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.5.5.tgz", "integrity": "sha512-27d4lZoomVyo51VegxI20xZPuSHusqbQag/ztrBC7wegWoQ1nLREPVSKSW8byhTlzTKyNE4ifaTA6lCp7JjpFw==", - "dev": true, "requires": { "@babel/highlight": "^7.0.0" } @@ -17,7 +14,6 @@ "version": "7.5.0", "resolved": "https://registry.npmjs.org/@babel/highlight/-/highlight-7.5.0.tgz", "integrity": "sha512-7dV4eu9gBxoM0dAnj/BCFDW9LFU0zvTrkq0ugM7pnHEgguOEeOz1so2ZghEdzviYzQEED0r4EAgpsBChKy1TRQ==", - "dev": true, "requires": { "chalk": "^2.0.0", "esutils": "^2.0.2", @@ -27,14 +23,12 @@ "@types/parsimmon": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/@types/parsimmon/-/parsimmon-1.10.0.tgz", - "integrity": "sha512-bsTIJFVQv7jnvNiC42ld2pQW2KRI+pAG243L+iATvqzy3X6+NH1obz2itRKDZZ8VVhN3wjwYax/VBGCcXzgTqQ==", - "dev": true + "integrity": "sha512-bsTIJFVQv7jnvNiC42ld2pQW2KRI+pAG243L+iATvqzy3X6+NH1obz2itRKDZZ8VVhN3wjwYax/VBGCcXzgTqQ==" }, "ansi-styles": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-3.2.1.tgz", "integrity": "sha512-VT0ZI6kZRdTh8YyJw3SMbYm/u+NqfsAxEpWO0Pf9sq8/e94WxxOpPKx9FR1FlyCtOVDNOQ+8ntlqFxiRc+r5qA==", - "dev": true, "requires": { "color-convert": "^1.9.0" } @@ -43,7 +37,6 @@ "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", - "dev": true, "requires": { "sprintf-js": "~1.0.2" } @@ -51,8 +44,7 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "bn.js": { "version": "4.11.8", @@ -63,7 +55,6 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -82,14 +73,12 @@ "builtin-modules": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/builtin-modules/-/builtin-modules-1.1.1.tgz", - "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=", - "dev": true + "integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=" }, "chalk": { "version": "2.4.2", "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", - "dev": true, "requires": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", @@ -100,7 +89,6 @@ "version": "1.9.3", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-1.9.3.tgz", "integrity": "sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==", - "dev": true, "requires": { "color-name": "1.1.3" } @@ -108,20 +96,17 @@ "color-name": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.3.tgz", - "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=", - "dev": true + "integrity": "sha1-p9BVi9icQveV3UIyj3QIMcpTvCU=" }, "commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", - "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", - "dev": true + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==" }, "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "decode-uri-component": { "version": "0.2.0", @@ -148,7 +133,6 @@ "version": "1.2.0", "resolved": "https://registry.npmjs.org/definitelytyped-header-parser/-/definitelytyped-header-parser-1.2.0.tgz", "integrity": "sha512-xpg8uu/2YD/reaVsZV4oJ4g7UDYFqQGWvT1W9Tsj6q4VtWBSaig38Qgah0ZMnQGF9kAsAim08EXDO1nSi0+Nog==", - "dev": true, "requires": { "@types/parsimmon": "^1.3.0", "parsimmon": "^1.2.0" @@ -157,8 +141,7 @@ "diff": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.1.tgz", - "integrity": "sha512-s2+XdvhPCOF01LRQBC8hf4vhbVmI2CGS5aZnxLJlT5FtdhPCDFq80q++zK2KlrVorVDdL5BOGZ/VfLrVtYNF+Q==", - "dev": true + "integrity": "sha512-s2+XdvhPCOF01LRQBC8hf4vhbVmI2CGS5aZnxLJlT5FtdhPCDFq80q++zK2KlrVorVDdL5BOGZ/VfLrVtYNF+Q==" }, "dom-walk": { "version": "0.1.1", @@ -169,7 +152,6 @@ "version": "0.4.2", "resolved": "https://registry.npmjs.org/dtslint/-/dtslint-0.4.2.tgz", "integrity": "sha512-ph4GXLw3HYzlQMJOFcpCqWHuL3MxJ/344OR7wn0wlQGchQGTIVNsSUl8iKEMatpy2geNMysgA9fQa6xVhHOkTQ==", - "dev": true, "requires": { "definitelytyped-header-parser": "github:Microsoft/definitelytyped-header-parser#production", "fs-extra": "^6.0.1", @@ -181,7 +163,6 @@ "definitelytyped-header-parser": { "version": "github:Microsoft/definitelytyped-header-parser#d957ad0bb2f4ecb60ac04f734e0b38fbc8e70b8a", "from": "github:Microsoft/definitelytyped-header-parser#production", - "dev": true, "requires": { "@types/parsimmon": "^1.3.0", "parsimmon": "^1.2.0" @@ -229,20 +210,17 @@ "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", - "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=", - "dev": true + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" }, "esprima": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", - "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", - "dev": true + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==" }, "esutils": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", - "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==", - "dev": true + "integrity": "sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==" }, "eth-lib": { "version": "0.2.7", @@ -290,7 +268,6 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-6.0.1.tgz", "integrity": "sha512-GnyIkKhhzXZUWFCaJzvyDLEEgDkPfb4/TPvJCJVuS8MWZgoSsErf++QpiAlDnKFcqhRlm+tIOcencCjyJE6ZCA==", - "dev": true, "requires": { "graceful-fs": "^4.1.2", "jsonfile": "^4.0.0", @@ -300,8 +277,7 @@ "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", - "dev": true + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=" }, "function-bind": { "version": "1.1.1", @@ -312,7 +288,6 @@ "version": "7.1.4", "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.4.tgz", "integrity": "sha512-hkLPepehmnKk41pUGm3sYxoFs/umurYfYJCerbXEyFIWcAzvpipAgVkBqqT9RBKMGjnq6kMuyYwha6csxbiM1A==", - "dev": true, "requires": { "fs.realpath": "^1.0.0", "inflight": "^1.0.4", @@ -334,8 +309,7 @@ "graceful-fs": { "version": "4.2.2", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.2.tgz", - "integrity": "sha512-IItsdsea19BoLC7ELy13q1iJFNmd7ofZH5+X/pJr90/nRoPEX0DJo1dHDbgtYWOhJhcCgMDTOw84RZ72q6lB+Q==", - "dev": true + "integrity": "sha512-IItsdsea19BoLC7ELy13q1iJFNmd7ofZH5+X/pJr90/nRoPEX0DJo1dHDbgtYWOhJhcCgMDTOw84RZ72q6lB+Q==" }, "has": { "version": "1.0.3", @@ -348,8 +322,7 @@ "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", - "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", - "dev": true + "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=" }, "has-symbols": { "version": "1.0.0", @@ -379,7 +352,6 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -434,14 +406,12 @@ "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", - "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", - "dev": true + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" }, "js-yaml": { "version": "3.13.1", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", - "dev": true, "requires": { "argparse": "^1.0.7", "esprima": "^4.0.0" @@ -451,7 +421,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-4.0.0.tgz", "integrity": "sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=", - "dev": true, "requires": { "graceful-fs": "^4.1.6" } @@ -483,7 +452,6 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -491,14 +459,12 @@ "minimist": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", - "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=", - "dev": true + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" }, "mkdirp": { "version": "0.5.1", "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", - "dev": true, "requires": { "minimist": "0.0.8" } @@ -549,20 +515,17 @@ "parsimmon": { "version": "1.13.0", "resolved": "https://registry.npmjs.org/parsimmon/-/parsimmon-1.13.0.tgz", - "integrity": "sha512-5UIrOCW+gjbILkjKPgTgmq8LKf8TT3Iy7kN2VD7OtQ81facKn8B4gG1X94jWqXYZsxG2KbJhrv/Yq/5H6BQn7A==", - "dev": true + "integrity": "sha512-5UIrOCW+gjbILkjKPgTgmq8LKf8TT3Iy7kN2VD7OtQ81facKn8B4gG1X94jWqXYZsxG2KbJhrv/Yq/5H6BQn7A==" }, "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, "path-parse": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", - "dev": true + "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==" }, "process": { "version": "0.5.2", @@ -591,7 +554,6 @@ "version": "1.12.0", "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.12.0.tgz", "integrity": "sha512-B/dOmuoAik5bKcD6s6nXDCjzUKnaDvdkRyAk6rsmsKLipWj4797iothd7jmmUhWTfinVMU+wc56rYKsit2Qy4w==", - "dev": true, "requires": { "path-parse": "^1.0.6" } @@ -604,8 +566,7 @@ "semver": { "version": "5.7.1", "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.1.tgz", - "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==", - "dev": true + "integrity": "sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==" }, "simple-concat": { "version": "1.0.0", @@ -625,8 +586,7 @@ "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", - "dev": true + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" }, "strict-uri-encode": { "version": "1.1.0", @@ -654,14 +614,12 @@ "strip-json-comments": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-2.0.1.tgz", - "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=", - "dev": true + "integrity": "sha1-PFMZQukIwml8DsNEhYwobHygpgo=" }, "supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", "integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==", - "dev": true, "requires": { "has-flag": "^3.0.0" } @@ -674,14 +632,12 @@ "tslib": { "version": "1.10.0", "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.10.0.tgz", - "integrity": "sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==", - "dev": true + "integrity": "sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==" }, "tslint": { "version": "5.20.0", "resolved": "https://registry.npmjs.org/tslint/-/tslint-5.20.0.tgz", "integrity": "sha512-2vqIvkMHbnx8acMogAERQ/IuINOq6DFqgF8/VDvhEkBqQh/x6SP0Y+OHnKth9/ZcHQSroOZwUQSN18v8KKF0/g==", - "dev": true, "requires": { "@babel/code-frame": "^7.0.0", "builtin-modules": "^1.1.1", @@ -702,7 +658,6 @@ "version": "2.29.0", "resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz", "integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==", - "dev": true, "requires": { "tslib": "^1.8.1" } @@ -710,8 +665,7 @@ "typescript": { "version": "3.7.0-dev.20191015", "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.0-dev.20191015.tgz", - "integrity": "sha512-Cpfj1n4pEUVKL+jtS0mkZodJffyMmf3Wk/UjyZMGX4fsjK5KBPJf3NUlyXij8I8p1E2CAomdS5NPFrAR+z8pKw==", - "dev": true + "integrity": "sha512-Cpfj1n4pEUVKL+jtS0mkZodJffyMmf3Wk/UjyZMGX4fsjK5KBPJf3NUlyXij8I8p1E2CAomdS5NPFrAR+z8pKw==" }, "underscore": { "version": "1.9.1", @@ -721,8 +675,7 @@ "universalify": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/universalify/-/universalify-0.1.2.tgz", - "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==", - "dev": true + "integrity": "sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==" }, "url-set-query": { "version": "1.0.0", diff --git a/packages/web3/angular-patch.js b/packages/web3/angular-patch.js index 3a76568e4cd..5caeb5cca74 100644 --- a/packages/web3/angular-patch.js +++ b/packages/web3/angular-patch.js @@ -1,5 +1,5 @@ -const fs = require('fs'); -const f = '../../node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js'; +var fs = require('fs'); +var f = '../../node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js'; // This is because we have to replace the `node:false` in the `/angular-cli-files/models/webpack-configs/browser.js` // with `node: {crypto: true, stream: true}` to allow web3 to work with angular (as they enforce node: false.) diff --git a/test/contract.js b/test/contract.js index d6b5f21dc66..18d0a9c6c77 100644 --- a/test/contract.js +++ b/test/contract.js @@ -3031,6 +3031,8 @@ describe('typical usage', function() { gasPrice: '0xbb8', chainId: '0x1', nonce: '0x1', + chain: 'mainnet', + hardfork: 'petersburg' }).then(function (tx) { const expected = tx.rawTransaction; assert.equal(payload.method, 'eth_sendRawTransaction'); @@ -3090,6 +3092,8 @@ describe('typical usage', function() { gasPrice: 3000, chainId: 1, nonce: 1, + chain: 'mainnet', + hardfork: 'petersburg' }) .on('transactionHash', function (value) { assert.equal('0x5550000000000000000000000000000000000000000000000000000000000032', value); diff --git a/test/e2e.method.signing.js b/test/e2e.method.signing.js index b37d2b25ec2..c3d6d9d2ef4 100644 --- a/test/e2e.method.signing.js +++ b/test/e2e.method.signing.js @@ -1,12 +1,10 @@ let assert = require('assert'); -let EJSCommon = require('ethereumjs-common'); let Web3 = require('../packages/web3'); describe('transaction and message signing [ @E2E ]', function() { let web3; let accounts; let wallet; - let Common = EJSCommon.default; before(async function(){ web3 = new Web3('http://localhost:8545'); @@ -46,7 +44,7 @@ describe('transaction and message signing [ @E2E ]', function() { assert(receipt.status === true); }); - it('sendSignedTransaction (with eth.accounts.signTransaction)', async function(){ + it('sendSignedTransaction (accounts.signTransaction with signing options)', async function(){ const source = wallet[0].address; const destination = wallet[1].address; @@ -55,15 +53,15 @@ describe('transaction and message signing [ @E2E ]', function() { const chainId = await web3.eth.getChainId(); - const customCommon = Common.forCustomChain( - 'mainnet', - { - name: 'my-network', + const customCommon = { + baseChain: 'mainnet', + customChain: { + name: 'custom-network', networkId: networkId, chainId: chainId, }, - 'petersburg', - ); + harfork: 'petersburg', + }; const txObject = { nonce: web3.utils.toHex(txCount), @@ -80,6 +78,97 @@ describe('transaction and message signing [ @E2E ]', function() { assert(receipt.status === true); }); + it('sendSignedTransaction (accounts.signTransaction / without signing options)', async function(){ + const source = wallet[0].address; + const destination = wallet[1].address; + + const txCount = await web3.eth.getTransactionCount(source); + + const txObject = { + nonce: web3.utils.toHex(txCount), + to: destination, + value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')), + gasLimit: web3.utils.toHex(21000), + gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), + }; + + const signed = await web3.eth.accounts.signTransaction(txObject, wallet[0].privateKey); + const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction); + + assert(receipt.status === true); + }); + + it('accounts.signTransaction errors when common, chain and hardfork all defined', async function(){ + const source = wallet[0].address; + const destination = wallet[1].address; + + const txCount = await web3.eth.getTransactionCount(source); + + const txObject = { + nonce: web3.utils.toHex(txCount), + to: destination, + value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')), + gasLimit: web3.utils.toHex(21000), + gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), + chain: "ropsten", + common: {}, + hardfork: "istanbul" + }; + + try { + await web3.eth.accounts.signTransaction(txObject, wallet[0].privateKey); + assert.fail() + } catch (err) { + assert(err.message.includes('common object or the chain and hardfork')); + } + }); + + it('accounts.signTransaction errors when chain specified without hardfork', async function(){ + const source = wallet[0].address; + const destination = wallet[1].address; + + const txCount = await web3.eth.getTransactionCount(source); + + const txObject = { + nonce: web3.utils.toHex(txCount), + to: destination, + value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')), + gasLimit: web3.utils.toHex(21000), + gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), + chain: "ropsten" + }; + + try { + await web3.eth.accounts.signTransaction(txObject, wallet[0].privateKey); + assert.fail() + } catch (err) { + assert(err.message.includes('both values must be defined')); + } + }); + + it('accounts.signTransaction errors when hardfork specified without chain', async function(){ + const source = wallet[0].address; + const destination = wallet[1].address; + + const txCount = await web3.eth.getTransactionCount(source); + + const txObject = { + nonce: web3.utils.toHex(txCount), + to: destination, + value: web3.utils.toHex(web3.utils.toWei('0.1', 'ether')), + gasLimit: web3.utils.toHex(21000), + gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')), + hardfork: "istanbul" + }; + + try { + await web3.eth.accounts.signTransaction(txObject, wallet[0].privateKey); + assert.fail() + } catch (err) { + assert(err.message.includes('both values must be defined')); + } + }); + it('eth.personal.sign', async function(){ // ganache does not support eth_sign if (process.env.GANACHE) return; diff --git a/test/eth.accounts.signTransaction.js b/test/eth.accounts.signTransaction.js index 39cd5028b50..c9ab4776e86 100644 --- a/test/eth.accounts.signTransaction.js +++ b/test/eth.accounts.signTransaction.js @@ -3,9 +3,20 @@ var Web3 = require('../packages/web3'); var Accounts = require("./../packages/web3-eth-accounts"); var ethjsSigner = require("ethjs-signer"); var chai = require('chai'); +var _ = require('underscore'); var assert = chai.assert; -var clone = function (object) { return object ? JSON.parse(JSON.stringify(object)) : []; }; +var common = { + baseChain: 'mainnet', + customChain: { + name: 'custom-network', + networkId: 1, + chainId: 1, + }, + harfork: 'petersburg', +}; + +var clone = function (object) { return object ? _.clone(object) : []; }; var tests = [ { @@ -20,7 +31,8 @@ var tests = [ to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', toIban: 'XE04S1IRT2PR8A8422TPBL9SR6U0HODDCUT', // will be switched to "to" in the test value: "1000000000", - data: "" + data: "", + common: common }, // signature from eth_signTransaction rawTransaction: "0xf868808504a817c80082520894f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008026a0afa02d193471bb974081585daabf8a751d4decbb519604ac7df612cc11e9226da04bf1bd55e82cebb2b09ed39bbffe35107ea611fa212c2d9a1f1ada4952077118", @@ -40,7 +52,8 @@ var tests = [ to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', toIban: 'XE04S1IRT2PR8A8422TPBL9SR6U0HODDCUT', // will be switched to "to" in the test value: "0", - data: "" + data: "", + common: common }, // expected r and s values from signature r: "0x22f17b38af35286ffbb0c6376c86ec91c20ecbad93f84913a0cc15e7580cd9", @@ -63,7 +76,8 @@ var tests = [ to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', toIban: 'XE04S1IRT2PR8A8422TPBL9SR6U0HODDCUT', // will be switched to "to" in the test value: "1000000000", - data: "" + data: "", + common: common }, // expected r and s values from signature r: "0x9ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9c", @@ -86,7 +100,8 @@ var tests = [ to: '0xF0109fC8DF283027b6285cc889F5aA624EaC1F55', toIban: 'XE04S1IRT2PR8A8422TPBL9SR6U0HODDCUT', // will be switched to "to" in the test value: "0", - data: "" + data: "", + common: common }, // expected r and s values from signature r: "0x22f17b38af35286ffbb0c6376c86ec91c20ecbad93f84913a0cc15e7580cd9", @@ -109,7 +124,8 @@ var tests = [ to: '0x3535353535353535353535353535353535353535', toIban: 'XE4967QZMA14MI680T89KSPPJEJMU68MEYD', // will be switched to "to" in the test value: "1000000000000000000", - data: "" + data: "", + common: common }, // signature from eth_signTransaction rawTransaction: "0xf86c808504a817c800825208943535353535353535353535353535353535353535880de0b6b3a76400008025a04f4c17305743700648bc4f6cd3038ec6f6af0df73e31757007b7f59df7bee88da07e1941b264348e80c78c4027afc65a87b0a5e43e86742b8ca0823584c6788fd0", @@ -128,7 +144,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, // web3.eth.signTransaction({from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0", gasPrice: "230000000000", gas: "50000", to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', value: "1000000000000000000", data: "0x0123abcd"}).then(console.log); // signature from eth_signTransaction @@ -148,7 +165,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, // web3.eth.signTransaction({from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0", gasPrice: "230000000000", gas: "50000", to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', value: "1000000000000000000", data: "0x0123abcd"}).then(console.log); // signature from eth_signTransaction @@ -168,7 +186,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, // web3.eth.signTransaction({from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0", gasPrice: "230000000000", gas: "50000", to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', value: "1000000000000000000", data: "0x0123abcd"}).then(console.log); // signature from eth_signTransaction @@ -188,7 +207,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, // web3.eth.signTransaction({from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0", gasPrice: "230000000000", gas: "50000", to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', value: "1000000000000000000", data: "0x0123abcd"}).then(console.log); // signature from eth_signTransaction @@ -208,7 +228,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, // web3.eth.signTransaction({from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0", gasPrice: "230000000000", gas: "50000", to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', value: "1000000000000000000", data: "0x0123abcd"}).then(console.log); // signature from eth_signTransaction @@ -228,7 +249,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, // web3.eth.signTransaction({from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0", gasPrice: "230000000000", gas: "50000", to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', value: "1000000000000000000", data: "0x0123abcd"}).then(console.log); // signature from eth_signTransaction @@ -248,7 +270,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - input: "0x0123abcd" + input: "0x0123abcd", + common: common }, // web3.eth.signTransaction({from: "0xEB014f8c8B418Db6b45774c326A0E64C78914dC0", gasPrice: "230000000000", gas: "50000", to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', value: "1000000000000000000", data: "0x0123abcd"}).then(console.log); // signature from eth_signTransaction @@ -269,7 +292,8 @@ var tests = [ toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", data: "0x0123abcd", - input: "0x0123abcd" + input: "0x0123abcd", + common: common }, error: true }, @@ -285,7 +309,8 @@ var tests = [ toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", data: "0x0123abcd", - input: "0x0123abcd" + input: "0x0123abcd", + common: common }, error: true }, @@ -300,7 +325,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, error: true }, @@ -315,7 +341,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "test" + data: "test", + common: common }, error: true }, @@ -330,7 +357,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, error: true }, @@ -345,7 +373,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, error: true }, @@ -360,7 +389,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, error: true }, @@ -375,7 +405,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, error: true }, @@ -390,7 +421,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, error: true }, @@ -405,7 +437,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, error: true }, @@ -420,7 +453,8 @@ var tests = [ to: '0xFCAd0B19bB29D4674531d6f115237E16AfCE377c', toIban: 'XE63TIJX31ZHSLZ6F601ZPKVDKKYHMIK03G', // will be switched to "to" in the test value: "1000000000000000000", - data: "0x0123abcd" + data: "0x0123abcd", + common: common }, error: true }, @@ -557,7 +591,34 @@ describe("eth", function () { }); }); - it("signTransaction will call for nonce, gasPrice and chainId", function(done) { + it("signTransaction will call for networkId", function(done) { + var provider = new FakeHttpProvider(); + var web3 = new Web3(provider); + + provider.injectResult(1); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, 'eth_networkId'); + assert.deepEqual(payload.params, []); + }); + + var ethAccounts = new Accounts(web3); + + var testAccount = ethAccounts.privateKeyToAccount(test.privateKey); + assert.equal(testAccount.address, test.address); + + var transaction = clone(test.transaction); + delete transaction.common; + testAccount.signTransaction(transaction) + .then(function (tx) { + assert.isObject(tx); + assert.isString(tx.rawTransaction); + + done(); + }); + }); + + it("signTransaction will call for nonce, gasPrice, chainId and networkId", function(done) { var provider = new FakeHttpProvider(); var web3 = new Web3(provider); @@ -579,6 +640,12 @@ describe("eth", function () { assert.equal(payload.method, 'eth_getTransactionCount'); assert.deepEqual(payload.params, [test.address, "latest"]); }); + provider.injectResult(1); + provider.injectValidation(function (payload) { + assert.equal(payload.jsonrpc, '2.0'); + assert.equal(payload.method, 'eth_networkId'); + assert.deepEqual(payload.params, []); + }); var ethAccounts = new Accounts(web3); @@ -589,6 +656,7 @@ describe("eth", function () { delete transaction.chainId; delete transaction.gasPrice; delete transaction.nonce; + delete transaction.common; testAccount.signTransaction(transaction) .then(function (tx) { assert.isObject(tx); diff --git a/test/eth.sendTransaction.js b/test/eth.sendTransaction.js index ac5dc56537c..4a77bd8e791 100644 --- a/test/eth.sendTransaction.js +++ b/test/eth.sendTransaction.js @@ -129,7 +129,9 @@ var tests = [{ to: '0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', value: '1234567654321', gasPrice: '324234234234', - gas: 500000 + gas: 500000, + chain: 'mainnet', + hardfork: 'petersburg' }], formattedArgs: ['0xf86b0a854b7dddc97a8307a12094dbdbdb2cbd23b783741e8d7fcf51e459b497e4a686011f71f76bb18026a0ce66ccabda889012314677073ded7bec9f763e564dfcff1135e7c6a3c5b89353a07bfa06fe1ba3f1804e4677295a5147e6c8b2224647cc2b7b62063081f6490bd3'], result: '0x12345678976543213456786543212345675432', @@ -154,7 +156,9 @@ var tests = [{ to: '0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', value: '1234567654321', gasPrice: '324234234234', - gas: 500000 + gas: 500000, + chain: 'mainnet', + hardfork: 'petersburg' }], formattedArgs: ['0xf86b0a854b7dddc97a8307a12094dbdbdb2cbd23b783741e8d7fcf51e459b497e4a686011f71f76bb18026a0fe620c94cc14fdcdef494a40caf9e2860d1a5929d95730e1b7a6a2041c9c507fa01d3d22e7ab1010fa95a357322ad14a8ce1b1b631d3bb9c123922ff8042c8fc8b'], result: '0x12345678976543213456786543212345675432', @@ -182,7 +186,9 @@ var tests = [{ to: '0xdbdbdb2cbd23b783741e8d7fcf51e459b497e4a6', value: '1234567654321', gasPrice: '324234234234', - gas: 500000 + gas: 500000, + chain: 'mainnet', + hardfork: 'petersburg' }], formattedArgs: ['0xf86b0a854b7dddc97a8307a12094dbdbdb2cbd23b783741e8d7fcf51e459b497e4a686011f71f76bb18026a016a5bc4e1808e60a5d370f6b335be158673bd95c457ee7925dc8ae1bec69647fa03831c5e0a966a0aad0c67d6ddea55288f76ae1d73dfe11c6174a8682c2ec165d'], result: '0x12345678976543213456786543212345675432', From df540fb8eaa2f9db9c1906b623100c776414bbf4 Mon Sep 17 00:00:00 2001 From: nivida Date: Mon, 21 Oct 2019 15:41:49 +0200 Subject: [PATCH 09/12] documentation updated for new default properties --- docs/web3-eth-accounts.rst | 12 ++- docs/web3-eth-contract.rst | 133 +++++++++++++++++++++++++++++++ docs/web3-eth.rst | 155 ++++++++++++++++++++++++++++++++++--- 3 files changed, 287 insertions(+), 13 deletions(-) diff --git a/docs/web3-eth-accounts.rst b/docs/web3-eth-accounts.rst index 9d9dd03589e..d5ae49b7fd8 100644 --- a/docs/web3-eth-accounts.rst +++ b/docs/web3-eth-accounts.rst @@ -143,9 +143,6 @@ Parameters ---------- 1. ``tx`` - ``Object``: The transaction object as follows: - - ``chain`` - ``String``: (optional) Defaults to ``mainnet``. - - ``hardfork`` - ``String``: (optional) Defaults to ``petersburg``. - - ``common`` - ``String``: (optional) The initiated common object of the `ethereumjs-common `_ package. `(Example) `_ - ``nonce`` - ``String``: (optional) The nonce to use when signing this transaction. Default will use :ref:`web3.eth.getTransactionCount() `. - ``chainId`` - ``String``: (optional) The chain id to use when signing this transaction. Default will use :ref:`web3.eth.net.getId() `. - ``to`` - ``String``: (optional) The recevier of the transaction, can be empty when deploying a contract. @@ -153,6 +150,15 @@ Parameters - ``value`` - ``String``: (optional) The value of the transaction in wei. - ``gasPrice`` - ``String``: (optional) The gas price set by this transaction, if empty, it will use :ref:`web3.eth.gasPrice() ` - ``gas`` - ``String``: The gas provided by the transaction. + - ``chain`` - ``String``: (optional) Defaults to ``mainnet``. + - ``hardfork`` - ``String``: (optional) Defaults to ``petersburg``. + - ``common`` - ``Object``: (optional) The common object + - ``customChain`` - ``Object``: The custom chain properties + - ``name`` - ``string``: (optional) The name of the chain + - ``networkId`` - ``number``: Network ID of the custom chain + - ``chainId`` - ``number``: Chain ID of the custom chain + - ``baseChain`` - ``string``: (optional) ``mainnet``, ``goerli``, ``kovan``, ``rinkeby``, or ``ropsten`` + - ``hardfork`` - ``string``: (optional) ``chainstart``, ``homestead``, ``dao``, ``tangerineWhistle``, ``spuriousDragon``, ``byzantium``, ``constantinople``, ``petersburg``, or ``istanbul`` 2. ``privateKey`` - ``String``: The private key to sign with. 3. ``callback`` - ``Function``: (optional) Optional callback, returns an error object as first parameter and the result as second. diff --git a/docs/web3-eth-contract.rst b/docs/web3-eth-contract.rst index 7509b800257..8ba9077f044 100644 --- a/docs/web3-eth-contract.rst +++ b/docs/web3-eth-contract.rst @@ -161,6 +161,139 @@ Example contract.defaultBlock = 231; + +------------------------------------------------------------------------------ + +.. _eth-contract-defaulthardfork: + +defaultHardfork +===================== + +.. code-block:: javascript + + contract.defaultHardfork + +The default hardfork property is used for signing transactions locally. + +---------- +Property +---------- + + +Default hardfork property can be one of the following: + +- ``"chainstart"`` - ``String`` +- ``"homestead"`` - ``String`` +- ``"dao"`` - ``String`` +- ``"tangerineWhistle"`` - ``String`` +- ``"spuriousDragon"`` - ``String`` +- ``"byzantium"`` - ``String`` +- ``"constantinople"`` - ``String`` +- ``"petersburg"`` - ``String`` +- ``"istanbul"`` - ``String`` + +Default is ``"petersburg"`` + + +------- +Example +------- + +.. code-block:: javascript + + contract.defaultHardfork; + > "petersburg" + + // set the default block + contract.defaultHardfork = 'istanbul'; + + +------------------------------------------------------------------------------ + +.. _eth-contract-defaultchain: + +defaultChain +===================== + +.. code-block:: javascript + + contract.defaultChain + +The default chain property is used for signing transactions locally. + +---------- +Property +---------- + + +Default chain property can be one of the following: + +- ``"mainnet"`` - ``String`` +- ``"goerli"`` - ``String`` +- ``"kovan"`` - ``String`` +- ``"rinkeby"`` - ``String`` +- ``"ropsten"`` - ``String`` + +Default is ``"mainnet"`` + + +------- +Example +------- + +.. code-block:: javascript + + contract.defaultChain; + > "mainnet" + + // set the default chain + contract.defaultChain = 'goerli'; + + +------------------------------------------------------------------------------ + +.. _eth-contract-defaultcommon: + +defaultCommon +===================== + +.. code-block:: javascript + + contract.defaultCommon + +The default common property is used for signing transactions locally. + +---------- +Property +---------- + + +Default chain property does contain the following ``Common`` object: + +- ``customChain`` - ``Object``: The custom chain properties + - ``name`` - ``string``: (optional) The name of the chain + - ``networkId`` - ``number``: Network ID of the custom chain + - ``chainId`` - ``number``: Chain ID of the custom chain +- ``baseChain`` - ``string``: (optional) ``mainnet``, ``goerli``, ``kovan``, ``rinkeby``, or ``ropsten`` +- ``hardfork`` - ``string``: (optional) ``chainstart``, ``homestead``, ``dao``, ``tangerineWhistle``, ``spuriousDragon``, ``byzantium``, ``constantinople``, ``petersburg``, or ``istanbul`` + + +Default is ``undefined``. + + +------- +Example +------- + +.. code-block:: javascript + + contract.defaultCommon; + > {customChain: {name: 'custom-network', chainId: 1, networkId: 1}, baseChain: 'mainnet', hardfork: 'petersburg'} + + // set the default common + contract.defaultCommon = {customChain: {name: 'custom-network', chainId: 1, networkId: 1}, baseChain: 'mainnet', hardfork: 'petersburg'}; + + ------------------------------------------------------------------------------ .. _eth-contract-transactionblocktimeout: diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 9a7edf01108..700585cbd16 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -208,6 +208,135 @@ Example web3.eth.defaultBlock = 231; +------------------------------------------------------------------------------ +.. _eth-defaulthardfork: + +defaultHardfork +===================== + +.. code-block:: javascript + + web3.eth.defaultHardfork + +The default hardfork property is used for signing transactions locally. + +---------- +Property +---------- + + +Default hardfork property can be one of the following: + +- ``"chainstart"`` - ``String`` +- ``"homestead"`` - ``String`` +- ``"dao"`` - ``String`` +- ``"tangerineWhistle"`` - ``String`` +- ``"spuriousDragon"`` - ``String`` +- ``"byzantium"`` - ``String`` +- ``"constantinople"`` - ``String`` +- ``"petersburg"`` - ``String`` +- ``"istanbul"`` - ``String`` + +Default is ``"petersburg"`` + + +------- +Example +------- + +.. code-block:: javascript + + web3.eth.defaultHardfork; + > "petersburg" + + // set the default block + web3.eth.defaultHardfork = 'istanbul'; + + +------------------------------------------------------------------------------ +.. _eth-defaultchain: + +defaultChain +===================== + +.. code-block:: javascript + + web3.eth.defaultChain + +The default chain property is used for signing transactions locally. + +---------- +Property +---------- + + +Default chain property can be one of the following: + +- ``"mainnet"`` - ``String`` +- ``"goerli"`` - ``String`` +- ``"kovan"`` - ``String`` +- ``"rinkeby"`` - ``String`` +- ``"ropsten"`` - ``String`` + +Default is ``"mainnet"`` + + +------- +Example +------- + +.. code-block:: javascript + + web3.eth.defaultChain; + > "mainnet" + + // set the default chain + web3.eth.defaultChain = 'goerli'; + + +------------------------------------------------------------------------------ +.. _eth-defaultcommon: + +defaultCommon +===================== + +.. code-block:: javascript + + web3.eth.defaultCommon + +The default common property is used for signing transactions locally. + +---------- +Property +---------- + + +Default chain property does contain the following ``Common`` object: + +- ``customChain`` - ``Object``: The custom chain properties + - ``name`` - ``string``: (optional) The name of the chain + - ``networkId`` - ``number``: Network ID of the custom chain + - ``chainId`` - ``number``: Chain ID of the custom chain +- ``baseChain`` - ``string``: (optional) ``mainnet``, ``goerli``, ``kovan``, ``rinkeby``, or ``ropsten`` +- ``hardfork`` - ``string``: (optional) ``chainstart``, ``homestead``, ``dao``, ``tangerineWhistle``, ``spuriousDragon``, ``byzantium``, ``constantinople``, ``petersburg``, or ``istanbul`` + + +Default is ``undefined``. + + +------- +Example +------- + +.. code-block:: javascript + + web3.eth.defaultCommon; + > {customChain: {name: 'custom-network', chainId: 1, networkId: 1}, baseChain: 'mainnet', hardfork: 'petersburg'} + + // set the default common + web3.eth.defaultCommon = {customChain: {name: 'custom-network', chainId: 1, networkId: 1}, baseChain: 'mainnet', hardfork: 'petersburg'}; + + ------------------------------------------------------------------------------ .. _web3-module-transactionblocktimeout: @@ -1049,16 +1178,22 @@ Parameters 1. ``Object`` - The transaction object to send: - - ``from`` - ``String|Number``: The address for the sending account. Uses the :ref:`web3.eth.defaultAccount ` property, if not specified. Or an address or index of a local wallet in :ref:`web3.eth.accounts.wallet `. - - ``to`` - ``String``: (optional) The destination address of the message, left undefined for a contract-creation transaction. - - ``value`` - ``Number|String|BN|BigNumber``: (optional) The value transferred for the transaction in :ref:`wei `, also the endowment if it's a contract-creation transaction. - - ``gas`` - ``Number``: (optional, default: To-Be-Determined) The amount of gas to use for the transaction (unused gas is refunded). - - ``gasPrice`` - ``Number|String|BN|BigNumber``: (optional) The price of gas for this transaction in :ref:`wei `, defaults to :ref:`web3.eth.gasPrice `. - - ``data`` - ``String``: (optional) Either a `ABI byte string `_ containing the data of the function call on a contract, or in the case of a contract-creation transaction the initialisation code. - - ``nonce`` - ``Number``: (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce. - - ``chain`` - ``String``: (optional) Defaults to ``mainnet``. - - ``hardfork`` - ``String``: (optional) Defaults to ``petersburg``. - - ``common`` - ``String``: (optional) The initiated common object of the `ethereumjs-common `_ package. `(Example) `_ + - ``from`` - ``String|Number``: The address for the sending account. Uses the :ref:`web3.eth.defaultAccount ` property, if not specified. Or an address or index of a local wallet in :ref:`web3.eth.accounts.wallet `. + - ``to`` - ``String``: (optional) The destination address of the message, left undefined for a contract-creation transaction. + - ``value`` - ``Number|String|BN|BigNumber``: (optional) The value transferred for the transaction in :ref:`wei `, also the endowment if it's a contract-creation transaction. + - ``gas`` - ``Number``: (optional, default: To-Be-Determined) The amount of gas to use for the transaction (unused gas is refunded). + - ``gasPrice`` - ``Number|String|BN|BigNumber``: (optional) The price of gas for this transaction in :ref:`wei `, defaults to :ref:`web3.eth.gasPrice `. + - ``data`` - ``String``: (optional) Either a `ABI byte string `_ containing the data of the function call on a contract, or in the case of a contract-creation transaction the initialisation code. + - ``nonce`` - ``Number``: (optional) Integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce. + - ``chain`` - ``String``: (optional) Defaults to ``mainnet``. + - ``hardfork`` - ``String``: (optional) Defaults to ``petersburg``. + - ``common`` - ``Object``: (optional) The common object + - ``customChain`` - ``Object``: The custom chain properties + - ``name`` - ``string``: (optional) The name of the chain + - ``networkId`` - ``number``: Network ID of the custom chain + - ``chainId`` - ``number``: Chain ID of the custom chain + - ``baseChain`` - ``string``: (optional) ``mainnet``, ``goerli``, ``kovan``, ``rinkeby``, or ``ropsten`` + - ``hardfork`` - ``string``: (optional) ``chainstart``, ``homestead``, ``dao``, ``tangerineWhistle``, ``spuriousDragon``, ``byzantium``, ``constantinople``, ``petersburg``, or ``istanbul`` 2. ``callback`` - ``Function``: (optional) Optional callback, returns an error object as first parameter and the result as second. From fbb260dcf25496a46deba0699d34ff29910a2cde Mon Sep 17 00:00:00 2001 From: nivida Date: Mon, 21 Oct 2019 16:02:52 +0200 Subject: [PATCH 10/12] typo fixed in docs and conditions for the new default properties updated in the Method class of the web3-core-method module --- docs/web3-eth-contract.rst | 8 ++++---- docs/web3-eth.rst | 6 +++--- packages/web3-core-method/src/index.js | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/web3-eth-contract.rst b/docs/web3-eth-contract.rst index 8ba9077f044..a55d5a46e2b 100644 --- a/docs/web3-eth-contract.rst +++ b/docs/web3-eth-contract.rst @@ -138,7 +138,7 @@ Property ---------- -Default block parameters can be one of the following: +The default block parameters can be one of the following: - ``Number``: A block number - ``"genesis"`` - ``String``: The genesis block @@ -180,7 +180,7 @@ Property ---------- -Default hardfork property can be one of the following: +The default hardfork property can be one of the following: - ``"chainstart"`` - ``String`` - ``"homestead"`` - ``String`` @@ -226,7 +226,7 @@ Property ---------- -Default chain property can be one of the following: +The default chain property can be one of the following: - ``"mainnet"`` - ``String`` - ``"goerli"`` - ``String`` @@ -268,7 +268,7 @@ Property ---------- -Default chain property does contain the following ``Common`` object: +The default common property does contain the following ``Common`` object: - ``customChain`` - ``Object``: The custom chain properties - ``name`` - ``string``: (optional) The name of the chain diff --git a/docs/web3-eth.rst b/docs/web3-eth.rst index 700585cbd16..65dbfd434bf 100644 --- a/docs/web3-eth.rst +++ b/docs/web3-eth.rst @@ -225,7 +225,7 @@ Property ---------- -Default hardfork property can be one of the following: +The default hardfork property can be one of the following: - ``"chainstart"`` - ``String`` - ``"homestead"`` - ``String`` @@ -270,7 +270,7 @@ Property ---------- -Default chain property can be one of the following: +The default chain property can be one of the following: - ``"mainnet"`` - ``String`` - ``"goerli"`` - ``String`` @@ -311,7 +311,7 @@ Property ---------- -Default chain property does contain the following ``Common`` object: +The default common property does contain the following ``Common`` object: - ``customChain`` - ``Object``: The custom chain properties - ``name`` - ``string``: (optional) The name of the chain diff --git a/packages/web3-core-method/src/index.js b/packages/web3-core-method/src/index.js index f9a31144f30..611cd91d12b 100644 --- a/packages/web3-core-method/src/index.js +++ b/packages/web3-core-method/src/index.js @@ -602,15 +602,15 @@ Method.prototype.buildCall = function() { if (wallet && wallet.privateKey) { var txOptions = _.omit(tx, 'from'); - if (method.defaultChain) { + if (method.defaultChain && !txOptions.chain) { txOptions.chain = method.defaultChain; } - if (method.defaultHardfork) { + if (method.defaultHardfork && !txOptions.hardfork) { txOptions.hardfork = method.defaultHardfork; } - if (method.defaultCommon) { + if (method.defaultCommon && !txOptions.common) { txOptions.common = method.defaultCommon; } From 4edc7e78b2d3a8dfd48f5507ef16744e15312a2a Mon Sep 17 00:00:00 2001 From: nivida Date: Mon, 21 Oct 2019 17:01:57 +0200 Subject: [PATCH 11/12] funcDocs fixed in utils and missing option properties added in the web3-eth-contract module --- packages/web3-eth-contract/src/index.js | 3 +++ packages/web3-utils/src/utils.js | 18 +++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/web3-eth-contract/src/index.js b/packages/web3-eth-contract/src/index.js index 1c51f0bd6f0..0920b10b8b6 100644 --- a/packages/web3-eth-contract/src/index.js +++ b/packages/web3-eth-contract/src/index.js @@ -900,6 +900,9 @@ Contract.prototype._executeMethod = function _executeMethod(){ transactionBlockTimeout: _this._parent.transactionBlockTimeout, transactionConfirmationBlocks: _this._parent.transactionConfirmationBlocks, transactionPollingTimeout: _this._parent.transactionPollingTimeout, + defaultCommon: _this._parent.defaultCommon, + defaultChain: _this._parent.defaultChain, + defaultHardfork: _this._parent.defaultHardfork, extraFormatters: extraFormatters })).createFunction(); diff --git a/packages/web3-utils/src/utils.js b/packages/web3-utils/src/utils.js index 97e1554e662..a5b4df3171c 100644 --- a/packages/web3-utils/src/utils.js +++ b/packages/web3-utils/src/utils.js @@ -393,7 +393,7 @@ var isHex = function (hex) { * Returns true if given string is a valid Ethereum block header bloom. * * @method isBloom - * @param {String} hex encoded bloom filter + * @param {String} bloom encoded bloom filter * @return {Boolean} */ var isBloom = function (bloom) { @@ -405,8 +405,8 @@ var isBloom = function (bloom) { * note: false positives are possible. * * @method isUserEthereumAddressInBloom - * @param {String} hex encoded bloom filter - * @param {String} hex ethereum addresss + * @param {String} ethereumAddress encoded bloom filter + * @param {String} bloom ethereum addresss * @return {Boolean} */ var isUserEthereumAddressInBloom = function (bloom, ethereumAddress) { @@ -418,8 +418,8 @@ var isUserEthereumAddressInBloom = function (bloom, ethereumAddress) { * note: false positives are possible. * * @method isUserEthereumAddressInBloom - * @param {String} hex encoded bloom filter - * @param {String} hex contract addresss + * @param {String} bloom encoded bloom filter + * @param {String} contractAddress contract addresss * @return {Boolean} */ var isContractAddressInBloom = function (bloom, contractAddress) { @@ -430,7 +430,7 @@ var isContractAddressInBloom = function (bloom, contractAddress) { * Returns true if given string is a valid log topic. * * @method isTopic - * @param {String} hex encoded topic + * @param {String} topic encoded topic * @return {Boolean} */ var isTopic = function (topic) { @@ -442,8 +442,8 @@ var isTopic = function (topic) { * note: false positives are possible. * * @method isTopicInBloom - * @param {String} hex encoded bloom filter - * @param {String} hex encoded topic + * @param {String} bloom encoded bloom filter + * @param {String} topic encoded topic * @return {Boolean} */ var isTopicInBloom = function (bloom, topic) { @@ -455,7 +455,7 @@ var isTopicInBloom = function (bloom, topic) { * note: false positives are possible. * * @method isInBloom - * @param {String} hex encoded bloom filter + * @param {String} bloom encoded bloom filter * @param {String | Uint8Array} topic encoded value * @return {Boolean} */ From 06cd8163205662efe142ab7313695aca07899ca1 Mon Sep 17 00:00:00 2001 From: nivida Date: Mon, 21 Oct 2019 17:03:50 +0200 Subject: [PATCH 12/12] copy&paste issue fixed --- packages/web3-eth/src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web3-eth/src/index.js b/packages/web3-eth/src/index.js index 90663836e34..0d6c5414484 100644 --- a/packages/web3-eth/src/index.js +++ b/packages/web3-eth/src/index.js @@ -86,7 +86,7 @@ var Eth = function Eth() { var transactionPollingTimeout = 750; var defaultChain, defaultHardfork, defaultCommon; - Object.defineProperty(this, '≈', { + Object.defineProperty(this, 'defaultCommon', { get: function () { return defaultCommon; },