@dynamic-labs/midnight 4.90.0 → 4.91.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/package.cjs +1 -1
  3. package/package.js +1 -1
  4. package/package.json +8 -6
  5. package/src/MidnightWalletConnectors.cjs +6 -0
  6. package/src/MidnightWalletConnectors.js +6 -0
  7. package/src/connectors/MidnightWalletConnector.cjs +41 -0
  8. package/src/connectors/MidnightWalletConnector.d.ts +42 -1
  9. package/src/connectors/MidnightWalletConnector.js +41 -0
  10. package/src/index.cjs +8 -6
  11. package/src/index.d.ts +1 -1
  12. package/src/index.js +4 -2
  13. package/src/types.d.ts +11 -2
  14. package/src/utils/MidnightUiTransaction/MidnightUiTransaction.cjs +109 -0
  15. package/src/utils/MidnightUiTransaction/MidnightUiTransaction.d.ts +2 -2
  16. package/src/utils/MidnightUiTransaction/MidnightUiTransaction.js +105 -0
  17. package/src/waas/DynamicWaasMidnightConnectors.cjs +16 -0
  18. package/src/waas/DynamicWaasMidnightConnectors.d.ts +6 -0
  19. package/src/waas/DynamicWaasMidnightConnectors.js +12 -0
  20. package/src/waas/WaasMidnightWalletConnector.cjs +109 -0
  21. package/src/waas/WaasMidnightWalletConnector.d.ts +44 -0
  22. package/src/waas/WaasMidnightWalletConnector.js +105 -0
  23. package/src/waas/connector/DynamicWaasMidnightConnector.cjs +584 -0
  24. package/src/waas/connector/DynamicWaasMidnightConnector.d.ts +470 -0
  25. package/src/waas/connector/DynamicWaasMidnightConnector.js +580 -0
  26. package/src/waas/connector/buildMidnightWaasConnectedApi.cjs +48 -0
  27. package/src/waas/connector/buildMidnightWaasConnectedApi.d.ts +26 -0
  28. package/src/waas/connector/buildMidnightWaasConnectedApi.js +42 -0
  29. package/src/waas/connector/index.d.ts +1 -0
  30. package/src/waas/index.d.ts +4 -0
  31. package/src/waas/isMidnightWallet.cjs +15 -0
  32. package/src/waas/isMidnightWallet.d.ts +7 -0
  33. package/src/waas/isMidnightWallet.js +11 -0
  34. package/src/wallet/MidnightWallet.cjs +31 -0
  35. package/src/wallet/MidnightWallet.d.ts +15 -0
  36. package/src/wallet/MidnightWallet.js +31 -0
