2
0
mirror of synced 2025-02-25 04:25:16 +00:00

Check all transaction parameters are valid; protect against typos (#299).

This commit is contained in:
Richard Moore 2018-10-11 15:16:31 -04:00
parent 9b118af304
commit 84344ac4c2
No known key found for this signature in database
GPG Key ID: 525F70A6FCABC295

View File

@ -24,6 +24,9 @@ import { BlockTag, TransactionRequest, TransactionResponse } from './providers/a
import * as errors from './errors'; import * as errors from './errors';
const allowedTransactionKeys: { [ key: string ]: boolean } = {
chainId: true, data: true, gasLimit: true, gasPrice:true, nonce: true, to: true, value: true
}
export class Wallet extends AbstractSigner { export class Wallet extends AbstractSigner {
@ -69,6 +72,15 @@ export class Wallet extends AbstractSigner {
} }
sign(transaction: TransactionRequest): Promise<string> { sign(transaction: TransactionRequest): Promise<string> {
for (let key in transaction) {
if (!allowedTransactionKeys[key]) {
errors.throwError('unsupported transaction property - ' + key, errors.INVALID_ARGUMENT, {
argument: 'transaction',
value: transaction,
key: key
});
}
}
return resolveProperties(transaction).then((tx) => { return resolveProperties(transaction).then((tx) => {
let rawTx = serializeTransaction(tx); let rawTx = serializeTransaction(tx);
let signature = this.signingKey.signDigest(keccak256(rawTx)); let signature = this.signingKey.signDigest(keccak256(rawTx));
@ -98,17 +110,12 @@ export class Wallet extends AbstractSigner {
throw new Error('invalid transaction object'); throw new Error('invalid transaction object');
} }
var tx = shallowCopy(transaction); let tx = shallowCopy(transaction);
if (tx.to != null) { if (tx.to != null) {
tx.to = this.provider.resolveName(tx.to); tx.to = this.provider.resolveName(tx.to);
} }
if (tx.gasLimit == null) {
tx.from = this.getAddress();
tx.gasLimit = this.provider.estimateGas(tx);
}
if (tx.gasPrice == null) { if (tx.gasPrice == null) {
tx.gasPrice = this.provider.getGasPrice(); tx.gasPrice = this.provider.getGasPrice();
} }
@ -117,6 +124,12 @@ export class Wallet extends AbstractSigner {
tx.nonce = this.getTransactionCount(); tx.nonce = this.getTransactionCount();
} }
if (tx.gasLimit == null) {
let estimate = shallowCopy(tx);
estimate.from = this.getAddress();
tx.gasLimit = this.provider.estimateGas(estimate);
}
if (tx.chainId == null) { if (tx.chainId == null) {
tx.chainId = this.provider.getNetwork().then((network) => network.chainId); tx.chainId = this.provider.getNetwork().then((network) => network.chainId);
} }