@tetherto/wdk-wallet-evm 1.0.0-beta.24 → 1.0.0-beta.25

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tetherto/wdk-wallet-evm",
3
- "version": "1.0.0-beta.24",
3
+ "version": "1.0.0-beta.25",
4
4
  "description": "EVM wallet module for WDK (Wallet Development Kit) by Tether.",
5
5
  "homepage": "https://docs.wdk.tether.io/sdk/wallet-modules/wallet-evm/",
6
6
  "keywords": [
@@ -33,7 +33,7 @@
33
33
  "@noble/hashes": "1.8.0",
34
34
  "@noble/secp256k1": "2.2.3",
35
35
  "@tetherto/wdk-failover-provider": "1.0.0-beta.2",
36
- "@tetherto/wdk-wallet": "1.0.0-beta.21",
36
+ "@tetherto/wdk-wallet": "1.0.0-beta.22",
37
37
  "bare-node-runtime": "^1.5.2",
38
38
  "bip39": "3.1.0",
39
39
  "ethers": "6.17.0",
@@ -15,7 +15,7 @@
15
15
 
16
16
  import { BaseWallet } from 'ethers'
17
17
 
18
- import { UnsupportedOperationError } from '@tetherto/wdk-wallet'
18
+ import { DisposalError, UnsupportedOperationError } from '@tetherto/wdk-wallet'
19
19
 
20
20
  import MemorySafeSigningKey from '../memory-safe/signing-key.js'
21
21
 
@@ -57,6 +57,9 @@ export default class PrivateKeySignerEvm {
57
57
 
58
58
  /** @private */
59
59
  this._address = this._wallet.address
60
+
61
+ /** @private */
62
+ this._disposed = false
60
63
  }
61
64
 
62
65
  /**
@@ -68,6 +71,15 @@ export default class PrivateKeySignerEvm {
68
71
  return false
69
72
  }
70
73
 
74
+ /**
75
+ * True if the signer has been disposed.
76
+ *
77
+ * @type {boolean}
78
+ */
79
+ get disposed () {
80
+ return this._disposed
81
+ }
82
+
71
83
  /**
72
84
  * The BIP 0044 derivation path.
73
85
  *
@@ -101,10 +113,13 @@ export default class PrivateKeySignerEvm {
101
113
  *
102
114
  * @param {string} path - The relative derivation path.
103
115
  * @returns {Promise<never>} The derived signer.
116
+ * @throws {DisposalError} If the signer has been disposed.
104
117
  * @throws {UnsupportedOperationError} If the signer does not support account derivation.
105
118
  * @throws {ValueError} If the path is not valid.
106
119
  */
107
120
  async derive (path) {
121
+ this._guardDisposed()
122
+
108
123
  throw new UnsupportedOperationError('derive(path)')
109
124
  }
110
125
 
@@ -122,8 +137,11 @@ export default class PrivateKeySignerEvm {
122
137
  *
123
138
  * @param {string} message - The message to sign.
124
139
  * @returns {Promise<string>} The message's signature.
140
+ * @throws {DisposalError} If the signer has been disposed.
125
141
  */
126
142
  async sign (message) {
143
+ this._guardDisposed()
144
+
127
145
  return this._wallet.signMessage(message)
128
146
  }
129
147
 
@@ -132,8 +150,11 @@ export default class PrivateKeySignerEvm {
132
150
  *
133
151
  * @param {TransactionLike} tx - The transaction to sign.
134
152
  * @returns {Promise<string>} The signed transaction as a hex string.
153
+ * @throws {DisposalError} If the signer has been disposed.
135
154
  */
136
155
  async signTransaction (tx) {
156
+ this._guardDisposed()
157
+
137
158
  return this._wallet.signTransaction(tx)
138
159
  }
139
160
 
@@ -142,8 +163,11 @@ export default class PrivateKeySignerEvm {
142
163
  *
143
164
  * @param {TypedData} typedData - The typed data to sign.
144
165
  * @returns {Promise<string>} The typed data signature.
166
+ * @throws {DisposalError} If the signer has been disposed.
145
167
  */
146
168
  async signTypedData ({ domain, types, message }) {
169
+ this._guardDisposed()
170
+
147
171
  return this._wallet.signTypedData(domain, types, message)
148
172
  }
149
173
 
@@ -152,8 +176,11 @@ export default class PrivateKeySignerEvm {
152
176
  *
153
177
  * @param {AuthorizationRequest} auth - The authorization request.
154
178
  * @returns {Promise<Authorization>} The signed authorization.
179
+ * @throws {DisposalError} If the signer has been disposed.
155
180
  */
156
181
  async signAuthorization (auth) {
182
+ this._guardDisposed()
183
+
157
184
  return this._wallet.authorizeSync(auth)
158
185
  }
159
186
 
@@ -161,6 +188,16 @@ export default class PrivateKeySignerEvm {
161
188
  * Disposes the signer, securely erasing its internal copy of the private key from memory.
162
189
  */
163
190
  dispose () {
191
+ if (this._disposed) return
192
+
164
193
  this._signingKey.dispose()
194
+ this._disposed = true
195
+ }
196
+
197
+ /** @private */
198
+ _guardDisposed () {
199
+ if (this.disposed) {
200
+ throw new DisposalError('The signer has been disposed.')
201
+ }
165
202
  }
166
203
  }
@@ -15,7 +15,7 @@
15
15
 
16
16
  import * as bip39 from 'bip39'
17
17
 
18
- import { ValueError } from '@tetherto/wdk-wallet'
18
+ import { DisposalError, ValueError } from '@tetherto/wdk-wallet'
19
19
 
20
20
  // eslint-disable-next-line camelcase
21
21
  import { sodium_memzero } from 'sodium-universal'
@@ -78,6 +78,9 @@ export default class SeedSignerEvm {
78
78
 
79
79
  /** @private */
80
80
  this._account = account
81
+
82
+ /** @private */
83
+ this._disposed = false
81
84
  }
82
85
 
83
86
  /**
@@ -90,6 +93,15 @@ export default class SeedSignerEvm {
90
93
  return true
91
94
  }
92
95
 
96
+ /**
97
+ * True if the signer has been disposed.
98
+ *
99
+ * @type {boolean}
100
+ */
101
+ get disposed () {
102
+ return this._disposed
103
+ }
104
+
93
105
  /**
94
106
  * The signer's absolute derivation path.
95
107
  *
@@ -130,10 +142,14 @@ export default class SeedSignerEvm {
130
142
  *
131
143
  * @param {string} relPath - The path segment to derive, relative to this signer's own path.
132
144
  * @returns {Promise<SeedSignerEvm>} The derived child signer.
145
+ * @throws {DisposalError} If the signer has been disposed.
133
146
  */
134
147
  async derive (relPath) {
148
+ this._guardDisposed()
149
+
135
150
  const signer = Object.create(SeedSignerEvm.prototype)
136
151
  signer._account = this._account.derivePath(relPath)
152
+ signer._disposed = false
137
153
  return signer
138
154
  }
139
155
 
@@ -151,8 +167,11 @@ export default class SeedSignerEvm {
151
167
  *
152
168
  * @param {string} message - The message to sign.
153
169
  * @returns {Promise<string>} The message's signature.
170
+ * @throws {DisposalError} If the signer has been disposed.
154
171
  */
155
172
  async sign (message) {
173
+ this._guardDisposed()
174
+
156
175
  return this._account.signMessage(message)
157
176
  }
158
177
 
@@ -161,8 +180,11 @@ export default class SeedSignerEvm {
161
180
  *
162
181
  * @param {TransactionLike} tx - The transaction to sign.
163
182
  * @returns {Promise<string>} The signed transaction as a hex string.
183
+ * @throws {DisposalError} If the signer has been disposed.
164
184
  */
165
185
  async signTransaction (tx) {
186
+ this._guardDisposed()
187
+
166
188
  return this._account.signTransaction(tx)
167
189
  }
168
190
 
@@ -171,8 +193,11 @@ export default class SeedSignerEvm {
171
193
  *
172
194
  * @param {TypedData} typedData - The typed data to sign.
173
195
  * @returns {Promise<string>} The typed data signature.
196
+ * @throws {DisposalError} If the signer has been disposed.
174
197
  */
175
198
  async signTypedData ({ domain, types, message }) {
199
+ this._guardDisposed()
200
+
176
201
  return this._account.signTypedData(domain, types, message)
177
202
  }
178
203
 
@@ -181,8 +206,11 @@ export default class SeedSignerEvm {
181
206
  *
182
207
  * @param {AuthorizationRequest} auth - The authorization request.
183
208
  * @returns {Promise<Authorization>} The signed authorization.
209
+ * @throws {DisposalError} If the signer has been disposed.
184
210
  */
185
211
  async signAuthorization (auth) {
212
+ this._guardDisposed()
213
+
186
214
  return this._account.authorizeSync(auth)
187
215
  }
188
216
 
@@ -190,6 +218,16 @@ export default class SeedSignerEvm {
190
218
  * Disposes the signer, erasing its secrets from memory.
191
219
  */
192
220
  dispose () {
221
+ if (this._disposed) return
222
+
193
223
  this._account.dispose()
224
+ this._disposed = true
225
+ }
226
+
227
+ /** @private */
228
+ _guardDisposed () {
229
+ if (this.disposed) {
230
+ throw new DisposalError('The signer has been disposed.')
231
+ }
194
232
  }
195
233
  }
@@ -16,7 +16,7 @@
16
16
 
17
17
  import { Contract, VoidSigner, Transaction, ZeroAddress } from 'ethers'
18
18
 
19
- import { AssertionError, MaximumFeeExceededError, ProviderRequiredError, ValueError } from '@tetherto/wdk-wallet'
19
+ import { AssertionError, DisposalError, MaximumFeeExceededError, ProviderRequiredError, ValueError } from '@tetherto/wdk-wallet'
20
20
 
21
21
  import WalletAccountReadOnlyEvm from './wallet-account-read-only-evm.js'
22
22
 
@@ -127,6 +127,9 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
127
127
 
128
128
  /** @private */
129
129
  this._signer = signer
130
+
131
+ /** @private */
132
+ this._disposed = false
130
133
  }
131
134
 
132
135
  /**
@@ -152,6 +155,20 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
152
155
  return this._signer.keyPair
153
156
  }
154
157
 
158
+ /**
159
+ * True once this account's own {@link dispose} method has been called.
160
+ *
161
+ * This reflects the account's lifecycle only. A caller-supplied signer disposed externally
162
+ * does not flip this flag -- the account is still considered alive until it is disposed itself,
163
+ * even though its signer-dependent methods will throw a {@link DisposalError} (propagated from
164
+ * the disposed signer) in the meantime.
165
+ *
166
+ * @type {boolean}
167
+ */
168
+ get disposed () {
169
+ return this._disposed
170
+ }
171
+
155
172
  /**
156
173
  * Creates a new evm wallet account from a raw private key.
157
174
  *
@@ -178,8 +195,11 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
178
195
  *
179
196
  * @param {string} message - The message to sign.
180
197
  * @returns {Promise<string>} The message's signature.
198
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
181
199
  */
182
200
  async sign (message) {
201
+ this._guardDisposed()
202
+
183
203
  return await this._signer.sign(message)
184
204
  }
185
205
 
@@ -188,8 +208,11 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
188
208
  *
189
209
  * @param {TypedData} typedData - The typed data to sign.
190
210
  * @returns {Promise<string>} The typed data signature.
211
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
191
212
  */
192
213
  async signTypedData ({ domain, types, message }) {
214
+ this._guardDisposed()
215
+
193
216
  return await this._signer.signTypedData({ domain, types, message })
194
217
  }
195
218
 
@@ -202,8 +225,11 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
202
225
  * @returns {Promise<string>} The signed transaction as a hex string.
203
226
  * @throws {ValueError} If the transaction is an EIP-4844 (type 3) blob transaction.
204
227
  * @throws {MaximumFeeExceededError} If a provider is set, and the transaction's cost surpasses the transaction max. fee option.
228
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
205
229
  */
206
230
  async signTransaction (tx) {
231
+ this._guardDisposed()
232
+
207
233
  if (WalletAccountReadOnlyEvm._isBlobTransaction(tx)) {
208
234
  throw new ValueError('eip-4844 blob transactions are not supported')
209
235
  }
@@ -225,32 +251,63 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
225
251
  * Sends a transaction.
226
252
  *
227
253
  * @param {EvmTransaction | string} tx - The transaction, or a signed raw transaction as a hex string.
228
- * @returns {Promise<TransactionResult>} The transaction's result.
254
+ * @returns {Promise<TransactionResult>} The transaction's result; the fee is the maximum cost of the transaction as signed.
229
255
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
230
256
  * @throws {MaximumFeeExceededError} If the transaction's cost exceeds the maximum transaction fee option.
231
257
  * @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or if it is an EIP-4844 (type 3) blob transaction.
258
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
259
+ * @throws {Error} If the simulation of the transaction reverts, as an ethers error with code `CALL_EXCEPTION`.
232
260
  */
233
261
  async sendTransaction (tx) {
262
+ this._guardDisposed()
263
+
234
264
  if (!this._provider) {
235
265
  throw new ProviderRequiredError('The wallet must be connected to a provider to send transactions.')
236
266
  }
237
267
 
238
- const { fee } = await this.quoteSendTransaction(tx)
239
- if (this._config.transactionMaxFee !== undefined && fee > this._config.transactionMaxFee) {
240
- throw new MaximumFeeExceededError('Exceeded maximum fee cost for transaction operation.')
241
- }
268
+ const maxFeeExceededErrorMessage = 'Exceeded maximum fee cost for transaction operation.'
242
269
 
243
270
  if (typeof tx === 'string') {
271
+ const { fee } = await this.quoteSendTransaction(tx)
272
+
273
+ this._assertMaxFee(fee, this._config.transactionMaxFee, maxFeeExceededErrorMessage)
274
+
244
275
  const hash = await this._provider.send('eth_sendRawTransaction', [tx])
245
276
  return { hash, fee }
246
277
  }
247
278
 
248
- // Build, sign and broadcast raw transaction using the signer
279
+ return await this._sendPopulated(tx, this._config.transactionMaxFee, maxFeeExceededErrorMessage)
280
+ }
281
+
282
+ /** @private */
283
+ _assertMaxFee (fee, maxFee, maxFeeExceededErrorMessage) {
284
+ if (maxFee !== undefined && fee > maxFee) {
285
+ throw new MaximumFeeExceededError(maxFeeExceededErrorMessage)
286
+ }
287
+ }
288
+
289
+ /** @private */
290
+ static _getMaxFee (tx) {
291
+ return BigInt(tx.gasLimit) * BigInt(tx.maxFeePerGas ?? tx.gasPrice)
292
+ }
293
+
294
+ /** @private */
295
+ async _sendPopulated (tx, maxFee, maxFeeExceededErrorMessage) {
296
+ WalletAccountReadOnlyEvm._validateFeeFields(tx)
297
+
249
298
  const address = await this.getAddress()
299
+
300
+ const gasLimit = await this._estimateGas({ from: address, ...tx })
301
+
250
302
  const voidSigner = new VoidSigner(address, this._provider)
251
- const populated = await voidSigner.populateTransaction(tx)
303
+ const populated = await voidSigner.populateTransaction({ ...tx, gasLimit: tx.gasLimit ?? gasLimit })
304
+ const fee = WalletAccountEvm._getMaxFee(populated)
305
+
306
+ this._assertMaxFee(fee, maxFee, maxFeeExceededErrorMessage)
307
+
252
308
  const signed = await this._signer.signTransaction(populated)
253
309
  const hash = await this._provider.send('eth_sendRawTransaction', [signed])
310
+
254
311
  return { hash, fee }
255
312
  }
256
313
 
@@ -285,7 +342,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
285
342
 
286
343
  await this._estimateGas({ from, to, value, data, gasLimit, gasPrice, maxFeePerGas, maxPriorityFeePerGas, type, nonce, chainId, authorizationList })
287
344
 
288
- return { fee: gasLimit * (maxFeePerGas ?? gasPrice) }
345
+ return { fee: WalletAccountEvm._getMaxFee(parsedTx) }
289
346
  }
290
347
 
291
348
  return await super.quoteSendTransaction(tx)
@@ -298,23 +355,20 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
298
355
  * @returns {Promise<TransferResult>} The transfer's result.
299
356
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
300
357
  * @throws {MaximumFeeExceededError} If the transfer's cost exceeds the maximum transfer fee option.
358
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
359
+ * @throws {ValueError} If the options mix gas override fields that the transaction's type doesn't support.
360
+ * @throws {Error} If the simulation of the transfer reverts, as an ethers error with code `CALL_EXCEPTION`.
301
361
  */
302
362
  async transfer (options) {
363
+ this._guardDisposed()
364
+
303
365
  if (!this._provider) {
304
366
  throw new ProviderRequiredError('The wallet must be connected to a provider to transfer tokens.')
305
367
  }
306
368
 
307
369
  const tx = await WalletAccountEvm._getTransferTransaction(options)
308
370
 
309
- const { fee } = await this.quoteSendTransaction(tx)
310
-
311
- if (this._config.transferMaxFee !== undefined && fee > this._config.transferMaxFee) {
312
- throw new MaximumFeeExceededError('Exceeded maximum fee cost for transfer operation.')
313
- }
314
-
315
- const { hash } = await this.sendTransaction(tx)
316
-
317
- return { hash, fee }
371
+ return await this._sendPopulated(tx, this._config.transferMaxFee, 'Exceeded maximum fee cost for transfer operation.')
318
372
  }
319
373
 
320
374
  /**
@@ -324,8 +378,11 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
324
378
  * @returns {Promise<TransactionResult>} The transaction's result.
325
379
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
326
380
  * @throws {ValueError} If trying to approve usdts on ethereum with allowance not equal to zero (due to the usdt allowance reset requirement).
381
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
327
382
  */
328
383
  async approve (options) {
384
+ this._guardDisposed()
385
+
329
386
  if (!this._provider) {
330
387
  throw new ProviderRequiredError('The wallet must be connected to a provider to approve funds.')
331
388
  }
@@ -371,19 +428,28 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
371
428
  /**
372
429
  * Signs an ERC-7702 authorization tuple.
373
430
  *
374
- * The chainId and nonce are populated from the provider when not explicitly provided.
431
+ * The chainId is taken from the request, else the wallet config, else the provider; the nonce from the request,
432
+ * else the account's pending nonce from the provider.
375
433
  *
376
434
  * A chainId of 0 makes the authorization valid on every chain, so it is only signed when given explicitly, never
377
- * when reported by the provider.
435
+ * when reported by the provider or set on the wallet config.
378
436
  *
379
437
  * @see https://eips.ethereum.org/EIPS/eip-7702
380
438
  * @param {AuthorizationRequest} auth - The authorization request.
381
439
  * @returns {Promise<Authorization>} The signed authorization.
382
- * @throws {ProviderRequiredError} If the chainId or nonce are not provided and the wallet is not connected to a provider.
383
- * @throws {AssertionError} If the chainId is not provided and the provider reports a chain id of 0.
440
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
441
+ * @throws {ProviderRequiredError} If the wallet is not connected to a provider and a value must come from it: the nonce, or the chainId when none is configured.
442
+ * @throws {AssertionError} If no chainId is provided or configured and the provider reports a chain id of 0.
384
443
  */
385
444
  async signAuthorization (auth) {
445
+ this._guardDisposed()
446
+
386
447
  const populated = { ...auth }
448
+
449
+ if (populated.chainId == null && this._config.chainId) {
450
+ populated.chainId = this._config.chainId
451
+ }
452
+
387
453
  if (populated.chainId == null || populated.nonce == null) {
388
454
  if (!this._provider) {
389
455
  throw new ProviderRequiredError('The wallet must be connected to a provider to populate the authorization chainId and nonce. Provide them explicitly to sign offline.')
@@ -397,7 +463,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
397
463
  }
398
464
  if (populated.nonce == null) {
399
465
  const address = await this.getAddress()
400
- populated.nonce = await this._provider.getTransactionCount(address)
466
+ populated.nonce = await this._provider.getTransactionCount(address, 'pending')
401
467
  }
402
468
  }
403
469
  return await this._signer.signAuthorization(populated)
@@ -410,18 +476,38 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
410
476
  * A fixed gas limit is used because `eth_estimateGas` may revert when
411
477
  * the delegate contract lacks a `receive`/`fallback` function.
412
478
  *
479
+ * The chain id is taken from the wallet config, else the provider. When the config also passes a pre-built provider,
480
+ * the configured chain id is checked against that provider's network before anything is signed.
481
+ *
482
+ * If a transaction from the account is still pending, the delegation is sent after it. Nodes that allow a delegated
483
+ * account only one in-flight transaction reject a revocation or re-delegation until that transaction is mined.
484
+ *
413
485
  * @param {string} delegateAddress - The address of the contract to delegate to.
414
486
  * @returns {Promise<TransactionResult>} The transaction result.
415
487
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
416
- * @throws {AssertionError} If the provider reports a chain id of 0.
488
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
489
+ * @throws {AssertionError} If the provider is on a different chain than the configured one.
490
+ * @throws {AssertionError} If no chain id is configured and the provider reports a chain id of 0.
417
491
  */
418
492
  async delegate (delegateAddress) {
493
+ this._guardDisposed()
494
+
419
495
  if (!this._provider) {
420
496
  throw new ProviderRequiredError('The wallet must be connected to a provider to delegate.')
421
497
  }
422
498
 
423
499
  const address = await this.getAddress()
424
- const nonceHex = await this._provider.send('eth_getTransactionCount', [address, 'latest'])
500
+ const chainId = this._config.chainId || undefined
501
+
502
+ if (chainId !== undefined) {
503
+ const network = await this._provider.getNetwork()
504
+
505
+ if (network.chainId !== BigInt(chainId)) {
506
+ throw new AssertionError(`The provider is on chain ${network.chainId}, not on chain ${chainId}.`)
507
+ }
508
+ }
509
+
510
+ const nonceHex = await this._provider.send('eth_getTransactionCount', [address, 'pending'])
425
511
  const nonce = Number(nonceHex)
426
512
 
427
513
  const auth = await this.signAuthorization({
@@ -431,6 +517,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
431
517
 
432
518
  return await this.sendTransaction({
433
519
  type: 4,
520
+ chainId,
434
521
  nonce,
435
522
  to: address,
436
523
  value: 0,
@@ -444,18 +531,33 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
444
531
  *
445
532
  * @returns {Promise<TransactionResult>} The transaction result.
446
533
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
447
- * @throws {AssertionError} If the provider reports a chain id of 0.
534
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
535
+ * @throws {AssertionError} If the provider is on a different chain than the configured one.
536
+ * @throws {AssertionError} If no chain id is configured and the provider reports a chain id of 0.
448
537
  */
449
538
  async revokeDelegation () {
450
539
  return await this.delegate(ZeroAddress)
451
540
  }
452
541
 
453
542
  /**
454
- * Disposes the wallet account, erasing the private key from the memory.
543
+ * Disposes the wallet account. Also wipes the signer if the account owns it (i.e. for an
544
+ * account created from a seed or private key, or when `shouldWipeSignerOnDisposal` was set in
545
+ * the config); a caller-supplied signer is left untouched otherwise.
455
546
  */
456
547
  dispose () {
548
+ if (this._disposed) return
549
+
457
550
  if (this._shouldWipeSignerOnDisposal) {
458
551
  this._signer.dispose()
459
552
  }
553
+
554
+ this._disposed = true
555
+ }
556
+
557
+ /** @private */
558
+ _guardDisposed () {
559
+ if (this.disposed) {
560
+ throw new DisposalError('The account has been disposed.')
561
+ }
460
562
  }
461
563
  }
@@ -14,7 +14,7 @@
14
14
 
15
15
  'use strict'
16
16
 
17
- import WalletManager, { InvalidSignerError, ProviderRequiredError } from '@tetherto/wdk-wallet'
17
+ import WalletManager, { DisposalError, InvalidSignerError, ProviderRequiredError } from '@tetherto/wdk-wallet'
18
18
 
19
19
  import WalletAccountEvm from './wallet-account-evm.js'
20
20
  import SeedSignerEvm, { BIP_44_ETH_DERIVATION_PATH_PREFIX } from './signers/seed-signer-evm.js'
@@ -118,6 +118,7 @@ export default class WalletManagerEvm extends WalletManager {
118
118
  * @returns {Promise<WalletAccountEvm>} The account.
119
119
  * @throws {NoSuchElementError} If a signer name is given but no signer exists with that name.
120
120
  * @throws {InvalidSignerError} If the signer doesn't support account derivation.
121
+ * @throws {DisposalError} If the wallet manager or the given signer has been disposed.
121
122
  */
122
123
 
123
124
  /**
@@ -140,15 +141,23 @@ export default class WalletManagerEvm extends WalletManager {
140
141
  * @param {string} signerName - The signer name registered via {@link addSigner}.
141
142
  * @returns {Promise<WalletAccountEvm>} The account.
142
143
  * @throws {NoSuchElementError} If no signer exists with the given name.
144
+ * @throws {DisposalError} If the wallet manager or the given signer has been disposed.
143
145
  */
144
146
 
145
147
  async getAccount (indexOrSignerName = 0, options = {}) {
146
148
  if (typeof indexOrSignerName === 'string') {
149
+ if (this.disposed) {
150
+ throw new DisposalError('The wallet manager has been disposed.')
151
+ }
152
+
147
153
  const key = indexOrSignerName
148
154
  if (this._accounts[key]) {
149
155
  return this._accounts[key]
150
156
  }
151
157
  const signer = this.getSigner(indexOrSignerName)
158
+ if (signer.disposed) {
159
+ throw new DisposalError('The given signer has been disposed.')
160
+ }
152
161
  const account = new WalletAccountEvm(signer, this._accountConfig())
153
162
  this._accounts[key] = account
154
163
  return account
@@ -167,14 +176,22 @@ export default class WalletManagerEvm extends WalletManager {
167
176
  * @returns {Promise<WalletAccountEvm>} The account.
168
177
  * @throws {NoSuchElementError} If a signer name is given but no signer exists with that name.
169
178
  * @throws {InvalidSignerError} If the signer doesn't support account derivation.
179
+ * @throws {DisposalError} If the wallet manager or the given signer has been disposed.
170
180
  */
171
181
  async getAccountByPath (path, options = {}) {
182
+ if (this.disposed) {
183
+ throw new DisposalError('The wallet manager has been disposed.')
184
+ }
185
+
172
186
  const { signerName } = options
173
187
  const key = signerName ? `${signerName}:${path}` : path
174
188
  if (this._accounts[key]) {
175
189
  return this._accounts[key]
176
190
  }
177
191
  const signer = this.getSigner(signerName)
192
+ if (signer.disposed) {
193
+ throw new DisposalError('The given signer has been disposed.')
194
+ }
178
195
  if (!signer.isDerivable) {
179
196
  throw new InvalidSignerError('The signer does not support account derivation.')
180
197
  }
@@ -22,12 +22,20 @@ export default class PrivateKeySignerEvm implements ISignerEvm {
22
22
  private _wallet;
23
23
  /** @private */
24
24
  private _address;
25
+ /** @private */
26
+ private _disposed;
25
27
  /**
26
28
  * Whether this signer can derive child signers.
27
29
  *
28
30
  * @type {false}
29
31
  */
30
32
  get isDerivable(): false;
33
+ /**
34
+ * True if the signer has been disposed.
35
+ *
36
+ * @type {boolean}
37
+ */
38
+ get disposed(): boolean;
31
39
  /**
32
40
  * The BIP 0044 derivation path.
33
41
  *
@@ -53,6 +61,7 @@ export default class PrivateKeySignerEvm implements ISignerEvm {
53
61
  *
54
62
  * @param {string} path - The relative derivation path.
55
63
  * @returns {Promise<never>} The derived signer.
64
+ * @throws {DisposalError} If the signer has been disposed.
56
65
  * @throws {UnsupportedOperationError} If the signer does not support account derivation.
57
66
  * @throws {ValueError} If the path is not valid.
58
67
  */
@@ -68,6 +77,7 @@ export default class PrivateKeySignerEvm implements ISignerEvm {
68
77
  *
69
78
  * @param {string} message - The message to sign.
70
79
  * @returns {Promise<string>} The message's signature.
80
+ * @throws {DisposalError} If the signer has been disposed.
71
81
  */
72
82
  sign(message: string): Promise<string>;
73
83
  /**
@@ -75,6 +85,7 @@ export default class PrivateKeySignerEvm implements ISignerEvm {
75
85
  *
76
86
  * @param {TransactionLike} tx - The transaction to sign.
77
87
  * @returns {Promise<string>} The signed transaction as a hex string.
88
+ * @throws {DisposalError} If the signer has been disposed.
78
89
  */
79
90
  signTransaction(tx: TransactionLike): Promise<string>;
80
91
  /**
@@ -82,6 +93,7 @@ export default class PrivateKeySignerEvm implements ISignerEvm {
82
93
  *
83
94
  * @param {TypedData} typedData - The typed data to sign.
84
95
  * @returns {Promise<string>} The typed data signature.
96
+ * @throws {DisposalError} If the signer has been disposed.
85
97
  */
86
98
  signTypedData(typedData: TypedData): Promise<string>;
87
99
  /**
@@ -89,12 +101,15 @@ export default class PrivateKeySignerEvm implements ISignerEvm {
89
101
  *
90
102
  * @param {AuthorizationRequest} auth - The authorization request.
91
103
  * @returns {Promise<Authorization>} The signed authorization.
104
+ * @throws {DisposalError} If the signer has been disposed.
92
105
  */
93
106
  signAuthorization(auth: AuthorizationRequest): Promise<Authorization>;
94
107
  /**
95
108
  * Disposes the signer, securely erasing its internal copy of the private key from memory.
96
109
  */
97
110
  dispose(): void;
111
+ /** @private */
112
+ private _guardDisposed;
98
113
  }
99
114
  export type ISignerEvm = import("./signer-evm.js").ISignerEvm;
100
115
  export type KeyPair = import("@tetherto/wdk-wallet").KeyPair;
@@ -18,6 +18,8 @@ export default class SeedSignerEvm implements ISignerEvm {
18
18
  constructor(seed: string | Uint8Array, path?: string);
19
19
  /** @private */
20
20
  private _account;
21
+ /** @private */
22
+ private _disposed;
21
23
  /**
22
24
  * Whether this signer can derive child signers. Always true: every seed signer holds an
23
25
  * HD node with a private key and can derive below its own path.
@@ -25,6 +27,12 @@ export default class SeedSignerEvm implements ISignerEvm {
25
27
  * @type {true}
26
28
  */
27
29
  get isDerivable(): true;
30
+ /**
31
+ * True if the signer has been disposed.
32
+ *
33
+ * @type {boolean}
34
+ */
35
+ get disposed(): boolean;
28
36
  /**
29
37
  * The signer's absolute derivation path.
30
38
  *
@@ -53,6 +61,7 @@ export default class SeedSignerEvm implements ISignerEvm {
53
61
  *
54
62
  * @param {string} relPath - The path segment to derive, relative to this signer's own path.
55
63
  * @returns {Promise<SeedSignerEvm>} The derived child signer.
64
+ * @throws {DisposalError} If the signer has been disposed.
56
65
  */
57
66
  derive(relPath: string): Promise<SeedSignerEvm>;
58
67
  /**
@@ -66,6 +75,7 @@ export default class SeedSignerEvm implements ISignerEvm {
66
75
  *
67
76
  * @param {string} message - The message to sign.
68
77
  * @returns {Promise<string>} The message's signature.
78
+ * @throws {DisposalError} If the signer has been disposed.
69
79
  */
70
80
  sign(message: string): Promise<string>;
71
81
  /**
@@ -73,6 +83,7 @@ export default class SeedSignerEvm implements ISignerEvm {
73
83
  *
74
84
  * @param {TransactionLike} tx - The transaction to sign.
75
85
  * @returns {Promise<string>} The signed transaction as a hex string.
86
+ * @throws {DisposalError} If the signer has been disposed.
76
87
  */
77
88
  signTransaction(tx: TransactionLike): Promise<string>;
78
89
  /**
@@ -80,6 +91,7 @@ export default class SeedSignerEvm implements ISignerEvm {
80
91
  *
81
92
  * @param {TypedData} typedData - The typed data to sign.
82
93
  * @returns {Promise<string>} The typed data signature.
94
+ * @throws {DisposalError} If the signer has been disposed.
83
95
  */
84
96
  signTypedData(typedData: TypedData): Promise<string>;
85
97
  /**
@@ -87,12 +99,15 @@ export default class SeedSignerEvm implements ISignerEvm {
87
99
  *
88
100
  * @param {AuthorizationRequest} auth - The authorization request.
89
101
  * @returns {Promise<Authorization>} The signed authorization.
102
+ * @throws {DisposalError} If the signer has been disposed.
90
103
  */
91
104
  signAuthorization(auth: AuthorizationRequest): Promise<Authorization>;
92
105
  /**
93
106
  * Disposes the signer, erasing its secrets from memory.
94
107
  */
95
108
  dispose(): void;
109
+ /** @private */
110
+ private _guardDisposed;
96
111
  }
97
112
  /**
98
113
  * Memory-safe BIP-32 HD node (implemented by the internal src/memory-safe/hd-node-wallet.js)
@@ -8,6 +8,8 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
8
8
  * @returns {WalletAccountEvm} The wallet account.
9
9
  */
10
10
  static fromPrivateKey(privateKey: string | Uint8Array, config?: EvmWalletConfig): WalletAccountEvm;
11
+ /** @private */
12
+ private static _getMaxFee;
11
13
  /**
12
14
  * Creates a new evm wallet account from a BIP-39 seed, deriving the account's key at the
13
15
  * given BIP-44 path.
@@ -67,6 +69,17 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
67
69
  * @type {KeyPair | null}
68
70
  */
69
71
  get keyPair(): KeyPair | null;
72
+ /**
73
+ * True once this account's own {@link dispose} method has been called.
74
+ *
75
+ * This reflects the account's lifecycle only. A caller-supplied signer disposed externally
76
+ * does not flip this flag -- the account is still considered alive until it is disposed itself,
77
+ * even though its signer-dependent methods will throw a {@link DisposalError} (propagated from
78
+ * the disposed signer) in the meantime.
79
+ *
80
+ * @type {boolean}
81
+ */
82
+ get disposed(): boolean;
70
83
  /**
71
84
  * Returns the account's address.
72
85
  *
@@ -78,6 +91,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
78
91
  *
79
92
  * @param {string} message - The message to sign.
80
93
  * @returns {Promise<string>} The message's signature.
94
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
81
95
  */
82
96
  sign(message: string): Promise<string>;
83
97
  /**
@@ -85,6 +99,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
85
99
  *
86
100
  * @param {TypedData} typedData - The typed data to sign.
87
101
  * @returns {Promise<string>} The typed data signature.
102
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
88
103
  */
89
104
  signTypedData({ domain, types, message }: TypedData): Promise<string>;
90
105
  /**
@@ -96,18 +111,25 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
96
111
  * @returns {Promise<string>} The signed transaction as a hex string.
97
112
  * @throws {ValueError} If the transaction is an EIP-4844 (type 3) blob transaction.
98
113
  * @throws {MaximumFeeExceededError} If a provider is set, and the transaction's cost surpasses the transaction max. fee option.
114
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
99
115
  */
100
116
  signTransaction(tx: EvmTransaction): Promise<string>;
101
117
  /**
102
118
  * Sends a transaction.
103
119
  *
104
120
  * @param {EvmTransaction | string} tx - The transaction, or a signed raw transaction as a hex string.
105
- * @returns {Promise<TransactionResult>} The transaction's result.
121
+ * @returns {Promise<TransactionResult>} The transaction's result; the fee is the maximum cost of the transaction as signed.
106
122
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
107
123
  * @throws {MaximumFeeExceededError} If the transaction's cost exceeds the maximum transaction fee option.
108
124
  * @throws {ValueError} If the transaction mixes fee fields that its type doesn't support, or if it is an EIP-4844 (type 3) blob transaction.
125
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
126
+ * @throws {Error} If the simulation of the transaction reverts, as an ethers error with code `CALL_EXCEPTION`.
109
127
  */
110
128
  sendTransaction(tx: EvmTransaction | string): Promise<TransactionResult>;
129
+ /** @private */
130
+ private _assertMaxFee;
131
+ /** @private */
132
+ private _sendPopulated;
111
133
  /**
112
134
  * Quotes the costs of a send transaction operation.
113
135
  *
@@ -131,6 +153,9 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
131
153
  * @returns {Promise<TransferResult>} The transfer's result.
132
154
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
133
155
  * @throws {MaximumFeeExceededError} If the transfer's cost exceeds the maximum transfer fee option.
156
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
157
+ * @throws {ValueError} If the options mix gas override fields that the transaction's type doesn't support.
158
+ * @throws {Error} If the simulation of the transfer reverts, as an ethers error with code `CALL_EXCEPTION`.
134
159
  */
135
160
  transfer(options: EvmTransferOptions): Promise<TransferResult>;
136
161
  /**
@@ -140,6 +165,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
140
165
  * @returns {Promise<TransactionResult>} The transaction's result.
141
166
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
142
167
  * @throws {ValueError} If trying to approve usdts on ethereum with allowance not equal to zero (due to the usdt allowance reset requirement).
168
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
143
169
  */
144
170
  approve(options: EvmApproveOptions): Promise<TransactionResult>;
145
171
  /**
@@ -151,16 +177,18 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
151
177
  /**
152
178
  * Signs an ERC-7702 authorization tuple.
153
179
  *
154
- * The chainId and nonce are populated from the provider when not explicitly provided.
180
+ * The chainId is taken from the request, else the wallet config, else the provider; the nonce from the request,
181
+ * else the account's pending nonce from the provider.
155
182
  *
156
183
  * A chainId of 0 makes the authorization valid on every chain, so it is only signed when given explicitly, never
157
- * when reported by the provider.
184
+ * when reported by the provider or set on the wallet config.
158
185
  *
159
186
  * @see https://eips.ethereum.org/EIPS/eip-7702
160
187
  * @param {AuthorizationRequest} auth - The authorization request.
161
188
  * @returns {Promise<Authorization>} The signed authorization.
162
- * @throws {ProviderRequiredError} If the chainId or nonce are not provided and the wallet is not connected to a provider.
163
- * @throws {AssertionError} If the chainId is not provided and the provider reports a chain id of 0.
189
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
190
+ * @throws {ProviderRequiredError} If the wallet is not connected to a provider and a value must come from it: the nonce, or the chainId when none is configured.
191
+ * @throws {AssertionError} If no chainId is provided or configured and the provider reports a chain id of 0.
164
192
  */
165
193
  signAuthorization(auth: AuthorizationRequest): Promise<Authorization>;
166
194
  /**
@@ -170,10 +198,18 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
170
198
  * A fixed gas limit is used because `eth_estimateGas` may revert when
171
199
  * the delegate contract lacks a `receive`/`fallback` function.
172
200
  *
201
+ * The chain id is taken from the wallet config, else the provider. When the config also passes a pre-built provider,
202
+ * the configured chain id is checked against that provider's network before anything is signed.
203
+ *
204
+ * If a transaction from the account is still pending, the delegation is sent after it. Nodes that allow a delegated
205
+ * account only one in-flight transaction reject a revocation or re-delegation until that transaction is mined.
206
+ *
173
207
  * @param {string} delegateAddress - The address of the contract to delegate to.
174
208
  * @returns {Promise<TransactionResult>} The transaction result.
175
209
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
176
- * @throws {AssertionError} If the provider reports a chain id of 0.
210
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
211
+ * @throws {AssertionError} If the provider is on a different chain than the configured one.
212
+ * @throws {AssertionError} If no chain id is configured and the provider reports a chain id of 0.
177
213
  */
178
214
  delegate(delegateAddress: string): Promise<TransactionResult>;
179
215
  /**
@@ -181,16 +217,19 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm implement
181
217
  *
182
218
  * @returns {Promise<TransactionResult>} The transaction result.
183
219
  * @throws {ProviderRequiredError} If the wallet is not connected to a provider.
184
- * @throws {AssertionError} If the provider reports a chain id of 0.
220
+ * @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
221
+ * @throws {AssertionError} If the provider is on a different chain than the configured one.
222
+ * @throws {AssertionError} If no chain id is configured and the provider reports a chain id of 0.
185
223
  */
186
224
  revokeDelegation(): Promise<TransactionResult>;
187
225
  /**
188
- * Disposes the wallet account, erasing the private key from the memory. The signer is
189
- * wiped only if the account owns it: always for an account created from a seed, otherwise
190
- * only when `shouldWipeSignerOnDisposal` was set in the config -- a caller-supplied signer
191
- * is left untouched by default.
226
+ * Disposes the wallet account. Also wipes the signer if the account owns it (i.e. for an
227
+ * account created from a seed or private key, or when `shouldWipeSignerOnDisposal` was set in
228
+ * the config); a caller-supplied signer is left untouched otherwise.
192
229
  */
193
230
  dispose(): void;
231
+ /** @private */
232
+ private _guardDisposed;
194
233
  }
195
234
  export type ISignerEvm = import("./signers/signer-evm.js").ISignerEvm;
196
235
  export type HDNodeWallet = import("ethers").HDNodeWallet;
@@ -65,6 +65,7 @@ export default class WalletManagerEvm extends WalletManager<ISignerEvm> {
65
65
  * @returns {Promise<WalletAccountEvm>} The account.
66
66
  * @throws {NoSuchElementError} If a signer name is given but no signer exists with that name.
67
67
  * @throws {InvalidSignerError} If the signer doesn't support account derivation.
68
+ * @throws {DisposalError} If the wallet manager or the given signer has been disposed.
68
69
  */
69
70
  getAccount(index?: number, options?: {
70
71
  signerName?: string;
@@ -88,6 +89,7 @@ export default class WalletManagerEvm extends WalletManager<ISignerEvm> {
88
89
  * @param {string} signerName - The signer name registered via {@link addSigner}.
89
90
  * @returns {Promise<WalletAccountEvm>} The account.
90
91
  * @throws {NoSuchElementError} If no signer exists with the given name.
92
+ * @throws {DisposalError} If the wallet manager or the given signer has been disposed.
91
93
  */
92
94
  getAccount(signerName: string): Promise<WalletAccountEvm>;
93
95
  /**
@@ -99,6 +101,7 @@ export default class WalletManagerEvm extends WalletManager<ISignerEvm> {
99
101
  * @returns {Promise<WalletAccountEvm>} The account.
100
102
  * @throws {NoSuchElementError} If a signer name is given but no signer exists with that name.
101
103
  * @throws {InvalidSignerError} If the signer doesn't support account derivation.
104
+ * @throws {DisposalError} If the wallet manager or the given signer has been disposed.
102
105
  */
103
106
  getAccountByPath(path: string, options?: {
104
107
  signerName?: string;