@@ -0,0 +1,584 @@
1
+ 'use client'
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var _tslib = require('../../../_virtual/_tslib.cjs');
7
+ var sdkApiCore = require('@dynamic-labs/sdk-api-core');
8
+ var utils = require('@dynamic-labs/utils');
9
+ var waas = require('@dynamic-labs/waas');
10
+ var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
11
+ var logger = require('@dynamic-labs/logger');
12
+ var constants = require('../../constants.cjs');
13
+ var MidnightUiTransaction = require('../../utils/MidnightUiTransaction/MidnightUiTransaction.cjs');
14
+ var MidnightWallet = require('../../wallet/MidnightWallet.cjs');
15
+ var WaasMidnightWalletConnector = require('../WaasMidnightWalletConnector.cjs');
16
+ var buildMidnightWaasConnectedApi = require('./buildMidnightWaasConnectedApi.cjs');
17
+
18
+ /**
19
+ * WaaS (Wallet-as-a-Service) connector for Midnight blockchain.
20
+ * Extends the WaaS-specific Midnight base with MPC wallet functionality.
21
+ */
22
+ class DynamicWaasMidnightConnector extends waas.withDynamicWaas(WaasMidnightWalletConnector.WaasMidnightWalletConnector) {
23
+ constructor(props) {
24
+ super('Dynamic Waas', props);
25
+ this.ChainWallet = MidnightWallet.MidnightWallet;
26
+ this.name = 'Dynamic Waas';
27
+ this.overrideKey = 'dynamicwaas';
28
+ this.isEmbeddedWallet = true;
29
+ this.logger = new logger.Logger('DynamicWaasMidnightConnector');
30
+ this.walletUiUtils = props.walletUiUtils;
31
+ }
32
+ /**
33
+ * Signs a message with additional context information
34
+ * @param message - The message to sign (string or object with raw property)
35
+ * @param context - Additional context for the message signing
36
+ * @returns The signed message as a string
37
+ * @throws {Error} Method not implemented for Midnight
38
+ */
39
+ /* eslint-disable @typescript-eslint/no-unused-vars */
40
+ signMessageWithContext({ message, context, }) {
41
+ /* eslint-enable @typescript-eslint/no-unused-vars */
42
+ throw new Error('Method not implemented.');
43
+ }
44
+ /**
45
+ * Override setVerifiedCredentials to filter and set Midnight WaaS credentials
46
+ * Filters for credentials with walletName === 'dynamicwaas' and chain === 'midnight'
47
+ */
48
+ setVerifiedCredentials(verifiedCredentials) {
49
+ this.verifiedCredentials = verifiedCredentials.filter((vc) => vc.walletName === 'dynamicwaas' && vc.chain === 'midnight');
50
+ }
51
+ /**
52
+ * Override setEnvironmentId to ensure it's set on the logger
53
+ */
54
+ setEnvironmentId(environmentId) {
55
+ super.setEnvironmentId(environmentId);
56
+ }
57
+ /**
58
+ * Gets the wallet client for a specific account address and sets it as active
59
+ * @param accountAddress - The Midnight account address
60
+ * @returns The WaaS wallet client instance
61
+ */
62
+ getWalletClientByAddress(_a) {
63
+ return _tslib.__awaiter(this, arguments, void 0, function* ({ accountAddress, }) {
64
+ this.setActiveAccountAddress(accountAddress);
65
+ return this.getWaasWalletClient();
66
+ });
67
+ }
68
+ /**
69
+ * Gets the currently active account address
70
+ * @returns The active account address or undefined if not set
71
+ */
72
+ getActiveAccountAddress() {
73
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
74
+ return this.activeAccountAddress;
75
+ });
76
+ }
77
+ /**
78
+ * Sets the active account address for this connector
79
+ * @param accountAddress - The Midnight account address to set as active
80
+ */
81
+ setActiveAccountAddress(accountAddress) {
82
+ this.activeAccountAddress = accountAddress;
83
+ }
84
+ /**
85
+ * Called when the user selects this wallet in the modal.
86
+ * Sets the active account so getAddress/getNetwork work correctly during network validation.
87
+ */
88
+ afterWalletSelectHook(walletAddress) {
89
+ this.setActiveAccountAddress(walletAddress);
90
+ }
91
+ /**
92
+ * Requires and returns the signed session ID
93
+ * @returns The signed session ID
94
+ * @throws {DynamicError} If signed session ID is not available
95
+ */
96
+ requireSignedSessionId() {
97
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
98
+ var _a;
99
+ const signedSessionId = yield ((_a = this.getSignedSessionId) === null || _a === void 0 ? void 0 : _a.call(this));
100
+ if (!signedSessionId) {
101
+ throw new utils.DynamicError('Signed session ID is required');
102
+ }
103
+ return signedSessionId;
104
+ });
105
+ }
106
+ /**
107
+ * Validates that the expected wallet address matches the active wallet
108
+ * Updates the active account address if it doesn't match
109
+ * @param expectedAddress - The expected wallet address to validate
110
+ * @throws {DynamicError} If signed session ID is not available or account is not found
111
+ */
112
+ validateActiveWallet(expectedAddress) {
113
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
114
+ var _a;
115
+ const walletClient = yield this.getWaasWalletClient();
116
+ const signedSessionId = yield this.requireSignedSessionId();
117
+ const targetWallet = yield walletClient.getWallet({
118
+ accountAddress: expectedAddress,
119
+ authToken: (_a = this.getAuthToken) === null || _a === void 0 ? void 0 : _a.call(this),
120
+ signedSessionId,
121
+ });
122
+ if (!targetWallet) {
123
+ throw new utils.DynamicError('Account not found');
124
+ }
125
+ const isWalletActive = walletConnectorCore.isSameAddress(targetWallet.accountAddress, this.activeAccountAddress || '', this.connectedChain);
126
+ if (!isWalletActive) {
127
+ this.activeAccountAddress = targetWallet.accountAddress;
128
+ }
129
+ });
130
+ }
131
+ /**
132
+ * When active account is not set (e.g. primary wallet restored from session),
133
+ * fall back to the first Midnight verified credential so signing and getAddress work.
134
+ */
135
+ ensureActiveAccountFromVerifiedCredentials() {
136
+ var _a, _b;
137
+ if (this.activeAccountAddress)
138
+ return;
139
+ const address = (_b = (_a = this.verifiedCredentials) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.address;
140
+ if (typeof address === 'string') {
141
+ this.setActiveAccountAddress(address);
142
+ }
143
+ }
144
+ /**
145
+ * Gets the active wallet address
146
+ * Falls back to verified credential address if no active account is set
147
+ * @returns The wallet address or empty string if not found
148
+ */
149
+ getAddress() {
150
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
151
+ this.ensureActiveAccountFromVerifiedCredentials();
152
+ return this.activeAccountAddress || '';
153
+ });
154
+ }
155
+ /**
156
+ * Connects to the WaaS wallet client
157
+ */
158
+ connect() {
159
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
160
+ yield this.getWaasWalletClient();
161
+ });
162
+ }
163
+ /**
164
+ * Disconnects from the wallet (no-op for WaaS)
165
+ */
166
+ disconnect() {
167
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
168
+ // WaaS doesn't require explicit disconnect
169
+ });
170
+ }
171
+ /**
172
+ * Signs a message using the wallet UI utils for user interaction
173
+ * @param message - The message to sign
174
+ * @returns The signed message as a string
175
+ */
176
+ signMessage(message) {
177
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
178
+ return this.walletUiUtils.signMessage({
179
+ handler: () => _tslib.__awaiter(this, void 0, void 0, function* () { return this.internalSignMessage(message); }),
180
+ message,
181
+ walletConnector: this,
182
+ });
183
+ });
184
+ }
185
+ /**
186
+ * Internal method to sign a message with MFA token
187
+ * @param message - The message to sign
188
+ * @returns The signed message as a string
189
+ * @throws {DynamicError} If active account address is not set
190
+ * @throws {DynamicError} If signed session ID is not available
191
+ */
192
+ internalSignMessage(message) {
193
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
194
+ var _a, _b;
195
+ this.ensureActiveAccountFromVerifiedCredentials();
196
+ const walletClient = yield this.getWaasWalletClient();
197
+ if (!this.activeAccountAddress) {
198
+ throw new utils.DynamicError('Active account address is required');
199
+ }
200
+ const signedSessionId = yield this.requireSignedSessionId();
201
+ const mfaToken = yield ((_a = this.getMfaToken) === null || _a === void 0 ? void 0 : _a.call(this, {
202
+ mfaAction: sdkApiCore.MFAAction.WalletWaasSign,
203
+ }));
204
+ const password = yield this.getPasswordIfNeeded({
205
+ accountAddress: this.activeAccountAddress,
206
+ });
207
+ const signedMessage = yield walletClient.signMessage({
208
+ accountAddress: this.activeAccountAddress,
209
+ authToken: (_b = this.getAuthToken) === null || _b === void 0 ? void 0 : _b.call(this),
210
+ message,
211
+ mfaToken,
212
+ password,
213
+ signedSessionId,
214
+ });
215
+ return signedMessage;
216
+ });
217
+ }
218
+ /**
219
+ * Ends the current session and clears the active account address
220
+ */
221
+ endSession() {
222
+ const _super = Object.create(null, {
223
+ endSession: { get: () => super.endSession }
224
+ });
225
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
226
+ yield _super.endSession.call(this);
227
+ this.activeAccountAddress = undefined;
228
+ });
229
+ }
230
+ /**
231
+ * Signs and finalizes (ZK proves) a Midnight transaction via MPC.
232
+ * Step 2 of the 3-step flow: createTransferTransaction → signTransaction → submitTransaction.
233
+ *
234
+ * @param serializedTransaction - Base64-encoded unsigned UnprovenTransaction
235
+ * @returns Base64-encoded FinalizedTransaction, ready for submitTransaction
236
+ */
237
+ signTransaction(serializedTransaction) {
238
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
239
+ var _a, _b, _c, _d;
240
+ if (!this.activeAccountAddress) {
241
+ throw new utils.DynamicError('Active account address is required');
242
+ }
243
+ const walletClient = yield this.getWaasWalletClient();
244
+ const signedSessionId = yield this.requireSignedSessionId();
245
+ const mfaToken = yield ((_a = this.getMfaToken) === null || _a === void 0 ? void 0 : _a.call(this, {
246
+ mfaAction: sdkApiCore.MFAAction.WalletWaasSign,
247
+ }));
248
+ const password = yield this.getPasswordIfNeeded({
249
+ accountAddress: this.activeAccountAddress,
250
+ });
251
+ // Derive the chain name (e.g. "mainnet" / "preview") — not the numeric
252
+ // network ID — because `chainId` is typed as a string and resolved by
253
+ // name on the wallet-client side. Passing a stringified numeric id
254
+ // (`"1"`) reaches `getNetworkIdFromChainId`, which doesn't recognize it.
255
+ const selectedNetwork = this.getSelectedNetwork();
256
+ const isTestnet = (_c = (_b = selectedNetwork === null || selectedNetwork === void 0 ? void 0 : selectedNetwork.name) === null || _b === void 0 ? void 0 : _b.toLowerCase().includes('testnet')) !== null && _c !== void 0 ? _c : false;
257
+ const chainId = isTestnet ? 'preview' : 'mainnet';
258
+ // Sign the transaction using MPC
259
+ const signature = yield walletClient.signTransaction({
260
+ authToken: (_d = this.getAuthToken) === null || _d === void 0 ? void 0 : _d.call(this),
261
+ chainId,
262
+ mfaToken,
263
+ password,
264
+ senderAddress: this.activeAccountAddress,
265
+ signedSessionId,
266
+ transaction: serializedTransaction,
267
+ });
268
+ return signature;
269
+ });
270
+ }
271
+ /**
272
+ * Creates a UI transaction for the send balance flow
273
+ * @param from - The sender's address
274
+ * @returns A new IUITransaction instance
275
+ */
276
+ createUiTransaction(from) {
277
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
278
+ yield this.validateActiveWallet(from);
279
+ return new MidnightUiTransaction.MidnightUiTransaction({
280
+ // No on-chain dust-fee estimate is exposed yet; DUST fees are charged
281
+ // at submit time. TODO: surface a real estimate from the wallet client.
282
+ fetchDustFee: () => _tslib.__awaiter(this, void 0, void 0, function* () { return BigInt(0); }),
283
+ from,
284
+ // Balances come from the connector (MPC state), reusing the cached
285
+ // synced wallet — never from the redcoast public-balance endpoint,
286
+ // which doesn't support Midnight addresses.
287
+ getShieldedBalance: () => _tslib.__awaiter(this, void 0, void 0, function* () {
288
+ var _a;
289
+ return (_a = (yield this.getBalances()).shielded[constants.MIDNIGHT_NATIVE_SHIELDED_TOKEN_TYPE]) !== null && _a !== void 0 ? _a : BigInt(0);
290
+ }),
291
+ getUnshieldedBalance: () => _tslib.__awaiter(this, void 0, void 0, function* () {
292
+ var _b;
293
+ return (_b = (yield this.getBalances()).unshielded[constants.MIDNIGHT_NATIVE_TOKEN_TYPE]) !== null && _b !== void 0 ? _b : BigInt(0);
294
+ }),
295
+ onSubmit: (tx) => _tslib.__awaiter(this, void 0, void 0, function* () {
296
+ if (tx.to === undefined || tx.value === undefined) {
297
+ throw new utils.DynamicError('Recipient and amount are required before submitting');
298
+ }
299
+ // 3-step Midnight transfer: build → sign (ZK prove) → submit.
300
+ const { serializedTransaction } = yield this.createTransferTransaction({
301
+ transfers: [
302
+ {
303
+ amount: tx.value.toString(),
304
+ recipientAddress: tx.to,
305
+ type: tx.activePool,
306
+ },
307
+ ],
308
+ });
309
+ const finalized = yield this.signTransaction(serializedTransaction);
310
+ // Return the tx hash so the Send flow advances to the success view.
311
+ const { txHash } = yield this.submitTransaction(finalized);
312
+ return txHash;
313
+ }),
314
+ });
315
+ });
316
+ }
317
+ /**
318
+ * Returns the provider.
319
+ * For WaaS, there is no traditional browser provider.
320
+ */
321
+ getProvider() {
322
+ return undefined;
323
+ }
324
+ /**
325
+ * Creates a new Midnight wallet account.
326
+ * Midnight wallets automatically create multiple address types:
327
+ * - Primary: unshielded address (for public transfers)
328
+ * - Additional: dust address (for gas/fee management) and shielded address (for private transfers)
329
+ *
330
+ * All address types are derived from a single keygen using different BIP-44 derivation paths.
331
+ *
332
+ * @param thresholdSignatureScheme - The threshold signature scheme (default: 'TWO_OF_TWO')
333
+ * @param password - Optional password for the wallet
334
+ * @returns The created wallet account information with primary (unshielded) address
335
+ */
336
+ createWalletAccount() {
337
+ const _super = Object.create(null, {
338
+ createWalletAccount: { get: () => super.createWalletAccount }
339
+ });
340
+ return _tslib.__awaiter(this, arguments, void 0, function* ({ thresholdSignatureScheme = 'TWO_OF_TWO', password, } = {}) {
341
+ // Midnight automatically creates all address types (unshielded, dust, shielded)
342
+ // from a single keygen. The primary address is unshielded, additional addresses
343
+ // (dust and shielded) are returned as additionalAddresses from the server.
344
+ return _super.createWalletAccount.call(this, {
345
+ password,
346
+ thresholdSignatureScheme,
347
+ });
348
+ });
349
+ }
350
+ // Passes through the wallet client's shape. `dust` carries `{ balance, cap }`
351
+ // (wallet-SDK v4 — cap = summed dust maxCap). `dustSynced` is false while dust
352
+ // finishes syncing in the background (balance resolves on shielded+unshielded
353
+ // first). Matches the base `IDynamicWaasConnector` / `DynamicWaasMixin`.
354
+ getPrivateBalance() {
355
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
356
+ var _a, _b, _c, _d;
357
+ this.ensureActiveAccountFromVerifiedCredentials();
358
+ if (!this.activeAccountAddress) {
359
+ throw new utils.DynamicError('Active account address is required');
360
+ }
361
+ const walletClient = yield this.getWaasWalletClient();
362
+ const signedSessionId = yield this.requireSignedSessionId();
363
+ const mfaToken = yield ((_a = this.getMfaToken) === null || _a === void 0 ? void 0 : _a.call(this, {
364
+ mfaAction: sdkApiCore.MFAAction.WalletWaasSign,
365
+ }));
366
+ const elevatedAccessToken = yield ((_b = this.getElevatedAccessToken) === null || _b === void 0 ? void 0 : _b.call(this, {
367
+ scope: sdkApiCore.TokenScope.Walletsign,
368
+ }));
369
+ const result = yield walletClient.getPrivateBalance({
370
+ accountAddress: this.activeAccountAddress,
371
+ authToken: (_c = this.getAuthToken) === null || _c === void 0 ? void 0 : _c.call(this),
372
+ elevatedAccessToken,
373
+ mfaToken,
374
+ signedSessionId,
375
+ });
376
+ // Defensive: wallet-client builds that predate the dust decouple don't
377
+ // return `dustSynced` — treat those as already-synced (no loading state),
378
+ // preserving prior behavior until the client is republished.
379
+ return Object.assign(Object.assign({}, result), { dustSynced: (_d = result.dustSynced) !== null && _d !== void 0 ? _d : true });
380
+ });
381
+ }
382
+ /**
383
+ * Exposes the same `ConnectedAPI` surface injected Midnight wallets provide,
384
+ * adapted from WaaS MPC state. This is what lets the shared
385
+ * `MidnightWalletConnector` balance methods (`getShieldedBalances`,
386
+ * `getUnshieldedBalances`, `getDustBalance`) work for WaaS wallets — they all
387
+ * delegate to `getConnectedAPI()`, which throws (returns undefined) by
388
+ * default. Address lookups and transfers stay on the WaaS-specific paths.
389
+ */
390
+ getConnectedAPI() {
391
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
392
+ return buildMidnightWaasConnectedApi.buildMidnightWaasConnectedApi(() => this.getPrivateBalance());
393
+ });
394
+ }
395
+ /**
396
+ * Adapts the WaaS MPC `getPrivateBalance()` result to the shape
397
+ * `MidnightWallet.getFormattedBalances()` expects. The base
398
+ * `MidnightWalletConnector.getBalances()` throws unconditionally (it does not
399
+ * route through `getConnectedAPI`), so WaaS still overrides it directly —
400
+ * here in a single `getPrivateBalance()` call.
401
+ *
402
+ * Cap-tolerant: `dust.cap` is 0n on browser-wallet-client <= 1.0.16 and the
403
+ * real cap once the wallet-SDK-v4 change is published (no code change needed).
404
+ */
405
+ getBalances() {
406
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
407
+ const { shielded, unshielded, dust, dustSynced } = yield this.getPrivateBalance();
408
+ return {
409
+ dust: buildMidnightWaasConnectedApi.normalizeDustBalance(dust),
410
+ // True while dust is still catching up (balance resolved on
411
+ // shielded+unshielded). Drives the "dust loading" UI; only WaaS sets it.
412
+ dustSyncing: !dustSynced,
413
+ shielded: buildMidnightWaasConnectedApi.toBigIntBalanceMap(shielded),
414
+ unshielded: buildMidnightWaasConnectedApi.toBigIntBalanceMap(unshielded),
415
+ };
416
+ });
417
+ }
418
+ registerDust() {
419
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
420
+ var _a, _b, _c;
421
+ this.ensureActiveAccountFromVerifiedCredentials();
422
+ if (!this.activeAccountAddress) {
423
+ throw new utils.DynamicError('Active account address is required');
424
+ }
425
+ const walletClient = yield this.getWaasWalletClient();
426
+ const signedSessionId = yield this.requireSignedSessionId();
427
+ const mfaToken = yield ((_a = this.getMfaToken) === null || _a === void 0 ? void 0 : _a.call(this, {
428
+ mfaAction: sdkApiCore.MFAAction.WalletWaasSign,
429
+ }));
430
+ const elevatedAccessToken = yield ((_b = this.getElevatedAccessToken) === null || _b === void 0 ? void 0 : _b.call(this, {
431
+ scope: sdkApiCore.TokenScope.Walletsign,
432
+ }));
433
+ return walletClient.registerDust({
434
+ accountAddress: this.activeAccountAddress,
435
+ authToken: (_c = this.getAuthToken) === null || _c === void 0 ? void 0 : _c.call(this),
436
+ elevatedAccessToken,
437
+ mfaToken,
438
+ signedSessionId,
439
+ });
440
+ });
441
+ }
442
+ /**
443
+ * One-call transfer: runs the full build → sign (ZK prove) → submit flow and
444
+ * returns the broadcast tx hash. This is what backs `wallet.sendBalance` for
445
+ * embedded Midnight wallets; use the individual steps
446
+ * (`createTransferTransaction` / `signTransaction` / `submitTransaction`) when
447
+ * you need to inspect or persist the transaction between stages.
448
+ */
449
+ createTransfer(params) {
450
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
451
+ const { serializedTransaction } = yield this.createTransferTransaction(params);
452
+ const finalizedTransaction = yield this.signTransaction(serializedTransaction);
453
+ const { txHash } = yield this.submitTransaction(finalizedTransaction);
454
+ return txHash;
455
+ });
456
+ }
457
+ /**
458
+ * Step 1 of the Midnight transfer flow.
459
+ * Builds an unsigned UnprovenTransaction from one or more transfer specs.
460
+ * Accepts any combination of unshielded / shielded transfers in a single tx.
461
+ * No MPC, no proof server, no broadcast.
462
+ */
463
+ createTransferTransaction(params) {
464
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
465
+ var _a, _b, _c;
466
+ this.ensureActiveAccountFromVerifiedCredentials();
467
+ if (!this.activeAccountAddress) {
468
+ throw new utils.DynamicError('Active account address is required');
469
+ }
470
+ const walletClient = yield this.getWaasWalletClient();
471
+ const signedSessionId = yield this.requireSignedSessionId();
472
+ const mfaToken = yield ((_a = this.getMfaToken) === null || _a === void 0 ? void 0 : _a.call(this, {
473
+ mfaAction: sdkApiCore.MFAAction.WalletWaasSign,
474
+ }));
475
+ const elevatedAccessToken = yield ((_b = this.getElevatedAccessToken) === null || _b === void 0 ? void 0 : _b.call(this, {
476
+ scope: sdkApiCore.TokenScope.Walletsign,
477
+ }));
478
+ return walletClient.createTransferTransaction({
479
+ accountAddress: this.activeAccountAddress,
480
+ authToken: (_c = this.getAuthToken) === null || _c === void 0 ? void 0 : _c.call(this),
481
+ elevatedAccessToken,
482
+ mfaToken,
483
+ signedSessionId,
484
+ transfers: params.transfers,
485
+ ttlMinutes: params.ttlMinutes,
486
+ });
487
+ });
488
+ }
489
+ /**
490
+ * Release UTXOs reserved by an abandoned draft or signed-but-never-submitted
491
+ * transaction. Accepts either an unsigned (Step 1 output) or finalized
492
+ * (Step 2 output) serialized transaction.
493
+ */
494
+ revertTransaction(serializedTransaction) {
495
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
496
+ return this.callRevertTransaction({ transaction: serializedTransaction });
497
+ });
498
+ }
499
+ /**
500
+ * Revert every finalized-but-unsubmitted transaction the SDK knows about
501
+ * (`state.pending.all`). Does not cover unfinalized Step-1-only drafts —
502
+ * those are never registered with the pending-tx service by design. If
503
+ * UTXOs are still stuck after this, use `resetWalletCache()`.
504
+ */
505
+ revertAllPending() {
506
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
507
+ return this.callRevertTransaction({ all: true });
508
+ });
509
+ }
510
+ /**
511
+ * Nuclear reset. Stops the cached wallet, clears its persisted IndexedDB
512
+ * entry, and forces the next operation to cold-sync from chain. Midnight's
513
+ * officially recommended workaround when UTXOs get stuck in pending state
514
+ * that can't be reverted through the normal API.
515
+ */
516
+ resetWalletCache() {
517
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
518
+ return this.callRevertTransaction({ resetCache: true });
519
+ });
520
+ }
521
+ // Internal shared glue: resolves auth + forwards to the iframe handler.
522
+ callRevertTransaction(args) {
523
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
524
+ var _a, _b, _c;
525
+ this.ensureActiveAccountFromVerifiedCredentials();
526
+ if (!this.activeAccountAddress) {
527
+ throw new utils.DynamicError('Active account address is required');
528
+ }
529
+ const walletClient = yield this.getWaasWalletClient();
530
+ const signedSessionId = yield this.requireSignedSessionId();
531
+ const mfaToken = yield ((_a = this.getMfaToken) === null || _a === void 0 ? void 0 : _a.call(this, {
532
+ mfaAction: sdkApiCore.MFAAction.WalletWaasSign,
533
+ }));
534
+ const elevatedAccessToken = yield ((_b = this.getElevatedAccessToken) === null || _b === void 0 ? void 0 : _b.call(this, {
535
+ scope: sdkApiCore.TokenScope.Walletsign,
536
+ }));
537
+ return walletClient.revertTransaction({
538
+ accountAddress: this.activeAccountAddress,
539
+ all: args.all,
540
+ authToken: (_c = this.getAuthToken) === null || _c === void 0 ? void 0 : _c.call(this),
541
+ elevatedAccessToken,
542
+ mfaToken,
543
+ resetCache: args.resetCache,
544
+ signedSessionId,
545
+ transaction: args.transaction,
546
+ });
547
+ });
548
+ }
549
+ /**
550
+ * Step 3 of the unshielded public transfer flow.
551
+ * Broadcasts a finalized transaction via the wallet's NodeClient.
552
+ * Optionally waits for the unshielded balance to stabilize.
553
+ */
554
+ submitTransaction(serializedFinalizedTransaction, options) {
555
+ return _tslib.__awaiter(this, void 0, void 0, function* () {
556
+ var _a, _b, _c;
557
+ this.ensureActiveAccountFromVerifiedCredentials();
558
+ if (!this.activeAccountAddress) {
559
+ throw new utils.DynamicError('Active account address is required');
560
+ }
561
+ const walletClient = yield this.getWaasWalletClient();
562
+ const signedSessionId = yield this.requireSignedSessionId();
563
+ const mfaToken = yield ((_a = this.getMfaToken) === null || _a === void 0 ? void 0 : _a.call(this, {
564
+ mfaAction: sdkApiCore.MFAAction.WalletWaasSign,
565
+ }));
566
+ const elevatedAccessToken = yield ((_b = this.getElevatedAccessToken) === null || _b === void 0 ? void 0 : _b.call(this, {
567
+ scope: sdkApiCore.TokenScope.Walletsign,
568
+ }));
569
+ return walletClient.submitTransaction({
570
+ accountAddress: this.activeAccountAddress,
571
+ authToken: (_c = this.getAuthToken) === null || _c === void 0 ? void 0 : _c.call(this),
572
+ balanceBefore: options === null || options === void 0 ? void 0 : options.balanceBefore,
573
+ elevatedAccessToken,
574
+ mfaToken,
575
+ signedSessionId,
576
+ tokenType: options === null || options === void 0 ? void 0 : options.tokenType,
577
+ transaction: serializedFinalizedTransaction,
578
+ waitForBalance: options === null || options === void 0 ? void 0 : options.waitForBalance,
579
+ });
580
+ });
581
+ }
582
+ }
583
+
584
+ exports.DynamicWaasMidnightConnector = DynamicWaasMidnightConnector;