@tetherto/wdk-wallet-evm-erc-4337 1.0.0-beta.24 → 1.0.0-beta.26
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/README.md +26 -1
- package/index.js +1 -0
- package/package.json +10 -4
- package/src/errors.js +3 -2
- package/src/signers/bare.js +18 -0
- package/src/signers/index.js +19 -0
- package/src/wallet-account-evm-erc-4337.js +183 -40
- package/src/wallet-account-read-only-evm-erc-4337.js +166 -34
- package/src/wallet-manager-evm-erc-4337.js +135 -18
- package/types/index.d.ts +1 -0
- package/types/src/errors.d.ts +2 -1
- package/types/src/signers/index.d.ts +2 -0
- package/types/src/wallet-account-evm-erc-4337.d.ts +89 -15
- package/types/src/wallet-account-read-only-evm-erc-4337.d.ts +55 -6
- package/types/src/wallet-manager-evm-erc-4337.d.ts +60 -7
package/README.md
CHANGED
|
@@ -73,10 +73,35 @@ that authenticate via the URL (e.g. an `apikey` query parameter) keep working un
|
|
|
73
73
|
- **BIP-44 Derivation Paths**: Standard Ethereum derivation (m/44'/60')
|
|
74
74
|
- **Multi-Account Management**: Derive multiple smart accounts from a single seed phrase
|
|
75
75
|
- **ERC-20 Token Support**: Query balances and transfer tokens via UserOperations
|
|
76
|
-
- **Message Signing**: Sign and
|
|
76
|
+
- **Message Signing**: Sign messages and typed data as the Safe (ERC-1271 / SafeMessage), and verify them against the Safe
|
|
77
77
|
- **Per-Call Config Overrides**: Switch gas payment modes on a per-transaction basis
|
|
78
78
|
- **Parallel Nonce Lanes**: Send independent operations concurrently via ERC-4337 two-dimensional nonces (`parallel: true` or a reusable `nonceKey`)
|
|
79
79
|
- **Secure Memory Disposal**: Clear private keys from memory when done
|
|
80
|
+
- **Pluggable Signers**: Use a seed phrase, a derivable signer, or a private-key signer as the safe's owner
|
|
81
|
+
|
|
82
|
+
## Signers
|
|
83
|
+
|
|
84
|
+
The safe's owner can be any evm signer, not just a seed phrase. The signers are re-exported from `@tetherto/wdk-wallet-evm-erc-4337/signers`, so you don't need to install `@tetherto/wdk-wallet-evm` yourself.
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
import WalletManagerEvmErc4337, { WalletAccountEvmErc4337 } from '@tetherto/wdk-wallet-evm-erc-4337'
|
|
88
|
+
import { PrivateKeySignerEvm, SeedSignerEvm } from '@tetherto/wdk-wallet-evm-erc-4337/signers'
|
|
89
|
+
|
|
90
|
+
// The default signer must be derivable.
|
|
91
|
+
const wallet = new WalletManagerEvmErc4337(new SeedSignerEvm(seedPhrase, "m/44'/60'"), config)
|
|
92
|
+
|
|
93
|
+
// Other signers are registered by name.
|
|
94
|
+
wallet.addSigner('treasury', new PrivateKeySignerEvm(privateKey))
|
|
95
|
+
const treasury = await wallet.getAccount('treasury')
|
|
96
|
+
|
|
97
|
+
// Standalone accounts, without a wallet manager.
|
|
98
|
+
const account = WalletAccountEvmErc4337.fromPrivateKey(privateKey, config)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Notes:**
|
|
102
|
+
- A wallet created from a seed wipes its default signer on `dispose()`. Signers you supply are never wiped by the wallet, and an account wipes a signer you give it only with `shouldWipeSignerOnDisposal: true`.
|
|
103
|
+
- The safe's address is predicted from the signer's `getAddress()` on first use, so signers that only provide their address asynchronously (e.g. hardware signers) are supported.
|
|
104
|
+
- `account.path` and `account.keyPair` are `null` for signers without a derivation path or key material.
|
|
80
105
|
|
|
81
106
|
## Concurrent / Parallel Sends (nonce lanes)
|
|
82
107
|
|
package/index.js
CHANGED
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
/** @typedef {import('@tetherto/wdk-wallet-evm').FeeRates} FeeRates */
|
|
27
27
|
|
|
28
28
|
/** @typedef {import('@tetherto/wdk-wallet-evm').KeyPair} KeyPair */
|
|
29
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').SignerOptions} SignerOptions */
|
|
29
30
|
/** @typedef {import('./src/wallet-account-read-only-evm-erc-4337.js').EvmErc4337Transaction} EvmErc4337Transaction */
|
|
30
31
|
/** @typedef {import('./src/wallet-account-read-only-evm-erc-4337.js').EvmErc4337GasOverrides} EvmErc4337GasOverrides */
|
|
31
32
|
/** @typedef {import('./src/wallet-account-read-only-evm-erc-4337.js').EvmErc4337TransferOptions} EvmErc4337TransferOptions */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tetherto/wdk-wallet-evm-erc-4337",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.26",
|
|
4
4
|
"description": "ERC-4337 wallet module for EVM-compatible blockchains in WDK (Wallet Development Kit) by Tether.",
|
|
5
5
|
"homepage": "https://docs.wdk.tether.io/sdk/wallet-modules/wallet-evm-erc-4337/",
|
|
6
6
|
"keywords": [
|
|
@@ -32,9 +32,9 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@tetherto/wdk-failover-provider": "1.0.0-beta.2",
|
|
35
|
-
"@tetherto/wdk-wallet": "1.0.0-beta.
|
|
36
|
-
"@tetherto/wdk-wallet-evm": "1.0.0-beta.
|
|
37
|
-
"abstractionkit": "0.4.
|
|
35
|
+
"@tetherto/wdk-wallet": "1.0.0-beta.25",
|
|
36
|
+
"@tetherto/wdk-wallet-evm": "1.0.0-beta.26",
|
|
37
|
+
"abstractionkit": "0.4.3",
|
|
38
38
|
"bare-node-runtime": "^1.5.2",
|
|
39
39
|
"ethers": "6.17.0"
|
|
40
40
|
},
|
|
@@ -68,6 +68,11 @@
|
|
|
68
68
|
"bare": "./bare.js",
|
|
69
69
|
"default": "./index.js"
|
|
70
70
|
},
|
|
71
|
+
"./signers": {
|
|
72
|
+
"types": "./types/src/signers/index.d.ts",
|
|
73
|
+
"bare": "./src/signers/bare.js",
|
|
74
|
+
"default": "./src/signers/index.js"
|
|
75
|
+
},
|
|
71
76
|
"./package": {
|
|
72
77
|
"default": "./package.json"
|
|
73
78
|
}
|
|
@@ -79,6 +84,7 @@
|
|
|
79
84
|
"standard": {
|
|
80
85
|
"ignore": [
|
|
81
86
|
"bare.js",
|
|
87
|
+
"src/signers/bare.js",
|
|
82
88
|
"tests/**/*.js"
|
|
83
89
|
]
|
|
84
90
|
}
|
package/src/errors.js
CHANGED
|
@@ -26,9 +26,10 @@ export class ConfigurationError extends ValueError {
|
|
|
26
26
|
* Create a new configuration error.
|
|
27
27
|
*
|
|
28
28
|
* @param {string} message - The error message.
|
|
29
|
+
* @param {ErrorOptions} [options] - The error's options.
|
|
29
30
|
*/
|
|
30
|
-
constructor (message) {
|
|
31
|
-
super(message)
|
|
31
|
+
constructor (message, options) {
|
|
32
|
+
super(message, options)
|
|
32
33
|
|
|
33
34
|
this.name = 'ConfigurationError'
|
|
34
35
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Copyright 2024 Tether Operations Limited
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
'use strict'
|
|
15
|
+
|
|
16
|
+
import 'bare-node-runtime/global'
|
|
17
|
+
|
|
18
|
+
export * from './index.js' with { imports: 'bare-node-runtime/imports' }
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// Copyright 2024 Tether Operations Limited
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
'use strict'
|
|
16
|
+
|
|
17
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm/signers').TransactionLike} TransactionLike */
|
|
18
|
+
|
|
19
|
+
export * from '@tetherto/wdk-wallet-evm/signers'
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
|
|
15
15
|
'use strict'
|
|
16
16
|
|
|
17
|
-
import { Contract, hexlify, keccak256, randomBytes, toUtf8Bytes } from 'ethers'
|
|
17
|
+
import { Contract, TypedDataEncoder, hashMessage, hexlify, keccak256, randomBytes, toUtf8Bytes } from 'ethers'
|
|
18
18
|
|
|
19
|
-
import { MaximumFeeExceededError, ProviderRequiredError, TransactionError, TransactionErrorReason, UnsupportedOperationError, ValueError } from '@tetherto/wdk-wallet'
|
|
19
|
+
import { DisposalError, MaximumFeeExceededError, ProviderRequiredError, TransactionError, TransactionErrorReason, UnsupportedOperationError, ValueError } from '@tetherto/wdk-wallet'
|
|
20
20
|
|
|
21
|
-
import {
|
|
21
|
+
import { PrivateKeySignerEvm, SeedSignerEvm } from '@tetherto/wdk-wallet-evm/signers'
|
|
22
22
|
|
|
23
23
|
import { ENTRYPOINT_V7, calculateUserOperationMaxGasCost, fetchAccountNonce } from 'abstractionkit'
|
|
24
24
|
|
|
@@ -40,6 +40,8 @@ import WalletAccountReadOnlyEvmErc4337, { FEE_TOLERANCE_COEFFICIENT } from './wa
|
|
|
40
40
|
/** @typedef {import('@tetherto/wdk-wallet').IWalletAccount} IWalletAccount */
|
|
41
41
|
|
|
42
42
|
/** @typedef {import('@tetherto/wdk-wallet-evm').KeyPair} KeyPair */
|
|
43
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm/signers').ISignerEvm} ISignerEvm */
|
|
44
|
+
/** @typedef {import('@tetherto/wdk-wallet-evm').SignerOptions} SignerOptions */
|
|
43
45
|
|
|
44
46
|
/** @typedef {import('@tetherto/wdk-wallet-evm').TransactionResult} TransactionResult */
|
|
45
47
|
/** @typedef {import('@tetherto/wdk-wallet-evm').TransferResult} TransferResult */
|
|
@@ -68,22 +70,59 @@ const MAX_UINT192 = (1n << 192n) - 1n
|
|
|
68
70
|
|
|
69
71
|
const USDT_MAINNET_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7'
|
|
70
72
|
|
|
73
|
+
const BIP_44_ETH_DERIVATION_PATH_PREFIX = "m/44'/60'"
|
|
74
|
+
|
|
75
|
+
const DEFAULT_ACCOUNT_PATH = "0'/0/0"
|
|
76
|
+
|
|
71
77
|
/** @implements {IWalletAccount<UserOperationV7>} */
|
|
72
78
|
export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc4337 {
|
|
73
79
|
/**
|
|
74
|
-
* Creates a new evm [erc-4337](https://www.erc4337.io/docs) wallet account.
|
|
80
|
+
* Creates a new evm [erc-4337](https://www.erc4337.io/docs) wallet account from a BIP-39 seed.
|
|
81
|
+
*
|
|
82
|
+
* @overload
|
|
83
|
+
* @param {string | Uint8Array} seed - A [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonic seed phrase, or a raw BIP-32 master seed (16-64 bytes).
|
|
84
|
+
* @param {string} path - The BIP-44 derivation path, relative to "m/44'/60'" (e.g. "0'/0/0").
|
|
85
|
+
* @param {EvmErc4337WalletConfig} config - The configuration object.
|
|
86
|
+
* @throws {ValueError} If the given seed phrase is invalid.
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Creates a new evm [erc-4337](https://www.erc4337.io/docs) wallet account from a BIP-39 seed at path m/44'/60'/0'/0/0.
|
|
75
91
|
*
|
|
92
|
+
* @overload
|
|
76
93
|
* @param {string | Uint8Array} seed - A [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) mnemonic seed phrase, or a raw BIP-32 master seed (16-64 bytes).
|
|
77
|
-
* @param {string} path - The BIP-44 derivation path (e.g. "0'/0/0").
|
|
78
94
|
* @param {EvmErc4337WalletConfig} config - The configuration object.
|
|
95
|
+
* @throws {ValueError} If the given seed phrase is invalid.
|
|
79
96
|
*/
|
|
80
|
-
constructor (seed, path, config) {
|
|
81
|
-
// Owner EOA only needs keys/address for 4337; strip `provider` so
|
|
82
|
-
// wdk-wallet-evm does not open a second JsonRpcProvider.
|
|
83
|
-
const { provider: _provider, ...ownerConfig } = config
|
|
84
|
-
const ownerAccount = new WalletAccountEvm(seed, path, ownerConfig)
|
|
85
97
|
|
|
86
|
-
|
|
98
|
+
/**
|
|
99
|
+
* Creates a new evm [erc-4337](https://www.erc4337.io/docs) wallet account using a signer as the safe's owner.
|
|
100
|
+
*
|
|
101
|
+
* The signer is wiped on {@link dispose} only if `shouldWipeSignerOnDisposal` is set. The safe's address is
|
|
102
|
+
* predicted from the signer's address on the first call to {@link getAddress}.
|
|
103
|
+
*
|
|
104
|
+
* @overload
|
|
105
|
+
* @param {ISignerEvm} signer - A signer implementing the EVM signer interface.
|
|
106
|
+
* @param {EvmErc4337WalletConfig & SignerOptions} config - The configuration object.
|
|
107
|
+
*/
|
|
108
|
+
|
|
109
|
+
constructor (seedOrSigner, pathOrConfig, config) {
|
|
110
|
+
const isSeed = typeof seedOrSigner === 'string' || seedOrSigner instanceof Uint8Array
|
|
111
|
+
const hasPath = typeof pathOrConfig === 'string'
|
|
112
|
+
const [signer, configuration] = isSeed
|
|
113
|
+
? [
|
|
114
|
+
new SeedSignerEvm(seedOrSigner, `${BIP_44_ETH_DERIVATION_PATH_PREFIX}/${hasPath ? pathOrConfig : DEFAULT_ACCOUNT_PATH}`),
|
|
115
|
+
hasPath ? config : pathOrConfig
|
|
116
|
+
]
|
|
117
|
+
: [seedOrSigner, pathOrConfig]
|
|
118
|
+
|
|
119
|
+
try {
|
|
120
|
+
super(undefined, configuration)
|
|
121
|
+
} catch (error) {
|
|
122
|
+
if (isSeed) signer.dispose()
|
|
123
|
+
|
|
124
|
+
throw error
|
|
125
|
+
}
|
|
87
126
|
|
|
88
127
|
/**
|
|
89
128
|
* The evm erc-4337 wallet account configuration.
|
|
@@ -91,18 +130,46 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
91
130
|
* @protected
|
|
92
131
|
* @type {EvmErc4337WalletConfig}
|
|
93
132
|
*/
|
|
94
|
-
this._config =
|
|
133
|
+
this._config = configuration
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* If true, disposes the signer on calls to the 'dispose' method.
|
|
137
|
+
*
|
|
138
|
+
* @protected
|
|
139
|
+
* @type {boolean}
|
|
140
|
+
*/
|
|
141
|
+
this._shouldWipeSignerOnDisposal = isSeed || Boolean(configuration.shouldWipeSignerOnDisposal)
|
|
142
|
+
|
|
143
|
+
/** @private */
|
|
144
|
+
this._signer = signer
|
|
95
145
|
|
|
96
146
|
/** @private */
|
|
97
|
-
this.
|
|
147
|
+
this._safeAddress = undefined
|
|
98
148
|
|
|
99
149
|
/**
|
|
100
|
-
* Cached quotes from fee estimations, keyed by serialized transaction.
|
|
150
|
+
* Cached quotes from fee estimations, keyed by serialized transaction and paymaster configuration.
|
|
101
151
|
*
|
|
102
152
|
* @private
|
|
103
153
|
* @type {Map<string, TransactionQuote>}
|
|
104
154
|
*/
|
|
105
155
|
this._quoteCache = new Map()
|
|
156
|
+
|
|
157
|
+
/** @private */
|
|
158
|
+
this._disposed = false
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* True once this account's own {@link dispose} method has been called.
|
|
163
|
+
*
|
|
164
|
+
* This reflects the account's lifecycle only. A caller-supplied signer disposed externally
|
|
165
|
+
* does not flip this flag -- the account is still considered alive until it is disposed itself,
|
|
166
|
+
* even though its signer-dependent methods will throw a {@link DisposalError} (propagated from
|
|
167
|
+
* the disposed signer) in the meantime.
|
|
168
|
+
*
|
|
169
|
+
* @type {boolean}
|
|
170
|
+
*/
|
|
171
|
+
get disposed () {
|
|
172
|
+
return this._disposed
|
|
106
173
|
}
|
|
107
174
|
|
|
108
175
|
/**
|
|
@@ -119,21 +186,34 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
119
186
|
}
|
|
120
187
|
|
|
121
188
|
/**
|
|
122
|
-
*
|
|
189
|
+
* Creates a new evm [erc-4337](https://www.erc4337.io/docs) wallet account whose safe owner is a raw private key.
|
|
190
|
+
*
|
|
191
|
+
* The account owns the key and wipes it on {@link dispose}.
|
|
123
192
|
*
|
|
124
|
-
* @
|
|
193
|
+
* @param {string | Uint8Array} privateKey - The raw private key (hex string with or without 0x, or 32 bytes).
|
|
194
|
+
* @param {EvmErc4337WalletConfig} config - The configuration object.
|
|
195
|
+
* @returns {WalletAccountEvmErc4337} The wallet account.
|
|
125
196
|
*/
|
|
126
|
-
|
|
127
|
-
|
|
197
|
+
static fromPrivateKey (privateKey, config) {
|
|
198
|
+
const signer = new PrivateKeySignerEvm(privateKey)
|
|
199
|
+
|
|
200
|
+
try {
|
|
201
|
+
return new WalletAccountEvmErc4337(signer, { ...config, shouldWipeSignerOnDisposal: true })
|
|
202
|
+
} catch (error) {
|
|
203
|
+
signer.dispose()
|
|
204
|
+
|
|
205
|
+
throw error
|
|
206
|
+
}
|
|
128
207
|
}
|
|
129
208
|
|
|
130
209
|
/**
|
|
131
|
-
* The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki))
|
|
210
|
+
* The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)),
|
|
211
|
+
* or null if the account's signer is not bound to a BIP-44 position (e.g. private-key signers).
|
|
132
212
|
*
|
|
133
|
-
* @type {string}
|
|
213
|
+
* @type {string | null}
|
|
134
214
|
*/
|
|
135
215
|
get path () {
|
|
136
|
-
return this.
|
|
216
|
+
return this._signer.path
|
|
137
217
|
}
|
|
138
218
|
|
|
139
219
|
/**
|
|
@@ -143,30 +223,48 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
143
223
|
* it's strongly recommended to treat the key pair as a read-only view of the keys. While it's still technically possible to alter their
|
|
144
224
|
* content, client code should never do so.
|
|
145
225
|
*
|
|
146
|
-
* @type {KeyPair}
|
|
226
|
+
* @type {KeyPair | null}
|
|
147
227
|
*/
|
|
148
228
|
get keyPair () {
|
|
149
|
-
return this.
|
|
229
|
+
return this._signer.keyPair
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Returns the account's address, which is the safe's address predicted from the signer's address.
|
|
234
|
+
*
|
|
235
|
+
* @returns {Promise<string>} The account's address.
|
|
236
|
+
* @throws {ValueError} If the signer's address is not a well-formed evm address.
|
|
237
|
+
*/
|
|
238
|
+
async getAddress () {
|
|
239
|
+
this._safeAddress ??= WalletAccountReadOnlyEvmErc4337.predictSafeAddress(await this._signer.getAddress(), this._config)
|
|
240
|
+
|
|
241
|
+
return this._safeAddress
|
|
150
242
|
}
|
|
151
243
|
|
|
152
244
|
/**
|
|
153
|
-
* Signs a message.
|
|
245
|
+
* Signs a message as the safe (ERC-1271 / SafeMessage), so the safe's `isValidSignature` accepts it.
|
|
154
246
|
*
|
|
155
247
|
* @param {string} message - The message to sign.
|
|
156
248
|
* @returns {Promise<string>} The message's signature.
|
|
249
|
+
* @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
|
|
157
250
|
*/
|
|
158
251
|
async sign (message) {
|
|
159
|
-
|
|
252
|
+
this._guardDisposed()
|
|
253
|
+
|
|
254
|
+
return await this._signer.signTypedData(await this._getSafeMessageTypedData(hashMessage(message)))
|
|
160
255
|
}
|
|
161
256
|
|
|
162
257
|
/**
|
|
163
|
-
* Signs typed data according to EIP-712.
|
|
258
|
+
* Signs typed data according to EIP-712 as the safe (ERC-1271 / SafeMessage), so the safe's `isValidSignature` accepts it.
|
|
164
259
|
*
|
|
165
260
|
* @param {TypedData} typedData - The typed data to sign.
|
|
166
261
|
* @returns {Promise<string>} The typed data signature.
|
|
262
|
+
* @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
|
|
167
263
|
*/
|
|
168
264
|
async signTypedData ({ domain, types, message }) {
|
|
169
|
-
|
|
265
|
+
this._guardDisposed()
|
|
266
|
+
|
|
267
|
+
return await this._signer.signTypedData(await this._getSafeMessageTypedData(TypedDataEncoder.hash(domain, types, message)))
|
|
170
268
|
}
|
|
171
269
|
|
|
172
270
|
/**
|
|
@@ -181,8 +279,11 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
181
279
|
* @throws {ConfigurationError} If, in token mode, the configured `paymasterAddress` does not match the paymaster address returned by the paymaster RPC. This guards against the auto-generated ERC-20 approval targeting an unexpected paymaster contract.
|
|
182
280
|
* @throws {MaximumFeeExceededError} If the transaction is not sponsored, and the transaction's cost surpasses the transaction max. fee option.
|
|
183
281
|
* @throws {TransactionError} If the paymaster reports AA50 (the account cannot repay the paymaster).
|
|
282
|
+
* @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
|
|
184
283
|
*/
|
|
185
284
|
async signTransaction (tx, config) {
|
|
285
|
+
this._guardDisposed()
|
|
286
|
+
|
|
186
287
|
const mergedConfig = { ...this._config, ...config }
|
|
187
288
|
|
|
188
289
|
if (config) {
|
|
@@ -212,8 +313,11 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
212
313
|
* @returns {Promise<TransactionResult>} - The transaction's result.
|
|
213
314
|
* @throws {ProviderRequiredError} - If the wallet is not connected to a provider.
|
|
214
315
|
* @throws {ValueError} - If trying to approve usdts on ethereum with allowance not equal to zero (due to the usdt allowance reset requirement).
|
|
316
|
+
* @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
|
|
215
317
|
*/
|
|
216
318
|
async approve (options) {
|
|
319
|
+
this._guardDisposed()
|
|
320
|
+
|
|
217
321
|
if (!this._provider) {
|
|
218
322
|
throw new ProviderRequiredError('The wallet must be connected to a provider to approve funds.')
|
|
219
323
|
}
|
|
@@ -249,8 +353,9 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
249
353
|
* Quotes the costs of a send transaction operation.
|
|
250
354
|
*
|
|
251
355
|
* The result is cached internally for up to 2 minutes. A subsequent `sendTransaction` with the
|
|
252
|
-
* same transaction reuses the cached operation — skipping the
|
|
253
|
-
* round-trips — after a lightweight on-chain nonce check, re-quoting
|
|
356
|
+
* same transaction and paymaster configuration reuses the cached operation — skipping the
|
|
357
|
+
* gas-estimation and paymaster round-trips — after a lightweight on-chain nonce check, re-quoting
|
|
358
|
+
* only if the nonce has moved.
|
|
254
359
|
*
|
|
255
360
|
* In a batched call (`tx` passed as `[tx1, tx2, ...]`), only the gas overrides on `tx1` are
|
|
256
361
|
* honored — a UserOperation has a single set of gas fields regardless of how many calls it batches.
|
|
@@ -277,7 +382,7 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
277
382
|
return { fee: mergedConfig.isSponsored ? 0n : WalletAccountEvmErc4337._getSignedUserOperationFee(tx) }
|
|
278
383
|
}
|
|
279
384
|
|
|
280
|
-
const txKey = WalletAccountEvmErc4337._getTxKey(tx)
|
|
385
|
+
const txKey = WalletAccountEvmErc4337._getTxKey(tx, mergedConfig)
|
|
281
386
|
|
|
282
387
|
if (mergedConfig.isSponsored) {
|
|
283
388
|
this._quoteCache.set(txKey, { fee: 0n, createdAt: Date.now() })
|
|
@@ -319,8 +424,11 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
319
424
|
* @throws {MaximumFeeExceededError} If the transaction is not sponsored, and the transaction's cost surpasses the transaction max. fee option.
|
|
320
425
|
* @throws {ValueError} If `nonceKey` is a bigint outside the uint192 range (0 to 2^192 - 1).
|
|
321
426
|
* @throws {TransactionError} If the paymaster reports AA50 (the account cannot repay the paymaster).
|
|
427
|
+
* @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
|
|
322
428
|
*/
|
|
323
429
|
async sendTransaction (tx, config) {
|
|
430
|
+
this._guardDisposed()
|
|
431
|
+
|
|
324
432
|
const mergedConfig = { ...this._config, ...config }
|
|
325
433
|
|
|
326
434
|
if (config) {
|
|
@@ -360,8 +468,11 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
360
468
|
* @throws {MaximumFeeExceededError} If the transaction is not sponsored, and the transfer's cost surpasses the transfer max. fee option.
|
|
361
469
|
* @throws {ValueError} If `nonceKey` is a bigint outside the uint192 range (0 to 2^192 - 1).
|
|
362
470
|
* @throws {TransactionError} If the paymaster reports AA50 (the account cannot repay the paymaster).
|
|
471
|
+
* @throws {DisposalError} If the account, or the externally supplied signer it uses, has been disposed.
|
|
363
472
|
*/
|
|
364
473
|
async transfer (options, config) {
|
|
474
|
+
this._guardDisposed()
|
|
475
|
+
|
|
365
476
|
const mergedConfig = { ...this._config, ...config }
|
|
366
477
|
|
|
367
478
|
if (config) {
|
|
@@ -389,18 +500,41 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
389
500
|
* @returns {Promise<WalletAccountReadOnlyEvmErc4337>} The read-only account.
|
|
390
501
|
*/
|
|
391
502
|
async toReadOnlyAccount () {
|
|
392
|
-
const address = await this.
|
|
503
|
+
const address = await this._getOwnerAddress()
|
|
393
504
|
|
|
394
505
|
const readOnlyAccount = new WalletAccountReadOnlyEvmErc4337(address, this._config)
|
|
395
506
|
|
|
396
507
|
return readOnlyAccount
|
|
397
508
|
}
|
|
398
509
|
|
|
510
|
+
/**
|
|
511
|
+
* Returns the safe owner's address, as given by the signer.
|
|
512
|
+
*
|
|
513
|
+
* @protected
|
|
514
|
+
* @returns {Promise<string>} The owner's address.
|
|
515
|
+
*/
|
|
516
|
+
async _getOwnerAddress () {
|
|
517
|
+
return await this._signer.getAddress()
|
|
518
|
+
}
|
|
519
|
+
|
|
399
520
|
/**
|
|
400
521
|
* Disposes the wallet account, erasing the private key from the memory.
|
|
401
522
|
*/
|
|
402
523
|
dispose () {
|
|
403
|
-
this.
|
|
524
|
+
if (this._disposed) return
|
|
525
|
+
|
|
526
|
+
if (this._shouldWipeSignerOnDisposal) {
|
|
527
|
+
this._signer.dispose()
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
this._disposed = true
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/** @private */
|
|
534
|
+
_guardDisposed () {
|
|
535
|
+
if (this.disposed) {
|
|
536
|
+
throw new DisposalError('The account has been disposed.')
|
|
537
|
+
}
|
|
404
538
|
}
|
|
405
539
|
|
|
406
540
|
/** @private */
|
|
@@ -408,7 +542,7 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
408
542
|
const nonce = await this._resolveNonce(config)
|
|
409
543
|
|
|
410
544
|
if (nonce === undefined) {
|
|
411
|
-
const cached = this._consumeCachedQuote(tx)
|
|
545
|
+
const cached = this._consumeCachedQuote(tx, config)
|
|
412
546
|
if (cached?.userOp) {
|
|
413
547
|
const onChainNonce = await fetchAccountNonce(this._eip1193Provider, cached.smartAccount.entrypointAddress, cached.smartAccount.accountAddress)
|
|
414
548
|
if (cached.userOp.nonce === onChainNonce) {
|
|
@@ -460,7 +594,7 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
460
594
|
throw new ValueError('nonceKey must be within the uint192 range (0 to 2^192 - 1).')
|
|
461
595
|
}
|
|
462
596
|
}
|
|
463
|
-
return await fetchAccountNonce(this._eip1193Provider, ENTRYPOINT_V7, this.
|
|
597
|
+
return await fetchAccountNonce(this._eip1193Provider, ENTRYPOINT_V7, await this.getAddress(), key)
|
|
464
598
|
}
|
|
465
599
|
|
|
466
600
|
if (config.parallel) {
|
|
@@ -471,13 +605,22 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
471
605
|
}
|
|
472
606
|
|
|
473
607
|
/** @private */
|
|
474
|
-
static _getTxKey (tx) {
|
|
475
|
-
return JSON.stringify(
|
|
608
|
+
static _getTxKey (tx, config = {}) {
|
|
609
|
+
return JSON.stringify({
|
|
610
|
+
txs: [tx].flat(),
|
|
611
|
+
useNativeCoins: Boolean(config.useNativeCoins),
|
|
612
|
+
isSponsored: Boolean(config.isSponsored),
|
|
613
|
+
paymasterUrl: config.paymasterUrl,
|
|
614
|
+
paymasterHeaders: config.paymasterHeaders,
|
|
615
|
+
paymasterToken: config.paymasterToken?.address?.toLowerCase(),
|
|
616
|
+
paymasterAddress: config.paymasterAddress?.toLowerCase(),
|
|
617
|
+
sponsorshipPolicyId: config.sponsorshipPolicyId
|
|
618
|
+
}, (_, v) => typeof v === 'bigint' ? v.toString() : v)
|
|
476
619
|
}
|
|
477
620
|
|
|
478
621
|
/** @private */
|
|
479
|
-
_consumeCachedQuote (tx) {
|
|
480
|
-
const txKey = WalletAccountEvmErc4337._getTxKey(tx)
|
|
622
|
+
_consumeCachedQuote (tx, config) {
|
|
623
|
+
const txKey = WalletAccountEvmErc4337._getTxKey(tx, config)
|
|
481
624
|
const quote = this._quoteCache.get(txKey)
|
|
482
625
|
|
|
483
626
|
if (!quote) {
|
|
@@ -504,8 +647,8 @@ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc
|
|
|
504
647
|
)
|
|
505
648
|
|
|
506
649
|
const signer = {
|
|
507
|
-
address: this.
|
|
508
|
-
|
|
650
|
+
address: await this._getOwnerAddress(),
|
|
651
|
+
signTypedData: async (typedData) => await this._signer.signTypedData(typedData)
|
|
509
652
|
}
|
|
510
653
|
userOp.signature = await smartAccount.signUserOperationWithSigners(
|
|
511
654
|
userOp,
|