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

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.
@@ -15,274 +15,155 @@
15
15
 
16
16
  import * as bip39 from 'bip39'
17
17
 
18
- import MemorySafeHDNodeWallet from '../memory-safe/hd-node-wallet.js'
19
- import { ISigner, InvalidSignerError, NotImplementedError, ValueError } from '@tetherto/wdk-wallet'
18
+ import { ValueError } from '@tetherto/wdk-wallet'
20
19
 
21
- const BIP_44_ETH_DERIVATION_PATH_PREFIX = "m/44'/60'"
20
+ // eslint-disable-next-line camelcase
21
+ import { sodium_memzero } from 'sodium-universal'
22
22
 
23
- // Relative path of the account derived when none is provided.
24
- const DEFAULT_ACCOUNT_PATH = "0'/0/0"
23
+ import MemorySafeHDNodeWallet from '../memory-safe/hd-node-wallet.js'
25
24
 
26
- /** @typedef {import('../utils/tx-populator-evm.js').UnsignedEvmTransaction} UnsignedEvmTransaction */
27
- /** @typedef {import('@tetherto/wdk-wallet').ISigner} ISigner */
25
+ /** @typedef {import('./signer-evm.js').ISignerEvm} ISignerEvm */
28
26
  /** @typedef {import('@tetherto/wdk-wallet').KeyPair} KeyPair */
27
+ /** @typedef {import('ethers').TransactionLike} TransactionLike */
29
28
  /** @typedef {import('ethers').AuthorizationRequest} AuthorizationRequest */
30
29
  /** @typedef {import('ethers').Authorization} Authorization */
31
30
  /** @typedef {import('../wallet-account-read-only-evm.js').TypedData} TypedData */
32
31
  /** @typedef {import('../memory-safe/hd-node-wallet.js').default} MemorySafeHDNodeWallet */
33
32
 
34
33
  /**
35
- * @typedef {Object} SeedSignerEvmOpts
36
- * @property {MemorySafeHDNodeWallet} [root] - An existing HD node wallet root to derive from.
37
- * @property {string} [path] - Relative BIP-44 path segment (e.g. "0'/0/0"). Defaults to the account at index 0.
38
- * @property {boolean} [isChild] - When true, the signer is a derived child and does not retain the root.
39
- */
40
-
41
- /**
42
- * Interface for EVM signers, extending the base `ISigner` from `@tetherto/wdk-wallet`.
34
+ * Absolute BIP-44 prefix for Ethereum (m/purpose'/coin_type'). Exported so callers that want
35
+ * "the standard Ethereum path" (WalletAccountEvm's seed overload, WalletManagerEvm's own
36
+ * internal default signer) can compose an absolute path without hardcoding it themselves.
43
37
  *
44
- * @extends {ISigner}
45
- * @interface
38
+ * @internal
46
39
  */
