@tetherto/wdk-wallet-evm-erc-4337 1.0.0-beta.1

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.
@@ -0,0 +1,113 @@
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
+ import WalletManager from '@tetherto/wdk-wallet'
18
+
19
+ import WalletManagerEvm from '@tetherto/wdk-wallet-evm'
20
+
21
+ import { BrowserProvider, JsonRpcProvider } from 'ethers'
22
+
23
+ import WalletAccountEvmErc4337 from './wallet-account-evm-erc-4337.js'
24
+
25
+ /** @typedef {import('ethers').Provider} Provider */
26
+
27
+ /** @typedef {import('@tetherto/wdk-wallet-evm').FeeRates} FeeRates */
28
+
29
+ /** @typedef {import('./wallet-account-evm-erc-4337.js').EvmErc4337WalletConfig} EvmErc4337WalletConfig */
30
+
31
+ export default class WalletManagerEvmErc4337 extends WalletManager {
32
+ /**
33
+ * Creates a new wallet manager for evm blockchains that implements the [erc-4337](https://www.erc4337.io/docs) standard and its account abstraction features.
34
+ *
35
+ * @param {string | Uint8Array} seed - The wallet's [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
36
+ * @param {EvmErc4337WalletConfig} config - The configuration object.
37
+ */
38
+ constructor (seed, config) {
39
+ super(seed, config)
40
+
41
+ /**
42
+ * The evm erc-4337 wallet configuration.
43
+ *
44
+ * @protected
45
+ * @type {EvmErc4337WalletConfig}
46
+ */
47
+ this._config = config
48
+
49
+ const { provider } = config
50
+
51
+ if (provider) {
52
+ /**
53
+ * An ethers provider to interact with a node of the blockchain.
54
+ *
55
+ * @protected
56
+ * @type {Provider | undefined}
57
+ */
58
+ this._provider = typeof provider === 'string'
59
+ ? new JsonRpcProvider(provider)
60
+ : new BrowserProvider(provider)
61
+ }
62
+ }
63
+
64
+ /**
65
+ * Returns the wallet account at a specific index (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
66
+ *
67
+ * @example
68
+ * // Returns the account with derivation path m/44'/60'/0'/0/1
69
+ * const account = await wallet.getAccount(1);
70
+ * @param {number} [index] - The index of the account to get (default: 0).
71
+ * @returns {Promise<WalletAccountEvmErc4337>} The account.
72
+ */
73
+ async getAccount (index = 0) {
74
+ return await this.getAccountByPath(`0'/0/${index}`)
75
+ }
76
+
77
+ /**
78
+ * Returns the wallet account at a specific BIP-44 derivation path.
79
+ *
80
+ * @example
81
+ * // Returns the account with derivation path m/44'/60'/0'/0/1
82
+ * const account = await wallet.getAccountByPath("0'/0/1");
83
+ * @param {string} path - The derivation path (e.g. "0'/0/0").
84
+ * @returns {Promise<WalletAccountEvmErc4337>} The account.
85
+ */
86
+ async getAccountByPath (path) {
87
+ if (!this._accounts[path]) {
88
+ const account = new WalletAccountEvmErc4337(this.seed, path, this._config)
89
+
90
+ this._accounts[path] = account
91
+ }
92
+
93
+ return this._accounts[path]
94
+ }
95
+
96
+ /**
97
+ * Returns the current fee rates.
98
+ *
99
+ * @returns {Promise<FeeRates>} The fee rates (in weis).
100
+ */
101
+ async getFeeRates () {
102
+ if (!this._provider) {
103
+ throw new Error('The wallet must be connected to a provider to get fee rates.')
104
+ }
105
+
106
+ const { maxFeePerGas } = await this._provider.getFeeData()
107
+
108
+ return {
109
+ normal: maxFeePerGas * WalletManagerEvm._FEE_RATE_NORMAL_MULTIPLIER / 100n,
110
+ fast: maxFeePerGas * WalletManagerEvm._FEE_RATE_FAST_MULTIPLIER / 100n
111
+ }
112
+ }
113
+ }
@@ -0,0 +1,11 @@
1
+ export { default } from "./src/wallet-manager-evm-erc-4337.js";
2
+ export { default as WalletAccountReadOnlyEvmErc4337 } from "./src/wallet-account-read-only-evm-erc-4337.js";
3
+ export { default as WalletAccountEvmErc4337 } from "./src/wallet-account-evm-erc-4337.js";
4
+ export type FeeRates = import("@tetherto/wdk-wallet-evm").FeeRates;
5
+ export type KeyPair = import("@tetherto/wdk-wallet-evm").KeyPair;
6
+ export type EvmTransaction = import("@tetherto/wdk-wallet-evm").EvmTransaction;
7
+ export type TransactionResult = import("@tetherto/wdk-wallet-evm").TransactionResult;
8
+ export type TransferOptions = import("@tetherto/wdk-wallet-evm").TransferOptions;
9
+ export type TransferResult = import("@tetherto/wdk-wallet-evm").TransferResult;
10
+ export type EvmTransactionReceipt = import("@tetherto/wdk-wallet-evm").EvmTransactionReceipt;
11
+ export type EvmErc4337WalletConfig = import("./src/wallet-manager-evm-erc-4337.js").EvmErc4337WalletConfig;
@@ -0,0 +1,90 @@
1
+ /** @implements {IWalletAccount} */
2
+ export default class WalletAccountEvmErc4337 extends WalletAccountReadOnlyEvmErc4337 implements IWalletAccount {
3
+ /**
4
+ * Creates a new evm [erc-4337](https://www.erc4337.io/docs) wallet account.
5
+ *
6
+ * @param {string | Uint8Array} seed - The wallet's [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
7
+ * @param {string} path - The BIP-44 derivation path (e.g. "0'/0/0").
8
+ * @param {EvmErc4337WalletConfig} config - The configuration object.
9
+ */
10
+ constructor(seed: string | Uint8Array, path: string, config: EvmErc4337WalletConfig);
11
+ /**
12
+ * The wallet account configuration.
13
+ *
14
+ * @protected
15
+ * @type {EvmErc4337WalletConfig}
16
+ */
17
+ protected _config: EvmErc4337WalletConfig;
18
+ /** @private */
19
+ private _ownerAccount;
20
+ /**
21
+ * The derivation path's index of this account.
22
+ *
23
+ * @type {number}
24
+ */
25
+ get index(): number;
26
+ /**
27
+ * The derivation path of this account (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
28
+ *
29
+ * @type {string}
30
+ */
31
+ get path(): string;
32
+ /**
33
+ * The account's key pair.
34
+ *
35
+ * @type {KeyPair}
36
+ */
37
+ get keyPair(): KeyPair;
38
+ /**
39
+ * Signs a message.
40
+ *
41
+ * @param {string} message - The message to sign.
42
+ * @returns {Promise<string>} The message's signature.
43
+ */
44
+ sign(message: string): Promise<string>;
45
+ /**
46
+ * Verifies a message's signature.
47
+ *
48
+ * @param {string} message - The original message.
49
+ * @param {string} signature - The signature to verify.
50
+ * @returns {Promise<boolean>} True if the signature is valid.
51
+ */
52
+ verify(message: string, signature: string): Promise<boolean>;
53
+ /**
54
+ * Sends a transaction.
55
+ *
56
+ * @param {EvmTransaction | EvmTransaction[]} tx - The transaction, or an array of multiple transactions to send in batch.
57
+ * @param {Pick<EvmErc4337WalletConfig, 'paymasterToken'>} [config] - If set, overrides the 'paymasterToken' option defined in the wallet account configuration.
58
+ * @returns {Promise<TransactionResult>} The transaction's result.
59
+ */
60
+ sendTransaction(tx: EvmTransaction | EvmTransaction[], config?: Pick<EvmErc4337WalletConfig, "paymasterToken">): Promise<TransactionResult>;
61
+ /**
62
+ * Transfers a token to another address.
63
+ *
64
+ * @param {TransferOptions} options - The transfer's options.
65
+ * @param {Pick<EvmErc4337WalletConfig, 'paymasterToken' | 'transferMaxFee'>} [config] - If set, overrides the 'paymasterToken' and 'transferMaxFee' options defined in the wallet account configuration.
66
+ * @returns {Promise<TransferResult>} The transfer's result.
67
+ */
68
+ transfer(options: TransferOptions, config?: Pick<EvmErc4337WalletConfig, "paymasterToken" | "transferMaxFee">): Promise<TransferResult>;
69
+ /**
70
+ * Returns a read-only copy of the account.
71
+ *
72
+ * @returns {Promise<WalletAccountReadOnlyEvmErc4337>} The read-only account.
73
+ */
74
+ toReadOnlyAccount(): Promise<WalletAccountReadOnlyEvmErc4337>;
75
+ /**
76
+ * Disposes the wallet account, erasing the private key from the memory.
77
+ */
78
+ dispose(): void;
79
+ /** @private */
80
+ private _sendUserOperation;
81
+ }
82
+ export type Eip1193Provider = import("ethers").Eip1193Provider;
83
+ export type IWalletAccount = import("@tetherto/wdk-wallet").IWalletAccount;
84
+ export type KeyPair = import("@tetherto/wdk-wallet-evm").KeyPair;
85
+ export type EvmTransaction = import("@tetherto/wdk-wallet-evm").EvmTransaction;
86
+ export type TransactionResult = import("@tetherto/wdk-wallet-evm").TransactionResult;
87
+ export type TransferOptions = import("@tetherto/wdk-wallet-evm").TransferOptions;
88
+ export type TransferResult = import("@tetherto/wdk-wallet-evm").TransferResult;
89
+ export type EvmErc4337WalletConfig = import("./wallet-account-read-only-evm-erc-4337.js").EvmErc4337WalletConfig;
90
+ import WalletAccountReadOnlyEvmErc4337 from './wallet-account-read-only-evm-erc-4337.js';
@@ -0,0 +1,133 @@
1
+ export default class WalletAccountReadOnlyEvmErc4337 extends WalletAccountReadOnly {
2
+ /**
3
+ * Creates a new read-only evm [erc-4337](https://www.erc4337.io/docs) wallet account.
4
+ *
5
+ * @param {string} address - The evm account's address.
6
+ * @param {Omit<EvmErc4337WalletConfig, 'transferMaxFee'>} config - The configuration object.
7
+ */
8
+ constructor(address: string, config: Omit<EvmErc4337WalletConfig, "transferMaxFee">);
9
+ /**
10
+ * The read-only evm erc-4337 wallet account configuration.
11
+ *
12
+ * @protected
13
+ * @type {Omit<EvmErc4337WalletConfig, 'transferMaxFee'>}
14
+ */
15
+ protected _config: Omit<EvmErc4337WalletConfig, "transferMaxFee">;
16
+ /**
17
+ * The safe's implementation of the erc-4337 standard.
18
+ *
19
+ * @protected
20
+ * @type {Safe4337Pack | undefined}
21
+ */
22
+ protected _safe4337Pack: Safe4337Pack | undefined;
23
+ /**
24
+ * The safe's fee estimator.
25
+ *
26
+ * @protected
27
+ * @type {GenericFeeEstimator}
28
+ */
29
+ protected _feeEstimator: GenericFeeEstimator;
30
+ /** @private */
31
+ private _ownerAccountAddress;
32
+ /**
33
+ * Returns the account's eth balance.
34
+ *
35
+ * @returns {Promise<bigint>} The eth balance (in weis).
36
+ */
37
+ getBalance(): Promise<bigint>;
38
+ /**
39
+ * Returns the account balance for a specific token.
40
+ *
41
+ * @param {string} tokenAddress - The smart contract address of the token.
42
+ * @returns {Promise<bigint>} The token balance (in base unit).
43
+ */
44
+ getTokenBalance(tokenAddress: string): Promise<bigint>;
45
+ /**
46
+ * Returns the account's balance for the paymaster token provided in the wallet account configuration.
47
+ *
48
+ * @returns {Promise<bigint>} The paymaster token balance (in base unit).
49
+ */
50
+ getPaymasterTokenBalance(): Promise<bigint>;
51
+ /**
52
+ * Quotes the costs of a send transaction operation.
53
+ *
54
+ * @param {EvmTransaction | EvmTransaction[]} tx - The transaction, or an array of multiple transactions to send in batch.
55
+ * @param {Pick<EvmErc4337WalletConfig, 'paymasterToken'>} [config] - If set, overrides the 'paymasterToken' option defined in the wallet account configuration.
56
+ * @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
57
+ */
58
+ quoteSendTransaction(tx: EvmTransaction | EvmTransaction[], config?: Pick<EvmErc4337WalletConfig, "paymasterToken">): Promise<Omit<TransactionResult, "hash">>;
59
+ /**
60
+ * Quotes the costs of a transfer operation.
61
+ *
62
+ * @param {TransferOptions} options - The transfer's options.
63
+ * @param {Pick<EvmErc4337WalletConfig, 'paymasterToken'>} [config] - If set, overrides the 'paymasterToken' option defined in the wallet account configuration.
64
+ * @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
65
+ */
66
+ quoteTransfer(options: TransferOptions, config?: Pick<EvmErc4337WalletConfig, "paymasterToken">): Promise<Omit<TransferResult, "hash">>;
67
+ /**
68
+ * Returns a transaction's receipt.
69
+ *
70
+ * @param {string} hash - The user operation hash.
71
+ * @returns {Promise<EvmTransactionReceipt | null>} – The receipt, or null if the transaction has not been included in a block yet.
72
+ */
73
+ getTransactionReceipt(hash: string): Promise<EvmTransactionReceipt | null>;
74
+ /**
75
+ * Returns the safe's erc-4337 pack of the account.
76
+ *
77
+ * @protected
78
+ * @returns {Promise<Safe4337Pack>} The safe's erc-4337 pack.
79
+ */
80
+ protected _getSafe4337Pack(): Promise<Safe4337Pack>;
81
+ /** @private */
82
+ private _getEvmReadOnlyAccount;
83
+ /** @private */
84
+ private _getUserOperationGasCost;
85
+ }
86
+ export type Eip1193Provider = import("ethers").Eip1193Provider;
87
+ export type EvmTransaction = import("@tetherto/wdk-wallet-evm").EvmTransaction;
88
+ export type TransactionResult = import("@tetherto/wdk-wallet-evm").TransactionResult;
89
+ export type TransferOptions = import("@tetherto/wdk-wallet-evm").TransferOptions;
90
+ export type TransferResult = import("@tetherto/wdk-wallet-evm").TransferResult;
91
+ export type EvmTransactionReceipt = import("@tetherto/wdk-wallet-evm").EvmTransactionReceipt;
92
+ export type EvmErc4337WalletConfig = {
93
+ /**
94
+ * - The blockchain's id (e.g., 1 for ethereum).
95
+ */
96
+ chainId: number;
97
+ /**
98
+ * - The url of the rpc provider, or an instance of a class that implements eip-1193.
99
+ */
100
+ provider: string | Eip1193Provider;
101
+ /**
102
+ * - The url of the bundler service.
103
+ */
104
+ bundlerUrl: string;
105
+ /**
106
+ * - The url of the paymaster service.
107
+ */
108
+ paymasterUrl: string;
109
+ /**
110
+ * - The address of the paymaster smart contract.
111
+ */
112
+ paymasterAddress: string;
113
+ /**
114
+ * - The address of the entry point smart contract.
115
+ */
116
+ entryPointAddress: string;
117
+ /**
118
+ * - The safe modules version.
119
+ */
120
+ safeModulesVersion: string;
121
+ /**
122
+ * - The paymaster token configuration.
123
+ */
124
+ paymasterToken: {
125
+ address: string;
126
+ };
127
+ /**
128
+ * - The maximum fee amount for transfer operations.
129
+ */
130
+ transferMaxFee?: number | bigint;
131
+ };
132
+ import { WalletAccountReadOnly } from '@tetherto/wdk-wallet';
133
+ import { GenericFeeEstimator, Safe4337Pack } from '@wdk-safe-global/relay-kit';
@@ -0,0 +1,54 @@
1
+ export default class WalletManagerEvmErc4337 extends WalletManager {
2
+ /**
3
+ * Creates a new wallet manager for evm blockchains that implements the [erc-4337](https://www.erc4337.io/docs) standard and its account abstraction features.
4
+ *
5
+ * @param {string | Uint8Array} seed - The wallet's [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
6
+ * @param {EvmErc4337WalletConfig} config - The configuration object.
7
+ */
8
+ constructor(seed: string | Uint8Array, config: EvmErc4337WalletConfig);
9
+ /**
10
+ * The evm erc-4337 wallet configuration.
11
+ *
12
+ * @protected
13
+ * @type {EvmErc4337WalletConfig}
14
+ */
15
+ protected _config: EvmErc4337WalletConfig;
16
+ /**
17
+ * An ethers provider to interact with a node of the blockchain.
18
+ *
19
+ * @protected
20
+ * @type {Provider | undefined}
21
+ */
22
+ protected _provider: Provider | undefined;
23
+ /**
24
+ * Returns the wallet account at a specific index (see [BIP-44](https://github.com/bitcoin/bips/blob/master/bip-0044.mediawiki)).
25
+ *
26
+ * @example
27
+ * // Returns the account with derivation path m/44'/60'/0'/0/1
28
+ * const account = await wallet.getAccount(1);
29
+ * @param {number} [index] - The index of the account to get (default: 0).
30
+ * @returns {Promise<WalletAccountEvmErc4337>} The account.
31
+ */
32
+ getAccount(index?: number): Promise<WalletAccountEvmErc4337>;
33
+ /**
34
+ * Returns the wallet account at a specific BIP-44 derivation path.
35
+ *
36
+ * @example
37
+ * // Returns the account with derivation path m/44'/60'/0'/0/1
38
+ * const account = await wallet.getAccountByPath("0'/0/1");
39
+ * @param {string} path - The derivation path (e.g. "0'/0/0").
40
+ * @returns {Promise<WalletAccountEvmErc4337>} The account.
41
+ */
42
+ getAccountByPath(path: string): Promise<WalletAccountEvmErc4337>;
43
+ /**
44
+ * Returns the current fee rates.
45
+ *
46
+ * @returns {Promise<FeeRates>} The fee rates (in weis).
47
+ */
48
+ getFeeRates(): Promise<FeeRates>;
49
+ }
50
+ export type Provider = import("ethers").Provider;
51
+ export type FeeRates = import("@tetherto/wdk-wallet-evm").FeeRates;
52
+ export type EvmErc4337WalletConfig = import("./wallet-account-evm-erc-4337.js").EvmErc4337WalletConfig;
53
+ import WalletManager from '@tetherto/wdk-wallet';
54
+ import WalletAccountEvmErc4337 from './wallet-account-evm-erc-4337.js';