@tetherto/wdk-wallet-evm 1.0.0-beta.13 → 1.0.0-beta.15
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 +295 -24
- package/index.js +2 -0
- package/package.json +8 -5
- package/src/memory-safe/hd-node-wallet.js +5 -4
- package/src/memory-safe/signing-key.js +4 -3
- package/src/signers/index.js +25 -0
- package/src/signers/private-key-signer-evm.js +156 -0
- package/src/signers/seed-signer-evm.js +315 -0
- package/src/utils/tx-populator-evm.js +187 -0
- package/src/wallet-account-evm.js +94 -56
- package/src/wallet-account-read-only-evm.js +4 -3
- package/src/wallet-manager-evm.js +69 -20
- package/types/index.d.ts +1 -0
- package/types/src/signers/index.d.ts +3 -0
- package/types/src/signers/private-key-signer-evm.d.ts +81 -0
- package/types/src/signers/seed-signer-evm.d.ts +187 -0
- package/types/src/utils/tx-populator-evm.d.ts +84 -0
- package/types/src/wallet-account-evm.d.ts +33 -9
- package/types/src/wallet-account-read-only-evm.d.ts +10 -6
- package/types/src/wallet-manager-evm.d.ts +39 -19
|
@@ -16,12 +16,13 @@
|
|
|
16
16
|
|
|
17
17
|
import { Contract, ZeroAddress } from 'ethers'
|
|
18
18
|
|
|
19
|
-
import * as bip39 from 'bip39'
|
|
20
|
-
|
|
21
19
|
import WalletAccountReadOnlyEvm from './wallet-account-read-only-evm.js'
|
|
22
20
|
|
|
23
|
-
import
|
|
21
|
+
import SeedSignerEvm from './signers/seed-signer-evm.js'
|
|
22
|
+
import PrivateKeySignerEvm from './signers/private-key-signer-evm.js'
|
|
23
|
+
import { populateTransactionEvm } from './utils/tx-populator-evm.js'
|
|
24
24
|
|
|
25
|
+
/** @typedef {import('./signers/seed-signer-evm.js').ISignerEvm} ISignerEvm */
|
|
25
26
|
/** @typedef {import('ethers').HDNodeWallet} HDNodeWallet */
|
|
26
27
|
/** @typedef {import('ethers').AuthorizationRequest} AuthorizationRequest */
|
|
27
28
|
/** @typedef {import('ethers').Authorization} Authorization */
|
|
@@ -45,8 +46,6 @@ import MemorySafeHDNodeWallet from './memory-safe/hd-node-wallet.js'
|
|
|
45
46
|
* @property {number | bigint} amount - The amount of tokens to approve to the spender.
|
|
46
47
|
*/
|
|
47
48
|
|
|
48
|
-
const BIP_44_ETH_DERIVATION_PATH_PREFIX = "m/44'/60'"
|
|
49
|
-
|
|
50
49
|
const USDT_MAINNET_ADDRESS = '0xdAC17F958D2ee523a2206206994597C13D831ec7'
|
|
51
50
|
|
|
52
51
|
const DELEGATION_TX_GAS_LIMIT = 100_000
|
|
@@ -54,27 +53,29 @@ const DELEGATION_TX_GAS_LIMIT = 100_000
|
|
|
54
53
|
/** @implements {IWalletAccount} */
|
|
55
54
|
export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
56
55
|
/**
|
|
57
|
-
* Creates a new evm wallet account
|
|
56
|
+
* Creates a new evm wallet account from a BIP-39 seed, deriving the account's key at the
|
|
57
|
+
* given BIP-44 path.
|
|
58
58
|
*
|
|
59
|
-
* @
|
|
59
|
+
* @overload
|
|
60
|
+
* @param {string | Uint8Array} seed - The wallet's BIP-39 seed phrase or seed bytes.
|
|
60
61
|
* @param {string} path - The BIP-44 derivation path (e.g. "0'/0/0").
|
|
61
62
|
* @param {EvmWalletConfig} [config] - The configuration object.
|
|
62
63
|
*/
|
|
63
|
-
constructor (seed, path, config = {}) {
|
|
64
|
-
if (typeof seed === 'string') {
|
|
65
|
-
if (!bip39.validateMnemonic(seed)) {
|
|
66
|
-
throw new Error('The seed phrase is invalid.')
|
|
67
|
-
}
|
|
68
64
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Creates a new evm wallet account using a signer.
|
|
67
|
+
*
|
|
68
|
+
* @overload
|
|
69
|
+
* @param {ISignerEvm} signer - A signer implementing the EVM signer interface.
|
|
70
|
+
* @param {EvmWalletConfig} [config] - The configuration object.
|
|
71
|
+
*/
|
|
73
72
|
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
constructor (seedOrSigner, pathOrConfig = {}, config = {}) {
|
|
74
|
+
const [signer, configuration] = typeof seedOrSigner === 'string' || seedOrSigner instanceof Uint8Array
|
|
75
|
+
? [new SeedSignerEvm(seedOrSigner, { path: pathOrConfig, isChild: true }), config]
|
|
76
|
+
: [seedOrSigner, pathOrConfig]
|
|
76
77
|
|
|
77
|
-
super(
|
|
78
|
+
super(signer.address, configuration)
|
|
78
79
|
|
|
79
80
|
/**
|
|
80
81
|
* The wallet account configuration.
|
|
@@ -82,19 +83,10 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
82
83
|
* @protected
|
|
83
84
|
* @type {EvmWalletConfig}
|
|
84
85
|
*/
|
|
85
|
-
this._config =
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* The account.
|
|
89
|
-
*
|
|
90
|
-
* @protected
|
|
91
|
-
* @type {HDNodeWallet}
|
|
92
|
-
*/
|
|
93
|
-
this._account = account
|
|
86
|
+
this._config = configuration
|
|
94
87
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
88
|
+
/** @private */
|
|
89
|
+
this._signer = signer
|
|
98
90
|
}
|
|
99
91
|
|
|
100
92
|
/**
|
|
@@ -103,7 +95,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
103
95
|
* @type {number}
|
|
104
96
|
*/
|
|
105
97
|
get index () {
|
|
106
|
-
return this.
|
|
98
|
+
return this._signer.index
|
|
107
99
|
}
|
|
108
100
|
|
|
109
101
|
/**
|
|
@@ -112,7 +104,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
112
104
|
* @type {string}
|
|
113
105
|
*/
|
|
114
106
|
get path () {
|
|
115
|
-
return this.
|
|
107
|
+
return this._signer.path
|
|
116
108
|
}
|
|
117
109
|
|
|
118
110
|
/**
|
|
@@ -125,10 +117,32 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
125
117
|
* @type {KeyPair}
|
|
126
118
|
*/
|
|
127
119
|
get keyPair () {
|
|
128
|
-
return
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
120
|
+
return this._signer.keyPair
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Creates a new evm wallet account from a raw private key.
|
|
125
|
+
*
|
|
126
|
+
* @param {string | Uint8Array} privateKey - The raw private key (hex string with or without 0x, or 32 bytes).
|
|
127
|
+
* @param {EvmWalletConfig} [config] - The configuration object.
|
|
128
|
+
* @returns {WalletAccountEvm} The wallet account.
|
|
129
|
+
*/
|
|
130
|
+
static fromPrivateKey (privateKey, config = {}) {
|
|
131
|
+
const signer = new PrivateKeySignerEvm(privateKey)
|
|
132
|
+
return new WalletAccountEvm(signer, config)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Returns the account's address. If it wasn't resolved at construction time (e.g hardware signers), it asks the
|
|
137
|
+
* underlying signer to resolve it, then caches it locally.
|
|
138
|
+
*
|
|
139
|
+
* @returns {Promise<string>} The account's address.
|
|
140
|
+
*/
|
|
141
|
+
async getAddress () {
|
|
142
|
+
if (this._address) return this._address
|
|
143
|
+
const addr = await this._signer.getAddress()
|
|
144
|
+
this.__address = addr
|
|
145
|
+
return addr
|
|
132
146
|
}
|
|
133
147
|
|
|
134
148
|
/**
|
|
@@ -138,7 +152,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
138
152
|
* @returns {Promise<string>} The message's signature.
|
|
139
153
|
*/
|
|
140
154
|
async sign (message) {
|
|
141
|
-
return await this.
|
|
155
|
+
return await this._signer.sign(message)
|
|
142
156
|
}
|
|
143
157
|
|
|
144
158
|
/**
|
|
@@ -148,17 +162,27 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
148
162
|
* @returns {Promise<string>} The typed data signature.
|
|
149
163
|
*/
|
|
150
164
|
async signTypedData ({ domain, types, message }) {
|
|
151
|
-
return await this.
|
|
165
|
+
return await this._signer.signTypedData({ domain, types, message })
|
|
152
166
|
}
|
|
153
167
|
|
|
154
168
|
/**
|
|
155
169
|
* Signs a transaction.
|
|
156
170
|
*
|
|
171
|
+
* If a provider is set, it also estimates the transaction's costs and checks them against the transaction max. fee option.
|
|
172
|
+
*
|
|
157
173
|
* @param {EvmTransaction} tx - The transaction to sign.
|
|
158
174
|
* @returns {Promise<string>} The signed transaction as a hex string.
|
|
175
|
+
* @throws {Error} If a provider is set, and the transaction's cost surpasses the transaction max. fee option.
|
|
159
176
|
*/
|
|
160
177
|
async signTransaction (tx) {
|
|
161
|
-
|
|
178
|
+
if (this._provider && this._config.transactionMaxFee !== undefined) {
|
|
179
|
+
const { fee } = await this.quoteSendTransaction(tx)
|
|
180
|
+
|
|
181
|
+
if (fee > this._config.transactionMaxFee) {
|
|
182
|
+
throw new Error('Exceeded maximum fee cost for transaction operation.')
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return await this._signer.signTransaction({
|
|
162
186
|
from: await this.getAddress(),
|
|
163
187
|
...tx
|
|
164
188
|
})
|
|
@@ -169,19 +193,21 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
169
193
|
*
|
|
170
194
|
* @param {EvmTransaction} tx - The transaction.
|
|
171
195
|
* @returns {Promise<TransactionResult>} The transaction's result.
|
|
196
|
+
* @throws {Error} If the transaction's cost exceeds the maximum transaction fee option.
|
|
172
197
|
*/
|
|
173
198
|
async sendTransaction (tx) {
|
|
174
|
-
if (!this.
|
|
199
|
+
if (!this._provider) {
|
|
175
200
|
throw new Error('The wallet must be connected to a provider to send transactions.')
|
|
176
201
|
}
|
|
177
|
-
|
|
178
202
|
const { fee } = await this.quoteSendTransaction(tx)
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
203
|
+
if (this._config.transactionMaxFee !== undefined && fee > this._config.transactionMaxFee) {
|
|
204
|
+
throw new Error('Exceeded maximum fee cost for transaction operation.')
|
|
205
|
+
}
|
|
206
|
+
// Build, sign and broadcast raw transaction using the signer
|
|
207
|
+
const from = await this.getAddress()
|
|
208
|
+
const unsignedTx = await populateTransactionEvm(this._provider, from, tx)
|
|
209
|
+
const signed = await this._signer.signTransaction(unsignedTx)
|
|
210
|
+
const hash = await this._provider.send('eth_sendRawTransaction', [signed])
|
|
185
211
|
return { hash, fee }
|
|
186
212
|
}
|
|
187
213
|
|
|
@@ -190,9 +216,10 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
190
216
|
*
|
|
191
217
|
* @param {EvmTransferOptions} options - The transfer's options.
|
|
192
218
|
* @returns {Promise<TransferResult>} The transfer's result.
|
|
219
|
+
* @throws {Error} If the transfer's cost exceeds the maximum transfer fee option.
|
|
193
220
|
*/
|
|
194
221
|
async transfer (options) {
|
|
195
|
-
if (!this.
|
|
222
|
+
if (!this._provider) {
|
|
196
223
|
throw new Error('The wallet must be connected to a provider to transfer tokens.')
|
|
197
224
|
}
|
|
198
225
|
|
|
@@ -200,11 +227,11 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
200
227
|
|
|
201
228
|
const { fee } = await this.quoteSendTransaction(tx)
|
|
202
229
|
|
|
203
|
-
if (this._config.transferMaxFee !== undefined && fee
|
|
230
|
+
if (this._config.transferMaxFee !== undefined && fee > this._config.transferMaxFee) {
|
|
204
231
|
throw new Error('Exceeded maximum fee cost for transfer operation.')
|
|
205
232
|
}
|
|
206
233
|
|
|
207
|
-
const { hash } = await this.
|
|
234
|
+
const { hash } = await this.sendTransaction(tx)
|
|
208
235
|
|
|
209
236
|
return { hash, fee }
|
|
210
237
|
}
|
|
@@ -217,7 +244,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
217
244
|
* @throws {Error} If trying to approve usdts on ethereum with allowance not equal to zero (due to the usdt allowance reset requirement).
|
|
218
245
|
*/
|
|
219
246
|
async approve (options) {
|
|
220
|
-
if (!this.
|
|
247
|
+
if (!this._provider) {
|
|
221
248
|
throw new Error('The wallet must be connected to a provider to approve funds.')
|
|
222
249
|
}
|
|
223
250
|
|
|
@@ -252,7 +279,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
252
279
|
*/
|
|
253
280
|
async toReadOnlyAccount () {
|
|
254
281
|
if (!this._evmReadOnlyAccount) {
|
|
255
|
-
this._evmReadOnlyAccount = new WalletAccountReadOnlyEvm(this.
|
|
282
|
+
this._evmReadOnlyAccount = new WalletAccountReadOnlyEvm(await this.getAddress(), this._config)
|
|
256
283
|
}
|
|
257
284
|
|
|
258
285
|
return this._evmReadOnlyAccount
|
|
@@ -265,7 +292,18 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
265
292
|
* @returns {Promise<Authorization>} The signed authorization.
|
|
266
293
|
*/
|
|
267
294
|
async signAuthorization (auth) {
|
|
268
|
-
|
|
295
|
+
const populated = { ...auth }
|
|
296
|
+
if (this._provider) {
|
|
297
|
+
if (populated.chainId == null) {
|
|
298
|
+
const { chainId } = await this._provider.getNetwork()
|
|
299
|
+
populated.chainId = chainId
|
|
300
|
+
}
|
|
301
|
+
if (populated.nonce == null) {
|
|
302
|
+
const address = await this.getAddress()
|
|
303
|
+
populated.nonce = await this._provider.getTransactionCount(address)
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return await this._signer.signAuthorization(populated)
|
|
269
307
|
}
|
|
270
308
|
|
|
271
309
|
/**
|
|
@@ -279,7 +317,7 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
279
317
|
* @returns {Promise<TransactionResult>} The transaction result.
|
|
280
318
|
*/
|
|
281
319
|
async delegate (delegateAddress) {
|
|
282
|
-
if (!this.
|
|
320
|
+
if (!this._provider) {
|
|
283
321
|
throw new Error('The wallet must be connected to a provider to delegate.')
|
|
284
322
|
}
|
|
285
323
|
|
|
@@ -315,6 +353,6 @@ export default class WalletAccountEvm extends WalletAccountReadOnlyEvm {
|
|
|
315
353
|
* Disposes the wallet account, erasing the private key from the memory.
|
|
316
354
|
*/
|
|
317
355
|
dispose () {
|
|
318
|
-
this.
|
|
356
|
+
this._signer.dispose()
|
|
319
357
|
}
|
|
320
358
|
}
|
|
@@ -47,7 +47,7 @@ import FailoverProvider from '@tetherto/wdk-failover-provider'
|
|
|
47
47
|
|
|
48
48
|
/**
|
|
49
49
|
* @typedef {Object} EvmTransaction
|
|
50
|
-
* @property {string} to - The transaction's recipient.
|
|
50
|
+
* @property {string | null} [to] - The transaction's recipient. Omit or pass null to deploy a contract.
|
|
51
51
|
* @property {number | bigint} value - The amount of ethers to send to the recipient (in weis).
|
|
52
52
|
* @property {string} [data] - The transaction's data in hex format.
|
|
53
53
|
* @property {number | bigint} [gasLimit] - The maximum amount of gas this transaction is permitted to use.
|
|
@@ -74,6 +74,7 @@ import FailoverProvider from '@tetherto/wdk-failover-provider'
|
|
|
74
74
|
* @property {number} [retries] - If set and if 'provider' is a list of urls or EIP 1193 providers, the number of additional retry attempts after the initial call fails. Total attempts = `1 + retries`. For example, `retries: 3` with 4 providers will try each provider once before throwing. If `retries` exceeds the number of providers, the failover will loop back and retry already-failed providers in round-robin order. Default: 3.
|
|
75
75
|
* @property {number} [chainId] - The chain ID of the network. When provided, skips automatic chain ID detection from the provider.
|
|
76
76
|
* @property {number | bigint} [transferMaxFee] - The maximum fee amount for transfer operations.
|
|
77
|
+
* @property {number | bigint} [transactionMaxFee] - The maximum fee amount for sendTransaction and signTransaction operations.
|
|
77
78
|
*/
|
|
78
79
|
|
|
79
80
|
const DELEGATION_DESIGNATOR_PREFIX = '0xef0100'
|
|
@@ -85,7 +86,7 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
|
|
|
85
86
|
* Creates a new evm read-only wallet account.
|
|
86
87
|
*
|
|
87
88
|
* @param {string} address - The account's address.
|
|
88
|
-
* @param {Omit<EvmWalletConfig, 'transferMaxFee'>} [config] - The configuration object.
|
|
89
|
+
* @param {Omit<EvmWalletConfig, 'transferMaxFee' | 'transactionMaxFee'>} [config] - The configuration object.
|
|
89
90
|
*/
|
|
90
91
|
constructor (address, config = { }) {
|
|
91
92
|
super(address)
|
|
@@ -94,7 +95,7 @@ export default class WalletAccountReadOnlyEvm extends WalletAccountReadOnly {
|
|
|
94
95
|
* The read-only wallet account configuration.
|
|
95
96
|
*
|
|
96
97
|
* @protected
|
|
97
|
-
* @type {Omit<EvmWalletConfig, 'transferMaxFee'>}
|
|
98
|
+
* @type {Omit<EvmWalletConfig, 'transferMaxFee' | 'transactionMaxFee'>}
|
|
98
99
|
*/
|
|
99
100
|
this._config = config
|
|
100
101
|
|
|
@@ -21,10 +21,14 @@ import { BrowserProvider, JsonRpcProvider } from 'ethers'
|
|
|
21
21
|
import FailoverProvider from '@tetherto/wdk-failover-provider'
|
|
22
22
|
|
|
23
23
|
import WalletAccountEvm from './wallet-account-evm.js'
|
|
24
|
+
import SeedSignerEvm from './signers/seed-signer-evm.js'
|
|
24
25
|
|
|
26
|
+
/** @typedef {import('./signers/seed-signer-evm.js').ISignerEvm} ISignerEvm */
|
|
25
27
|
/** @typedef {import('ethers').Provider} Provider */
|
|
26
28
|
|
|
27
29
|
/** @typedef {import("@tetherto/wdk-wallet").FeeRates} FeeRates */
|
|
30
|
+
/** @typedef {import("@tetherto/wdk-wallet").ISigner} ISigner */
|
|
31
|
+
/** @typedef {import("@tetherto/wdk-wallet").SignerError} SignerError */
|
|
28
32
|
|
|
29
33
|
/** @typedef {import('./wallet-account-evm.js').EvmWalletConfig} EvmWalletConfig */
|
|
30
34
|
|
|
@@ -48,11 +52,23 @@ export default class WalletManagerEvm extends WalletManager {
|
|
|
48
52
|
/**
|
|
49
53
|
* Creates a new wallet manager for evm blockchains.
|
|
50
54
|
*
|
|
51
|
-
*
|
|
55
|
+
* Accepts either a BIP-39 seed (string/Uint8Array) for backwards compatibility, or a
|
|
56
|
+
* pre-built root signer object. The default signer must be derivable (it must be able to
|
|
57
|
+
* derive child accounts); non-derivable signers (e.g. private-key signers) are not allowed
|
|
58
|
+
* 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.
|
|
59
|
+
*
|
|
60
|
+
* @param {string|Uint8Array|ISigner} seedOrSigner - A BIP-39 seed phrase, seed 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}.
|
|
52
61
|
* @param {EvmWalletConfig} [config] - The configuration object.
|
|
53
62
|
*/
|
|
54
|
-
constructor (
|
|
55
|
-
|
|
63
|
+
constructor (seedOrSigner, config = {}) {
|
|
64
|
+
let signer = seedOrSigner
|
|
65
|
+
if (typeof seedOrSigner === 'string' || seedOrSigner instanceof Uint8Array) {
|
|
66
|
+
signer = new SeedSignerEvm(seedOrSigner)
|
|
67
|
+
}
|
|
68
|
+
if (!signer.isDerivable) {
|
|
69
|
+
throw new Error('The default signer must be derivable. Non-derivable signers (e.g. private-key signers) can only be registered by name via addSigner.')
|
|
70
|
+
}
|
|
71
|
+
super(signer, config)
|
|
56
72
|
|
|
57
73
|
/**
|
|
58
74
|
* The evm wallet configuration.
|
|
@@ -94,35 +110,68 @@ export default class WalletManagerEvm extends WalletManager {
|
|
|
94
110
|
}
|
|
95
111
|
|
|
96
112
|
/**
|
|
97
|
-
* Returns the wallet account at a specific index
|
|
113
|
+
* Returns the wallet account at a specific index.
|
|
98
114
|
*
|
|
99
|
-
* @
|
|
100
|
-
* // Returns the account with derivation path m/44'/60'/0'/0/1
|
|
101
|
-
* const account = await wallet.getAccount(1);
|
|
115
|
+
* @overload
|
|
102
116
|
* @param {number} [index] - The index of the account to get (default: 0).
|
|
117
|
+
* @param {Object} [options] - Account options.
|
|
118
|
+
* @param {string} [options.signerName] - The signer name. Omit to use the default signer.
|
|
103
119
|
* @returns {Promise<WalletAccountEvm>} The account.
|
|
120
|
+
* @throws {Error} If a signer name is given but no signer exists with that name.
|
|
121
|
+
* @throws {SignerError} If the signer doesn't support account derivation.
|
|
104
122
|
*/
|
|
105
|
-
async getAccount (index = 0) {
|
|
106
|
-
return await this.getAccountByPath(`0'/0/${index}`)
|
|
107
|
-
}
|
|
108
123
|
|
|
109
124
|
/**
|
|
110
|
-
* Returns the wallet account
|
|
125
|
+
* Returns the wallet account associated with a registered signer. Non-derivable
|
|
126
|
+
* signers (e.g. private-key signers) return the signer's single account; derivable signers
|
|
127
|
+
* derive a detached child at the signer's own account (the root is never handed out).
|
|
111
128
|
*
|
|
112
|
-
* @
|
|
113
|
-
*
|
|
114
|
-
* const account = await wallet.getAccountByPath("0'/0/1");
|
|
115
|
-
* @param {string} path - The derivation path (e.g. "0'/0/0").
|
|
129
|
+
* @overload
|
|
130
|
+
* @param {string} signerName - The signer name registered via {@link addSigner}.
|
|
116
131
|
* @returns {Promise<WalletAccountEvm>} The account.
|
|
132
|
+
* @throws {Error} If no signer exists with the given name.
|
|
117
133
|
*/
|
|
118
|
-
async getAccountByPath (path) {
|
|
119
|
-
if (!this._accounts[path]) {
|
|
120
|
-
const account = new WalletAccountEvm(this.seed, path, this._config)
|
|
121
134
|
|
|
122
|
-
|
|
135
|
+
async getAccount (indexOrSignerName = 0, options = {}) {
|
|
136
|
+
if (typeof indexOrSignerName === 'string') {
|
|
137
|
+
const key = `${indexOrSignerName}#self`
|
|
138
|
+
if (this._accounts[key]) {
|
|
139
|
+
return this._accounts[key]
|
|
140
|
+
}
|
|
141
|
+
const signer = this.getSigner(indexOrSignerName)
|
|
142
|
+
const accountSigner = signer.isDerivable
|
|
143
|
+
? await signer.derive(signer.path.split('/').slice(-3).join('/'))
|
|
144
|
+
: signer
|
|
145
|
+
const account = new WalletAccountEvm(accountSigner, this._config)
|
|
146
|
+
this._accounts[key] = account
|
|
147
|
+
return account
|
|
123
148
|
}
|
|
124
149
|
|
|
125
|
-
|
|
150
|
+
const { signerName } = options
|
|
151
|
+
return await this.getAccountByPath(`0'/0/${indexOrSignerName}`, { signerName })
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Returns the wallet account at a specific derivation path.
|
|
156
|
+
*
|
|
157
|
+
* @param {string} path - The derivation path (e.g. "0'/0/0").
|
|
158
|
+
* @param {Object} [options] - Account options.
|
|
159
|
+
* @param {string} [options.signerName] - The signer name. Omit to use the default signer.
|
|
160
|
+
* @returns {Promise<WalletAccountEvm>} The account.
|
|
161
|
+
* @throws {Error} If a signer name is given but no signer exists with that name.
|
|
162
|
+
* @throws {SignerError} If the signer doesn't support account derivation.
|
|
163
|
+
*/
|
|
164
|
+
async getAccountByPath (path, options = {}) {
|
|
165
|
+
const { signerName } = options
|
|
166
|
+
const key = `${signerName ?? ''}:${path}`
|
|
167
|
+
if (this._accounts[key]) {
|
|
168
|
+
return this._accounts[key]
|
|
169
|
+
}
|
|
170
|
+
const signer = this.getSigner(signerName)
|
|
171
|
+
const childSigner = await signer.derive(path)
|
|
172
|
+
const account = new WalletAccountEvm(childSigner, this._config)
|
|
173
|
+
this._accounts[key] = account
|
|
174
|
+
return account
|
|
126
175
|
}
|
|
127
176
|
|
|
128
177
|
/**
|
package/types/index.d.ts
CHANGED
|
@@ -17,3 +17,4 @@ export type EvmTransaction = import("./src/wallet-account-read-only-evm.js").Evm
|
|
|
17
17
|
export type EvmTransferOptions = import("./src/wallet-account-read-only-evm.js").EvmTransferOptions;
|
|
18
18
|
export type EvmWalletConfig = import("./src/wallet-account-read-only-evm.js").EvmWalletConfig;
|
|
19
19
|
export type ApproveOptions = import("./src/wallet-account-evm.js").ApproveOptions;
|
|
20
|
+
export type UnsignedEvmTransaction = import("./src/utils/tx-populator-evm.js").UnsignedEvmTransaction;
|
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
/**
|
|
8
|
+
* @extends {ISignerEvm}
|
|
9
|
+
* Signer that wraps a raw private key in a memory-safe buffer, exposing a minimal
|
|
10
|
+
* interface for signing messages, transactions and typed data. This signer does
|
|
11
|
+
* not support derivation and always represents a single account.
|
|
12
|
+
*/
|
|
13
|
+
export default class PrivateKeySignerEvm extends ISignerEvm {
|
|
14
|
+
/**
|
|
15
|
+
* @param {string|Uint8Array} privateKey - Hex string (with/without 0x) or raw key bytes.
|
|
16
|
+
*/
|
|
17
|
+
constructor(privateKey: string | Uint8Array);
|
|
18
|
+
/** @private */
|
|
19
|
+
private _signingKey;
|
|
20
|
+
/** @private */
|
|
21
|
+
private _wallet;
|
|
22
|
+
/** @private */
|
|
23
|
+
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} */
|
|
33
|
+
get address(): string;
|
|
34
|
+
/**
|
|
35
|
+
* The account's key pair (private and public key buffers).
|
|
36
|
+
* @type {KeyPair}
|
|
37
|
+
*/
|
|
38
|
+
get keyPair(): KeyPair;
|
|
39
|
+
/**
|
|
40
|
+
* PrivateKeySignerEvm is not a hierarchical signer and cannot derive.
|
|
41
|
+
* @returns {Promise<never>}
|
|
42
|
+
* @throws {SignerError} Always — private-key signers do not support derivation.
|
|
43
|
+
*/
|
|
44
|
+
derive(): Promise<never>;
|
|
45
|
+
/** @returns {Promise<string>} */
|
|
46
|
+
getAddress(): Promise<string>;
|
|
47
|
+
/**
|
|
48
|
+
* Signs a message.
|
|
49
|
+
*
|
|
50
|
+
* @param {string} message - The message to sign.
|
|
51
|
+
* @returns {Promise<string>} The message's signature.
|
|
52
|
+
*/
|
|
53
|
+
sign(message: string): Promise<string>;
|
|
54
|
+
/**
|
|
55
|
+
* Signs a transaction and returns the serialized signed transaction hex.
|
|
56
|
+
*
|
|
57
|
+
* @param {UnsignedEvmTransaction} unsignedTx - The unsigned transaction object.
|
|
58
|
+
* @returns {Promise<string>}
|
|
59
|
+
*/
|
|
60
|
+
signTransaction(unsignedTx: UnsignedEvmTransaction): Promise<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Signs typed data according to EIP-712.
|
|
63
|
+
*
|
|
64
|
+
* @param {TypedData} typedData - The typed data to sign.
|
|
65
|
+
* @returns {Promise<string>} The typed data signature.
|
|
66
|
+
*/
|
|
67
|
+
signTypedData({ domain, types, message }: TypedData): Promise<string>;
|
|
68
|
+
/**
|
|
69
|
+
* Sign an ERC-7702 authorization tuple.
|
|
70
|
+
* @param {AuthorizationRequest} auth
|
|
71
|
+
* @returns {Promise<Authorization>}
|
|
72
|
+
*/
|
|
73
|
+
signAuthorization(auth: AuthorizationRequest): Promise<Authorization>;
|
|
74
|
+
/** Dispose secrets from memory. */
|
|
75
|
+
dispose(): void;
|
|
76
|
+
}
|
|
77
|
+
export type UnsignedEvmTransaction = import("./seed-signer-evm.js").UnsignedEvmTransaction;
|
|
78
|
+
export type TypedData = import("../wallet-account-read-only-evm.js").TypedData;
|
|
79
|
+
export type KeyPair = import("@tetherto/wdk-wallet").KeyPair;
|
|
80
|
+
export type AuthorizationRequest = import("ethers").AuthorizationRequest;
|
|
81
|
+
export type Authorization = import("ethers").Authorization;
|