2022-09-05 23:57:11 +03:00
|
|
|
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2023-02-02 12:05:47 +03:00
|
|
|
exports.VoidSigner = exports.AbstractSigner = void 0;
|
2022-11-30 23:44:23 +03:00
|
|
|
/**
|
2023-06-02 00:52:58 +03:00
|
|
|
* Generally the [[Wallet]] and [[JsonRpcSigner]] and their sub-classes
|
|
|
|
* are sufficent for most developers, but this is provided to
|
|
|
|
* fascilitate more complex Signers.
|
2022-11-30 23:44:23 +03:00
|
|
|
*
|
|
|
|
* @_section: api/providers/abstract-signer: Subclassing Signer [abstract-signer]
|
|
|
|
*/
|
2022-12-31 00:35:04 +03:00
|
|
|
const index_js_1 = require("../address/index.js");
|
|
|
|
const index_js_2 = require("../transaction/index.js");
|
|
|
|
const index_js_3 = require("../utils/index.js");
|
|
|
|
const provider_js_1 = require("./provider.js");
|
2023-02-02 12:05:47 +03:00
|
|
|
function checkProvider(signer, operation) {
|
|
|
|
if (signer.provider) {
|
|
|
|
return signer.provider;
|
|
|
|
}
|
|
|
|
(0, index_js_3.assert)(false, "missing provider", "UNSUPPORTED_OPERATION", { operation });
|
|
|
|
}
|
|
|
|
async function populate(signer, tx) {
|
|
|
|
let pop = (0, provider_js_1.copyRequest)(tx);
|
|
|
|
if (pop.to != null) {
|
|
|
|
pop.to = (0, index_js_1.resolveAddress)(pop.to, signer);
|
|
|
|
}
|
|
|
|
if (pop.from != null) {
|
|
|
|
const from = pop.from;
|
|
|
|
pop.from = Promise.all([
|
|
|
|
signer.getAddress(),
|
|
|
|
(0, index_js_1.resolveAddress)(from, signer)
|
|
|
|
]).then(([address, from]) => {
|
|
|
|
(0, index_js_3.assertArgument)(address.toLowerCase() === from.toLowerCase(), "transaction from mismatch", "tx.from", from);
|
|
|
|
return address;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pop.from = signer.getAddress();
|
|
|
|
}
|
|
|
|
return await (0, index_js_3.resolveProperties)(pop);
|
|
|
|
}
|
2023-06-02 00:52:58 +03:00
|
|
|
/**
|
|
|
|
* An **AbstractSigner** includes most of teh functionality required
|
|
|
|
* to get a [[Signer]] working as expected, but requires a few
|
|
|
|
* Signer-specific methods be overridden.
|
|
|
|
*
|
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
class AbstractSigner {
|
2023-06-02 00:52:58 +03:00
|
|
|
/**
|
|
|
|
* The provider this signer is connected to.
|
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
provider;
|
2023-06-02 00:52:58 +03:00
|
|
|
/**
|
|
|
|
* Creates a new Signer connected to %%provider%%.
|
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
constructor(provider) {
|
2022-12-31 00:35:04 +03:00
|
|
|
(0, index_js_3.defineProperties)(this, { provider: (provider || null) });
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
async getNonce(blockTag) {
|
2023-02-02 12:05:47 +03:00
|
|
|
return checkProvider(this, "getTransactionCount").getTransactionCount(await this.getAddress(), blockTag);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
async populateCall(tx) {
|
2023-02-02 12:05:47 +03:00
|
|
|
const pop = await populate(this, tx);
|
2022-09-05 23:57:11 +03:00
|
|
|
return pop;
|
|
|
|
}
|
|
|
|
async populateTransaction(tx) {
|
2023-02-02 12:05:47 +03:00
|
|
|
const provider = checkProvider(this, "populateTransaction");
|
|
|
|
const pop = await populate(this, tx);
|
2022-09-05 23:57:11 +03:00
|
|
|
if (pop.nonce == null) {
|
|
|
|
pop.nonce = await this.getNonce("pending");
|
|
|
|
}
|
|
|
|
if (pop.gasLimit == null) {
|
|
|
|
pop.gasLimit = await this.estimateGas(pop);
|
|
|
|
}
|
2022-09-16 05:58:45 +03:00
|
|
|
// Populate the chain ID
|
|
|
|
const network = await (this.provider).getNetwork();
|
|
|
|
if (pop.chainId != null) {
|
2022-12-31 00:35:04 +03:00
|
|
|
const chainId = (0, index_js_3.getBigInt)(pop.chainId);
|
|
|
|
(0, index_js_3.assertArgument)(chainId === network.chainId, "transaction chainId mismatch", "tx.chainId", tx.chainId);
|
2022-09-16 05:58:45 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
pop.chainId = network.chainId;
|
|
|
|
}
|
2022-10-20 12:03:32 +03:00
|
|
|
// Do not allow mixing pre-eip-1559 and eip-1559 properties
|
|
|
|
const hasEip1559 = (pop.maxFeePerGas != null || pop.maxPriorityFeePerGas != null);
|
|
|
|
if (pop.gasPrice != null && (pop.type === 2 || hasEip1559)) {
|
2022-12-31 00:35:04 +03:00
|
|
|
(0, index_js_3.assertArgument)(false, "eip-1559 transaction do not support gasPrice", "tx", tx);
|
2022-10-20 12:03:32 +03:00
|
|
|
}
|
|
|
|
else if ((pop.type === 0 || pop.type === 1) && hasEip1559) {
|
2022-12-31 00:35:04 +03:00
|
|
|
(0, index_js_3.assertArgument)(false, "pre-eip-1559 transaction do not support maxFeePerGas/maxPriorityFeePerGas", "tx", tx);
|
2022-10-20 12:03:32 +03:00
|
|
|
}
|
|
|
|
if ((pop.type === 2 || pop.type == null) && (pop.maxFeePerGas != null && pop.maxPriorityFeePerGas != null)) {
|
|
|
|
// Fully-formed EIP-1559 transaction (skip getFeeData)
|
|
|
|
pop.type = 2;
|
|
|
|
}
|
|
|
|
else if (pop.type === 0 || pop.type === 1) {
|
|
|
|
// Explicit Legacy or EIP-2930 transaction
|
|
|
|
// We need to get fee data to determine things
|
|
|
|
const feeData = await provider.getFeeData();
|
2022-12-31 00:35:04 +03:00
|
|
|
(0, index_js_3.assert)(feeData.gasPrice != null, "network does not support gasPrice", "UNSUPPORTED_OPERATION", {
|
2022-11-09 10:57:02 +03:00
|
|
|
operation: "getGasPrice"
|
|
|
|
});
|
2022-10-20 12:03:32 +03:00
|
|
|
// Populate missing gasPrice
|
|
|
|
if (pop.gasPrice == null) {
|
|
|
|
pop.gasPrice = feeData.gasPrice;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// We need to get fee data to determine things
|
|
|
|
const feeData = await provider.getFeeData();
|
|
|
|
if (pop.type == null) {
|
|
|
|
// We need to auto-detect the intended type of this transaction...
|
|
|
|
if (feeData.maxFeePerGas != null && feeData.maxPriorityFeePerGas != null) {
|
|
|
|
// The network supports EIP-1559!
|
|
|
|
// Upgrade transaction from null to eip-1559
|
|
|
|
pop.type = 2;
|
|
|
|
if (pop.gasPrice != null) {
|
|
|
|
// Using legacy gasPrice property on an eip-1559 network,
|
|
|
|
// so use gasPrice as both fee properties
|
|
|
|
const gasPrice = pop.gasPrice;
|
|
|
|
delete pop.gasPrice;
|
|
|
|
pop.maxFeePerGas = gasPrice;
|
|
|
|
pop.maxPriorityFeePerGas = gasPrice;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Populate missing fee data
|
|
|
|
if (pop.maxFeePerGas == null) {
|
|
|
|
pop.maxFeePerGas = feeData.maxFeePerGas;
|
|
|
|
}
|
|
|
|
if (pop.maxPriorityFeePerGas == null) {
|
|
|
|
pop.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (feeData.gasPrice != null) {
|
|
|
|
// Network doesn't support EIP-1559...
|
|
|
|
// ...but they are trying to use EIP-1559 properties
|
2023-01-27 09:12:44 +03:00
|
|
|
(0, index_js_3.assert)(!hasEip1559, "network does not support EIP-1559", "UNSUPPORTED_OPERATION", {
|
2022-11-09 10:57:02 +03:00
|
|
|
operation: "populateTransaction"
|
|
|
|
});
|
2022-10-20 12:03:32 +03:00
|
|
|
// Populate missing fee data
|
|
|
|
if (pop.gasPrice == null) {
|
|
|
|
pop.gasPrice = feeData.gasPrice;
|
|
|
|
}
|
|
|
|
// Explicitly set untyped transaction to legacy
|
|
|
|
// @TODO: Maybe this shold allow type 1?
|
|
|
|
pop.type = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// getFeeData has failed us.
|
2022-12-31 00:35:04 +03:00
|
|
|
(0, index_js_3.assert)(false, "failed to get consistent fee data", "UNSUPPORTED_OPERATION", {
|
2022-10-20 12:03:32 +03:00
|
|
|
operation: "signer.getFeeData"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-04-10 04:13:01 +03:00
|
|
|
else if (pop.type === 2 || pop.type === 3) {
|
|
|
|
// Explicitly using EIP-1559 or EIP-4844
|
2022-10-20 12:03:32 +03:00
|
|
|
// Populate missing fee data
|
|
|
|
if (pop.maxFeePerGas == null) {
|
|
|
|
pop.maxFeePerGas = feeData.maxFeePerGas;
|
|
|
|
}
|
|
|
|
if (pop.maxPriorityFeePerGas == null) {
|
|
|
|
pop.maxPriorityFeePerGas = feeData.maxPriorityFeePerGas;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-16 05:58:45 +03:00
|
|
|
//@TOOD: Don't await all over the place; save them up for
|
|
|
|
// the end for better batching
|
2022-12-31 00:35:04 +03:00
|
|
|
return await (0, index_js_3.resolveProperties)(pop);
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
async estimateGas(tx) {
|
2023-02-02 12:05:47 +03:00
|
|
|
return checkProvider(this, "estimateGas").estimateGas(await this.populateCall(tx));
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
async call(tx) {
|
2023-02-02 12:05:47 +03:00
|
|
|
return checkProvider(this, "call").call(await this.populateCall(tx));
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
async resolveName(name) {
|
2023-02-02 12:05:47 +03:00
|
|
|
const provider = checkProvider(this, "resolveName");
|
2022-09-05 23:57:11 +03:00
|
|
|
return await provider.resolveName(name);
|
|
|
|
}
|
|
|
|
async sendTransaction(tx) {
|
2023-02-02 12:05:47 +03:00
|
|
|
const provider = checkProvider(this, "sendTransaction");
|
2022-10-20 12:03:32 +03:00
|
|
|
const pop = await this.populateTransaction(tx);
|
2023-01-17 21:53:49 +03:00
|
|
|
delete pop.from;
|
2022-12-31 00:35:04 +03:00
|
|
|
const txObj = index_js_2.Transaction.from(pop);
|
2022-09-05 23:57:11 +03:00
|
|
|
return await provider.broadcastTransaction(await this.signTransaction(txObj));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.AbstractSigner = AbstractSigner;
|
2023-06-02 00:52:58 +03:00
|
|
|
/**
|
|
|
|
* A **VoidSigner** is a class deisgned to allow an address to be used
|
|
|
|
* in any API which accepts a Signer, but for which there are no
|
|
|
|
* credentials available to perform any actual signing.
|
|
|
|
*
|
|
|
|
* This for example allow impersonating an account for the purpose of
|
|
|
|
* static calls or estimating gas, but does not allow sending transactions.
|
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
class VoidSigner extends AbstractSigner {
|
2023-06-02 00:52:58 +03:00
|
|
|
/**
|
|
|
|
* The signer address.
|
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
address;
|
2023-06-02 00:52:58 +03:00
|
|
|
/**
|
|
|
|
* Creates a new **VoidSigner** with %%address%% attached to
|
|
|
|
* %%provider%%.
|
|
|
|
*/
|
2022-09-05 23:57:11 +03:00
|
|
|
constructor(address, provider) {
|
|
|
|
super(provider);
|
2022-12-31 00:35:04 +03:00
|
|
|
(0, index_js_3.defineProperties)(this, { address });
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
async getAddress() { return this.address; }
|
|
|
|
connect(provider) {
|
|
|
|
return new VoidSigner(this.address, provider);
|
|
|
|
}
|
|
|
|
#throwUnsupported(suffix, operation) {
|
2022-12-31 00:35:04 +03:00
|
|
|
(0, index_js_3.assert)(false, `VoidSigner cannot sign ${suffix}`, "UNSUPPORTED_OPERATION", { operation });
|
2022-09-05 23:57:11 +03:00
|
|
|
}
|
|
|
|
async signTransaction(tx) {
|
|
|
|
this.#throwUnsupported("transactions", "signTransaction");
|
|
|
|
}
|
|
|
|
async signMessage(message) {
|
|
|
|
this.#throwUnsupported("messages", "signMessage");
|
|
|
|
}
|
|
|
|
async signTypedData(domain, types, value) {
|
|
|
|
this.#throwUnsupported("typed-data", "signTypedData");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exports.VoidSigner = VoidSigner;
|
|
|
|
//# sourceMappingURL=abstract-signer.js.map
|