47
- export class ISignerEvm extends ISigner {
48
- /**
49
- * Whether this signer can derive child signers (i.e. it holds an HD root). Non-derivable
50
- * signers (e.g. private-key signers) are bound directly to an account; derivable signers
51
- * derive child accounts and keep the root for management only.
52
- * @type {boolean}
53
- */
54
- get isDerivable () {
55
- throw new NotImplementedError('isDerivable')
56
- }
57
-
58
- /**
59
- * The last component index for the derivation path of this signer, when applicable.
60
- * @type {number|undefined}
61
- */
62
- get index () {
63
- throw new NotImplementedError('index')
64
- }
65
-
66
- /**
67
- * The full derivation path if this is a child signer.
68
- * @type {string|undefined}
69
- */
70
- get path () {
71
- throw new NotImplementedError('path')
72
- }
73
-
74
- /**
75
- * The account's address, if available.
76
- * @type {string|undefined}
77
- */
78
- get address () {
79
- throw new NotImplementedError('address')
80
- }
81
-
82
- /**
83
- * The account's key pair.
84
- * @type {KeyPair}
85
- */
86
- get keyPair () {
87
- throw new NotImplementedError('keyPair')
88
- }
89
-
90
- /**
91
- * Derive a child signer from this signer using a relative path (e.g. "0'/0/0").
92
- *
93
- * @param {string} relPath - The relative BIP-44 path segment.
94
- * @returns {Promise<ISignerEvm>} The derived child signer.
95
- * @throws {InvalidSignerError} If the signer does not support derivation (e.g. private-key signers).
96
- */
97
- async derive (relPath) {
98
- throw new NotImplementedError('derive(relPath)')
99
- }
100
-
101
- /**
102
- * Returns the account's address.
103
- * @returns {Promise<string>}
104
- */
105
- async getAddress () {
106
- throw new NotImplementedError('getAddress()')
107
- }
108
-
109
- /**
110
- * Sign a plain message.
111
- * @param {string} message
112
- * @returns {Promise<string>}
113
- */
114
- async sign (message) {
115
- throw new NotImplementedError('sign(message)')
116
- }
117
-
118
- /**
119
- * Sign a transaction-like object compatible with ethers Transaction.from.
120
- * @param {UnsignedEvmTransaction} unsignedTx
121
- * @returns {Promise<string>} The serialized signed transaction hex.
122
- */
123
- async signTransaction (unsignedTx) {
124
- throw new NotImplementedError('signTransaction(unsignedTx)')
125
- }
126
-
127
- /**
128
- * Signs typed data according to EIP-712.
129
- *
130
- * @param {TypedData} typedData - The typed data to sign.
131
- * @returns {Promise<string>} The typed data signature.
132
- */
133
- async signTypedData ({ domain, types, message }) {
134
- throw new NotImplementedError('signTypedData(typedData)')
135
- }
136
-
137
- /**
138
- * Sign an ERC-7702 authorization tuple.
139
- * @param {AuthorizationRequest} auth
140
- * @returns {Promise<Authorization>}
141
- */
142
- async signAuthorization (auth) {
143
- throw new NotImplementedError('signAuthorization(auth)')
144
- }
145
-
146
- /** Clear any secret material from memory. */
147
- dispose () {
148
- throw new NotImplementedError('dispose()')
149
- }
150
- }
40
+ export const BIP_44_ETH_DERIVATION_PATH_PREFIX = "m/44'/60'"
151
41
 
152
42
  /**
153
- * @extends {ISignerEvm}
154
- * Signer implementation that derives keys from a BIP-39 seed using the BIP-44 Ethereum path.
155
- * Always holds a derived account (index 0 by default). A root signer also retains the HD root
156
- * and can derive child signers; a derived child holds only its own account.
43
+ * Signer implementation that derives keys from a BIP-39 seed using an HD path. Every signer
44
+ * holds exactly one HD node (the Ethereum BIP-44 coin node "m/44'/60'" by default, ready to
45
+ * derive accounts below it) and can derive child signers below its own path. Each signer owns
46
+ * an independent copy of its key, so disposing one never affects its parent, children or
47
+ * siblings.
48
+ *
49
+ * @implements {ISignerEvm}
157
50
  */
