@tetherto/wdk-wallet 1.0.0-beta.16 → 1.0.0-beta.17
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/index.js +11 -12
- package/package.json +1 -1
- package/src/errors.js +280 -19
- package/src/multisig/errors.js +54 -0
- package/src/multisig/index.js +15 -3
- package/src/multisig/multisig-message-signing-read-only.js +63 -0
- package/src/multisig/multisig-message-signing.js +64 -0
- package/src/multisig/multisig-owner-management.js +22 -5
- package/src/multisig/wallet-account-multisig.js +28 -39
- package/src/multisig/wallet-account-read-only-multisig.js +20 -34
- package/src/protocols/bridge-protocol.js +35 -1
- package/src/protocols/errors.js +510 -0
- package/src/protocols/fiat-protocol.js +104 -4
- package/src/protocols/index.js +13 -0
- package/src/protocols/lending-protocol.js +114 -2
- package/src/protocols/sda-protocol.js +103 -16
- package/src/protocols/swap-protocol.js +35 -1
- package/src/protocols/swidge-protocol.js +166 -55
- package/src/signer.js +8 -4
- package/src/wallet-account-read-only-simple.js +86 -1
- package/src/wallet-account-read-only.js +175 -6
- package/src/wallet-account.js +25 -1
- package/src/wallet-manager.js +38 -22
- package/types/index.d.ts +9 -2
- package/types/src/errors.d.ts +198 -14
- package/types/src/multisig/errors.d.ts +33 -0
- package/types/src/multisig/index.d.ts +7 -3
- package/types/src/multisig/multisig-message-signing-read-only.d.ts +53 -0
- package/types/src/multisig/multisig-message-signing.d.ts +40 -0
- package/types/src/multisig/multisig-owner-management.d.ts +20 -4
- package/types/src/multisig/wallet-account-multisig.d.ts +29 -35
- package/types/src/multisig/wallet-account-read-only-multisig.d.ts +20 -40
- package/types/src/protocols/bridge-protocol.d.ts +39 -3
- package/types/src/protocols/errors.d.ts +366 -0
- package/types/src/protocols/fiat-protocol.d.ts +103 -9
- package/types/src/protocols/index.d.ts +11 -0
- package/types/src/protocols/lending-protocol.d.ts +118 -4
- package/types/src/protocols/sda-protocol.d.ts +120 -23
- package/types/src/protocols/swap-protocol.d.ts +40 -4
- package/types/src/protocols/swidge-protocol.d.ts +106 -38
- package/types/src/signer.d.ts +6 -4
- package/types/src/wallet-account-read-only-simple.d.ts +99 -1
- package/types/src/wallet-account-read-only.d.ts +95 -3
- package/types/src/wallet-account.d.ts +27 -3
- package/types/src/wallet-manager.d.ts +37 -35
|
@@ -13,10 +13,20 @@
|
|
|
13
13
|
// limitations under the License.
|
|
14
14
|
'use strict'
|
|
15
15
|
|
|
16
|
-
import { NotImplementedError } from './errors.js'
|
|
17
|
-
|
|
18
16
|
import { IWalletAccountReadOnlySimple } from './wallet-account-read-only-simple.js'
|
|
19
17
|
|
|
18
|
+
import { AssertionError, NoSuchElementError, NotImplementedError, ProviderError, TimeoutError, UnsupportedOperationError } from './errors.js'
|
|
19
|
+
|
|
20
|
+
/** @typedef {import('./wallet-account-read-only-simple.js').Finality} Finality */
|
|
21
|
+
/** @typedef {import('./wallet-account-read-only-simple.js').TransactionReceipt} TransactionReceipt */
|
|
22
|
+
/** @typedef {import('./wallet-account-read-only-simple.js').WaitForTransactionOptions} WaitForTransactionOptions */
|
|
23
|
+
|
|
24
|
+
/** @typedef {import('./errors.js').InvalidTokenError} InvalidTokenError */
|
|
25
|
+
/** @typedef {import('./errors.js').ProviderRequiredError} ProviderRequiredError */
|
|
26
|
+
/** @typedef {import('./errors.js').TransactionError} TransactionError */
|
|
27
|
+
/** @typedef {import('./errors.js').TransferError} TransferError */
|
|
28
|
+
/** @typedef {import('./errors.js').ValueError} ValueError */
|
|
29
|
+
|
|
20
30
|
/**
|
|
21
31
|
* @typedef {Object} Transaction
|
|
22
32
|
* @property {string} to - The transaction's recipient.
|
|
@@ -42,6 +52,30 @@ import { IWalletAccountReadOnlySimple } from './wallet-account-read-only-simple.
|
|
|
42
52
|
* @property {bigint} fee - The gas cost.
|
|
43
53
|
*/
|
|
44
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Enum that assigns a comparable ordinal to each finality level, used to check
|
|
57
|
+
* whether an observed finality satisfies a requested target.
|
|
58
|
+
*
|
|
59
|
+
* @readonly
|
|
60
|
+
* @enum {number}
|
|
61
|
+
*/
|
|
62
|
+
export const FINALITY = {
|
|
63
|
+
pending: 0,
|
|
64
|
+
dropped: 1,
|
|
65
|
+
confirmed: 2,
|
|
66
|
+
final: 3
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Resolves after the given number of milliseconds.
|
|
71
|
+
*
|
|
72
|
+
* @param {number} amount - The delay, in milliseconds.
|
|
73
|
+
* @returns {Promise<void>} A promise that resolves once the delay elapses.
|
|
74
|
+
*/
|
|
75
|
+
async function sleep (amount) {
|
|
76
|
+
await new Promise(resolve => setTimeout(resolve, amount))
|
|
77
|
+
}
|
|
78
|
+
|
|
45
79
|
/** @interface */
|
|
46
80
|
export class IWalletAccountReadOnly extends IWalletAccountReadOnlySimple {
|
|
47
81
|
/**
|
|
@@ -49,6 +83,10 @@ export class IWalletAccountReadOnly extends IWalletAccountReadOnlySimple {
|
|
|
49
83
|
*
|
|
50
84
|
* @param {Transaction} tx - The transaction.
|
|
51
85
|
* @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
|
|
86
|
+
* @throws {ValueError} If the transaction is not valid.
|
|
87
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
88
|
+
* @throws {ProviderError} If the provider fails to estimate the costs of the transaction.
|
|
89
|
+
* @throws {TransactionError} If the transaction fails with an error.
|
|
52
90
|
*/
|
|
53
91
|
async quoteSendTransaction (tx) {
|
|
54
92
|
throw new NotImplementedError('quoteSendTransaction(tx)')
|
|
@@ -59,6 +97,11 @@ export class IWalletAccountReadOnly extends IWalletAccountReadOnlySimple {
|
|
|
59
97
|
*
|
|
60
98
|
* @param {TransferOptions} options - The transfer's options.
|
|
61
99
|
* @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
|
|
100
|
+
* @throws {ValueError} If the transfer options are not valid.
|
|
101
|
+
* @throws {InvalidTokenError} If the token is not a valid ERC 20 token's address.
|
|
102
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
103
|
+
* @throws {ProviderError} If the provider fails to estimate the costs of the transfer.
|
|
104
|
+
* @throws {TransferError} If the transfer fails with an error.
|
|
62
105
|
*/
|
|
63
106
|
async quoteTransfer (options) {
|
|
64
107
|
throw new NotImplementedError('quoteTransfer(options)')
|
|
@@ -70,6 +113,28 @@ export class IWalletAccountReadOnly extends IWalletAccountReadOnlySimple {
|
|
|
70
113
|
* @implements {IWalletAccountReadOnly}
|
|
71
114
|
*/
|
|
72
115
|
export default class WalletAccountReadOnly {
|
|
116
|
+
/**
|
|
117
|
+
* The default poll cadence for {@link waitForTransaction}, in milliseconds,
|
|
118
|
+
* applied when the caller doesn't provide an `interval`. Subclasses override
|
|
119
|
+
* it to match their chain's block time.
|
|
120
|
+
*
|
|
121
|
+
* @type {number}
|
|
122
|
+
*/
|
|
123
|
+
get defaultWaitInterval () {
|
|
124
|
+
return 4000
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* The default time budget for {@link waitForTransaction}, in milliseconds,
|
|
129
|
+
* applied when the caller doesn't provide a `timeout`. Subclasses override it
|
|
130
|
+
* to match their chain's finality expectations.
|
|
131
|
+
*
|
|
132
|
+
* @type {number}
|
|
133
|
+
*/
|
|
134
|
+
get defaultWaitTimeout () {
|
|
135
|
+
return 60000
|
|
136
|
+
}
|
|
137
|
+
|
|
73
138
|
/**
|
|
74
139
|
* Creates a new read-only wallet account.
|
|
75
140
|
*
|
|
@@ -97,7 +162,7 @@ export default class WalletAccountReadOnly {
|
|
|
97
162
|
*/
|
|
98
163
|
async getAddress () {
|
|
99
164
|
if (!this._address) {
|
|
100
|
-
throw new
|
|
165
|
+
throw new AssertionError("The account's address must be set to perform this operation.")
|
|
101
166
|
}
|
|
102
167
|
|
|
103
168
|
return this._address
|
|
@@ -106,14 +171,13 @@ export default class WalletAccountReadOnly {
|
|
|
106
171
|
/**
|
|
107
172
|
* Verifies a message's signature.
|
|
108
173
|
*
|
|
109
|
-
* @abstract
|
|
110
174
|
* @param {string} message - The original message.
|
|
111
175
|
* @param {string} signature - The signature to verify.
|
|
112
176
|
* @returns {Promise<boolean>} True if the signature is valid.
|
|
113
|
-
* @throws {
|
|
177
|
+
* @throws {UnsupportedOperationError} If the read-only wallet account class is not able to provide an implementation for the method.
|
|
114
178
|
*/
|
|
115
179
|
async verify (message, signature) {
|
|
116
|
-
throw new
|
|
180
|
+
throw new UnsupportedOperationError('verify(message, signature)')
|
|
117
181
|
}
|
|
118
182
|
|
|
119
183
|
/**
|
|
@@ -121,6 +185,8 @@ export default class WalletAccountReadOnly {
|
|
|
121
185
|
*
|
|
122
186
|
* @abstract
|
|
123
187
|
* @returns {Promise<bigint>} The native token balance.
|
|
188
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
189
|
+
* @throws {ProviderError} If the provider fails to fetch the account's balance.
|
|
124
190
|
*/
|
|
125
191
|
async getBalance () {
|
|
126
192
|
throw new NotImplementedError('getBalance()')
|
|
@@ -132,6 +198,10 @@ export default class WalletAccountReadOnly {
|
|
|
132
198
|
* @abstract
|
|
133
199
|
* @param {string} tokenAddress - The smart contract address of the token.
|
|
134
200
|
* @returns {Promise<bigint>} The token balance.
|
|
201
|
+
* @throws {ValueError} If the token's address is not valid.
|
|
202
|
+
* @throws {InvalidTokenError} If the token's address doesn't match an existing ERC 20 token.
|
|
203
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
204
|
+
* @throws {ProviderError} If the provider fails to fetch the account's token balance.
|
|
135
205
|
*/
|
|
136
206
|
async getTokenBalance (tokenAddress) {
|
|
137
207
|
throw new NotImplementedError('getTokenBalance(tokenAddress)')
|
|
@@ -143,6 +213,10 @@ export default class WalletAccountReadOnly {
|
|
|
143
213
|
* @abstract
|
|
144
214
|
* @param {Transaction} tx - The transaction.
|
|
145
215
|
* @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
|
|
216
|
+
* @throws {ValueError} If the transaction is not valid.
|
|
217
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
218
|
+
* @throws {ProviderError} If the provider fails to estimate the costs of the transaction.
|
|
219
|
+
* @throws {TransactionError} If the transaction fails with an error.
|
|
146
220
|
*/
|
|
147
221
|
async quoteSendTransaction (tx) {
|
|
148
222
|
throw new NotImplementedError('quoteSendTransaction(tx)')
|
|
@@ -154,6 +228,11 @@ export default class WalletAccountReadOnly {
|
|
|
154
228
|
* @abstract
|
|
155
229
|
* @param {TransferOptions} options - The transfer's options.
|
|
156
230
|
* @returns {Promise<Omit<TransferResult, 'hash'>>} The transfer's quotes.
|
|
231
|
+
* @throws {ValueError} If the transfer options are not valid.
|
|
232
|
+
* @throws {InvalidTokenError} If the token is not a valid ERC 20 token's address.
|
|
233
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
234
|
+
* @throws {ProviderError} If the provider fails to estimate the costs of the transfer.
|
|
235
|
+
* @throws {TransferError} If the transfer fails with an error.
|
|
157
236
|
*/
|
|
158
237
|
async quoteTransfer (options) {
|
|
159
238
|
throw new NotImplementedError('quoteTransfer(options)')
|
|
@@ -162,11 +241,101 @@ export default class WalletAccountReadOnly {
|
|
|
162
241
|
/**
|
|
163
242
|
* Returns a transaction's receipt.
|
|
164
243
|
*
|
|
244
|
+
* @deprecated Use {@link getTransaction} instead, which returns a normalized, finality-based receipt. The native receipt fields remain available on each module's extended return type.
|
|
165
245
|
* @abstract
|
|
166
246
|
* @param {string} hash - The transaction's hash.
|
|
167
247
|
* @returns {Promise<unknown | null>} The receipt, or null if the transaction has not been included in a block yet.
|
|
248
|
+
* @throws {ValueError} If the hash is not valid.
|
|
249
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
250
|
+
* @throws {ProviderError} If the provider fails to fetch the transaction's receipt.
|
|
168
251
|
*/
|
|
169
252
|
async getTransactionReceipt (hash) {
|
|
170
253
|
throw new NotImplementedError('getTransactionReceipt(hash)')
|
|
171
254
|
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Returns a normalized, finality-based receipt for a transaction.
|
|
258
|
+
*
|
|
259
|
+
* @abstract
|
|
260
|
+
* @param {string} hash - The transaction's identifier (hash / signature / lt:hash).
|
|
261
|
+
* @returns {Promise<TransactionReceipt>} The normalized receipt.
|
|
262
|
+
* @throws {ValueError} If the hash is not a valid identifier.
|
|
263
|
+
* @throws {NoSuchElementError} If no transaction has been found for the given hash.
|
|
264
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
265
|
+
* @throws {ProviderError} If the provider fails to fetch the transaction.
|
|
266
|
+
*/
|
|
267
|
+
async getTransaction (hash) {
|
|
268
|
+
throw new NotImplementedError('getTransaction(hash)')
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Blocks until a transaction reaches a terminal state (the requested finality
|
|
273
|
+
* target or `dropped`), or times out.
|
|
274
|
+
*
|
|
275
|
+
* The polling loop and target resolution are chain-agnostic: this method only
|
|
276
|
+
* interprets the normalized receipt returned by {@link getTransaction}. A
|
|
277
|
+
* {@link NoSuchElementError} is treated as a transient not-found, so the loop
|
|
278
|
+
* keeps polling until the timeout.
|
|
279
|
+
*
|
|
280
|
+
* @param {string} hash - The transaction's identifier.
|
|
281
|
+
* @param {WaitForTransactionOptions} [options] - The wait options.
|
|
282
|
+
* @returns {Promise<TransactionReceipt>} The terminal receipt: the finality target reached (inspect `success` to tell success from revert), or `dropped`.
|
|
283
|
+
* @throws {ValueError} If the hash is not a valid identifier.
|
|
284
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
285
|
+
* @throws {ProviderError} If the provider fails to fetch the transaction.
|
|
286
|
+
* @throws {TimeoutError} If the operation times out.
|
|
287
|
+
*/
|
|
288
|
+
async waitForTransaction (hash, options = {}) {
|
|
289
|
+
const {
|
|
290
|
+
target = 'confirmed',
|
|
291
|
+
interval = this.defaultWaitInterval,
|
|
292
|
+
timeout = this.defaultWaitTimeout,
|
|
293
|
+
maxPollErrors = 3
|
|
294
|
+
} = options
|
|
295
|
+
|
|
296
|
+
const deadline = Date.now() + timeout
|
|
297
|
+
let droppedStreak = 0
|
|
298
|
+
let errorStreak = 0
|
|
299
|
+
|
|
300
|
+
while (true) {
|
|
301
|
+
let receipt = null
|
|
302
|
+
|
|
303
|
+
try {
|
|
304
|
+
receipt = await this.getTransaction(hash)
|
|
305
|
+
errorStreak = 0
|
|
306
|
+
} catch (error) {
|
|
307
|
+
if (error instanceof NoSuchElementError) {
|
|
308
|
+
errorStreak = 0
|
|
309
|
+
} else if (error instanceof ProviderError) {
|
|
310
|
+
if (++errorStreak > maxPollErrors) {
|
|
311
|
+
throw error
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
throw error
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (receipt) {
|
|
319
|
+
if (receipt.finality === 'dropped') {
|
|
320
|
+
if (++droppedStreak >= 2) {
|
|
321
|
+
return receipt
|
|
322
|
+
}
|
|
323
|
+
} else {
|
|
324
|
+
droppedStreak = 0
|
|
325
|
+
|
|
326
|
+
if (FINALITY[receipt.finality] >= FINALITY[target]) {
|
|
327
|
+
return receipt
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
} else {
|
|
331
|
+
droppedStreak = 0
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
if (Date.now() >= deadline) {
|
|
335
|
+
throw new TimeoutError(`Transaction '${hash}' did not reach '${target}' within the timeout.`)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
await sleep(interval)
|
|
339
|
+
}
|
|
340
|
+
}
|
|
172
341
|
}
|
package/src/wallet-account.js
CHANGED
|
@@ -23,6 +23,14 @@ import { NotImplementedError } from './errors.js'
|
|
|
23
23
|
/** @typedef {import('./wallet-account-read-only.js').TransferOptions} TransferOptions */
|
|
24
24
|
/** @typedef {import('./wallet-account-read-only.js').TransferResult} TransferResult */
|
|
25
25
|
|
|
26
|
+
/** @typedef {import('./errors.js').InvalidTokenError} InvalidTokenError */
|
|
27
|
+
/** @typedef {import('./errors.js').MaximumFeeExceededError} MaximumFeeExceededError */
|
|
28
|
+
/** @typedef {import('./errors.js').ProviderError} ProviderError */
|
|
29
|
+
/** @typedef {import('./errors.js').ProviderRequiredError} ProviderRequiredError */
|
|
30
|
+
/** @typedef {import('./errors.js').TransactionError} TransactionError */
|
|
31
|
+
/** @typedef {import('./errors.js').TransferError} TransferError */
|
|
32
|
+
/** @typedef {import('./errors.js').ValueError} ValueError */
|
|
33
|
+
|
|
26
34
|
/**
|
|
27
35
|
* @typedef {Object} KeyPair
|
|
28
36
|
* @property {Uint8Array} publicKey - The public key.
|
|
@@ -72,10 +80,11 @@ export class IWalletAccount extends IWalletAccountReadOnly {
|
|
|
72
80
|
}
|
|
73
81
|
|
|
74
82
|
/**
|
|
75
|
-
* Signs a transaction
|
|
83
|
+
* Signs a transaction.
|
|
76
84
|
*
|
|
77
85
|
* @param {Transaction} tx - The transaction to sign.
|
|
78
86
|
* @returns {Promise<TSignedTransaction>} The signed transaction.
|
|
87
|
+
* @throws {ValueError} If the transaction is not valid.
|
|
79
88
|
*/
|
|
80
89
|
async signTransaction (tx) {
|
|
81
90
|
throw new NotImplementedError('signTransaction(tx)')
|
|
@@ -97,6 +106,11 @@ export class IWalletAccount extends IWalletAccountReadOnly {
|
|
|
97
106
|
*
|
|
98
107
|
* @param {Transaction | TSignedTransaction} tx - The transaction.
|
|
99
108
|
* @returns {Promise<TransactionResult>} The transaction's result.
|
|
109
|
+
* @throws {ValueError} If the transaction is not valid.
|
|
110
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
111
|
+
* @throws {ProviderError} If the provider fails to perform the transaction.
|
|
112
|
+
* @throws {TransactionError} If the transaction fails with an error.
|
|
113
|
+
* @throws {MaximumFeeExceededError} If the the costs of the transaction exceeds the transaction max. fee option.
|
|
100
114
|
*/
|
|
101
115
|
async sendTransaction (tx) {
|
|
102
116
|
throw new NotImplementedError('sendTransaction(tx)')
|
|
@@ -107,6 +121,10 @@ export class IWalletAccount extends IWalletAccountReadOnly {
|
|
|
107
121
|
*
|
|
108
122
|
* @param {Transaction | TSignedTransaction} tx - The transaction.
|
|
109
123
|
* @returns {Promise<Omit<TransactionResult, 'hash'>>} The transaction's quotes.
|
|
124
|
+
* @throws {ValueError} If the transaction is not valid.
|
|
125
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
126
|
+
* @throws {ProviderError} If the provider fails to estimate the costs of the transaction.
|
|
127
|
+
* @throws {TransactionError} If the transaction fails with an error.
|
|
110
128
|
*/
|
|
111
129
|
async quoteSendTransaction (tx) {
|
|
112
130
|
throw new NotImplementedError('quoteSendTransaction(tx)')
|
|
@@ -117,6 +135,12 @@ export class IWalletAccount extends IWalletAccountReadOnly {
|
|
|
117
135
|
*
|
|
118
136
|
* @param {TransferOptions} options - The transfer's options.
|
|
119
137
|
* @returns {Promise<TransferResult>} The transfer's result.
|
|
138
|
+
* @throws {ValueError} If the transfer options are not valid.
|
|
139
|
+
* @throws {InvalidTokenError} If the token is not a valid ERC 20 token's address.
|
|
140
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
141
|
+
* @throws {ProviderError} If the provider fails to perform the transfer.
|
|
142
|
+
* @throws {TransferError} If the transfer fails with an error.
|
|
143
|
+
* @throws {MaximumFeeExceededError} If the the costs of the transfer exceeds the transfer max. fee option.
|
|
120
144
|
*/
|
|
121
145
|
async transfer (options) {
|
|
122
146
|
throw new NotImplementedError('transfer(options)')
|
package/src/wallet-manager.js
CHANGED
|
@@ -15,15 +15,20 @@
|
|
|
15
15
|
|
|
16
16
|
import * as bip39 from 'bip39'
|
|
17
17
|
|
|
18
|
-
import { NotImplementedError } from './errors.js'
|
|
18
|
+
import { NoSuchElementError, NotImplementedError, ValueError } from './errors.js'
|
|
19
19
|
|
|
20
20
|
/** @typedef {import('./wallet-account.js').IWalletAccount} IWalletAccount */
|
|
21
|
+
|
|
21
22
|
/** @typedef {import('./signer.js').ISigner} ISigner */
|
|
22
23
|
|
|
24
|
+
/** @typedef {import('./errors.js').InvalidSignerError} InvalidSignerError */
|
|
25
|
+
/** @typedef {import('./errors.js').ProviderError} ProviderError */
|
|
26
|
+
/** @typedef {import('./errors.js').ProviderRequiredError} ProviderRequiredError */
|
|
27
|
+
|
|
23
28
|
/**
|
|
24
29
|
* @typedef {Object} WalletConfig
|
|
30
|
+
* @property {number | bigint} [transactionMaxFee] - The maximum fee amount for sending transactions.
|
|
25
31
|
* @property {number | bigint} [transferMaxFee] - The maximum fee amount for transfer operations.
|
|
26
|
-
* @property {number | bigint} [transactionMaxFee] - The maximum fee amount for sendTransaction and signTransaction operations.
|
|
27
32
|
*/
|
|
28
33
|
|
|
29
34
|
/**
|
|
@@ -40,6 +45,7 @@ export default class WalletManager {
|
|
|
40
45
|
* @overload
|
|
41
46
|
* @param {string | Uint8Array} seed - The BIP-39 seed phrase or raw seed bytes.
|
|
42
47
|
* @param {WalletConfig} [config] - The wallet configuration.
|
|
48
|
+
* @throws {ValueError} If the seed is not a valid seed or BIP-39 seed phrase.
|
|
43
49
|
*/
|
|
44
50
|
|
|
45
51
|
/**
|
|
@@ -48,11 +54,14 @@ export default class WalletManager {
|
|
|
48
54
|
* @overload
|
|
49
55
|
* @param {ISigner} signer - The default signer.
|
|
50
56
|
* @param {WalletConfig} [config] - The wallet configuration.
|
|
57
|
+
* @throws {InvalidSignerError} If the given signer doesn't support account derivation.
|
|
51
58
|
*/
|
|
52
59
|
constructor (seedOrSigner, config = {}) {
|
|
60
|
+
// TODO: Add check to assert that the default signer is derivable.
|
|
61
|
+
|
|
53
62
|
if (typeof seedOrSigner === 'string') {
|
|
54
63
|
if (!WalletManager.isValidSeedPhrase(seedOrSigner)) {
|
|
55
|
-
throw new
|
|
64
|
+
throw new ValueError('Invalid seed phrase.')
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
seedOrSigner = bip39.mnemonicToSeedSync(seedOrSigner)
|
|
@@ -101,11 +110,12 @@ export default class WalletManager {
|
|
|
101
110
|
/**
|
|
102
111
|
* Returns a random [BIP-39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki) seed phrase.
|
|
103
112
|
*
|
|
104
|
-
* @param {12 | 24} [wordCount
|
|
113
|
+
* @param {12 | 24} [wordCount] - The number of words in the seed phrase (default: 12).
|
|
105
114
|
* @returns {string} The seed phrase.
|
|
106
115
|
*/
|
|
107
116
|
static getRandomSeedPhrase (wordCount = 12) {
|
|
108
|
-
const strength = wordCount ===
|
|
117
|
+
const strength = wordCount === 12 ? 128 : 256
|
|
118
|
+
|
|
109
119
|
return bip39.generateMnemonic(strength)
|
|
110
120
|
}
|
|
111
121
|
|
|
@@ -129,43 +139,45 @@ export default class WalletManager {
|
|
|
129
139
|
}
|
|
130
140
|
|
|
131
141
|
/**
|
|
132
|
-
* Registers a signer
|
|
142
|
+
* Registers a signer with the given name.
|
|
133
143
|
*
|
|
134
144
|
* @param {string} signerName - The signer name.
|
|
135
145
|
* @param {ISigner} signer - The signer.
|
|
136
146
|
* @returns {WalletManager} The wallet manager.
|
|
137
|
-
* @throws {
|
|
147
|
+
* @throws {ValueError} If the signer name is an empty or blank string.
|
|
138
148
|
*/
|
|
139
149
|
addSigner (signerName, signer) {
|
|
140
150
|
if (!signerName.trim()) {
|
|
141
|
-
throw new
|
|
151
|
+
throw new ValueError('The signer name cannot be an empty or blank string.')
|
|
142
152
|
}
|
|
143
153
|
|
|
144
154
|
this._signers[signerName] = signer
|
|
155
|
+
|
|
145
156
|
return this
|
|
146
157
|
}
|
|
147
158
|
|
|
148
159
|
/**
|
|
149
|
-
* Returns
|
|
150
|
-
* at construction. With a name, returns the signer registered under that
|
|
151
|
-
* name via {@link addSigner}.
|
|
160
|
+
* Returns the default signer, or the signer with the given name.
|
|
152
161
|
*
|
|
153
|
-
* @param {string} [signerName] -
|
|
162
|
+
* @param {string} [signerName] - If set, returns the signer with the given name.
|
|
154
163
|
* @returns {ISigner} The signer.
|
|
155
|
-
* @throws {
|
|
156
|
-
* provided at construction, or if called with a name that is not registered.
|
|
164
|
+
* @throws {NoSuchElementError} If the default signer is not set, or no signers are found for the given name.
|
|
157
165
|
*/
|
|
158
166
|
getSigner (signerName) {
|
|
159
167
|
if (signerName === undefined) {
|
|
160
168
|
if (this._defaultSigner === undefined) {
|
|
161
|
-
throw new
|
|
169
|
+
throw new NoSuchElementError('No default signer set.')
|
|
162
170
|
}
|
|
171
|
+
|
|
163
172
|
return this._defaultSigner
|
|
164
173
|
}
|
|
174
|
+
|
|
165
175
|
const signer = this._signers[signerName]
|
|
176
|
+
|
|
166
177
|
if (signer === undefined) {
|
|
167
|
-
throw new
|
|
178
|
+
throw new NoSuchElementError(`No signer found with name "${signerName}".`)
|
|
168
179
|
}
|
|
180
|
+
|
|
169
181
|
return signer
|
|
170
182
|
}
|
|
171
183
|
|
|
@@ -186,10 +198,11 @@ export default class WalletManager {
|
|
|
186
198
|
* @overload
|
|
187
199
|
* @param {number} [index] - The index of the account to get (default: 0).
|
|
188
200
|
* @param {Object} [options] - Account options.
|
|
189
|
-
* @param {string} [options.signerName] - The signer name.
|
|
201
|
+
* @param {string} [options.signerName] - The signer name.
|
|
190
202
|
* @returns {Promise<IWalletAccount>} The account.
|
|
191
|
-
* @throws {
|
|
192
|
-
* @throws {
|
|
203
|
+
* @throws {ValueError} If the index is not valid.
|
|
204
|
+
* @throws {NoSuchElementError} If a signer name is given but no signer exists with that name.
|
|
205
|
+
* @throws {InvalidSignerError} If the signer doesn't support account derivation.
|
|
193
206
|
*/
|
|
194
207
|
|
|
195
208
|
/**
|
|
@@ -201,7 +214,7 @@ export default class WalletManager {
|
|
|
201
214
|
* @overload
|
|
202
215
|
* @param {string} signerName - The signer name registered via {@link addSigner}.
|
|
203
216
|
* @returns {Promise<IWalletAccount>} The account.
|
|
204
|
-
* @throws {
|
|
217
|
+
* @throws {NoSuchElementError} If no signer exists with the given name.
|
|
205
218
|
*/
|
|
206
219
|
|
|
207
220
|
/** @abstract */
|
|
@@ -217,8 +230,9 @@ export default class WalletManager {
|
|
|
217
230
|
* @param {Object} [options] - Account options.
|
|
218
231
|
* @param {string} [options.signerName] - The signer name. Omit to use the default signer.
|
|
219
232
|
* @returns {Promise<IWalletAccount>} The account.
|
|
220
|
-
* @throws {
|
|
221
|
-
* @throws {
|
|
233
|
+
* @throws {ValueError} If the path is not valid.
|
|
234
|
+
* @throws {NoSuchElementError} If a signer name is given but no signer exists with that name.
|
|
235
|
+
* @throws {InvalidSignerError} If the signer doesn't support account derivation.
|
|
222
236
|
*/
|
|
223
237
|
async getAccountByPath (path, options = {}) {
|
|
224
238
|
throw new NotImplementedError('getAccountByPath(path, options?)')
|
|
@@ -229,6 +243,8 @@ export default class WalletManager {
|
|
|
229
243
|
*
|
|
230
244
|
* @abstract
|
|
231
245
|
* @returns {Promise<FeeRates>} The fee rates (in base unit).
|
|
246
|
+
* @throws {ProviderRequiredError} If the method requires a provider.
|
|
247
|
+
* @throws {ProviderError} If the provider fails to fetch fee rates.
|
|
232
248
|
*/
|
|
233
249
|
async getFeeRates () {
|
|
234
250
|
throw new NotImplementedError('getFeeRates()')
|
package/types/index.d.ts
CHANGED
|
@@ -7,6 +7,13 @@ export type Transaction = import("./src/wallet-account-read-only.js").Transactio
|
|
|
7
7
|
export type TransactionResult = import("./src/wallet-account-read-only.js").TransactionResult;
|
|
8
8
|
export type TransferOptions = import("./src/wallet-account-read-only.js").TransferOptions;
|
|
9
9
|
export type TransferResult = import("./src/wallet-account-read-only.js").TransferResult;
|
|
10
|
+
export type Finality = import("./src/wallet-account-read-only-simple.js").Finality;
|
|
11
|
+
export type TransactionReceipt = import("./src/wallet-account-read-only-simple.js").TransactionReceipt;
|
|
12
|
+
export type WaitForTransactionTarget = import("./src/wallet-account-read-only-simple.js").WaitForTransactionTarget;
|
|
13
|
+
export type WaitForTransactionOptions = import("./src/wallet-account-read-only-simple.js").WaitForTransactionOptions;
|
|
10
14
|
export type KeyPair = import("./src/wallet-account.js").KeyPair;
|
|
11
|
-
export
|
|
12
|
-
export
|
|
15
|
+
export type ProviderErrorOptions = import("./src/errors.js").ProviderErrorOptions;
|
|
16
|
+
export type TransactionErrorOptions = import("./src/errors.js").TransactionErrorOptions;
|
|
17
|
+
export type TransferErrorOptions = import("./src/errors.js").TransferErrorOptions;
|
|
18
|
+
export { default as WalletAccountReadOnly, IWalletAccountReadOnly, FINALITY } from "./src/wallet-account-read-only.js";
|
|
19
|
+
export { AssertionError, InvalidTokenError, InvalidSignerError, MaximumFeeExceededError, NoSuchElementError, NotImplementedError, ProviderError, ProviderErrorReason, ProviderRequiredError, TimeoutError, TransactionError, TransactionErrorReason, TransferError, TransferErrorReason, UnsupportedOperationError, ValueError, WdkError } from "./src/errors.js";
|