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