@tetherto/wdk-wallet-evm 1.0.0-beta.20 → 1.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -191,9 +191,12 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
191
191
  }
192
192
 
193
193
  /**
194
- * The account's address.
194
+ * The account's address, or undefined if the account's signer doesn't expose its address
195
+ * synchronously.
195
196
  *
196
- * @type {string}
197
+ * @deprecated Use {@link getAddress} instead. This property will be removed in an upcoming
198
+ * release: not all signers (e.g. hardware signers) can expose the address synchronously.
199
+ * @type {string | undefined}
197
200
  */
198
201
  get address () {
199
202
  return this._address
@@ -276,6 +279,37 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
276
279
  }, {})
277
280
  }
278
281
 
282
+ /**
283
+ * Validates that a transaction does not mix fee fields its type doesn't support.
284
+ *
285
+ * @protected
286
+ * @param {EvmTransaction} tx - The transaction to validate.
287
+ * @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or a type 3 transaction omits `maxFeePerBlobGas`.
288
+ */
289
+ static _validateFeeFields (tx) {
290
+ const has1559 = tx.maxFeePerGas !== undefined || tx.maxPriorityFeePerGas !== undefined
291
+ const hasLegacy = tx.gasPrice !== undefined
292
+ const hasBlobs = tx.blobs !== undefined || tx.blobVersionedHashes !== undefined || tx.maxFeePerBlobGas !== undefined
293
+ const hasAuthList = tx.authorizationList !== undefined
294
+ const explicitType = (tx.type !== undefined) ? Number(tx.type) : null
295
+
296
+ if ((explicitType === 2 || (explicitType === null && has1559)) && hasLegacy) {
297
+ throw new ValueError('eip-1559 transaction does not support gasPrice')
298
+ }
299
+ if ((explicitType === 0 || explicitType === 1) && has1559) {
300
+ throw new ValueError('pre-eip-1559 transaction does not support maxFeePerGas/maxPriorityFeePerGas')
301
+ }
302
+ if ((explicitType === 3 || hasBlobs) && hasLegacy) {
303
+ throw new ValueError('blob transaction does not support gasPrice')
304
+ }
305
+ if ((explicitType === 3 || hasBlobs) && tx.maxFeePerBlobGas === undefined) {
306
+ throw new ValueError('maxFeePerBlobGas is required for type 3 transactions')
307
+ }
308
+ if ((explicitType === 4 || (explicitType === null && hasAuthList)) && hasLegacy) {
309
+ throw new ValueError('eip-7702 transaction does not support gasPrice')
310
+ }
311
+ }
312
+
279
313
  /**
280
314
  * Quotes the costs of a send transaction operation.
281
315
  *
@@ -287,6 +321,7 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
287
321
  * @param {EvmTransaction} tx - The transaction.
288
322
  * @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
289
323
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
324
+ * @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or a type 3 transaction omits `maxFeePerBlobGas`.
290
325
  * @throws {Error} If the simulation of the transaction reverts, as an ethers error with code `CALL_EXCEPTION`.
291
326
  */
292
327
  async quoteSendTransaction (tx) {
@@ -294,6 +329,8 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
294
329
  throw new ProviderRequiredError('The wallet must be connected to a provider to quote send transaction operations.')
295
330
  }
296
331
 
332
+ WalletAccountReadOnlyEvm._validateFeeFields(tx)
333
+
297
334
  const from = await this.getAddress()
298
335
 
299
336
  const estimatedGas = await this._estimateGas({ from, ...tx })
@@ -310,6 +347,8 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
310
347
  * @param {EvmTransferOptions} options - The transfer's options, including any gas overrides to carry onto the transaction.
311
348
  * @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
312
349
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
350
+ * @throws {ValueError} If the options mix gas override fields that the transaction's type doesn't support.
351
+ * @throws {Error} If the simulation of the transfer reverts, as an ethers error with code `CALL_EXCEPTION`.
313
352
  */
