@dynamic-labs/midnight 4.90.0 → 4.91.0

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 (35) hide show
  1. package/CHANGELOG.md +17 -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 +12 -0
  8. package/src/connectors/MidnightWalletConnector.d.ts +6 -1
  9. package/src/connectors/MidnightWalletConnector.js +12 -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 +571 -0
  24. package/src/waas/connector/DynamicWaasMidnightConnector.d.ts +444 -0
  25. package/src/waas/connector/DynamicWaasMidnightConnector.js +567 -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 +2 -0
  35. package/src/wallet/MidnightWallet.js +2 -0
@@ -0,0 +1,444 @@
1
+ import type { ConnectedAPI, TokenType } from '@midnight-ntwrk/dapp-connector-api';
2
+ import { JwtVerifiedCredential, MFAAction, SignMessageContext, TokenScope } from '@dynamic-labs/sdk-api-core';
3
+ import { IUITransaction, WalletUiUtils } from '@dynamic-labs/types';
4
+ import { IDynamicWaasConnector, InternalWalletConnector } from '@dynamic-labs/wallet-connector-core';
5
+ import { Logger } from '@dynamic-labs/logger';
6
+ import type { DynamicWaasMidnightConnectorProps } from '../../types';
7
+ import { MidnightWallet } from '../../wallet/MidnightWallet';
8
+ import { WaasMidnightWalletConnector } from '../WaasMidnightWalletConnector';
9
+ declare const DynamicWaasMidnightConnector_base: (abstract new (...args: any[]) => {
10
+ [x: string]: any;
11
+ name: string;
12
+ overrideKey: string;
13
+ isEmbeddedWallet: boolean;
14
+ getSignedSessionId?: (() => Promise<string>) | undefined;
15
+ getSessionPublicKey?: (() => Promise<string | undefined>) | undefined;
16
+ getMfaToken?: ((props?: {
17
+ mfaAction?: MFAAction | undefined;
18
+ } | undefined) => Promise<string | undefined>) | undefined;
19
+ getWalletPassword?: import("@dynamic-labs/wallet-connector-core").GetWalletPasswordFn | undefined;
20
+ getAuthToken?: (() => string) | undefined;
21
+ getElevatedAccessToken?: ((props: {
22
+ scope: TokenScope;
23
+ }) => Promise<string | undefined>) | undefined;
24
+ onUnauthorized?: (() => void | Promise<void>) | undefined;
25
+ environmentId?: string | undefined;
26
+ baseApiUrl?: string | undefined;
27
+ relayUrl?: string | undefined;
28
+ baseClientKeysharesRelayApiUrl?: string | undefined;
29
+ dynamicWaasClient: import("@dynamic-labs-wallet/browser-wallet-client").DynamicWalletClient | undefined;
30
+ chainName: string;
31
+ authMode: "cookie" | "header";
32
+ logger: Logger;
33
+ __exportHandler: import("@dynamic-labs/waas").WaasExportHandler;
34
+ validateActiveWallet(expectedAddress: string): Promise<void>;
35
+ setGetAuthTokenFunction(getAuthToken: () => string): void;
36
+ setOnUnauthorizedFunction(onUnauthorized: () => void | Promise<void>): void;
37
+ setWaasAuthMode(authMode: "cookie" | "header"): void;
38
+ setGetMfaTokenFunction(getMfaToken: (props?: {
39
+ mfaAction?: MFAAction | undefined;
40
+ } | undefined) => Promise<string | undefined>): void;
41
+ setGetElevatedAccessTokenFunction(getElevatedAccessToken: (params: {
42
+ scope: TokenScope;
43
+ }) => Promise<string | undefined>): void;
44
+ setGetWalletPasswordFunction(getWalletPassword: import("@dynamic-labs/wallet-connector-core").GetWalletPasswordFn): void;
45
+ getPasswordIfNeeded({ accountAddress, }: {
46
+ accountAddress: string;
47
+ }): Promise<string | undefined>;
48
+ getPassword({ accountAddress, }: {
49
+ accountAddress: string;
50
+ }): Promise<string | undefined>;
51
+ setEnvironmentId(environmentId: string): void;
52
+ setBaseApiUrl(baseApiUrl: string): void;
53
+ setBaseClientKeysharesRelayApiUrl(baseClientKeysharesRelayApiUrl?: string | undefined): void;
54
+ setRelayUrl(relayUrl: string): void;
55
+ setGetSignedSessionIdFunction(getSignedSessionId: () => Promise<string>): void;
56
+ setGetSessionPublicKeyFunction(getSessionPublicKey: () => Promise<string | undefined>): void;
57
+ delegateKeyShares({ accountAddress, password, }: {
58
+ accountAddress: string;
59
+ password?: string | undefined;
60
+ }): Promise<void>;
61
+ createDynamicWaasClient(traceContext?: import("dist/packages/waas/utils/instrumentation").TraceContext | undefined): Promise<import("@dynamic-labs-wallet/browser-wallet-client").DynamicWalletClient>;
62
+ getWaasWalletClient(traceContext?: import("dist/packages/waas/utils/instrumentation").TraceContext | undefined, { forceRebuild }?: {
63
+ forceRebuild?: boolean | undefined;
64
+ } | undefined): Promise<import("@dynamic-labs-wallet/browser-wallet-client").DynamicWalletClient>;
65
+ createWalletAccount({ thresholdSignatureScheme, password, bitcoinConfig, }?: {
66
+ thresholdSignatureScheme?: string | undefined;
67
+ password?: string | undefined;
68
+ bitcoinConfig?: import("@dynamic-labs-wallet/browser-wallet-client").BitcoinConfig | undefined;
69
+ } | undefined): Promise<{
70
+ chainName: string;
71
+ accountAddress: string;
72
+ publicKeyHex: string;
73
+ rawPublicKey: string | Uint8Array | undefined;
74
+ }>;
75
+ importPrivateKey({ privateKey, thresholdSignatureScheme, publicAddressCheck, addressType, legacyWalletId, password, }: {
76
+ privateKey: string;
77
+ thresholdSignatureScheme?: string | undefined;
78
+ publicAddressCheck?: string | undefined;
79
+ addressType?: string | undefined;
80
+ legacyWalletId?: string | undefined;
81
+ password?: string | undefined;
82
+ }): Promise<void>;
83
+ exportPrivateKey({ accountAddress, displayContainer, password, }?: {
84
+ accountAddress?: string | undefined;
85
+ displayContainer?: HTMLIFrameElement | undefined;
86
+ password?: string | undefined;
87
+ } | undefined): Promise<void>;
88
+ getExportHandler(): {
89
+ clear: () => void;
90
+ };
91
+ exportClientKeyshares({ accountAddress, password, }: {
92
+ accountAddress: string;
93
+ password?: string | undefined;
94
+ }): Promise<void>;
95
+ backupKeySharesToGoogleDrive({ accountAddress, password, googleDriveAccessToken, }: {
96
+ accountAddress: string;
97
+ password?: string | undefined;
98
+ googleDriveAccessToken?: string | undefined;
99
+ }): Promise<void>;
100
+ exportClientKeysharesFromGoogleDrive({ accountAddress, password, }: {
101
+ accountAddress: string;
102
+ password?: string | undefined;
103
+ }): Promise<void>;
104
+ backupKeySharesToICloud({ accountAddress, password, }: {
105
+ accountAddress: string;
106
+ password?: string | undefined;
107
+ }): Promise<void>;
108
+ displayICloudSignIn({ displayContainer, }: {
109
+ displayContainer: HTMLElement;
110
+ }): Promise<void>;
111
+ hideICloudSignIn(): Promise<void>;
112
+ isICloudAuthenticated(): Promise<boolean>;
113
+ refreshWalletAccountShares({ accountAddress, password, }: {
114
+ accountAddress: string;
115
+ password?: string | undefined;
116
+ }): Promise<void>;
117
+ reshareWalletAccountShares({ accountAddress, thresholdSignatureScheme, password, }: {
118
+ accountAddress: string;
119
+ thresholdSignatureScheme: string;
120
+ password?: string | undefined;
121
+ }): Promise<void>;
122
+ revokeDelegation({ accountAddress, password, }: {
123
+ accountAddress: string;
124
+ password?: string | undefined;
125
+ }): Promise<void>;
126
+ updatePassword({ accountAddress, existingPassword, newPassword, }: {
127
+ accountAddress: string;
128
+ existingPassword?: string | undefined;
129
+ newPassword: string;
130
+ }): Promise<void>;
131
+ setPassword({ accountAddress, newPassword, }: {
132
+ accountAddress: string;
133
+ newPassword: string; /**
134
+ * Disconnects from the wallet (no-op for WaaS)
135
+ */
136
+ }): Promise<void>;
137
+ signRawMessage({ accountAddress, message, password, }: {
138
+ accountAddress: string;
139
+ message: string;
140
+ password?: string | undefined;
141
+ }): Promise<string>;
142
+ unlockWallet({ accountAddress, password, }: {
143
+ accountAddress: string;
144
+ password?: string | undefined;
145
+ }): Promise<import("@dynamic-labs-wallet/browser-wallet-client").GetWalletResponse>;
146
+ getWalletRecoveryState({ accountAddress, }: {
147
+ accountAddress: string;
148
+ }): Promise<import("@dynamic-labs-wallet/browser-wallet-client").WalletRecoveryState>;
149
+ endSession(reason?: import("@dynamic-labs-sdk/client").LogoutReason | undefined): Promise<void>;
150
+ getActiveAccountAddress(): Promise<string | undefined>;
151
+ getConnectedAccounts(): Promise<string[]>;
152
+ generateTraceId(): string;
153
+ buildErrorInstrumentContext(error: unknown): {
154
+ errorCode: string | import("@dynamic-labs/utils").ErrorCode;
155
+ errorMessage: string;
156
+ errorType: string | undefined;
157
+ } | {
158
+ errorCode: string;
159
+ errorMessage?: undefined;
160
+ errorType?: undefined;
161
+ };
162
+ instrument(message: string, context: import("@dynamic-labs/logger").InstrumentOptions & import("dist/packages/waas/utils/instrumentation").InstrumentContext & Record<string, any>): void;
163
+ instrumentAsync<T_1>({ operation, resource, fn, context, }: {
164
+ operation: string;
165
+ resource: string;
166
+ fn: (timing: import("dist/packages/waas/utils/instrumentation").InstrumentationTimer) => Promise<T_1>;
167
+ context?: Record<string, any> | undefined;
168
+ }): Promise<T_1>;
169
+ getPrivateBalance(): Promise<{
170
+ unshielded: Record<string, string>;
171
+ shielded: Record<string, string>;
172
+ dust: {
173
+ balance: string;
174
+ cap: string;
175
+ };
176
+ address: string;
177
+ dustSynced: boolean;
178
+ }>;
179
+ }) & typeof WaasMidnightWalletConnector;
180
+ /**
181
+ * WaaS (Wallet-as-a-Service) connector for Midnight blockchain.
182
+ * Extends the WaaS-specific Midnight base with MPC wallet functionality.
183
+ *
184
+ * Phase 1: Supports unshielded assets (NIGHT, MNTs) only.
185
+ */
186
+ export declare class DynamicWaasMidnightConnector extends DynamicWaasMidnightConnector_base implements IDynamicWaasConnector {
187
+ ChainWallet: typeof MidnightWallet;
188
+ name: string;
189
+ overrideKey: string;
190
+ isEmbeddedWallet: boolean;
191
+ logger: Logger;
192
+ activeAccountAddress: string | undefined;
193
+ verifiedCredentials: JwtVerifiedCredential[] | undefined;
194
+ protected walletUiUtils: WalletUiUtils<InternalWalletConnector>;
195
+ constructor(props: DynamicWaasMidnightConnectorProps);
196
+ /**
197
+ * Signs a message with additional context information
198
+ * @param message - The message to sign (string or object with raw property)
199
+ * @param context - Additional context for the message signing
200
+ * @returns The signed message as a string
201
+ * @throws {Error} Method not implemented for Midnight
202
+ */
203
+ signMessageWithContext({ message, context, }: {
204
+ message: string | {
205
+ raw: string;
206
+ };
207
+ context: SignMessageContext;
208
+ }): Promise<string>;
209
+ /**
210
+ * Override setVerifiedCredentials to filter and set Midnight WaaS credentials
211
+ * Filters for credentials with walletName === 'dynamicwaas' and chain === 'midnight'
212
+ */
213
+ setVerifiedCredentials(verifiedCredentials: JwtVerifiedCredential[]): void;
214
+ /**
215
+ * Override setEnvironmentId to ensure it's set on the logger
216
+ */
217
+ setEnvironmentId(environmentId: string): void;
218
+ /**
219
+ * Gets the wallet client for a specific account address and sets it as active
220
+ * @param accountAddress - The Midnight account address
221
+ * @returns The WaaS wallet client instance
222
+ */
223
+ getWalletClientByAddress({ accountAddress, }: {
224
+ accountAddress: string;
225
+ }): Promise<import("@dynamic-labs-wallet/browser-wallet-client").DynamicWalletClient>;
226
+ /**
227
+ * Gets the currently active account address
228
+ * @returns The active account address or undefined if not set
229
+ */
230
+ getActiveAccountAddress(): Promise<string | undefined>;
231
+ /**
232
+ * Sets the active account address for this connector
233
+ * @param accountAddress - The Midnight account address to set as active
234
+ */
235
+ private setActiveAccountAddress;
236
+ /**
237
+ * Called when the user selects this wallet in the modal.
238
+ * Sets the active account so getAddress/getNetwork work correctly during network validation.
239
+ */
240
+ afterWalletSelectHook(walletAddress: string): void;
241
+ /**
242
+ * Requires and returns the signed session ID
243
+ * @returns The signed session ID
244
+ * @throws {DynamicError} If signed session ID is not available
245
+ */
246
+ private requireSignedSessionId;
247
+ /**
248
+ * Validates that the expected wallet address matches the active wallet
249
+ * Updates the active account address if it doesn't match
250
+ * @param expectedAddress - The expected wallet address to validate
251
+ * @throws {DynamicError} If signed session ID is not available or account is not found
252
+ */
253
+ validateActiveWallet(expectedAddress: string): Promise<void>;
254
+ /**
255
+ * When active account is not set (e.g. primary wallet restored from session),
256
+ * fall back to the first Midnight verified credential so signing and getAddress work.
257
+ */
258
+ private ensureActiveAccountFromVerifiedCredentials;
259
+ /**
260
+ * Gets the active wallet address
261
+ * Falls back to verified credential address if no active account is set
262
+ * @returns The wallet address or empty string if not found
263
+ */
264
+ getAddress(): Promise<string>;
265
+ /**
266
+ * Connects to the WaaS wallet client
267
+ */
268
+ connect(): Promise<void>;
269
+ /**
270
+ * Disconnects from the wallet (no-op for WaaS)
271
+ */
272
+ disconnect(): Promise<void>;
273
+ /**
274
+ * Signs a message using the wallet UI utils for user interaction
275
+ * @param message - The message to sign
276
+ * @returns The signed message as a string
277
+ */
278
+ signMessage(message: string): Promise<string>;
279
+ /**
280
+ * Internal method to sign a message with MFA token
281
+ * @param message - The message to sign
282
+ * @returns The signed message as a string
283
+ * @throws {DynamicError} If active account address is not set
284
+ * @throws {DynamicError} If signed session ID is not available
285
+ */
286
+ internalSignMessage(message: string): Promise<string>;
287
+ /**
288
+ * Ends the current session and clears the active account address
289
+ */
290
+ endSession(): Promise<void>;
291
+ /**
292
+ * Signs and finalizes (ZK proves) a Midnight transaction via MPC.
293
+ * Step 2 of the 3-step flow: createTransferTransaction → signTransaction → submitTransaction.
294
+ *
295
+ * @param serializedTransaction - Base64-encoded unsigned UnprovenTransaction
296
+ * @returns Base64-encoded FinalizedTransaction, ready for submitTransaction
297
+ */
298
+ signTransaction(serializedTransaction: string): Promise<string>;
299
+ /**
300
+ * Creates a UI transaction for the send balance flow
301
+ * @param from - The sender's address
302
+ * @returns A new IUITransaction instance
303
+ */
304
+ createUiTransaction(from: string): Promise<IUITransaction>;
305
+ /**
306
+ * Returns the provider.
307
+ * For WaaS, there is no traditional browser provider.
308
+ */
309
+ getProvider(): undefined;
310
+ /**
311
+ * Creates a new Midnight wallet account.
312
+ * Midnight wallets automatically create multiple address types:
313
+ * - Primary: unshielded address (for public transfers)
314
+ * - Additional: dust address (for gas/fee management) and shielded address (for private transfers)
315
+ *
316
+ * All address types are derived from a single keygen using different BIP-44 derivation paths.
317
+ *
318
+ * @param thresholdSignatureScheme - The threshold signature scheme (default: 'TWO_OF_TWO')
319
+ * @param password - Optional password for the wallet
320
+ * @returns The created wallet account information with primary (unshielded) address
321
+ */
322
+ createWalletAccount({ thresholdSignatureScheme, password, }?: {
323
+ thresholdSignatureScheme?: string;
324
+ password?: string;
325
+ }): Promise<{
326
+ chainName: string;
327
+ accountAddress: string;
328
+ publicKeyHex: string;
329
+ rawPublicKey: string | Uint8Array | undefined;
330
+ }>;
331
+ getPrivateBalance(): Promise<{
332
+ unshielded: Record<string, string>;
333
+ shielded: Record<string, string>;
334
+ dust: {
335
+ balance: string;
336
+ cap: string;
337
+ };
338
+ address: string;
339
+ dustSynced: boolean;
340
+ }>;
341
+ /**
342
+ * Exposes the same `ConnectedAPI` surface injected Midnight wallets provide,
343
+ * adapted from WaaS MPC state. This is what lets the shared
344
+ * `MidnightWalletConnector` balance methods (`getShieldedBalances`,
345
+ * `getUnshieldedBalances`, `getDustBalance`) work for WaaS wallets — they all
346
+ * delegate to `getConnectedAPI()`, which throws (returns undefined) by
347
+ * default. Address lookups and transfers stay on the WaaS-specific paths.
348
+ */
349
+ protected getConnectedAPI(): Promise<ConnectedAPI | undefined>;
350
+ /**
351
+ * Adapts the WaaS MPC `getPrivateBalance()` result to the shape
352
+ * `MidnightWallet.getFormattedBalances()` expects. The base
353
+ * `MidnightWalletConnector.getBalances()` throws unconditionally (it does not
354
+ * route through `getConnectedAPI`), so WaaS still overrides it directly —
355
+ * here in a single `getPrivateBalance()` call.
356
+ *
357
+ * Cap-tolerant: `dust.cap` is 0n on browser-wallet-client <= 1.0.16 and the
358
+ * real cap once the wallet-SDK-v4 change is published (no code change needed).
359
+ */
360
+ getBalances(): Promise<{
361
+ shielded: Record<TokenType, bigint>;
362
+ unshielded: Record<TokenType, bigint>;
363
+ dust: {
364
+ cap: bigint;
365
+ balance: bigint;
366
+ };
367
+ dustSyncing?: boolean;
368
+ }>;
369
+ registerDust(): Promise<{
370
+ dustBalance: string;
371
+ txId?: string;
372
+ status: 'registered' | 'already_registered' | 'already_has_dust' | 'no_utxos';
373
+ message: string;
374
+ registeredCount: number;
375
+ }>;
376
+ /**
377
+ * Step 1 of the Midnight transfer flow.
378
+ * Builds an unsigned UnprovenTransaction from one or more transfer specs.
379
+ * Accepts any combination of unshielded / shielded transfers in a single tx.
380
+ * No MPC, no proof server, no broadcast.
381
+ */
382
+ createTransferTransaction(params: {
383
+ transfers: Array<{
384
+ type: 'unshielded' | 'shielded';
385
+ recipientAddress: string;
386
+ amount: string;
387
+ tokenType?: string;
388
+ }>;
389
+ /** Optional TTL in minutes. Defaults to 5. */
390
+ ttlMinutes?: number;
391
+ }): Promise<{
392
+ serializedTransaction: string;
393
+ }>;
394
+ /**
395
+ * Release UTXOs reserved by an abandoned draft or signed-but-never-submitted
396
+ * transaction. Accepts either an unsigned (Step 1 output) or finalized
397
+ * (Step 2 output) serialized transaction.
398
+ */
399
+ revertTransaction(serializedTransaction: string): Promise<{
400
+ reverted: boolean;
401
+ count: number;
402
+ cacheCleared: boolean;
403
+ message: string;
404
+ }>;
405
+ /**
406
+ * Revert every finalized-but-unsubmitted transaction the SDK knows about
407
+ * (`state.pending.all`). Does not cover unfinalized Step-1-only drafts —
408
+ * those are never registered with the pending-tx service by design. If
409
+ * UTXOs are still stuck after this, use `resetWalletCache()`.
410
+ */
411
+ revertAllPending(): Promise<{
412
+ reverted: boolean;
413
+ count: number;
414
+ cacheCleared: boolean;
415
+ message: string;
416
+ }>;
417
+ /**
418
+ * Nuclear reset. Stops the cached wallet, clears its persisted IndexedDB
419
+ * entry, and forces the next operation to cold-sync from chain. Midnight's
420
+ * officially recommended workaround when UTXOs get stuck in pending state
421
+ * that can't be reverted through the normal API.
422
+ */
423
+ resetWalletCache(): Promise<{
424
+ reverted: boolean;
425
+ count: number;
426
+ cacheCleared: boolean;
427
+ message: string;
428
+ }>;
429
+ private callRevertTransaction;
430
+ /**
431
+ * Step 3 of the unshielded public transfer flow.
432
+ * Broadcasts a finalized transaction via the wallet's NodeClient.
433
+ * Optionally waits for the unshielded balance to stabilize.
434
+ */
435
+ submitTransaction(serializedFinalizedTransaction: string, options?: {
436
+ waitForBalance?: boolean;
437
+ tokenType?: string;
438
+ balanceBefore?: string;
439
+ }): Promise<{
440
+ txHash: string;
441
+ balanceAfter?: string;
442
+ }>;
443
+ }
444
+ export {};