ethers.js/lib.commonjs/wallet/base-wallet.js

105 lines
4.1 KiB
JavaScript
Raw Permalink Normal View History

2022-09-05 16:57:11 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseWallet = void 0;
const index_js_1 = require("../address/index.js");
const index_js_2 = require("../hash/index.js");
const index_js_3 = require("../providers/index.js");
const index_js_4 = require("../transaction/index.js");
const index_js_5 = require("../utils/index.js");
2022-11-30 15:44:23 -05:00
/**
* The **BaseWallet** is a stream-lined implementation of a
* [[Signer]] that operates with a private key.
*
* It is preferred to use the [[Wallet]] class, as it offers
* additional functionality and simplifies loading a variety
* of JSON formats, Mnemonic Phrases, etc.
*
* This class may be of use for those attempting to implement
* a minimal Signer.
*/
2022-09-05 16:57:11 -04:00
class BaseWallet extends index_js_3.AbstractSigner {
2022-11-30 15:44:23 -05:00
/**
* The wallet address.
*/
2022-09-05 16:57:11 -04:00
address;
#signingKey;
2022-11-30 15:44:23 -05:00
/**
* Creates a new BaseWallet for %%privateKey%%, optionally
* connected to %%provider%%.
*
* If %%provider%% is not specified, only offline methods can
* be used.
*/
2022-09-05 16:57:11 -04:00
constructor(privateKey, provider) {
super(provider);
2022-11-30 15:44:23 -05:00
(0, index_js_5.assertArgument)(privateKey && typeof (privateKey.sign) === "function", "invalid private key", "privateKey", "[ REDACTED ]");
2022-09-05 16:57:11 -04:00
this.#signingKey = privateKey;
const address = (0, index_js_4.computeAddress)(this.signingKey.publicKey);
(0, index_js_5.defineProperties)(this, { address });
}
2022-11-30 15:44:23 -05:00
// Store private values behind getters to reduce visibility
// in console.log
/**
* The [[SigningKey]] used for signing payloads.
*/
2022-09-05 16:57:11 -04:00
get signingKey() { return this.#signingKey; }
2022-11-30 15:44:23 -05:00
/**
* The private key for this wallet.
*/
2022-09-05 16:57:11 -04:00
get privateKey() { return this.signingKey.privateKey; }
async getAddress() { return this.address; }
connect(provider) {
return new BaseWallet(this.#signingKey, provider);
}
2022-10-20 05:03:32 -04:00
async signTransaction(tx) {
2022-09-05 16:57:11 -04:00
// Replace any Addressable or ENS name with an address
2022-10-20 05:03:32 -04:00
const { to, from } = await (0, index_js_5.resolveProperties)({
to: (tx.to ? (0, index_js_1.resolveAddress)(tx.to, this.provider) : undefined),
from: (tx.from ? (0, index_js_1.resolveAddress)(tx.from, this.provider) : undefined)
});
if (to != null) {
tx.to = to;
}
if (from != null) {
tx.from = from;
}
2022-09-05 16:57:11 -04:00
if (tx.from != null) {
2022-11-09 02:57:02 -05:00
(0, index_js_5.assertArgument)((0, index_js_1.getAddress)((tx.from)) === this.address, "transaction from address mismatch", "tx.from", tx.from);
2022-09-05 16:57:11 -04:00
delete tx.from;
}
// Build the transaction
const btx = index_js_4.Transaction.from(tx);
btx.signature = this.signingKey.sign(btx.unsignedHash);
return btx.serialized;
}
async signMessage(message) {
2022-11-30 15:44:23 -05:00
return this.signMessageSync(message);
2022-10-20 05:03:32 -04:00
}
// @TODO: Add a secialized signTx and signTyped sync that enforces
// all parameters are known?
2022-11-30 15:44:23 -05:00
/**
* Returns the signature for %%message%% signed with this wallet.
*/
2022-10-20 05:03:32 -04:00
signMessageSync(message) {
return this.signingKey.sign((0, index_js_2.hashMessage)(message)).serialized;
2022-09-05 16:57:11 -04:00
}
async signTypedData(domain, types, value) {
// Populate any ENS names
const populated = await index_js_2.TypedDataEncoder.resolveNames(domain, types, value, async (name) => {
2022-11-10 04:05:51 -05:00
// @TODO: this should use resolveName; addresses don't
// need a provider
2022-11-09 02:57:02 -05:00
(0, index_js_5.assert)(this.provider != null, "cannot resolve ENS names without a provider", "UNSUPPORTED_OPERATION", {
operation: "resolveName",
info: { name }
});
2022-09-05 16:57:11 -04:00
const address = await this.provider.resolveName(name);
2022-11-09 02:57:02 -05:00
(0, index_js_5.assert)(address != null, "unconfigured ENS name", "UNCONFIGURED_NAME", {
value: name
});
2022-09-05 16:57:11 -04:00
return address;
});
return this.signingKey.sign(index_js_2.TypedDataEncoder.hash(populated.domain, types, populated.value)).serialized;
}
}
exports.BaseWallet = BaseWallet;
//# sourceMappingURL=base-wallet.js.map