314
353
  async quoteTransfer (options) {
315
354
  if (!this._provider) {
@@ -17,17 +17,18 @@
17
17
  import WalletManager, { InvalidSignerError, ProviderRequiredError } from '@tetherto/wdk-wallet'
18
18
 
19
19
  import WalletAccountEvm from './wallet-account-evm.js'
20
- import SeedSignerEvm from './signers/seed-signer-evm.js'
20
+ import SeedSignerEvm, { BIP_44_ETH_DERIVATION_PATH_PREFIX } from './signers/seed-signer-evm.js'
21
21
 
22
- /** @typedef {import('./signers/seed-signer-evm.js').ISignerEvm} ISignerEvm */
22
+ /** @typedef {import('./signers/signer-evm.js').ISignerEvm} ISignerEvm */
23
23
  /** @typedef {import('ethers').Provider} Provider */
24
24
 
25
25
  /** @typedef {import("@tetherto/wdk-wallet").FeeRates} FeeRates */
26
- /** @typedef {import("@tetherto/wdk-wallet").ISigner} ISigner */
27
26
  /** @typedef {import("@tetherto/wdk-wallet").NoSuchElementError} NoSuchElementError */
27
+ /** @typedef {import("@tetherto/wdk-wallet").ValueError} ValueError */
28
28
 
29
29
  /** @typedef {import('./wallet-account-evm.js').EvmWalletConfig} EvmWalletConfig */
30
30
 
31
+ /** @extends {WalletManager<ISignerEvm>} */
31
32
  export default class WalletManagerEvm extends WalletManager {
32
33
  /**
33
34
  * Multiplier for normal fee rate calculations (in %).
@@ -46,27 +47,49 @@ export default class WalletManagerEvm extends WalletManager {
46
47
  static _FEE_RATE_FAST_MULTIPLIER = 200n
47
48
 
48
49
  /**
49
- * Creates a new wallet manager for evm blockchains.
50
+ * Creates a new wallet manager for evm blockchains from a BIP-39 seed.
50
51
  *
51
- * Accepts either a BIP-39 seed (string/Uint8Array) for backwards compatibility, or a
52
- * pre-built root signer object. The default signer must be derivable (it must be able to
53
- * derive child accounts); non-derivable signers (e.g. private-key signers) are not allowed
54
- * as the default but may be registered by name via {@link addSigner} - If not adding to your global account managment for using just one non derivable signer create a standalone account.
52
+ * @overload
53
+ * @param {string | Uint8Array} seed - A BIP-39 mnemonic seed phrase, or a raw BIP-32 master seed (16-64 bytes).
54
+ * @param {EvmWalletConfig} [config] - The configuration object.
55
+ * @throws {ValueError} If the seed phrase is invalid.
56
+ */
57
+
58
+ /**
59
+ * Creates a new wallet manager for evm blockchains from a default signer.
60
+ *
61
+ * The default signer must be derivable (it must be able to derive child accounts);
62
+ * non-derivable signers (e.g. private-key signers) are not allowed as the default but
63
+ * may be registered by name via {@link addSigner}. To use a single non-derivable signer
64
+ * outside of the wallet manager, create a standalone account instead.
65
+ * **Warning:** the signer is kept exactly as given, not cloned -- if you still hold a
66
+ * reference to it and dispose it directly, every subsequent {@link getAccount}/
67
+ * {@link getAccountByPath} call that falls back to the default signer (i.e. without an
68
+ * explicit `signerName`) fails, since deriving from a disposed signer isn't possible.
69
+ * Conversely, the manager never disposes a signer you supplied: {@link dispose} wipes
70
+ * only the default signer it creates internally from a seed.
55
71
  *
56
- * @param {string|Uint8Array|ISigner} seedOrSigner - A BIP-39 mnemonic seed phrase, a raw BIP-32 master seed (16-64 bytes), or a root signer. Root signers must be derivable — non-derivable signers (e.g. private-key signers) can only be registered by name via {@link addSigner}.
72
+ * @overload
73
+ * @param {ISignerEvm} signer - The default signer.
57
74
  * @param {EvmWalletConfig} [config] - The configuration object.
58
- * @throws {InvalidSignerError} If the default signer doesn't support account derivation.
75
+ * @throws {InvalidSignerError} If the default signer does not support account derivation.
59
76
  */
60
77
  constructor (seedOrSigner, config = {}) {
78
+ const isSeed = typeof seedOrSigner === 'string' || seedOrSigner instanceof Uint8Array
61
79
  let signer = seedOrSigner
62
- if (typeof seedOrSigner === 'string' || seedOrSigner instanceof Uint8Array) {
63
- signer = new SeedSignerEvm(seedOrSigner)
64
- }
65
- if (!signer.isDerivable) {
66
- throw new InvalidSignerError('The default signer must be derivable. Non-derivable signers (e.g. private-key signers) can only be registered by name via addSigner.')
80
+ if (isSeed) {
81
+ signer = new SeedSignerEvm(seedOrSigner, BIP_44_ETH_DERIVATION_PATH_PREFIX)
67
82
  }
68
83
  super(signer, config)
69
84
 
85
+ /**
86
+ * If true, disposes the default signer on calls to the 'dispose' method.
87
+ *
88
+ * @protected
89
+ * @type {boolean}
90
+ */
91
+ this._shouldWipeDefaultSignerOnDisposal = isSeed
92
+
70
93
  /**
71
94
  * The evm wallet configuration.
72
95
  *
@@ -98,9 +121,20 @@ export default class WalletManagerEvm extends WalletManager {
98
121
  */
99
122
 
100
123
  /**
101
- * Returns the wallet account associated with a registered signer. Non-derivable
102
- * signers (e.g. private-key signers) return the signer's single account; derivable signers
103
- * derive a detached child at the signer's own account (the root is never handed out).
124
+ * Returns the wallet account associated with a registered signer.
125
+ *
126
+ * The registered signer is used exactly as given, wherever it happens to sit -- this
127
+ * overload never derives. For a private-key signer that's its one account; for a derivable
128
+ * signer, it's the account at that signer's own current path, unchanged. If you want a
129
+ * derived leaf from a derivable named signer (e.g. a second seed registered as a bank of
130
+ * accounts), use {@link getAccount}(index, { signerName }) or {@link getAccountByPath}(path,
131
+ * { signerName }) instead -- both of those always derive, and throw clearly if the named
132
+ * signer can't.
133
+ *
134
+ * **Warning:** the returned account wraps the registered signer itself, exactly where it
135
+ * sits -- e.g. a second seed registered at the intermediate path "m/44'/60'" yields the
136
+ * account AT "m/44'/60'", not at a derived leaf, which is rarely what you want to transact
137
+ * with. Disposing the returned account leaves the registered signer untouched.
104
138
  *
105
139
  * @overload
106
140
  * @param {string} signerName - The signer name registered via {@link addSigner}.
@@ -110,15 +144,12 @@ export default class WalletManagerEvm extends WalletManager {
110
144
 
111
145
  async getAccount (indexOrSignerName = 0, options = {}) {
112
146
  if (typeof indexOrSignerName === 'string') {
113
- const key = `${indexOrSignerName}#self`
147
+ const key = indexOrSignerName
114
148
  if (this._accounts[key]) {
115
149
  return this._accounts[key]
116
150
  }
117
151
  const signer = this.getSigner(indexOrSignerName)
118
- const accountSigner = signer.isDerivable
119
- ? await signer.derive(signer.path.split('/').slice(-3).join('/'))
120
- : signer
121
- const account = new WalletAccountEvm(accountSigner, this._accountConfig())
152
+ const account = new WalletAccountEvm(signer, this._accountConfig())
122
153
  this._accounts[key] = account
123
154
  return account
124
155
  }
@@ -139,13 +170,16 @@ export default class WalletManagerEvm extends WalletManager {
139
170
  */
140
171
  async getAccountByPath (path, options = {}) {
141
172
  const { signerName } = options
142
- const key = `${signerName ?? ''}:${path}`
173
+ const key = signerName ? `${signerName}:${path}` : path
143
174
  if (this._accounts[key]) {
144
175
  return this._accounts[key]
145
176
  }
146
177
  const signer = this.getSigner(signerName)
178
+ if (!signer.isDerivable) {
179
+ throw new InvalidSignerError('The signer does not support account derivation.')
180
+ }
147
181
  const childSigner = await signer.derive(path)
148
- const account = new WalletAccountEvm(childSigner, this._accountConfig())
182
+ const account = new WalletAccountEvm(childSigner, { ...this._accountConfig(), shouldWipeSignerOnDisposal: true })
149
183
  this._accounts[key] = account
150
184
  return account
151
185
  }
@@ -181,4 +215,15 @@ export default class WalletManagerEvm extends WalletManager {
181
215
  fast: feeRate * WalletManagerEvm._FEE_RATE_FAST_MULTIPLIER / 100n
182
216
  }
183
217
  }
218
+
219
+ /**
220
+ * Disposes all the wallet accounts, erasing their private keys from the memory.
221
+ */
222
+ dispose () {
223
+ if (this._shouldWipeDefaultSignerOnDisposal) {
224
+ this._defaultSigner.dispose()
225
+ }
226
+
227
+ super.dispose()
228
+ }
184
229
  }
package/types/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { default } from "./src/wallet-manager-evm.js";
2
2
  export { default as WalletAccountReadOnlyEvm } from "./src/wallet-account-read-only-evm.js";
3
3
  export { default as WalletAccountEvm } from "./src/wallet-account-evm.js";
4
+ export { ISigner } from "@tetherto/wdk-wallet";
4
5
  export type TypedDataDomain = import("ethers").TypedDataDomain;
5
6
  export type TypedDataField = import("ethers").TypedDataField;
6
7
  export type AuthorizationRequest = import("ethers").AuthorizationRequest;
@@ -27,4 +28,3 @@ export type EvmWalletConfig = import("./src/wallet-account-read-only-evm.js").Ev
27
28
  export type EvmTransactionDetails = import("./src/wallet-account-read-only-evm.js").EvmTransactionDetails;
28
29
  export type ApproveOptions = import("./src/wallet-account-evm.js").ApproveOptions;
29
30
  export type EvmApproveOptions = import("./src/wallet-account-evm.js").EvmApproveOptions;
30
- export type UnsignedEvmTransaction = import("./src/utils/tx-populator-evm.js").UnsignedEvmTransaction;
@@ -1,3 +1,4 @@
1
- export { default as PrivateKeySignerEvm } from "./private-key-signer-evm.js";
1
+ export { ISignerEvm } from "./signer-evm.js";
2
2
  export { default, default as SeedSignerEvm } from "./seed-signer-evm.js";
3
- export type UnsignedEvmTransaction = import("../utils/tx-populator-evm.js").UnsignedEvmTransaction;
3
+ export { default as PrivateKeySignerEvm } from "./private-key-signer-evm.js";
4
+ export type TransactionLike = import("./signer-evm.js").TransactionLike;
@@ -1,18 +1,19 @@
1
- import { ISignerEvm } from "./seed-signer-evm.js";
2
- /** @typedef {import('./seed-signer-evm.js').UnsignedEvmTransaction} UnsignedEvmTransaction */
3
- /** @typedef {import('../wallet-account-read-only-evm.js').TypedData} TypedData */
4
- /** @typedef {import('@tetherto/wdk-wallet').KeyPair} KeyPair */
5
- /** @typedef {import('ethers').AuthorizationRequest} AuthorizationRequest */
6
- /** @typedef {import('ethers').Authorization} Authorization */
7
1
  /**
8
- * @extends {ISignerEvm}
9
2
  * Signer that wraps a raw private key in a memory-safe buffer, exposing a minimal
10
3
  * interface for signing messages, transactions and typed data. This signer does
11
4
  * not support derivation and always represents a single account.
5
+ *
6
+ * @implements {ISignerEvm}
12
7
  */
13
- export default class PrivateKeySignerEvm extends ISignerEvm {
8
+ export default class PrivateKeySignerEvm implements ISignerEvm {
14
9
  /**
15
- * @param {string|Uint8Array} privateKey - Hex string (with/without 0x) or raw key bytes.
10
+ * Create a signer from a raw private key.
11
+ *
12
+ * The supplied key is copied: the signer keeps its own internal copy alive until
13
+ * {@link dispose} zeroes it, and never wipes the supplied key, whose disposal
14
+ * remains the caller's responsibility.
15
+ *
16
+ * @param {string | Uint8Array} privateKey - The private key's hex string (with or without 0x) or byte sequence.
16
17
  */
17
18
  constructor(privateKey: string | Uint8Array);
18
19
  /** @private */
@@ -21,28 +22,46 @@ export default class PrivateKeySignerEvm extends ISignerEvm {
21
22
  private _wallet;
22
23
  /** @private */
23
24
  private _address;
24
- /** @private */
25
- private _path;
26
- /** @type {boolean} */
27
- get isDerivable(): boolean;
28
- /** @type {number|undefined} */
29
- get index(): number | undefined;
30
- /** @type {string|undefined} */
31
- get path(): string | undefined;
32
- /** @type {string} */
25
+ /**
26
+ * Whether this signer can derive child signers.
27
+ *
28
+ * @type {false}
29
+ */
30
+ get isDerivable(): false;
31
+ /**
32
+ * The BIP 0044 derivation path.
33
+ *
34
+ * @type {string | null}
35
+ */
36
+ get path(): string | null;
37
+ /**
38
+ * The account's address.
39
+ *
40
+ * @deprecated Use {@link getAddress} instead. This property will be removed in an upcoming
41
+ * release: not all signers (e.g. hardware signers) can expose the address synchronously.
42
+ * @type {string}
43
+ */
33
44
  get address(): string;
34
45
  /**
35
46
  * The account's key pair (private and public key buffers).
47
+ *
36
48
  * @type {KeyPair}
37
49
  */
38
50
  get keyPair(): KeyPair;
39
51
  /**
40
- * PrivateKeySignerEvm is not a hierarchical signer and cannot derive.
41
- * @returns {Promise<never>}
42
- * @throws {InvalidSignerError} Always — private-key signers do not support derivation.
52
+ * Derive a child signer using a relative path (e.g., "0'/0/0").
53
+ *
54
+ * @param {string} path - The relative derivation path.
55
+ * @returns {Promise<never>} The derived signer.
56
+ * @throws {UnsupportedOperationError} If the signer does not support account derivation.
57
+ * @throws {ValueError} If the path is not valid.
58
+ */
59
+ derive(path: string): Promise<never>;
60
+ /**
61
+ * Returns the account's address.
62
+ *
63
+ * @returns {Promise<string>} The account's address.
43
64
  */
44
- derive(): Promise<never>;
45
- /** @returns {Promise<string>} */
46
65
  getAddress(): Promise<string>;
47
66
  /**
48
67
  * Signs a message.
@@ -52,30 +71,36 @@ export default class PrivateKeySignerEvm extends ISignerEvm {
52
71
  */
53
72
  sign(message: string): Promise<string>;
54
73
  /**
55
- * Signs a transaction and returns the serialized signed transaction hex.
74
+ * Signs a transaction.
56
75
  *
57
- * @param {UnsignedEvmTransaction} unsignedTx - The unsigned transaction object.
58
- * @returns {Promise<string>}
76
+ * @param {TransactionLike} tx - The transaction to sign.
77
+ * @returns {Promise<string>} The signed transaction as a hex string.
59
78
  */
60
- signTransaction(unsignedTx: UnsignedEvmTransaction): Promise<string>;
79
+ signTransaction(tx: TransactionLike): Promise<string>;
61
80
  /**
62
81
  * Signs typed data according to EIP-712.
63
82
  *
64
83
  * @param {TypedData} typedData - The typed data to sign.
65
84
  * @returns {Promise<string>} The typed data signature.
66
85
  */
67
- signTypedData({ domain, types, message }: TypedData): Promise<string>;
86
+ signTypedData(typedData: TypedData): Promise<string>;
68
87
  /**
69
- * Sign an ERC-7702 authorization tuple.
70
- * @param {AuthorizationRequest} auth
71
- * @returns {Promise<Authorization>}
88
+ * Signs an ERC-7702 authorization tuple.
89
+ *
90
+ * @param {AuthorizationRequest} auth - The authorization request.
91
+ * @returns {Promise<Authorization>} The signed authorization.
72
92
  */
73
93
  signAuthorization(auth: AuthorizationRequest): Promise<Authorization>;
74
- /** Dispose secrets from memory. */
94
+ /**
95
+ * Disposes the signer, securely erasing its internal copy of the private key from memory.
96
+ */
75
97
  dispose(): void;
76
98
  }
77
- export type UnsignedEvmTransaction = import("./seed-signer-evm.js").UnsignedEvmTransaction;
78
- export type TypedData = import("../wallet-account-read-only-evm.js").TypedData;
99
+ export type ISignerEvm = import("./signer-evm.js").ISignerEvm;
79
100
  export type KeyPair = import("@tetherto/wdk-wallet").KeyPair;
101
+ export type UnsupportedOperationError = import("@tetherto/wdk-wallet").UnsupportedOperationError;
102
+ export type ValueError = import("@tetherto/wdk-wallet").ValueError;
103
+ export type TransactionLike = import("ethers").TransactionLike;
80
104
  export type AuthorizationRequest = import("ethers").AuthorizationRequest;
81
105
  export type Authorization = import("ethers").Authorization;
106
+ export type TypedData = import("../wallet-account-read-only-evm.js").TypedData;