2019-05-15 01:25:46 +03:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import { BlockTag, Provider, TransactionRequest, TransactionResponse } from "@ethersproject/abstract-provider";
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
|
|
|
import { Bytes } from "@ethersproject/bytes";
|
|
|
|
import { defineReadOnly, resolveProperties, shallowCopy } from "@ethersproject/properties";
|
|
|
|
|
2019-08-02 01:04:06 +03:00
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
const allowedTransactionKeys: Array<string> = [
|
|
|
|
"chainId", "data", "from", "gasLimit", "gasPrice", "nonce", "to", "value"
|
|
|
|
];
|
|
|
|
|
|
|
|
// Sub-classes of Signer may optionally extend this interface to indicate
|
|
|
|
// they have a private key available synchronously
|
|
|
|
export interface ExternallyOwnedAccount {
|
|
|
|
readonly address: string;
|
|
|
|
readonly privateKey: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 abstract class Signer {
|
|
|
|
readonly provider?: Provider;
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
// Sub-classes MUST implement these
|
|
|
|
|
|
|
|
// Returns the checksum address
|
|
|
|
abstract getAddress(): Promise<string>
|
|
|
|
|
|
|
|
// Returns the signed prefixed-message. This MUST treat:
|
|
|
|
// - Bytes as a binary message
|
|
|
|
// - string as a UTF8-message
|
|
|
|
// i.e. "0x1234" is a SIX (6) byte string, NOT 2 bytes of data
|
|
|
|
abstract signMessage(message: Bytes | string): Promise<string>;
|
|
|
|
|
|
|
|
// Signs a transaxction and returns the fully serialized, signed transaction.
|
|
|
|
// The EXACT transaction MUST be signed, and NO additional properties to be added.
|
|
|
|
// - This MAY throw if signing transactions is not supports, but if
|
|
|
|
// it does, sentTransaction MUST be overridden.
|
|
|
|
abstract signTransaction(transaction: TransactionRequest): Promise<string>;
|
|
|
|
|
|
|
|
// Returns a new instance of the Signer, connected to provider.
|
|
|
|
// This MAY throw if changing providers is not supported.
|
|
|
|
abstract connect(provider: Provider): Signer;
|
|
|
|
|
2019-06-11 05:25:46 +03:00
|
|
|
readonly _isSigner: boolean;
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
|
|
|
|
///////////////////
|
|
|
|
// Sub-classes MUST call super
|
|
|
|
constructor() {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.checkAbstract(new.target, Signer);
|
2019-06-11 05:25:46 +03:00
|
|
|
defineReadOnly(this, "_isSigner", true);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
// Sub-classes MAY override these
|
|
|
|
|
2020-05-01 23:32:32 +03:00
|
|
|
async getBalance(blockTag?: BlockTag): Promise<BigNumber> {
|
2019-05-15 01:25:46 +03:00
|
|
|
this._checkProvider("getBalance");
|
2020-05-01 23:32:32 +03:00
|
|
|
return await this.provider.getBalance(this.getAddress(), blockTag);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2020-05-01 23:32:32 +03:00
|
|
|
async getTransactionCount(blockTag?: BlockTag): Promise<number> {
|
2019-05-15 01:25:46 +03:00
|
|
|
this._checkProvider("getTransactionCount");
|
2020-05-01 23:32:32 +03:00
|
|
|
return await this.provider.getTransactionCount(this.getAddress(), blockTag);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populates "from" if unspecified, and estimates the gas for the transation
|
2020-05-01 23:32:32 +03:00
|
|
|
async estimateGas(transaction: TransactionRequest): Promise<BigNumber> {
|
2019-05-15 01:25:46 +03:00
|
|
|
this._checkProvider("estimateGas");
|
2020-05-01 23:32:32 +03:00
|
|
|
const tx = await resolveProperties(this.checkTransaction(transaction));
|
|
|
|
return await this.provider.estimateGas(tx);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populates "from" if unspecified, and calls with the transation
|
2020-05-01 23:32:32 +03:00
|
|
|
async call(transaction: TransactionRequest, blockTag?: BlockTag): Promise<string> {
|
2019-05-15 01:25:46 +03:00
|
|
|
this._checkProvider("call");
|
2020-05-01 23:32:32 +03:00
|
|
|
const tx = await resolveProperties(this.checkTransaction(transaction));
|
|
|
|
return await this.provider.call(tx, blockTag);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populates all fields in a transaction, signs it and sends it to the network
|
|
|
|
sendTransaction(transaction: TransactionRequest): Promise<TransactionResponse> {
|
|
|
|
this._checkProvider("sendTransaction");
|
|
|
|
return this.populateTransaction(transaction).then((tx) => {
|
|
|
|
return this.signTransaction(tx).then((signedTx) => {
|
|
|
|
return this.provider.sendTransaction(signedTx);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:32:32 +03:00
|
|
|
async getChainId(): Promise<number> {
|
2019-05-15 01:25:46 +03:00
|
|
|
this._checkProvider("getChainId");
|
2020-05-01 23:32:32 +03:00
|
|
|
const network = await this.provider.getNetwork();
|
|
|
|
return network.chainId;
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2020-05-01 23:32:32 +03:00
|
|
|
async getGasPrice(): Promise<BigNumber> {
|
2019-05-15 01:25:46 +03:00
|
|
|
this._checkProvider("getGasPrice");
|
2020-05-01 23:32:32 +03:00
|
|
|
return await this.provider.getGasPrice();
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
2020-05-01 23:32:32 +03:00
|
|
|
async resolveName(name: string): Promise<string> {
|
2019-05-15 01:25:46 +03:00
|
|
|
this._checkProvider("resolveName");
|
2020-05-01 23:32:32 +03:00
|
|
|
return await this.provider.resolveName(name);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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: TransactionRequest): TransactionRequest {
|
2019-11-01 17:33:51 +03:00
|
|
|
for (const key in transaction) {
|
2019-05-15 01:25:46 +03:00
|
|
|
if (allowedTransactionKeys.indexOf(key) === -1) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwArgumentError("invalid transaction key: " + key, "transaction", transaction);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
const tx = shallowCopy(transaction);
|
2020-02-01 11:38:19 +03:00
|
|
|
|
|
|
|
if (tx.from == null) {
|
|
|
|
tx.from = this.getAddress();
|
|
|
|
} else {
|
|
|
|
// Make sure any provided address matches this signer
|
|
|
|
tx.from = Promise.all([
|
|
|
|
Promise.resolve(tx.from),
|
|
|
|
this.getAddress()
|
|
|
|
]).then((result) => {
|
|
|
|
if (result[0] !== result[1]) {
|
|
|
|
logger.throwArgumentError("from address mismatch", "transaction", transaction);
|
|
|
|
}
|
|
|
|
return result[0];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-15 01:25:46 +03:00
|
|
|
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
|
2019-11-01 17:33:51 +03:00
|
|
|
async populateTransaction(transaction: TransactionRequest): Promise<TransactionRequest> {
|
2020-05-01 23:32:32 +03:00
|
|
|
|
2020-02-07 02:21:34 +03:00
|
|
|
const tx: TransactionRequest = await resolveProperties(this.checkTransaction(transaction))
|
2019-11-01 17:33:51 +03:00
|
|
|
|
|
|
|
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"); }
|
|
|
|
|
|
|
|
if (tx.gasLimit == null) {
|
|
|
|
tx.gasLimit = this.estimateGas(tx).catch((error) => {
|
2020-02-07 02:21:34 +03:00
|
|
|
return logger.throwError("cannot estimate gas; transaction may fail or may require manual gas limit", Logger.errors.UNPREDICTABLE_GAS_LIMIT, {
|
2020-05-13 05:53:16 +03:00
|
|
|
error: error,
|
2019-11-01 17:33:51 +03:00
|
|
|
tx: tx
|
2019-08-05 02:20:03 +03:00
|
|
|
});
|
2019-11-01 17:33:51 +03:00
|
|
|
});
|
|
|
|
}
|
2020-02-01 11:38:19 +03:00
|
|
|
|
|
|
|
if (tx.chainId == null) {
|
|
|
|
tx.chainId = this.getChainId();
|
|
|
|
} else {
|
|
|
|
tx.chainId = Promise.all([
|
|
|
|
Promise.resolve(tx.chainId),
|
|
|
|
this.getChainId()
|
|
|
|
]).then((results) => {
|
|
|
|
if (results[1] !== 0 && results[0] !== results[1]) {
|
|
|
|
logger.throwArgumentError("chainId address mismatch", "transaction", transaction);
|
|
|
|
}
|
|
|
|
return results[0];
|
|
|
|
});
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
|
2019-11-01 17:33:51 +03:00
|
|
|
return await resolveProperties(tx);
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////
|
|
|
|
// Sub-classes SHOULD leave these alone
|
|
|
|
|
|
|
|
_checkProvider(operation?: string): void {
|
2019-08-02 01:04:06 +03:00
|
|
|
if (!this.provider) { logger.throwError("missing provider", Logger.errors.UNSUPPORTED_OPERATION, {
|
2019-05-15 01:25:46 +03:00
|
|
|
operation: (operation || "_checkProvider") });
|
|
|
|
}
|
|
|
|
}
|
2019-06-11 05:25:46 +03:00
|
|
|
|
|
|
|
static isSigner(value: any): value is Signer {
|
|
|
|
return !!(value && value._isSigner);
|
|
|
|
}
|
2019-05-15 01:25:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export class VoidSigner extends Signer {
|
|
|
|
readonly address: string;
|
|
|
|
|
|
|
|
constructor(address: string, provider?: Provider) {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.checkNew(new.target, VoidSigner);
|
2019-05-15 01:25:46 +03:00
|
|
|
super();
|
|
|
|
defineReadOnly(this, "address", address);
|
|
|
|
defineReadOnly(this, "provider", provider || null);
|
|
|
|
}
|
|
|
|
|
|
|
|
getAddress(): Promise<string> {
|
|
|
|
return Promise.resolve(this.address);
|
|
|
|
}
|
|
|
|
|
|
|
|
_fail(message: string, operation: string): Promise<any> {
|
|
|
|
return Promise.resolve().then(() => {
|
2019-08-02 01:04:06 +03:00
|
|
|
logger.throwError(message, Logger.errors.UNSUPPORTED_OPERATION, { operation: operation });
|
2019-05-15 01:25:46 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
signMessage(message: Bytes | string): Promise<string> {
|
|
|
|
return this._fail("VoidSigner cannot sign messages", "signMessage");
|
|
|
|
}
|
|
|
|
|
|
|
|
signTransaction(transaction: TransactionRequest): Promise<string> {
|
|
|
|
return this._fail("VoidSigner cannot sign transactions", "signTransaction");
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(provider: Provider): VoidSigner {
|
|
|
|
return new VoidSigner(this.address, provider);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|