Skip to content

Commit 84344ac

Browse files
committed
Check all transaction parameters are valid; protect against typos (ethers-io#299).
1 parent 9b118af commit 84344ac

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src.ts/wallet.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import { BlockTag, TransactionRequest, TransactionResponse } from './providers/a
2424

2525
import * as errors from './errors';
2626

27+
const allowedTransactionKeys: { [ key: string ]: boolean } = {
28+
chainId: true, data: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
29+
}
2730

2831
export class Wallet extends AbstractSigner {
2932

@@ -69,6 +72,15 @@ export class Wallet extends AbstractSigner {
6972
}
7073

7174
sign(transaction: TransactionRequest): Promise<string> {
75+
for (let key in transaction) {
76+
if (!allowedTransactionKeys[key]) {
77+
errors.throwError('unsupported transaction property - ' + key, errors.INVALID_ARGUMENT, {
78+
argument: 'transaction',
79+
value: transaction,
80+
key: key
81+
});
82+
}
83+
}
7284
return resolveProperties(transaction).then((tx) => {
7385
let rawTx = serializeTransaction(tx);
7486
let signature = this.signingKey.signDigest(keccak256(rawTx));
@@ -98,17 +110,12 @@ export class Wallet extends AbstractSigner {
98110
throw new Error('invalid transaction object');
99111
}
100112

101-
var tx = shallowCopy(transaction);
113+
let tx = shallowCopy(transaction);
102114

103115
if (tx.to != null) {
104116
tx.to = this.provider.resolveName(tx.to);
105117
}
106118

107-
if (tx.gasLimit == null) {
108-
tx.from = this.getAddress();
109-
tx.gasLimit = this.provider.estimateGas(tx);
110-
}
111-
112119
if (tx.gasPrice == null) {
113120
tx.gasPrice = this.provider.getGasPrice();
114121
}
@@ -117,6 +124,12 @@ export class Wallet extends AbstractSigner {
117124
tx.nonce = this.getTransactionCount();
118125
}
119126

127+
if (tx.gasLimit == null) {
128+
let estimate = shallowCopy(tx);
129+
estimate.from = this.getAddress();
130+
tx.gasLimit = this.provider.estimateGas(estimate);
131+
}
132+
120133
if (tx.chainId == null) {
121134
tx.chainId = this.provider.getNetwork().then((network) => network.chainId);
122135
}

0 commit comments

Comments
 (0)