169 lines
6.4 KiB
JavaScript
169 lines
6.4 KiB
JavaScript
|
"use strict";
|
||
|
import { defineReadOnly, resolveProperties, shallowCopy } from "@ethersproject/properties";
|
||
|
import { Logger } from "@ethersproject/logger";
|
||
|
import { version } from "./_version";
|
||
|
const logger = new Logger(version);
|
||
|
const allowedTransactionKeys = [
|
||
|
"chainId", "data", "from", "gasLimit", "gasPrice", "nonce", "to", "value"
|
||
|
];
|
||
|
// Sub-Class Notes:
|
||
|
// - A Signer MUST always make sure, that if present, the "from" field
|
||
|
// matches the Signer, before sending or signing a transaction
|
||
|
// - A Signer SHOULD always wrap private information (such as a private
|
||
|
// key or mnemonic) in a function, so that console.log does not leak
|
||
|
// the data
|
||
|
export class Signer {
|
||
|
///////////////////
|
||
|
// Sub-classes MUST call super
|
||
|
constructor() {
|
||
|
logger.checkAbstract(new.target, Signer);
|
||
|
defineReadOnly(this, "_isSigner", true);
|
||
|
}
|
||
|
///////////////////
|
||
|
// Sub-classes MAY override these
|
||
|
getBalance(blockTag) {
|
||
|
this._checkProvider("getBalance");
|
||
|
return this.provider.getBalance(this.getAddress(), blockTag);
|
||
|
}
|
||
|
getTransactionCount(blockTag) {
|
||
|
this._checkProvider("getTransactionCount");
|
||
|
return this.provider.getTransactionCount(this.getAddress(), blockTag);
|
||
|
}
|
||
|
// Populates "from" if unspecified, and estimates the gas for the transation
|
||
|
estimateGas(transaction) {
|
||
|
this._checkProvider("estimateGas");
|
||
|
return resolveProperties(this.checkTransaction(transaction)).then((tx) => {
|
||
|
return this.provider.estimateGas(tx);
|
||
|
});
|
||
|
}
|
||
|
// Populates "from" if unspecified, and calls with the transation
|
||
|
call(transaction, blockTag) {
|
||
|
this._checkProvider("call");
|
||
|
return resolveProperties(this.checkTransaction(transaction)).then((tx) => {
|
||
|
return this.provider.call(tx);
|
||
|
});
|
||
|
}
|
||
|
// Populates all fields in a transaction, signs it and sends it to the network
|
||
|
sendTransaction(transaction) {
|
||
|
this._checkProvider("sendTransaction");
|
||
|
return this.populateTransaction(transaction).then((tx) => {
|
||
|
return this.signTransaction(tx).then((signedTx) => {
|
||
|
return this.provider.sendTransaction(signedTx);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
getChainId() {
|
||
|
this._checkProvider("getChainId");
|
||
|
return this.provider.getNetwork().then((network) => network.chainId);
|
||
|
}
|
||
|
getGasPrice() {
|
||
|
this._checkProvider("getGasPrice");
|
||
|
return this.provider.getGasPrice();
|
||
|
}
|
||
|
resolveName(name) {
|
||
|
this._checkProvider("resolveName");
|
||
|
return this.provider.resolveName(name);
|
||
|
}
|
||
|
// Checks a transaction does not contain invalid keys and if
|
||
|
// no "from" is provided, populates it.
|
||
|
// - does NOT require a provider
|
||
|
// - adds "from" is not present
|
||
|
// - returns a COPY (safe to mutate the result)
|
||
|
// By default called from: (overriding these prevents it)
|
||
|
// - call
|
||
|
// - estimateGas
|
||
|
// - populateTransaction (and therefor sendTransaction)
|
||
|
checkTransaction(transaction) {
|
||
|
for (let key in transaction) {
|
||
|
if (allowedTransactionKeys.indexOf(key) === -1) {
|
||
|
logger.throwArgumentError("invalid transaction key: " + key, "transaction", transaction);
|
||
|
}
|
||
|
}
|
||
|
let tx = shallowCopy(transaction);
|
||
|
if (tx.from == null) {
|
||
|
tx.from = this.getAddress();
|
||
|
}
|
||
|
return tx;
|
||
|
}
|
||
|
// Populates ALL keys for a transaction and checks that "from" matches
|
||
|
// this Signer. Should be used by sendTransaction but NOT by signTransaction.
|
||
|
// By default called from: (overriding these prevents it)
|
||
|
// - sendTransaction
|
||
|
populateTransaction(transaction) {
|
||
|
return resolveProperties(this.checkTransaction(transaction)).then((tx) => {
|
||
|
if (tx.to != null) {
|
||
|
tx.to = Promise.resolve(tx.to).then((to) => this.resolveName(to));
|
||
|
}
|
||
|
if (tx.gasPrice == null) {
|
||
|
tx.gasPrice = this.getGasPrice();
|
||
|
}
|
||
|
if (tx.nonce == null) {
|
||
|
tx.nonce = this.getTransactionCount("pending");
|
||
|
}
|
||
|
// Make sure any provided address matches this signer
|
||
|
if (tx.from == null) {
|
||
|
tx.from = this.getAddress();
|
||
|
}
|
||
|
else {
|
||
|
tx.from = Promise.all([
|
||
|
this.getAddress(),
|
||
|
this.provider.resolveName(tx.from)
|
||
|
]).then((results) => {
|
||
|
if (results[0] !== results[1]) {
|
||
|
logger.throwArgumentError("from address mismatch", "transaction", transaction);
|
||
|
}
|
||
|
return results[0];
|
||
|
});
|
||
|
}
|
||
|
if (tx.gasLimit == null) {
|
||
|
tx.gasLimit = this.estimateGas(tx).catch((error) => {
|
||
|
logger.throwError("cannot estimate gas; transaction may fail or may require manual gas limit", Logger.errors.UNPREDICTABLE_GAS_LIMIT, {
|
||
|
tx: tx
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
if (tx.chainId == null) {
|
||
|
tx.chainId = this.getChainId();
|
||
|
}
|
||
|
return resolveProperties(tx);
|
||
|
});
|
||
|
}
|
||
|
///////////////////
|
||
|
// Sub-classes SHOULD leave these alone
|
||
|
_checkProvider(operation) {
|
||
|
if (!this.provider) {
|
||
|
logger.throwError("missing provider", Logger.errors.UNSUPPORTED_OPERATION, {
|
||
|
operation: (operation || "_checkProvider")
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
static isSigner(value) {
|
||
|
return !!(value && value._isSigner);
|
||
|
}
|
||
|
}
|
||
|
export class VoidSigner extends Signer {
|
||
|
constructor(address, provider) {
|
||
|
logger.checkNew(new.target, VoidSigner);
|
||
|
super();
|
||
|
defineReadOnly(this, "address", address);
|
||
|
defineReadOnly(this, "provider", provider || null);
|
||
|
}
|
||
|
getAddress() {
|
||
|
return Promise.resolve(this.address);
|
||
|
}
|
||
|
_fail(message, operation) {
|
||
|
return Promise.resolve().then(() => {
|
||
|
logger.throwError(message, Logger.errors.UNSUPPORTED_OPERATION, { operation: operation });
|
||
|
});
|
||
|
}
|
||
|
signMessage(message) {
|
||
|
return this._fail("VoidSigner cannot sign messages", "signMessage");
|
||
|
}
|
||
|
signTransaction(transaction) {
|
||
|
return this._fail("VoidSigner cannot sign transactions", "signTransaction");
|
||
|
}
|
||
|
connect(provider) {
|
||
|
return new VoidSigner(this.address, provider);
|
||
|
}
|
||
|
}
|