base-wallet.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BaseWallet = void 0;
  4. const index_js_1 = require("../address/index.js");
  5. const index_js_2 = require("../hash/index.js");
  6. const index_js_3 = require("../providers/index.js");
  7. const index_js_4 = require("../transaction/index.js");
  8. const index_js_5 = require("../utils/index.js");
  9. /**
  10. * The **BaseWallet** is a stream-lined implementation of a
  11. * [[Signer]] that operates with a private key.
  12. *
  13. * It is preferred to use the [[Wallet]] class, as it offers
  14. * additional functionality and simplifies loading a variety
  15. * of JSON formats, Mnemonic Phrases, etc.
  16. *
  17. * This class may be of use for those attempting to implement
  18. * a minimal Signer.
  19. */
  20. class BaseWallet extends index_js_3.AbstractSigner {
  21. /**
  22. * The wallet address.
  23. */
  24. address;
  25. #signingKey;
  26. /**
  27. * Creates a new BaseWallet for %%privateKey%%, optionally
  28. * connected to %%provider%%.
  29. *
  30. * If %%provider%% is not specified, only offline methods can
  31. * be used.
  32. */
  33. constructor(privateKey, provider) {
  34. super(provider);
  35. (0, index_js_5.assertArgument)(privateKey && typeof (privateKey.sign) === "function", "invalid private key", "privateKey", "[ REDACTED ]");
  36. this.#signingKey = privateKey;
  37. const address = (0, index_js_4.computeAddress)(this.signingKey.publicKey);
  38. (0, index_js_5.defineProperties)(this, { address });
  39. }
  40. // Store private values behind getters to reduce visibility
  41. // in console.log
  42. /**
  43. * The [[SigningKey]] used for signing payloads.
  44. */
  45. get signingKey() { return this.#signingKey; }
  46. /**
  47. * The private key for this wallet.
  48. */
  49. get privateKey() { return this.signingKey.privateKey; }
  50. async getAddress() { return this.address; }
  51. connect(provider) {
  52. return new BaseWallet(this.#signingKey, provider);
  53. }
  54. async signTransaction(tx) {
  55. tx = (0, index_js_3.copyRequest)(tx);
  56. // Replace any Addressable or ENS name with an address
  57. const { to, from } = await (0, index_js_5.resolveProperties)({
  58. to: (tx.to ? (0, index_js_1.resolveAddress)(tx.to, this.provider) : undefined),
  59. from: (tx.from ? (0, index_js_1.resolveAddress)(tx.from, this.provider) : undefined)
  60. });
  61. if (to != null) {
  62. tx.to = to;
  63. }
  64. if (from != null) {
  65. tx.from = from;
  66. }
  67. if (tx.from != null) {
  68. (0, index_js_5.assertArgument)((0, index_js_1.getAddress)((tx.from)) === this.address, "transaction from address mismatch", "tx.from", tx.from);
  69. delete tx.from;
  70. }
  71. // Build the transaction
  72. const btx = index_js_4.Transaction.from(tx);
  73. btx.signature = this.signingKey.sign(btx.unsignedHash);
  74. return btx.serialized;
  75. }
  76. async signMessage(message) {
  77. return this.signMessageSync(message);
  78. }
  79. // @TODO: Add a secialized signTx and signTyped sync that enforces
  80. // all parameters are known?
  81. /**
  82. * Returns the signature for %%message%% signed with this wallet.
  83. */
  84. signMessageSync(message) {
  85. return this.signingKey.sign((0, index_js_2.hashMessage)(message)).serialized;
  86. }
  87. async signTypedData(domain, types, value) {
  88. // Populate any ENS names
  89. const populated = await index_js_2.TypedDataEncoder.resolveNames(domain, types, value, async (name) => {
  90. // @TODO: this should use resolveName; addresses don't
  91. // need a provider
  92. (0, index_js_5.assert)(this.provider != null, "cannot resolve ENS names without a provider", "UNSUPPORTED_OPERATION", {
  93. operation: "resolveName",
  94. info: { name }
  95. });
  96. const address = await this.provider.resolveName(name);
  97. (0, index_js_5.assert)(address != null, "unconfigured ENS name", "UNCONFIGURED_NAME", {
  98. value: name
  99. });
  100. return address;
  101. });
  102. return this.signingKey.sign(index_js_2.TypedDataEncoder.hash(populated.domain, types, populated.value)).serialized;
  103. }
  104. }
  105. exports.BaseWallet = BaseWallet;
  106. //# sourceMappingURL=base-wallet.js.map