158
- export default class SeedSignerEvm extends ISignerEvm {
51
+ export default class SeedSignerEvm {
159
52
  /**
160
- * Create a SeedSignerEvm.
161
- * Provide either a mnemonic/seed or an existing root via opts.root (for children root is not stored internally)
53
+ * Create a SeedSignerEvm from a BIP-39 seed.
162
54
  *
163
- * @param {string|Uint8Array|null} seed - A BIP-39 mnemonic seed phrase, or a raw BIP-32 master seed (16-64 bytes). Omit when providing `opts.root`.
164
- * @param {SeedSignerEvmOpts} [opts] - Construction options for root reuse, direct child derivation or path definition (default is index 0).
165
- * @throws {ValueError} If neither a seed nor a root is provided, or if both are provided.
166
- * @throws {ValueError} If a seed is provided but is not a valid BIP-39 mnemonic.
55
+ * @param {string|Uint8Array} seed - A BIP-39 mnemonic seed phrase, or a raw BIP-32 master seed (16-64 bytes).
56
+ * @param {string} [path] - A BIP-32 path (default: "m/44'/60'").
57
+ * @throws {ValueError} If the given seed phrase is invalid.
167
58
  */
168
- constructor (seed, opts = {}) {
169
- super()
170
-
171
- // If a root is provided, do not expect a seed
172
- if (opts.root && seed) {
173
- throw new ValueError('Provide either a seed or a root, not both.')
174
- }
175
-
176
- if (!opts.root && !seed) {
177
- throw new ValueError('Seed or root is required.')
178
- }
179
-
180
- if (typeof seed === 'string') {
59
+ constructor (seed, path = BIP_44_ETH_DERIVATION_PATH_PREFIX) {
60
+ const ownsSeed = typeof seed === 'string'
61
+ if (ownsSeed) {
181
62
  if (!bip39.validateMnemonic(seed)) {
182
63
  throw new ValueError('The seed phrase is invalid.')
183
64
  }
65
+
184
66
  seed = bip39.mnemonicToSeedSync(seed)
185
67
  }
186
68
 
187
- const root = opts.root || (seed ? MemorySafeHDNodeWallet.fromSeed(seed) : undefined)
188
-
189
- const fullPath = `${BIP_44_ETH_DERIVATION_PATH_PREFIX}/${opts.path || DEFAULT_ACCOUNT_PATH}`
190
- const account = root.derivePath(fullPath)
69
+ const root = MemorySafeHDNodeWallet.fromSeed(seed)
70
+ if (ownsSeed) sodium_memzero(seed)
191
71
 
192
- if (opts.isChild && !opts.root) root.dispose()
72
+ let account
73
+ try {
74
+ account = root.derivePath(path)
75
+ } finally {
76
+ if (account !== root) root.dispose()
77
+ }
193
78
 
194
79
  /** @private */
195
80
  this._account = account
196
- /** @private */
197
- this._address = account.address
198
- /** @private */
199
- this._path = fullPath
200
- /** @private */
201
- this._root = opts.isChild ? undefined : root
202
81
  }
203
82
 
204
83
  /**
205
- * Whether this signer can derive child signers. True for a root signer (which holds the
206
- * HD root); false for a derived child, which does not retain the root.
207
- * @type {boolean}
84
+ * Whether this signer can derive child signers. Always true: every seed signer holds an
85
+ * HD node with a private key and can derive below its own path.
86
+ *
87
+ * @type {true}
208
88
  */
209
89
  get isDerivable () {
210
- return Boolean(this._root)
211
- }
212
-
213
- /**
214
- * The last component index of the derivation path, if available.
215
- * @type {number|undefined}
216
- */
217
- get index () {
218
- if (!this._path) return undefined
219
- return +this._path.split('/').pop()
90
+ return true
220
91
  }
221
92
 
222
93
  /**
223
- * The full derivation path of this signer's account.
224
- * @type {string|undefined}
94
+ * The signer's absolute derivation path.
95
+ *
96
+ * @type {string}
225
97
  */
226
98
  get path () {
227
- return this._path
99
+ return this._account.path
228
100
  }
229
101
 
230
102
  /**
231
103
  * The account's derived address.
104
+ *
105
+ * @deprecated Use {@link getAddress} instead. This property will be removed in an upcoming
106
+ * release: not all signers (e.g. hardware signers) can expose the address synchronously.
232
107
  * @type {string}
233
108
  */
234
109
  get address () {
235
- return this._address
110
+ return this._account.address
236
111
  }
237
112
 
238
113
  /**
239
114
  * The account's key pair (private and public key buffers).
115
+ *
240
116
  * @type {KeyPair}
241
117
  */
242
118
  get keyPair () {
243
119
  return {
244
- privateKey: this._account ? this._account.privateKeyBuffer : null,
245
- publicKey: this._account ? this._account.publicKeyBuffer : null
120
+ privateKey: this._account.privateKeyBuffer ?? null,
121
+ publicKey: this._account.publicKeyBuffer
246
122
  }
247
123
  }
248
124
 
249
125
  /**
250
- * Derive a child signer using the provided relative path (e.g. "0'/0/0").
251
- * @param {string} relPath
252
- * @returns {Promise<SeedSignerEvm>}
253
- * @throws {InvalidSignerError} If called on a derived child signer, which does not retain the root.
126
+ * Derive a child signer relative to this signer's own path (e.g. calling derive("0'/0/1") on
127
+ * a signer at "m/44'/60'" yields a child at "m/44'/60'/0'/0/1"). Purely self-relative: no
128
+ * coin-specific prefix is ever assumed or injected. The child owns an independent copy of
129
+ * its key and can itself derive further.
130
+ *
131
+ * @param {string} relPath - The path segment to derive, relative to this signer's own path.
132
+ * @returns {Promise<SeedSignerEvm>} The derived child signer.
254
133
  */
255
134
  async derive (relPath) {
256
- if (!this._root) {
257
- throw new InvalidSignerError('Cannot derive: this signer has no root (it is a derived child or has been disposed).')
258
- }
259
- return new SeedSignerEvm(null, { root: this._root, path: relPath, isChild: true })
135
+ const signer = Object.create(SeedSignerEvm.prototype)
136
+ signer._account = this._account.derivePath(relPath)
137
+ return signer
260
138
  }
261
139
 
262
140
  /**
263
141
  * Returns the account's derived address.
264
- * @returns {Promise<string>}
142
+ *
143
+ * @returns {Promise<string>} The account's address.
265
144
  */
266
145
  async getAddress () {
267
- return this._address
146
+ return this._account.address
268
147
  }
269
148
 
270
149
  /**
271
- * Sign a plain message string.
272
- * @param {string} message
273
- * @returns {Promise<string>}
150
+ * Signs a message.
151
+ *
152
+ * @param {string} message - The message to sign.
153
+ * @returns {Promise<string>} The message's signature.
274
154
  */
275
155
  async sign (message) {
276
156
  return this._account.signMessage(message)
277
157
  }
278
158
 
279
159
  /**
280
- * Sign a transaction object and return its serialized form.
281
- * @param {UnsignedEvmTransaction} unsignedTx
282
- * @returns {Promise<string>}
160
+ * Signs a transaction.
161
+ *
162
+ * @param {TransactionLike} tx - The transaction to sign.
163
+ * @returns {Promise<string>} The signed transaction as a hex string.
283
164
  */
284
- async signTransaction (unsignedTx) {
285
- return this._account.signTransaction(unsignedTx)
165
+ async signTransaction (tx) {
166
+ return this._account.signTransaction(tx)
286
167
  }
287
168
 
288
169
  /**
@@ -296,19 +177,19 @@ export default class SeedSignerEvm extends ISignerEvm {
296
177
  }
297
178
 
298
179
  /**
299
- * Sign an ERC-7702 authorization tuple.
300
- * @param {AuthorizationRequest} auth
301
- * @returns {Promise<Authorization>}
180
+ * Signs an ERC-7702 authorization tuple.
181
+ *
182
+ * @param {AuthorizationRequest} auth - The authorization request.
183
+ * @returns {Promise<Authorization>} The signed authorization.
302
184
  */
303
185
  async signAuthorization (auth) {
304
186
  return this._account.authorizeSync(auth)
305
187
  }
306
188
 
307
- /** Disposes secrets from memory. */
189
+ /**
190
+ * Disposes the signer, erasing its secrets from memory.
191
+ */
308
192
  dispose () {
309
- if (this._account) this._account.dispose()
310
- this._account = undefined
311
- if (this._root) this._root.dispose()
312
- this._root = undefined
193
+ this._account.dispose()
313
194
  }
314
195
  }
@@ -0,0 +1,69 @@
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 { ISigner, NotImplementedError } from '@tetherto/wdk-wallet'
17
+
18
+ /** @typedef {import('ethers').TransactionLike} TransactionLike */
19
+ /** @typedef {import('ethers').AuthorizationRequest} AuthorizationRequest */
20
+ /** @typedef {import('ethers').Authorization} Authorization */
21
+ /** @typedef {import('../wallet-account-read-only-evm.js').TypedData} TypedData */
22
+
23
+ /**
24
+ * Interface for EVM signers, extending the base `ISigner` from `@tetherto/wdk-wallet`.
25
+ *
26
+ * @interface
27
+ */
28
+ export class ISignerEvm extends ISigner {
29
+ /**
30
+ * The account's address, if available.
31
+ *
32
+ * @deprecated Use {@link getAddress} instead. This property will be removed in an upcoming
33
+ * release: not all signers (e.g. hardware signers) can expose the address synchronously.
34
+ * @type {string | undefined}
35
+ */
36
+ get address () {
37
+ throw new NotImplementedError('address')
38
+ }
39
+
40
+ /**
41
+ * Signs a transaction.
42
+ *
43
+ * @param {TransactionLike} tx - The transaction to sign.
44
+ * @returns {Promise<string>} The signed transaction as a hex string.
45
+ */
46
+ async signTransaction (tx) {
47
+ throw new NotImplementedError('signTransaction(tx)')
48
+ }
49
+
50
+ /**
51
+ * Signs typed data according to EIP-712.
52
+ *
53
+ * @param {TypedData} typedData - The typed data to sign.
54
+ * @returns {Promise<string>} The typed data signature.
55
+ */
56
+ async signTypedData (typedData) {
57
+ throw new NotImplementedError('signTypedData(typedData)')
58
+ }
59
+
60
+ /**
61
+ * Signs an ERC-7702 authorization tuple.
62
+ *
63
+ * @param {AuthorizationRequest} auth - The authorization request.
64
+ * @returns {Promise<Authorization>} The signed authorization.
65
+ */
66
+ async signAuthorization (auth) {
67
+ throw new NotImplementedError('signAuthorization(auth)')
68
+ }
69
+ }