Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto fetch tx signing option values when not set by user #3143

Merged
merged 11 commits into from
Oct 21, 2019
23 changes: 9 additions & 14 deletions docs/web3-eth-accounts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion docs/web3-eth.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <eth_accounts_wallet>`. It will then sign locally using the private key of that account, and send the transaction via :ref:`web3.eth.sendSignedTransaction() <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 <eth_accounts_wallet>`. It will then sign locally using the private key of that account, and send the transaction via :ref:`web3.eth.sendSignedTransaction() <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:

Expand Down
120 changes: 3 additions & 117 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 18 additions & 1 deletion packages/web3-core-method/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
32 changes: 31 additions & 1 deletion packages/web3-core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
8 changes: 4 additions & 4 deletions packages/web3-eth-accounts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/web3-eth-accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading