@partylayer/sdk 0.22.0 → 0.22.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.
- package/README.md +44 -0
- package/dist/application-ledger-reader-DYaIuRCD.d.mts +774 -0
- package/dist/application-ledger-reader-DYaIuRCD.d.ts +774 -0
- package/dist/index.d.mts +11 -715
- package/dist/index.d.ts +11 -715
- package/dist/index.js +856 -836
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +857 -837
- package/dist/index.mjs.map +1 -1
- package/dist/internal/hosted-wallet-profiles.d.mts +16 -0
- package/dist/internal/hosted-wallet-profiles.d.ts +16 -0
- package/dist/internal/hosted-wallet-profiles.js +337 -0
- package/dist/internal/hosted-wallet-profiles.js.map +1 -0
- package/dist/internal/hosted-wallet-profiles.mjs +335 -0
- package/dist/internal/hosted-wallet-profiles.mjs.map +1 -0
- package/dist/native.d.mts +18 -0
- package/dist/native.d.ts +18 -0
- package/dist/native.js +2832 -0
- package/dist/native.js.map +1 -0
- package/dist/native.mjs +2827 -0
- package/dist/native.mjs.map +1 -0
- package/package.json +18 -6
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { toWalletId, toPartyId, isRecognizedNetwork, detectNetworkMismatch, ledgerApiBodyToObject, normalizeLedgerMethodLower, hashForPrivacy, createMetricsPayload, isOfficialAdapterFactory, isOfficialProviderAdapter, mapUnknownErrorToPartyLayerError, findMatchingWalletInfo, NetworkMismatchError, METRICS, WalletNotFoundError, AdapterNotRegisteredError, OriginNotAllowedError, capabilityGuard, installGuard,
|
|
1
|
+
import { toWalletId, toPartyId, isRecognizedNetwork, detectNetworkMismatch, ledgerApiBodyToObject, normalizeLedgerMethodLower, hashForPrivacy, createMetricsPayload, TransportError, toSignature, isOfficialAdapterFactory, isOfficialProviderAdapter, mapUnknownErrorToPartyLayerError, findMatchingWalletInfo, NetworkMismatchError, METRICS, WalletNotFoundError, AdapterNotRegisteredError, OriginNotAllowedError, capabilityGuard, installGuard, CapabilityNotSupportedError, toTransferIntent, errorMetricName, parseTokenHoldingV2, parseTokenHoldingV1, toCAIP2Network, toTransactionHash, generateSessionId, toSessionId } from '@partylayer/core';
|
|
2
2
|
export { AdapterNotRegisteredError, PartyLayerError as CantonConnectError, CapabilityNotSupportedError, ENABLEMENT_METRICS, ERROR_METRICS, InternalError, METRICS, OriginNotAllowedError, PartyLayerError, REGISTRY_METRICS, RegistryFetchFailedError, RegistrySchemaInvalidError, RegistryVerificationFailedError, SessionExpiredError, TRANSFER_INTENT_FIELDS, TimeoutError, TransportError, UserRejectedError, WalletNotFoundError, WalletNotInstalledError, errorMetricName, isOfficialAdapterFactory, isOfficialProviderAdapter, toTransferIntent } from '@partylayer/core';
|
|
3
3
|
import { RegistryClient } from '@partylayer/registry-client';
|
|
4
4
|
export { deriveGenericWalletName, findMatchingWallet, findMatchingWalletInfo, isCip0103Native, matchesProviderDetection } from '@partylayer/registry-client';
|
|
@@ -14,50 +14,7 @@ export { NightlyAdapter } from '@partylayer/adapter-nightly';
|
|
|
14
14
|
export { SendAdapter } from '@partylayer/adapter-send';
|
|
15
15
|
export { BronAdapter } from '@partylayer/adapter-bron';
|
|
16
16
|
|
|
17
|
-
// src/
|
|
18
|
-
var loaders = {
|
|
19
|
-
walley: async () => {
|
|
20
|
-
const { WalleyAdapter } = await import('@k2flabs/walley-dapp-sdk');
|
|
21
|
-
return { providerId: "walley", create: (host) => new WalleyAdapter({ host }) };
|
|
22
|
-
},
|
|
23
|
-
cauri: async () => {
|
|
24
|
-
const { cauriAdapterFactory } = await import('@lithiumdigital/cauri-dapp-sdk');
|
|
25
|
-
return cauriAdapterFactory;
|
|
26
|
-
},
|
|
27
|
-
oneswap: async (network) => {
|
|
28
|
-
const { createOneSwapProviderAdapter } = await import('@oneswap/wallet-cip0103-adapter');
|
|
29
|
-
return {
|
|
30
|
-
providerId: "oneswap",
|
|
31
|
-
create: (host) => createOneSwapProviderAdapter({
|
|
32
|
-
walletUrl: host,
|
|
33
|
-
networkId: toCAIP2Network(network).networkId
|
|
34
|
-
})
|
|
35
|
-
};
|
|
36
|
-
},
|
|
37
|
-
vala: async () => {
|
|
38
|
-
const { valaAdapterFactory } = await import('@vala-wallet/dapp-sdk');
|
|
39
|
-
return valaAdapterFactory;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
async function loadDefaultWallet(id, config, origin) {
|
|
43
|
-
if (id === "walletconnect" && config.walletConnect?.projectId.trim()) {
|
|
44
|
-
const { WalletConnectAdapter } = await import('@partylayer/adapter-walletconnect');
|
|
45
|
-
return new WalletConnectAdapter({
|
|
46
|
-
...config.walletConnect,
|
|
47
|
-
metadata: config.walletConnect.metadata ?? {
|
|
48
|
-
name: config.app.name,
|
|
49
|
-
description: config.app.name,
|
|
50
|
-
url: origin === "unknown" ? "" : origin,
|
|
51
|
-
icons: []
|
|
52
|
-
}
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
if (id === "bron" && config.bron) {
|
|
56
|
-
const { BronAdapter: BronAdapter2 } = await import('@partylayer/adapter-bron');
|
|
57
|
-
return new BronAdapter2(config.bron);
|
|
58
|
-
}
|
|
59
|
-
return Object.prototype.hasOwnProperty.call(loaders, id) ? loaders[id](config.network) : void 0;
|
|
60
|
-
}
|
|
17
|
+
// src/client-base.ts
|
|
61
18
|
var UNSUPPORTED_METHOD = 4200;
|
|
62
19
|
var METHOD_NOT_FOUND = -32601;
|
|
63
20
|
function isUnsupportedMethod(err) {
|
|
@@ -178,722 +135,324 @@ function assertConnectionAccepted(result) {
|
|
|
178
135
|
function assertPrimaryParty(account) {
|
|
179
136
|
if (typeof account?.partyId !== "string" || !account.partyId.trim()) throw new TransportError("The wallet did not return a primary party");
|
|
180
137
|
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
138
|
+
|
|
139
|
+
// src/native-owner-bindings.ts
|
|
140
|
+
var nativeOwnerBindings = /* @__PURE__ */ new WeakMap();
|
|
141
|
+
var nativeAdapterCopies = /* @__PURE__ */ new WeakMap();
|
|
142
|
+
|
|
143
|
+
// src/announce-adapter.ts
|
|
144
|
+
function getAnnounceCapabilities(config = {}) {
|
|
145
|
+
const caps = ["connect"];
|
|
146
|
+
if (config.signMessage !== false) caps.push("signMessage");
|
|
147
|
+
caps.push("submitTransaction");
|
|
148
|
+
if (config.restore) caps.push("restore");
|
|
149
|
+
if (config.ledgerApi) caps.push("ledgerApi");
|
|
150
|
+
if (config.events) caps.push("events");
|
|
151
|
+
return caps;
|
|
190
152
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
if (signal.aborted) {
|
|
195
|
-
reject(disconnected());
|
|
196
|
-
return;
|
|
197
|
-
}
|
|
198
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
199
|
-
promise.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
200
|
-
});
|
|
153
|
+
var ANNOUNCED_WALLET_ID_PREFIX = "browser:ext:";
|
|
154
|
+
function announcedWalletId(announceId) {
|
|
155
|
+
return toWalletId(`${ANNOUNCED_WALLET_ID_PREFIX}${announceId}`);
|
|
201
156
|
}
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
157
|
+
function mapTxStatus(status) {
|
|
158
|
+
switch (status) {
|
|
159
|
+
case "pending":
|
|
160
|
+
return "pending";
|
|
161
|
+
case "signed":
|
|
162
|
+
return "submitted";
|
|
163
|
+
case "executed":
|
|
164
|
+
return "committed";
|
|
165
|
+
case "failed":
|
|
166
|
+
default:
|
|
167
|
+
return "failed";
|
|
213
168
|
}
|
|
214
|
-
if (status?.isConnected === false || status?.connection?.isConnected === false)
|
|
215
|
-
throw disconnected();
|
|
216
|
-
const network = [status?.network?.networkId, account.networkId, registration.network].find(
|
|
217
|
-
(value) => typeof value === "string" && isRecognizedNetwork(value)
|
|
218
|
-
) ?? registration.network;
|
|
219
|
-
if (registration.expected && (account.partyId !== registration.expected.partyId || detectNetworkMismatch(registration.expected.network, network)))
|
|
220
|
-
throw disconnected();
|
|
221
|
-
if (signal.aborted) throw disconnected();
|
|
222
|
-
return { account, network, status };
|
|
223
169
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
170
|
+
function toBase64Message(message) {
|
|
171
|
+
const bytes = new TextEncoder().encode(message);
|
|
172
|
+
let binary = "";
|
|
173
|
+
for (const b of bytes) binary += String.fromCharCode(b);
|
|
174
|
+
return btoa(binary);
|
|
175
|
+
}
|
|
176
|
+
var GenericAnnounceAdapter = class _GenericAnnounceAdapter {
|
|
177
|
+
walletId;
|
|
178
|
+
name;
|
|
179
|
+
/** Icon surfaced to the picker (announce detail). */
|
|
180
|
+
icon;
|
|
181
|
+
rawProvider;
|
|
182
|
+
metadataEnabled;
|
|
183
|
+
staticMetadata;
|
|
184
|
+
mapError;
|
|
185
|
+
signMessageBase64;
|
|
186
|
+
/**
|
|
187
|
+
* Which execute verb this wallet turned out to support. Resolved on the first
|
|
188
|
+
* submit and reused, so the wallet is asked at most once.
|
|
189
|
+
*/
|
|
190
|
+
executeVerb = {};
|
|
191
|
+
// Optional WalletAdapter surface — assigned in the ctor ONLY when configured,
|
|
192
|
+
// so `getCapabilities()` and `'x' in adapter` feature-detection stay honest.
|
|
193
|
+
restore;
|
|
194
|
+
on;
|
|
195
|
+
ledgerApi;
|
|
196
|
+
// signMessage is optional too: assigned in the ctor only when the wallet
|
|
197
|
+
// declares support, so a wallet that cannot sign is not advertised as able to.
|
|
198
|
+
signMessage;
|
|
199
|
+
constructor(args) {
|
|
200
|
+
const configuration = { ...args, config: args.config && {
|
|
201
|
+
...args.config,
|
|
202
|
+
staticMetadata: args.config.staticMetadata && { ...args.config.staticMetadata }
|
|
203
|
+
} };
|
|
204
|
+
nativeAdapterCopies.set(this, () => new _GenericAnnounceAdapter(configuration));
|
|
205
|
+
this.walletId = args.walletId ?? announcedWalletId(args.announceId);
|
|
206
|
+
this.name = args.name && args.name.length > 0 ? args.name : `Canton Wallet (${args.announceId.slice(0, 6)}\u2026)`;
|
|
207
|
+
this.icon = args.icon;
|
|
208
|
+
this.rawProvider = args.provider;
|
|
209
|
+
const config = args.config;
|
|
210
|
+
this.metadataEnabled = config?.metadata === true;
|
|
211
|
+
this.staticMetadata = config?.staticMetadata;
|
|
212
|
+
this.mapError = config?.mapError;
|
|
213
|
+
this.signMessageBase64 = config?.signMessageBase64 === true;
|
|
214
|
+
if (config?.events) this.on = this.makeOn();
|
|
215
|
+
if (config?.restore) this.restore = this.makeRestore();
|
|
216
|
+
if (config?.ledgerApi) this.ledgerApi = this.makeLedgerApi();
|
|
217
|
+
if (config?.signMessage !== false) this.signMessage = this.signMessageImpl.bind(this);
|
|
228
218
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
registrations = /* @__PURE__ */ new Map();
|
|
232
|
-
operation = null;
|
|
233
|
-
pending = null;
|
|
234
|
-
closing = null;
|
|
235
|
-
projection = null;
|
|
236
|
-
unsubscribers = [];
|
|
237
|
-
disposed = false;
|
|
238
|
-
refreshRevision = 0;
|
|
239
|
-
identityRefreshing = false;
|
|
240
|
-
probes = /* @__PURE__ */ new Set();
|
|
241
|
-
get busy() {
|
|
242
|
-
return this.operation !== null || this.closing !== null;
|
|
219
|
+
get provider() {
|
|
220
|
+
return nativeOwnerBindings.get(this)?.getProvider(String(this.walletId)) ?? this.rawProvider;
|
|
243
221
|
}
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
222
|
+
/** @internal Register the discovered transport with the one official owner. */
|
|
223
|
+
async prepareNativeConnection(network) {
|
|
224
|
+
const owner = nativeOwnerBindings.get(this);
|
|
225
|
+
if (!owner) return;
|
|
226
|
+
await owner.prepare(String(this.walletId), {
|
|
227
|
+
providerId: String(this.walletId),
|
|
228
|
+
name: this.name,
|
|
229
|
+
type: "browser",
|
|
230
|
+
icon: this.icon,
|
|
231
|
+
provider: () => this.rawProvider,
|
|
232
|
+
detect: async () => true
|
|
233
|
+
}, network);
|
|
247
234
|
}
|
|
248
|
-
/**
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
})();
|
|
262
|
-
}
|
|
263
|
-
await this.initialization;
|
|
264
|
-
if (this.disposed || !this.client) throw disconnected();
|
|
265
|
-
for (const registration of this.registrations.values())
|
|
266
|
-
this.client.registerAdapter(this.wrap(registration));
|
|
235
|
+
/**
|
|
236
|
+
* Capabilities derived from actual presence: connect and submitTransaction are
|
|
237
|
+
* always present; signMessage, restore, ledgerApi, and events are each included
|
|
238
|
+
* only when the matching method was assigned, so the set is never advertised
|
|
239
|
+
* beyond what the adapter can do.
|
|
240
|
+
*/
|
|
241
|
+
getCapabilities() {
|
|
242
|
+
return getAnnounceCapabilities({
|
|
243
|
+
signMessage: !!this.signMessage,
|
|
244
|
+
restore: !!this.restore,
|
|
245
|
+
ledgerApi: !!this.ledgerApi,
|
|
246
|
+
events: !!this.on
|
|
247
|
+
});
|
|
267
248
|
}
|
|
268
|
-
|
|
269
|
-
|
|
249
|
+
/**
|
|
250
|
+
* The adapter is only ever constructed for a wallet that just announced, so
|
|
251
|
+
* its presence is established by the announce handshake itself.
|
|
252
|
+
*/
|
|
253
|
+
async detectInstalled() {
|
|
270
254
|
return {
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
type: "browser",
|
|
279
|
-
icon: source.icon
|
|
280
|
-
}),
|
|
281
|
-
detect: () => source.detect(),
|
|
282
|
-
provider: () => this.guard(source.provider(), registration),
|
|
283
|
-
teardown: () => teardown(source),
|
|
284
|
-
restore: async () => {
|
|
285
|
-
const operation = this.operation;
|
|
286
|
-
if (!operation) throw disconnected();
|
|
287
|
-
try {
|
|
288
|
-
const raw = source.restore ? await abortable(source.restore(), operation.abort.signal) : source.provider();
|
|
289
|
-
if (!raw) return null;
|
|
290
|
-
const provider = raw;
|
|
291
|
-
operation.identity = await readIdentity(provider, registration, operation.abort.signal);
|
|
292
|
-
return this.guard(provider, registration);
|
|
293
|
-
} catch (error) {
|
|
294
|
-
teardown(source);
|
|
295
|
-
throw error;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
}
|
|
300
|
-
guard(raw, registration) {
|
|
301
|
-
const provider = {
|
|
302
|
-
request: async (args) => {
|
|
303
|
-
if (this.disposed) throw disconnected();
|
|
304
|
-
if (args.method !== "connect") {
|
|
305
|
-
if (this.client?.getActiveSession()?.provider !== provider) throw disconnected();
|
|
306
|
-
if (!this.projection && args.method !== "disconnect") throw disconnected();
|
|
307
|
-
if (this.identityRefreshing && ![
|
|
308
|
-
"status",
|
|
309
|
-
"getPrimaryAccount",
|
|
310
|
-
"listAccounts",
|
|
311
|
-
"getActiveNetwork",
|
|
312
|
-
"isConnected"
|
|
313
|
-
].includes(args.method)) {
|
|
314
|
-
throw new Error("Native wallet identity is being refreshed");
|
|
315
|
-
}
|
|
316
|
-
const result = await (args.method === "disconnect" && this.operation ? abortable(raw.request(args), this.operation.abort.signal) : raw.request(args));
|
|
317
|
-
if (args.method === "status") {
|
|
318
|
-
const status = result;
|
|
319
|
-
if (status?.isConnected === false || status?.connection?.isConnected === false) {
|
|
320
|
-
this.invalidate(registration.id);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
return result;
|
|
324
|
-
}
|
|
325
|
-
const operation = this.operation;
|
|
326
|
-
if (!operation || operation.abort.signal.aborted) throw disconnected();
|
|
327
|
-
const response = await abortable(raw.request(args), operation.abort.signal);
|
|
328
|
-
assertConnectionAccepted(response);
|
|
329
|
-
operation.identity = await readIdentity(raw, registration, operation.abort.signal);
|
|
330
|
-
return response;
|
|
331
|
-
},
|
|
332
|
-
on: (event, listener) => {
|
|
333
|
-
raw.on(event, listener);
|
|
334
|
-
return provider;
|
|
335
|
-
},
|
|
336
|
-
emit: (event, ...args) => raw.emit(event, ...args),
|
|
337
|
-
removeListener: (event, listener) => {
|
|
338
|
-
raw.removeListener(event, listener);
|
|
339
|
-
return provider;
|
|
340
|
-
}
|
|
255
|
+
installed: true,
|
|
256
|
+
// Presence by CONSTRUCTION: this adapter only exists because an announce
|
|
257
|
+
// arrived on the wallet's own target channel. There is no probe to be
|
|
258
|
+
// wrong about, which is why this is the one path that cannot report a
|
|
259
|
+
// wallet that is not there.
|
|
260
|
+
availability: { kind: "installed" },
|
|
261
|
+
reason: "Announced via canton:announceProvider"
|
|
341
262
|
};
|
|
342
|
-
return provider;
|
|
343
263
|
}
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
const previousId = this.projection?.id;
|
|
355
|
-
this.projection = null;
|
|
356
|
-
if (previousId) this.changed(previousId, null);
|
|
357
|
-
const abort = () => operation.abort.abort();
|
|
358
|
-
if (signal?.aborted) abort();
|
|
359
|
-
signal?.addEventListener("abort", abort, { once: true });
|
|
360
|
-
const task = (async () => {
|
|
264
|
+
async connect(ctx, options) {
|
|
265
|
+
return this.guarded(async () => {
|
|
266
|
+
const owner = nativeOwnerBindings.get(this);
|
|
267
|
+
const native = owner ? await owner.connect(String(this.walletId), options?.signal) : void 0;
|
|
268
|
+
if (!owner) assertConnectionAccepted(await this.provider.request({ method: "connect" }));
|
|
269
|
+
const account = native?.account ?? await this.provider.request({
|
|
270
|
+
method: "getPrimaryAccount"
|
|
271
|
+
});
|
|
272
|
+
assertPrimaryParty(account);
|
|
273
|
+
let status;
|
|
361
274
|
try {
|
|
362
|
-
await this.
|
|
363
|
-
|
|
364
|
-
throw disconnected();
|
|
365
|
-
this.projection = { id, identity: operation.identity };
|
|
366
|
-
this.attach(id);
|
|
367
|
-
return operation.identity;
|
|
368
|
-
} catch (error) {
|
|
369
|
-
this.client?.destroy();
|
|
370
|
-
this.client = null;
|
|
371
|
-
this.initialization = null;
|
|
372
|
-
teardown(this.registrations.get(id)?.source);
|
|
373
|
-
throw error;
|
|
374
|
-
} finally {
|
|
375
|
-
signal?.removeEventListener("abort", abort);
|
|
376
|
-
if (this.operation === operation) this.operation = null;
|
|
275
|
+
status = native?.status ?? await this.provider.request({ method: "status" });
|
|
276
|
+
} catch {
|
|
377
277
|
}
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
278
|
+
const reportedNetwork = status?.network?.networkId;
|
|
279
|
+
const partyId = toPartyId(account.partyId);
|
|
280
|
+
const network = [reportedNetwork, account.networkId, ctx.network].find(
|
|
281
|
+
(n) => typeof n === "string" && isRecognizedNetwork(n)
|
|
282
|
+
) ?? ctx.network;
|
|
283
|
+
const session = {
|
|
284
|
+
walletId: this.walletId,
|
|
285
|
+
partyId,
|
|
286
|
+
network
|
|
287
|
+
};
|
|
288
|
+
if (this.metadataEnabled) {
|
|
289
|
+
session.metadata = { ...this.staticMetadata ?? {}, ...buildMetadata(status, account) };
|
|
386
290
|
}
|
|
387
|
-
|
|
388
|
-
|
|
291
|
+
return { partyId, session, capabilities: this.getCapabilities() };
|
|
292
|
+
});
|
|
389
293
|
}
|
|
390
|
-
async
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
const timeout = setTimeout(() => operation.abort.abort(), SILENT_PROBE_TIMEOUT_MS);
|
|
397
|
-
const task = (async () => {
|
|
398
|
-
try {
|
|
399
|
-
await this.client.restorePersistedSessionIfNeeded();
|
|
400
|
-
const active = this.client?.getActiveSession();
|
|
401
|
-
if (!active || !operation.identity || operation.abort.signal.aborted || this.disposed)
|
|
402
|
-
return null;
|
|
403
|
-
if (expected && active.providerId !== expected.id) {
|
|
404
|
-
teardown(active.adapter);
|
|
405
|
-
this.client.destroy();
|
|
406
|
-
this.client = null;
|
|
407
|
-
this.initialization = null;
|
|
408
|
-
return null;
|
|
409
|
-
}
|
|
410
|
-
const id = String(active.providerId);
|
|
411
|
-
this.projection = { id, identity: operation.identity };
|
|
412
|
-
this.attach(id);
|
|
413
|
-
return this.getSession();
|
|
414
|
-
} finally {
|
|
415
|
-
clearTimeout(timeout);
|
|
416
|
-
if (registration) delete registration.expected;
|
|
417
|
-
if (this.operation === operation) this.operation = null;
|
|
418
|
-
}
|
|
419
|
-
})();
|
|
420
|
-
this.pending = task;
|
|
294
|
+
async disconnect() {
|
|
295
|
+
const owner = nativeOwnerBindings.get(this);
|
|
296
|
+
if (owner) {
|
|
297
|
+
await owner.disconnect();
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
421
300
|
try {
|
|
422
|
-
|
|
423
|
-
}
|
|
424
|
-
if (this.pending === task) this.pending = null;
|
|
301
|
+
await this.provider.request({ method: "disconnect" });
|
|
302
|
+
} catch {
|
|
425
303
|
}
|
|
426
304
|
}
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
305
|
+
// Real signMessage implementation. Exposed as `this.signMessage` from the ctor
|
|
306
|
+
// only when the wallet declares support, so getCapabilities and feature
|
|
307
|
+
// detection stay honest for a wallet that cannot sign.
|
|
308
|
+
async signMessageImpl(_ctx, session, params) {
|
|
309
|
+
return this.guarded(async () => {
|
|
310
|
+
const rpcParams = this.signMessageBase64 ? { message: toBase64Message(params.message) } : { message: params.message };
|
|
311
|
+
const res = await this.provider.request({ method: "signMessage", params: rpcParams });
|
|
312
|
+
return normalizeSignedMessage(res, session, params);
|
|
313
|
+
});
|
|
430
314
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
315
|
+
async submitTransaction(ctx, _session, params) {
|
|
316
|
+
return this.guarded(
|
|
317
|
+
() => submitViaPrepareExecute(this.provider, params, {
|
|
318
|
+
walletId: String(this.walletId),
|
|
319
|
+
state: this.executeVerb,
|
|
320
|
+
logger: ctx.logger
|
|
321
|
+
})
|
|
322
|
+
);
|
|
434
323
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
324
|
+
// ── Optional-method factories (assigned only when configured) ──────────────
|
|
325
|
+
/** events: bridge the provider's CIP-0103 `txChanged` push event → `txStatus`. */
|
|
326
|
+
makeOn() {
|
|
327
|
+
return (event, handler) => {
|
|
328
|
+
if (event !== "txStatus") return () => {
|
|
329
|
+
};
|
|
330
|
+
const listener = (...args) => {
|
|
331
|
+
const tx = args[0];
|
|
332
|
+
if (!tx) return;
|
|
333
|
+
handler({ status: mapTxStatus(tx.status), commandId: tx.commandId, raw: tx });
|
|
334
|
+
};
|
|
335
|
+
try {
|
|
336
|
+
this.provider.on("txChanged", listener);
|
|
337
|
+
} catch {
|
|
338
|
+
return () => {
|
|
339
|
+
};
|
|
450
340
|
}
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
throw error;
|
|
454
|
-
} finally {
|
|
455
|
-
if (revision === this.refreshRevision) this.identityRefreshing = false;
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
async probe(provider, registration) {
|
|
459
|
-
const abort = new AbortController();
|
|
460
|
-
this.probes.add(abort);
|
|
461
|
-
const timeout = setTimeout(() => abort.abort(), SILENT_PROBE_TIMEOUT_MS);
|
|
462
|
-
try {
|
|
463
|
-
return await readIdentity(provider, registration, abort.signal);
|
|
464
|
-
} finally {
|
|
465
|
-
clearTimeout(timeout);
|
|
466
|
-
this.probes.delete(abort);
|
|
467
|
-
}
|
|
341
|
+
return () => this.provider.removeListener("txChanged", listener);
|
|
342
|
+
};
|
|
468
343
|
}
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
this.
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
344
|
+
/** restore: silent status()/getPrimaryAccount() probe + expiry + party-match. */
|
|
345
|
+
makeRestore() {
|
|
346
|
+
return (_ctx, persisted) => this.guarded(async () => {
|
|
347
|
+
const owner = nativeOwnerBindings.get(this);
|
|
348
|
+
if (owner) {
|
|
349
|
+
await this.prepareNativeConnection(_ctx.network);
|
|
350
|
+
const restored = await owner.restore({ id: String(this.walletId), partyId: persisted.partyId, network: persisted.network });
|
|
351
|
+
return restored?.id === String(this.walletId) ? { ...persisted, capabilitiesSnapshot: this.getCapabilities() } : null;
|
|
352
|
+
}
|
|
353
|
+
if (persisted.expiresAt && Date.now() >= persisted.expiresAt) return null;
|
|
354
|
+
let status;
|
|
475
355
|
try {
|
|
476
|
-
|
|
356
|
+
status = await this.provider.request({ method: "status" });
|
|
477
357
|
} catch {
|
|
478
358
|
}
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
return;
|
|
488
|
-
}
|
|
489
|
-
const revision = ++this.refreshRevision;
|
|
490
|
-
this.identityRefreshing = true;
|
|
491
|
-
const registration = this.registrations.get(id);
|
|
492
|
-
void this.probe(provider, registration).then(
|
|
493
|
-
(identity2) => {
|
|
494
|
-
if (revision !== this.refreshRevision || this.projection?.id !== id || this.disposed)
|
|
495
|
-
return;
|
|
496
|
-
this.identityRefreshing = false;
|
|
497
|
-
this.projection = { id, identity: identity2 };
|
|
498
|
-
this.changed(id, identity2);
|
|
499
|
-
},
|
|
500
|
-
() => {
|
|
501
|
-
if (revision === this.refreshRevision) this.invalidate(id);
|
|
502
|
-
}
|
|
359
|
+
const connected = status?.isConnected ?? status?.connection?.isConnected;
|
|
360
|
+
if (connected === false) return null;
|
|
361
|
+
const account = await this.provider.request({
|
|
362
|
+
method: "getPrimaryAccount"
|
|
363
|
+
});
|
|
364
|
+
if (account.partyId !== persisted.partyId) return null;
|
|
365
|
+
const network = [status?.network?.networkId, account.networkId].find(
|
|
366
|
+
(value) => typeof value === "string" && isRecognizedNetwork(value)
|
|
503
367
|
);
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
this.client = null;
|
|
517
|
-
this.initialization = null;
|
|
518
|
-
this.changed(id, null);
|
|
519
|
-
}
|
|
520
|
-
/** Local transport cleanup when switching to a proprietary integration. */
|
|
521
|
-
release() {
|
|
522
|
-
this.assertAvailable();
|
|
523
|
-
const id = this.projection?.id;
|
|
524
|
-
this.detach();
|
|
525
|
-
this.projection = null;
|
|
526
|
-
teardown(this.client?.getActiveSession()?.adapter);
|
|
527
|
-
this.client?.destroy();
|
|
528
|
-
this.client = null;
|
|
529
|
-
this.initialization = null;
|
|
530
|
-
if (id) this.changed(id, null);
|
|
368
|
+
if (network && detectNetworkMismatch(persisted.network, network)) return null;
|
|
369
|
+
return {
|
|
370
|
+
...persisted,
|
|
371
|
+
walletId: this.walletId,
|
|
372
|
+
// persisted (lowest) → static config → runtime RPC (wins).
|
|
373
|
+
metadata: {
|
|
374
|
+
...persisted.metadata ?? {},
|
|
375
|
+
...this.staticMetadata ?? {},
|
|
376
|
+
...buildMetadata(status, account)
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
});
|
|
531
380
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
this.
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
const operation = { abort: new AbortController() };
|
|
542
|
-
this.operation = operation;
|
|
543
|
-
const timeout = setTimeout(() => operation.abort.abort(), SILENT_PROBE_TIMEOUT_MS);
|
|
544
|
-
try {
|
|
545
|
-
await this.client?.disconnect();
|
|
546
|
-
} finally {
|
|
547
|
-
clearTimeout(timeout);
|
|
548
|
-
if (this.client?.getActiveSession()) {
|
|
549
|
-
this.client.destroy();
|
|
550
|
-
this.client = null;
|
|
551
|
-
this.initialization = null;
|
|
381
|
+
/** ledgerApi: proxy the standard CIP-0103 ledgerApi call; shape to {response:string}. */
|
|
382
|
+
makeLedgerApi() {
|
|
383
|
+
return (_ctx, _session, params) => this.guarded(async () => {
|
|
384
|
+
const result = await this.provider.request({
|
|
385
|
+
method: "ledgerApi",
|
|
386
|
+
params: {
|
|
387
|
+
requestMethod: normalizeLedgerMethodLower(params.requestMethod),
|
|
388
|
+
resource: params.resource,
|
|
389
|
+
body: ledgerApiBodyToObject(params.body)
|
|
552
390
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
}
|
|
556
|
-
})
|
|
557
|
-
this.closing = task;
|
|
558
|
-
void task.then(
|
|
559
|
-
() => {
|
|
560
|
-
if (this.closing === task) this.closing = null;
|
|
561
|
-
},
|
|
562
|
-
() => {
|
|
563
|
-
if (this.closing === task) this.closing = null;
|
|
564
|
-
}
|
|
565
|
-
);
|
|
566
|
-
return task;
|
|
391
|
+
});
|
|
392
|
+
if (result && typeof result.response === "string") return { response: result.response };
|
|
393
|
+
return { response: JSON.stringify(result ?? null) };
|
|
394
|
+
});
|
|
567
395
|
}
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
if (!this.
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
396
|
+
/**
|
|
397
|
+
* Run an operation, giving the optional `mapError` hook first crack at any
|
|
398
|
+
* thrown error. With NO hook configured this is a direct pass-through (no
|
|
399
|
+
* try/catch) — byte-identical to the baseline, and the SDK's built-in standard
|
|
400
|
+
* error mapping still applies downstream.
|
|
401
|
+
*/
|
|
402
|
+
async guarded(op) {
|
|
403
|
+
if (!this.mapError) return op();
|
|
404
|
+
try {
|
|
405
|
+
return await op();
|
|
406
|
+
} catch (err) {
|
|
407
|
+
const mapped = this.mapError(err);
|
|
408
|
+
throw mapped ?? err;
|
|
409
|
+
}
|
|
579
410
|
}
|
|
580
411
|
};
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
function announcedWalletId(announceId) {
|
|
596
|
-
return toWalletId(`${ANNOUNCED_WALLET_ID_PREFIX}${announceId}`);
|
|
597
|
-
}
|
|
598
|
-
function mapTxStatus(status) {
|
|
599
|
-
switch (status) {
|
|
600
|
-
case "pending":
|
|
601
|
-
return "pending";
|
|
602
|
-
case "signed":
|
|
603
|
-
return "submitted";
|
|
604
|
-
case "executed":
|
|
605
|
-
return "committed";
|
|
606
|
-
case "failed":
|
|
607
|
-
default:
|
|
608
|
-
return "failed";
|
|
609
|
-
}
|
|
412
|
+
function buildMetadata(status, account) {
|
|
413
|
+
const meta = {};
|
|
414
|
+
const put = (k, v) => {
|
|
415
|
+
if (typeof v === "string" && v.length > 0) meta[k] = v;
|
|
416
|
+
};
|
|
417
|
+
put("kernelId", status?.kernel?.id);
|
|
418
|
+
put("publicKey", account.publicKey);
|
|
419
|
+
put("namespace", account.namespace);
|
|
420
|
+
put("networkId", account.networkId);
|
|
421
|
+
put("signingProviderId", account.signingProviderId);
|
|
422
|
+
put("hint", account.hint);
|
|
423
|
+
put("ledgerApiBaseUrl", status?.network?.ledgerApi?.baseUrl);
|
|
424
|
+
put("userId", status?.session?.userId);
|
|
425
|
+
return meta;
|
|
610
426
|
}
|
|
611
|
-
function
|
|
612
|
-
const
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
427
|
+
function getDiscoveryCapabilities(declared) {
|
|
428
|
+
const capabilities = ["connect", "disconnect"];
|
|
429
|
+
if (declared?.signMessage !== false) capabilities.push("signMessage");
|
|
430
|
+
if (declared?.submitTransaction !== false) capabilities.push("submitTransaction");
|
|
431
|
+
if (declared?.ledgerApi) capabilities.push("ledgerApi");
|
|
432
|
+
return capabilities;
|
|
616
433
|
}
|
|
617
|
-
var
|
|
434
|
+
var GenericDiscoveryAdapter = class _GenericDiscoveryAdapter {
|
|
618
435
|
walletId;
|
|
619
436
|
name;
|
|
620
|
-
/** Icon surfaced to the picker (announce detail). */
|
|
621
437
|
icon;
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
438
|
+
/**
|
|
439
|
+
* Resolved official adapter. Set at construction for the instance form; for
|
|
440
|
+
* the factory form it stays null until {@link resolveOfficial} constructs it
|
|
441
|
+
* with the network-resolved host.
|
|
442
|
+
*/
|
|
443
|
+
official;
|
|
444
|
+
/** Factory form (null for the instance form). */
|
|
445
|
+
factory;
|
|
446
|
+
/** Network→host map for the factory form; injected by the SDK post-registry. */
|
|
447
|
+
networkHosts;
|
|
448
|
+
/** Lazily resolved from `official.provider()` — NOT at construction (SSR-safe). */
|
|
449
|
+
providerInstance = null;
|
|
627
450
|
/**
|
|
628
451
|
* Which execute verb this wallet turned out to support. Resolved on the first
|
|
629
452
|
* submit and reused, so the wallet is asked at most once.
|
|
630
453
|
*/
|
|
631
454
|
executeVerb = {};
|
|
632
|
-
|
|
633
|
-
// so `getCapabilities()` and `'x' in adapter` feature-detection stay honest.
|
|
634
|
-
restore;
|
|
635
|
-
on;
|
|
636
|
-
ledgerApi;
|
|
637
|
-
// signMessage is optional too: assigned in the ctor only when the wallet
|
|
638
|
-
// declares support, so a wallet that cannot sign is not advertised as able to.
|
|
639
|
-
signMessage;
|
|
640
|
-
constructor(args) {
|
|
641
|
-
const configuration = { ...args, config: args.config && {
|
|
642
|
-
...args.config,
|
|
643
|
-
staticMetadata: args.config.staticMetadata && { ...args.config.staticMetadata }
|
|
644
|
-
} };
|
|
645
|
-
nativeAdapterCopies.set(this, () => new _GenericAnnounceAdapter(configuration));
|
|
646
|
-
this.walletId = args.walletId ?? announcedWalletId(args.announceId);
|
|
647
|
-
this.name = args.name && args.name.length > 0 ? args.name : `Canton Wallet (${args.announceId.slice(0, 6)}\u2026)`;
|
|
648
|
-
this.icon = args.icon;
|
|
649
|
-
this.rawProvider = args.provider;
|
|
650
|
-
const config = args.config;
|
|
651
|
-
this.metadataEnabled = config?.metadata === true;
|
|
652
|
-
this.staticMetadata = config?.staticMetadata;
|
|
653
|
-
this.mapError = config?.mapError;
|
|
654
|
-
this.signMessageBase64 = config?.signMessageBase64 === true;
|
|
655
|
-
if (config?.events) this.on = this.makeOn();
|
|
656
|
-
if (config?.restore) this.restore = this.makeRestore();
|
|
657
|
-
if (config?.ledgerApi) this.ledgerApi = this.makeLedgerApi();
|
|
658
|
-
if (config?.signMessage !== false) this.signMessage = this.signMessageImpl.bind(this);
|
|
659
|
-
}
|
|
660
|
-
get provider() {
|
|
661
|
-
return nativeOwnerBindings.get(this)?.getProvider(String(this.walletId)) ?? this.rawProvider;
|
|
662
|
-
}
|
|
663
|
-
/** @internal Register the discovered transport with the one official owner. */
|
|
664
|
-
async prepareNativeConnection(network) {
|
|
665
|
-
const owner = nativeOwnerBindings.get(this);
|
|
666
|
-
if (!owner) return;
|
|
667
|
-
await owner.prepare(String(this.walletId), {
|
|
668
|
-
providerId: String(this.walletId),
|
|
669
|
-
name: this.name,
|
|
670
|
-
type: "browser",
|
|
671
|
-
icon: this.icon,
|
|
672
|
-
provider: () => this.rawProvider,
|
|
673
|
-
detect: async () => true
|
|
674
|
-
}, network);
|
|
675
|
-
}
|
|
676
|
-
/**
|
|
677
|
-
* Capabilities derived from actual presence: connect and submitTransaction are
|
|
678
|
-
* always present; signMessage, restore, ledgerApi, and events are each included
|
|
679
|
-
* only when the matching method was assigned, so the set is never advertised
|
|
680
|
-
* beyond what the adapter can do.
|
|
681
|
-
*/
|
|
682
|
-
getCapabilities() {
|
|
683
|
-
return getAnnounceCapabilities({
|
|
684
|
-
signMessage: !!this.signMessage,
|
|
685
|
-
restore: !!this.restore,
|
|
686
|
-
ledgerApi: !!this.ledgerApi,
|
|
687
|
-
events: !!this.on
|
|
688
|
-
});
|
|
689
|
-
}
|
|
690
|
-
/**
|
|
691
|
-
* The adapter is only ever constructed for a wallet that just announced, so
|
|
692
|
-
* its presence is established by the announce handshake itself.
|
|
693
|
-
*/
|
|
694
|
-
async detectInstalled() {
|
|
695
|
-
return {
|
|
696
|
-
installed: true,
|
|
697
|
-
// Presence by CONSTRUCTION: this adapter only exists because an announce
|
|
698
|
-
// arrived on the wallet's own target channel. There is no probe to be
|
|
699
|
-
// wrong about, which is why this is the one path that cannot report a
|
|
700
|
-
// wallet that is not there.
|
|
701
|
-
availability: { kind: "installed" },
|
|
702
|
-
reason: "Announced via canton:announceProvider"
|
|
703
|
-
};
|
|
704
|
-
}
|
|
705
|
-
async connect(ctx, options) {
|
|
706
|
-
return this.guarded(async () => {
|
|
707
|
-
const owner = nativeOwnerBindings.get(this);
|
|
708
|
-
const native = owner ? await owner.connect(String(this.walletId), options?.signal) : void 0;
|
|
709
|
-
if (!owner) assertConnectionAccepted(await this.provider.request({ method: "connect" }));
|
|
710
|
-
const account = native?.account ?? await this.provider.request({
|
|
711
|
-
method: "getPrimaryAccount"
|
|
712
|
-
});
|
|
713
|
-
assertPrimaryParty(account);
|
|
714
|
-
let status;
|
|
715
|
-
try {
|
|
716
|
-
status = native?.status ?? await this.provider.request({ method: "status" });
|
|
717
|
-
} catch {
|
|
718
|
-
}
|
|
719
|
-
const reportedNetwork = status?.network?.networkId;
|
|
720
|
-
const partyId = toPartyId(account.partyId);
|
|
721
|
-
const network = [reportedNetwork, account.networkId, ctx.network].find(
|
|
722
|
-
(n) => typeof n === "string" && isRecognizedNetwork(n)
|
|
723
|
-
) ?? ctx.network;
|
|
724
|
-
const session = {
|
|
725
|
-
walletId: this.walletId,
|
|
726
|
-
partyId,
|
|
727
|
-
network
|
|
728
|
-
};
|
|
729
|
-
if (this.metadataEnabled) {
|
|
730
|
-
session.metadata = { ...this.staticMetadata ?? {}, ...buildMetadata(status, account) };
|
|
731
|
-
}
|
|
732
|
-
return { partyId, session, capabilities: this.getCapabilities() };
|
|
733
|
-
});
|
|
734
|
-
}
|
|
735
|
-
async disconnect() {
|
|
736
|
-
const owner = nativeOwnerBindings.get(this);
|
|
737
|
-
if (owner) {
|
|
738
|
-
await owner.disconnect();
|
|
739
|
-
return;
|
|
740
|
-
}
|
|
741
|
-
try {
|
|
742
|
-
await this.provider.request({ method: "disconnect" });
|
|
743
|
-
} catch {
|
|
744
|
-
}
|
|
745
|
-
}
|
|
746
|
-
// Real signMessage implementation. Exposed as `this.signMessage` from the ctor
|
|
747
|
-
// only when the wallet declares support, so getCapabilities and feature
|
|
748
|
-
// detection stay honest for a wallet that cannot sign.
|
|
749
|
-
async signMessageImpl(_ctx, session, params) {
|
|
750
|
-
return this.guarded(async () => {
|
|
751
|
-
const rpcParams = this.signMessageBase64 ? { message: toBase64Message(params.message) } : { message: params.message };
|
|
752
|
-
const res = await this.provider.request({ method: "signMessage", params: rpcParams });
|
|
753
|
-
return normalizeSignedMessage(res, session, params);
|
|
754
|
-
});
|
|
755
|
-
}
|
|
756
|
-
async submitTransaction(ctx, _session, params) {
|
|
757
|
-
return this.guarded(
|
|
758
|
-
() => submitViaPrepareExecute(this.provider, params, {
|
|
759
|
-
walletId: String(this.walletId),
|
|
760
|
-
state: this.executeVerb,
|
|
761
|
-
logger: ctx.logger
|
|
762
|
-
})
|
|
763
|
-
);
|
|
764
|
-
}
|
|
765
|
-
// ── Optional-method factories (assigned only when configured) ──────────────
|
|
766
|
-
/** events: bridge the provider's CIP-0103 `txChanged` push event → `txStatus`. */
|
|
767
|
-
makeOn() {
|
|
768
|
-
return (event, handler) => {
|
|
769
|
-
if (event !== "txStatus") return () => {
|
|
770
|
-
};
|
|
771
|
-
const listener = (...args) => {
|
|
772
|
-
const tx = args[0];
|
|
773
|
-
if (!tx) return;
|
|
774
|
-
handler({ status: mapTxStatus(tx.status), commandId: tx.commandId, raw: tx });
|
|
775
|
-
};
|
|
776
|
-
try {
|
|
777
|
-
this.provider.on("txChanged", listener);
|
|
778
|
-
} catch {
|
|
779
|
-
return () => {
|
|
780
|
-
};
|
|
781
|
-
}
|
|
782
|
-
return () => this.provider.removeListener("txChanged", listener);
|
|
783
|
-
};
|
|
784
|
-
}
|
|
785
|
-
/** restore: silent status()/getPrimaryAccount() probe + expiry + party-match. */
|
|
786
|
-
makeRestore() {
|
|
787
|
-
return (_ctx, persisted) => this.guarded(async () => {
|
|
788
|
-
const owner = nativeOwnerBindings.get(this);
|
|
789
|
-
if (owner) {
|
|
790
|
-
await this.prepareNativeConnection(_ctx.network);
|
|
791
|
-
const restored = await owner.restore({ id: String(this.walletId), partyId: persisted.partyId, network: persisted.network });
|
|
792
|
-
return restored?.id === String(this.walletId) ? { ...persisted, capabilitiesSnapshot: this.getCapabilities() } : null;
|
|
793
|
-
}
|
|
794
|
-
if (persisted.expiresAt && Date.now() >= persisted.expiresAt) return null;
|
|
795
|
-
let status;
|
|
796
|
-
try {
|
|
797
|
-
status = await this.provider.request({ method: "status" });
|
|
798
|
-
} catch {
|
|
799
|
-
}
|
|
800
|
-
const connected = status?.isConnected ?? status?.connection?.isConnected;
|
|
801
|
-
if (connected === false) return null;
|
|
802
|
-
const account = await this.provider.request({
|
|
803
|
-
method: "getPrimaryAccount"
|
|
804
|
-
});
|
|
805
|
-
if (account.partyId !== persisted.partyId) return null;
|
|
806
|
-
const network = [status?.network?.networkId, account.networkId].find(
|
|
807
|
-
(value) => typeof value === "string" && isRecognizedNetwork(value)
|
|
808
|
-
);
|
|
809
|
-
if (network && detectNetworkMismatch(persisted.network, network)) return null;
|
|
810
|
-
return {
|
|
811
|
-
...persisted,
|
|
812
|
-
walletId: this.walletId,
|
|
813
|
-
// persisted (lowest) → static config → runtime RPC (wins).
|
|
814
|
-
metadata: {
|
|
815
|
-
...persisted.metadata ?? {},
|
|
816
|
-
...this.staticMetadata ?? {},
|
|
817
|
-
...buildMetadata(status, account)
|
|
818
|
-
}
|
|
819
|
-
};
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
/** ledgerApi: proxy the standard CIP-0103 ledgerApi call; shape to {response:string}. */
|
|
823
|
-
makeLedgerApi() {
|
|
824
|
-
return (_ctx, _session, params) => this.guarded(async () => {
|
|
825
|
-
const result = await this.provider.request({
|
|
826
|
-
method: "ledgerApi",
|
|
827
|
-
params: {
|
|
828
|
-
requestMethod: normalizeLedgerMethodLower(params.requestMethod),
|
|
829
|
-
resource: params.resource,
|
|
830
|
-
body: ledgerApiBodyToObject(params.body)
|
|
831
|
-
}
|
|
832
|
-
});
|
|
833
|
-
if (result && typeof result.response === "string") return { response: result.response };
|
|
834
|
-
return { response: JSON.stringify(result ?? null) };
|
|
835
|
-
});
|
|
836
|
-
}
|
|
837
|
-
/**
|
|
838
|
-
* Run an operation, giving the optional `mapError` hook first crack at any
|
|
839
|
-
* thrown error. With NO hook configured this is a direct pass-through (no
|
|
840
|
-
* try/catch) — byte-identical to the baseline, and the SDK's built-in standard
|
|
841
|
-
* error mapping still applies downstream.
|
|
842
|
-
*/
|
|
843
|
-
async guarded(op) {
|
|
844
|
-
if (!this.mapError) return op();
|
|
845
|
-
try {
|
|
846
|
-
return await op();
|
|
847
|
-
} catch (err) {
|
|
848
|
-
const mapped = this.mapError(err);
|
|
849
|
-
throw mapped ?? err;
|
|
850
|
-
}
|
|
851
|
-
}
|
|
852
|
-
};
|
|
853
|
-
function buildMetadata(status, account) {
|
|
854
|
-
const meta = {};
|
|
855
|
-
const put = (k, v) => {
|
|
856
|
-
if (typeof v === "string" && v.length > 0) meta[k] = v;
|
|
857
|
-
};
|
|
858
|
-
put("kernelId", status?.kernel?.id);
|
|
859
|
-
put("publicKey", account.publicKey);
|
|
860
|
-
put("namespace", account.namespace);
|
|
861
|
-
put("networkId", account.networkId);
|
|
862
|
-
put("signingProviderId", account.signingProviderId);
|
|
863
|
-
put("hint", account.hint);
|
|
864
|
-
put("ledgerApiBaseUrl", status?.network?.ledgerApi?.baseUrl);
|
|
865
|
-
put("userId", status?.session?.userId);
|
|
866
|
-
return meta;
|
|
867
|
-
}
|
|
868
|
-
function getDiscoveryCapabilities(declared) {
|
|
869
|
-
const capabilities = ["connect", "disconnect"];
|
|
870
|
-
if (declared?.signMessage !== false) capabilities.push("signMessage");
|
|
871
|
-
if (declared?.submitTransaction !== false) capabilities.push("submitTransaction");
|
|
872
|
-
if (declared?.ledgerApi) capabilities.push("ledgerApi");
|
|
873
|
-
return capabilities;
|
|
874
|
-
}
|
|
875
|
-
var GenericDiscoveryAdapter = class _GenericDiscoveryAdapter {
|
|
876
|
-
walletId;
|
|
877
|
-
name;
|
|
878
|
-
icon;
|
|
879
|
-
/**
|
|
880
|
-
* Resolved official adapter. Set at construction for the instance form; for
|
|
881
|
-
* the factory form it stays null until {@link resolveOfficial} constructs it
|
|
882
|
-
* with the network-resolved host.
|
|
883
|
-
*/
|
|
884
|
-
official;
|
|
885
|
-
/** Factory form (null for the instance form). */
|
|
886
|
-
factory;
|
|
887
|
-
/** Network→host map for the factory form; injected by the SDK post-registry. */
|
|
888
|
-
networkHosts;
|
|
889
|
-
/** Lazily resolved from `official.provider()` — NOT at construction (SSR-safe). */
|
|
890
|
-
providerInstance = null;
|
|
891
|
-
/**
|
|
892
|
-
* Which execute verb this wallet turned out to support. Resolved on the first
|
|
893
|
-
* submit and reused, so the wallet is asked at most once.
|
|
894
|
-
*/
|
|
895
|
-
executeVerb = {};
|
|
896
|
-
capabilities;
|
|
455
|
+
capabilities;
|
|
897
456
|
ledgerApi;
|
|
898
457
|
constructor(args) {
|
|
899
458
|
const configuration = { ...args, capabilities: args.capabilities && { ...args.capabilities } };
|
|
@@ -1224,7 +783,7 @@ function createSessionProvider(options) {
|
|
|
1224
783
|
};
|
|
1225
784
|
}
|
|
1226
785
|
|
|
1227
|
-
// src/config.ts
|
|
786
|
+
// src/config-base.ts
|
|
1228
787
|
var DEFAULT_REGISTRY_URL = "https://registry.partylayer.xyz";
|
|
1229
788
|
|
|
1230
789
|
// src/adapters.ts
|
|
@@ -1255,54 +814,36 @@ var DefaultCrypto = class {
|
|
|
1255
814
|
);
|
|
1256
815
|
}
|
|
1257
816
|
async encrypt(data, key) {
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
encoded
|
|
1270
|
-
);
|
|
1271
|
-
const combined = new Uint8Array(iv.length + encrypted.byteLength);
|
|
1272
|
-
combined.set(iv);
|
|
1273
|
-
combined.set(new Uint8Array(encrypted), iv.length);
|
|
1274
|
-
return btoa(String.fromCharCode(...combined));
|
|
1275
|
-
} catch {
|
|
1276
|
-
return btoa(data);
|
|
1277
|
-
}
|
|
817
|
+
const iv = new Uint8Array(this.randomBytes(12));
|
|
818
|
+
const cryptoKey = await this.getKey(key);
|
|
819
|
+
const encrypted = await globalThis.crypto.subtle.encrypt(
|
|
820
|
+
{ name: "AES-GCM", iv },
|
|
821
|
+
cryptoKey,
|
|
822
|
+
new TextEncoder().encode(data)
|
|
823
|
+
);
|
|
824
|
+
const combined = new Uint8Array(iv.length + encrypted.byteLength);
|
|
825
|
+
combined.set(iv);
|
|
826
|
+
combined.set(new Uint8Array(encrypted), iv.length);
|
|
827
|
+
return btoa(String.fromCharCode(...combined));
|
|
1278
828
|
}
|
|
1279
829
|
async decrypt(encrypted, key) {
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
return decoder.decode(decrypted);
|
|
1295
|
-
} catch {
|
|
1296
|
-
return atob(encrypted);
|
|
1297
|
-
}
|
|
830
|
+
const cryptoKey = await this.getKey(key);
|
|
831
|
+
const combined = Uint8Array.from(atob(encrypted), (character) => character.charCodeAt(0));
|
|
832
|
+
if (combined.length < 28) throw new Error("Invalid AES-GCM session ciphertext");
|
|
833
|
+
const decrypted = await globalThis.crypto.subtle.decrypt(
|
|
834
|
+
{ name: "AES-GCM", iv: combined.slice(0, 12) },
|
|
835
|
+
cryptoKey,
|
|
836
|
+
combined.slice(12)
|
|
837
|
+
);
|
|
838
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(decrypted);
|
|
839
|
+
}
|
|
840
|
+
randomBytes(byteLength) {
|
|
841
|
+
if (!globalThis.crypto?.getRandomValues) throw new Error("Secure random entropy is unavailable");
|
|
842
|
+
if (!Number.isInteger(byteLength) || byteLength < 1 || byteLength > 65536) throw new Error("Invalid secure byte length");
|
|
843
|
+
return globalThis.crypto.getRandomValues(new Uint8Array(byteLength));
|
|
1298
844
|
}
|
|
1299
845
|
async generateKey() {
|
|
1300
|
-
|
|
1301
|
-
return Math.random().toString(36).substring(2, 15);
|
|
1302
|
-
}
|
|
1303
|
-
const array = new Uint8Array(32);
|
|
1304
|
-
crypto.getRandomValues(array);
|
|
1305
|
-
return Array.from(array, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
846
|
+
return Array.from(this.randomBytes(32), (byte) => byte.toString(16).padStart(2, "0")).join("");
|
|
1306
847
|
}
|
|
1307
848
|
};
|
|
1308
849
|
var DefaultStorage = class {
|
|
@@ -1363,22 +904,6 @@ var DefaultTelemetry = class {
|
|
|
1363
904
|
error(_error, _properties) {
|
|
1364
905
|
}
|
|
1365
906
|
};
|
|
1366
|
-
function getBuiltinAdapters() {
|
|
1367
|
-
return [
|
|
1368
|
-
new LoopAdapter(),
|
|
1369
|
-
// 5N Loop - QR code / popup
|
|
1370
|
-
new Cantor8Adapter(),
|
|
1371
|
-
// Cantor8 - deep link transport
|
|
1372
|
-
new NightlyAdapter()
|
|
1373
|
-
// Nightly - multichain wallet (injected)
|
|
1374
|
-
// Console is served via the generic CIP-0103 announce path (registry
|
|
1375
|
-
// transport:'announce') — no longer in defaults; ConsoleAdapter stays
|
|
1376
|
-
// exported below for opt-in manual/bespoke use.
|
|
1377
|
-
// Send is served via the generic CIP-0103 announce path (registry
|
|
1378
|
-
// transport:'announce') — no longer in defaults; SendAdapter stays exported
|
|
1379
|
-
// below for opt-in manual/bespoke use.
|
|
1380
|
-
];
|
|
1381
|
-
}
|
|
1382
907
|
var SDK_VERSION = "0.3.0";
|
|
1383
908
|
var MetricsTelemetryAdapter = class {
|
|
1384
909
|
config;
|
|
@@ -1642,6 +1167,23 @@ function eventLogLevel(type) {
|
|
|
1642
1167
|
return "info";
|
|
1643
1168
|
}
|
|
1644
1169
|
}
|
|
1170
|
+
var SESSION_ENTROPY_BYTES = 32;
|
|
1171
|
+
function createSecureSessionId(crypto2) {
|
|
1172
|
+
if (typeof globalThis.crypto?.getRandomValues === "function") return generateSessionId();
|
|
1173
|
+
if (!crypto2.randomBytes) throw new Error("Secure session entropy requires platform getRandomValues or CryptoAdapter.randomBytes");
|
|
1174
|
+
const encode = (bytes2) => {
|
|
1175
|
+
if (!(bytes2 instanceof Uint8Array) || bytes2.byteLength !== SESSION_ENTROPY_BYTES) {
|
|
1176
|
+
throw new Error("Session entropy must be a Uint8Array of exactly 32 bytes");
|
|
1177
|
+
}
|
|
1178
|
+
return toSessionId(`session_${Date.now()}_${Array.from(bytes2, (byte) => byte.toString(16).padStart(2, "0")).join("")}`);
|
|
1179
|
+
};
|
|
1180
|
+
const bytes = crypto2.randomBytes(SESSION_ENTROPY_BYTES);
|
|
1181
|
+
if (bytes instanceof Uint8Array) return encode(bytes);
|
|
1182
|
+
if (bytes && typeof bytes.then === "function") return Promise.resolve(bytes).then(encode);
|
|
1183
|
+
throw new Error("Session entropy must be a Uint8Array of exactly 32 bytes");
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
// src/client-base.ts
|
|
1645
1187
|
var SESSION_STORAGE_KEY = "active_session";
|
|
1646
1188
|
var WALLETS_CHANGED_DEBOUNCE_MS = 50;
|
|
1647
1189
|
function deriveAnnounceConfig(entry) {
|
|
@@ -1674,7 +1216,55 @@ function matchKnownByIdentity(providerId, base) {
|
|
|
1674
1216
|
});
|
|
1675
1217
|
}
|
|
1676
1218
|
var DEFAULT_CONNECT_TIMEOUT_MS = 12e4;
|
|
1677
|
-
var
|
|
1219
|
+
var PartyLayerClientBase = class {
|
|
1220
|
+
constructor(config, runtime = {}) {
|
|
1221
|
+
this.runtime = runtime;
|
|
1222
|
+
this.config = config;
|
|
1223
|
+
if (config.app.origin) {
|
|
1224
|
+
this.origin = config.app.origin;
|
|
1225
|
+
} else if (typeof window !== "undefined") {
|
|
1226
|
+
this.origin = window.location.origin;
|
|
1227
|
+
} else {
|
|
1228
|
+
this.origin = "unknown";
|
|
1229
|
+
}
|
|
1230
|
+
this.logger = config.logger || new DefaultLogger();
|
|
1231
|
+
this.logLevel = config.logLevel ?? "info";
|
|
1232
|
+
this.crypto = config.crypto || new DefaultCrypto();
|
|
1233
|
+
this.storage = config.storage || new DefaultStorage();
|
|
1234
|
+
if (typeof window !== "undefined" && runtime.createOwner) {
|
|
1235
|
+
this.nativeOwner = runtime.createOwner((id, identity2) => this.onNativeIdentityChanged(id, identity2));
|
|
1236
|
+
}
|
|
1237
|
+
const telemetryAdapter = createTelemetryAdapter(config.telemetry);
|
|
1238
|
+
this.telemetry = telemetryAdapter || new DefaultTelemetry();
|
|
1239
|
+
const defaults = config.adapters ?? runtime.getBuiltinAdapters?.() ?? [];
|
|
1240
|
+
const adaptersToRegister = [...defaults, ...config.additionalAdapters ?? []];
|
|
1241
|
+
for (const adapterOrClass of adaptersToRegister) {
|
|
1242
|
+
let adapter;
|
|
1243
|
+
if (typeof adapterOrClass === "function") {
|
|
1244
|
+
adapter = new adapterOrClass();
|
|
1245
|
+
} else if (isOfficialAdapterFactory(adapterOrClass)) {
|
|
1246
|
+
adapter = new GenericDiscoveryAdapter({ factory: adapterOrClass });
|
|
1247
|
+
} else if (isOfficialProviderAdapter(adapterOrClass)) {
|
|
1248
|
+
adapter = new GenericDiscoveryAdapter({ official: adapterOrClass });
|
|
1249
|
+
} else {
|
|
1250
|
+
adapter = adapterOrClass;
|
|
1251
|
+
}
|
|
1252
|
+
this.registerAdapter(adapter);
|
|
1253
|
+
this.log("debug", "adapter:registered", "Registered wallet adapter", {
|
|
1254
|
+
walletId: adapter.walletId,
|
|
1255
|
+
name: adapter.name,
|
|
1256
|
+
capabilities: adapter.getCapabilities()
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
this.registryClient = new RegistryClient({
|
|
1260
|
+
registryUrl: config.registryUrl || DEFAULT_REGISTRY_URL,
|
|
1261
|
+
channel: config.channel || "stable",
|
|
1262
|
+
registryPublicKeys: config.registryPublicKeys,
|
|
1263
|
+
storage: this.storage
|
|
1264
|
+
});
|
|
1265
|
+
this.updateRegistryStatus();
|
|
1266
|
+
if (!config.deferInitialization) this.initialize();
|
|
1267
|
+
}
|
|
1678
1268
|
config;
|
|
1679
1269
|
adapters = /* @__PURE__ */ new Map();
|
|
1680
1270
|
destroyed = false;
|
|
@@ -1724,53 +1314,6 @@ var PartyLayerClient = class {
|
|
|
1724
1314
|
origin;
|
|
1725
1315
|
nativeOwner;
|
|
1726
1316
|
sessionProvider;
|
|
1727
|
-
constructor(config) {
|
|
1728
|
-
this.config = config;
|
|
1729
|
-
if (config.app.origin) {
|
|
1730
|
-
this.origin = config.app.origin;
|
|
1731
|
-
} else if (typeof window !== "undefined") {
|
|
1732
|
-
this.origin = window.location.origin;
|
|
1733
|
-
} else {
|
|
1734
|
-
this.origin = "unknown";
|
|
1735
|
-
}
|
|
1736
|
-
this.logger = config.logger || new DefaultLogger();
|
|
1737
|
-
this.logLevel = config.logLevel ?? "info";
|
|
1738
|
-
this.crypto = config.crypto || new DefaultCrypto();
|
|
1739
|
-
this.storage = config.storage || new DefaultStorage();
|
|
1740
|
-
if (typeof window !== "undefined") {
|
|
1741
|
-
this.nativeOwner = new NativeConnectionOwner((id, identity2) => this.onNativeIdentityChanged(id, identity2));
|
|
1742
|
-
}
|
|
1743
|
-
const telemetryAdapter = createTelemetryAdapter(config.telemetry);
|
|
1744
|
-
this.telemetry = telemetryAdapter || new DefaultTelemetry();
|
|
1745
|
-
const defaults = config.adapters ?? getBuiltinAdapters();
|
|
1746
|
-
const adaptersToRegister = [...defaults, ...config.additionalAdapters ?? []];
|
|
1747
|
-
for (const adapterOrClass of adaptersToRegister) {
|
|
1748
|
-
let adapter;
|
|
1749
|
-
if (typeof adapterOrClass === "function") {
|
|
1750
|
-
adapter = new adapterOrClass();
|
|
1751
|
-
} else if (isOfficialAdapterFactory(adapterOrClass)) {
|
|
1752
|
-
adapter = new GenericDiscoveryAdapter({ factory: adapterOrClass });
|
|
1753
|
-
} else if (isOfficialProviderAdapter(adapterOrClass)) {
|
|
1754
|
-
adapter = new GenericDiscoveryAdapter({ official: adapterOrClass });
|
|
1755
|
-
} else {
|
|
1756
|
-
adapter = adapterOrClass;
|
|
1757
|
-
}
|
|
1758
|
-
this.registerAdapter(adapter);
|
|
1759
|
-
this.log("debug", "adapter:registered", "Registered wallet adapter", {
|
|
1760
|
-
walletId: adapter.walletId,
|
|
1761
|
-
name: adapter.name,
|
|
1762
|
-
capabilities: adapter.getCapabilities()
|
|
1763
|
-
});
|
|
1764
|
-
}
|
|
1765
|
-
this.registryClient = new RegistryClient({
|
|
1766
|
-
registryUrl: config.registryUrl || DEFAULT_REGISTRY_URL,
|
|
1767
|
-
channel: config.channel || "stable",
|
|
1768
|
-
registryPublicKeys: config.registryPublicKeys,
|
|
1769
|
-
storage: this.storage
|
|
1770
|
-
});
|
|
1771
|
-
this.updateRegistryStatus();
|
|
1772
|
-
if (!config.deferInitialization) this.initialize();
|
|
1773
|
-
}
|
|
1774
1317
|
/** Start browser discovery/restoration once; safe for framework effect replay. */
|
|
1775
1318
|
initialize() {
|
|
1776
1319
|
if (this.initialized || this.destroyed) return;
|
|
@@ -1899,7 +1442,7 @@ var PartyLayerClient = class {
|
|
|
1899
1442
|
}
|
|
1900
1443
|
/** Resolve only reviewed default SDKs present in the active registry. */
|
|
1901
1444
|
async ensureDefaultWallets() {
|
|
1902
|
-
if (this.config.adapters !== void 0) return;
|
|
1445
|
+
if (this.config.adapters !== void 0 || !this.runtime.loadDefaultWallet) return;
|
|
1903
1446
|
let registry;
|
|
1904
1447
|
try {
|
|
1905
1448
|
registry = await this.registryClient.getRegistry();
|
|
@@ -1913,7 +1456,7 @@ var PartyLayerClient = class {
|
|
|
1913
1456
|
if (!pending) {
|
|
1914
1457
|
pending = (async () => {
|
|
1915
1458
|
try {
|
|
1916
|
-
const entryAdapter = await loadDefaultWallet(entry.id, this.config, this.origin);
|
|
1459
|
+
const entryAdapter = await this.runtime.loadDefaultWallet(entry.id, this.config, this.origin);
|
|
1917
1460
|
if (!entryAdapter || this.destroyed) return;
|
|
1918
1461
|
const adapter = isOfficialAdapterFactory(entryAdapter) ? new GenericDiscoveryAdapter({
|
|
1919
1462
|
factory: entryAdapter,
|
|
@@ -1960,12 +1503,13 @@ var PartyLayerClient = class {
|
|
|
1960
1503
|
* adapters-only) or every non-announce entry is registered.
|
|
1961
1504
|
*/
|
|
1962
1505
|
async gateUnconnectableEntries(wallets) {
|
|
1506
|
+
if (!this.runtime.browserDiscovery) return wallets.filter((wallet) => this.adapters.has(wallet.walletId));
|
|
1963
1507
|
let hidden = null;
|
|
1964
1508
|
const announceCapabilities = /* @__PURE__ */ new Map();
|
|
1965
1509
|
try {
|
|
1966
1510
|
const registry = await this.registryClient.getRegistry();
|
|
1967
1511
|
for (const entry of registry.wallets) {
|
|
1968
|
-
if (entry.adapter?.transport === "announce") {
|
|
1512
|
+
if (entry.adapter?.transport === "announce" && this.runtime.browserDiscovery) {
|
|
1969
1513
|
announceCapabilities.set(entry.id, getAnnounceCapabilities(deriveAnnounceConfig(entry)));
|
|
1970
1514
|
continue;
|
|
1971
1515
|
}
|
|
@@ -2008,7 +1552,7 @@ var PartyLayerClient = class {
|
|
|
2008
1552
|
* ON in the browser and OFF under SSR (no `window`). Canonical: provider.md.
|
|
2009
1553
|
*/
|
|
2010
1554
|
get announceEnabled() {
|
|
2011
|
-
if (typeof window === "undefined") return false;
|
|
1555
|
+
if (typeof window === "undefined" || !this.runtime.browserDiscovery) return false;
|
|
2012
1556
|
return this.config.discovery?.announce ?? true;
|
|
2013
1557
|
}
|
|
2014
1558
|
/**
|
|
@@ -2269,7 +1813,7 @@ var PartyLayerClient = class {
|
|
|
2269
1813
|
}
|
|
2270
1814
|
}
|
|
2271
1815
|
} else if (availableWallets.length === 0) {
|
|
2272
|
-
throw new WalletNotFoundError("No wallets available");
|
|
1816
|
+
throw new WalletNotFoundError(this.runtime.noWalletsMessage ?? "No wallets available");
|
|
2273
1817
|
} else {
|
|
2274
1818
|
selectedWallet = availableWallets[0];
|
|
2275
1819
|
}
|
|
@@ -2317,8 +1861,8 @@ var PartyLayerClient = class {
|
|
|
2317
1861
|
return { selectedWallet, adapter, ctx, isNativeWallet };
|
|
2318
1862
|
}
|
|
2319
1863
|
/**
|
|
2320
|
-
*
|
|
2321
|
-
* build / persist / emit the session. Separated from plan resolution so the
|
|
1864
|
+
* Generate secure entropy, invoke `adapter.connect()` without awaiting the
|
|
1865
|
+
* platform RNG path, then build / persist / emit the session. Separated from plan resolution so the
|
|
2322
1866
|
* popup-safe fast-path can call it gesture-synchronously.
|
|
2323
1867
|
*/
|
|
2324
1868
|
async completeConnect(plan, options, correlationId = newCorrelationId(), attempt) {
|
|
@@ -2328,6 +1872,9 @@ var PartyLayerClient = class {
|
|
|
2328
1872
|
if (attempt === void 0) ++this.sessionRevision;
|
|
2329
1873
|
if (this.destroyed || currentAttempt !== this.connectionAttempt) throw new Error("Wallet connection was superseded");
|
|
2330
1874
|
const { selectedWallet, adapter, ctx } = plan;
|
|
1875
|
+
const entropy = createSecureSessionId(this.crypto);
|
|
1876
|
+
const sessionId = typeof entropy === "string" ? entropy : await entropy;
|
|
1877
|
+
if (this.destroyed || currentAttempt !== this.connectionAttempt) throw new Error("Wallet connection was superseded");
|
|
2331
1878
|
if (!nativeOwnerBindings.has(adapter) && this.nativeOwner?.getSession()) this.nativeOwner.release();
|
|
2332
1879
|
const timeoutMs = options?.timeoutMs || DEFAULT_CONNECT_TIMEOUT_MS;
|
|
2333
1880
|
const abort = new AbortController();
|
|
@@ -2353,7 +1900,7 @@ var PartyLayerClient = class {
|
|
|
2353
1900
|
}
|
|
2354
1901
|
if (this.destroyed || currentAttempt !== this.connectionAttempt) throw new Error("Wallet connection was superseded");
|
|
2355
1902
|
const session = {
|
|
2356
|
-
sessionId
|
|
1903
|
+
sessionId,
|
|
2357
1904
|
walletId: selectedWallet.walletId,
|
|
2358
1905
|
partyId: result.partyId,
|
|
2359
1906
|
// The wallet's reported network (adapters that read the live wallet —
|
|
@@ -2972,6 +2519,8 @@ var PartyLayerClient = class {
|
|
|
2972
2519
|
this.activeSessionNeedsProbe = true;
|
|
2973
2520
|
return session;
|
|
2974
2521
|
} catch (err) {
|
|
2522
|
+
if (!current()) return this.activeSession;
|
|
2523
|
+
await this.removeSession();
|
|
2975
2524
|
this.log("warn", "session:restore_failed", "Failed to restore session", {
|
|
2976
2525
|
error: err instanceof Error ? err.message : String(err)
|
|
2977
2526
|
}, correlationId);
|
|
@@ -3071,6 +2620,477 @@ var PartyLayerClient = class {
|
|
|
3071
2620
|
}
|
|
3072
2621
|
}
|
|
3073
2622
|
};
|
|
2623
|
+
function getBuiltinAdapters() {
|
|
2624
|
+
return [
|
|
2625
|
+
new LoopAdapter(),
|
|
2626
|
+
// 5N Loop - QR code / popup
|
|
2627
|
+
new Cantor8Adapter(),
|
|
2628
|
+
// Cantor8 - deep link transport
|
|
2629
|
+
new NightlyAdapter()
|
|
2630
|
+
// Nightly - multichain wallet (injected)
|
|
2631
|
+
// Console is served via the generic CIP-0103 announce path (registry
|
|
2632
|
+
// transport:'announce') — no longer in defaults; ConsoleAdapter stays
|
|
2633
|
+
// exported below for opt-in manual/bespoke use.
|
|
2634
|
+
// Send is served via the generic CIP-0103 announce path (registry
|
|
2635
|
+
// transport:'announce') — no longer in defaults; SendAdapter stays exported
|
|
2636
|
+
// below for opt-in manual/bespoke use.
|
|
2637
|
+
];
|
|
2638
|
+
}
|
|
2639
|
+
var loaders = {
|
|
2640
|
+
walley: async () => {
|
|
2641
|
+
const { WalleyAdapter } = await import('@k2flabs/walley-dapp-sdk');
|
|
2642
|
+
return { providerId: "walley", create: (host) => new WalleyAdapter({ host }) };
|
|
2643
|
+
},
|
|
2644
|
+
cauri: async () => {
|
|
2645
|
+
const { cauriAdapterFactory } = await import('@lithiumdigital/cauri-dapp-sdk');
|
|
2646
|
+
return cauriAdapterFactory;
|
|
2647
|
+
},
|
|
2648
|
+
oneswap: async (network) => {
|
|
2649
|
+
const { createOneSwapHostedAdapter } = await import('#partylayer-hosted-wallet-profiles');
|
|
2650
|
+
return {
|
|
2651
|
+
providerId: "oneswap",
|
|
2652
|
+
create: (host) => createOneSwapHostedAdapter({
|
|
2653
|
+
walletUrl: host,
|
|
2654
|
+
networkId: toCAIP2Network(network).networkId
|
|
2655
|
+
})
|
|
2656
|
+
};
|
|
2657
|
+
},
|
|
2658
|
+
vala: async () => {
|
|
2659
|
+
const { valaAdapterFactory } = await import('@vala-wallet/dapp-sdk');
|
|
2660
|
+
return valaAdapterFactory;
|
|
2661
|
+
}
|
|
2662
|
+
};
|
|
2663
|
+
async function loadDefaultWallet(id, config, origin) {
|
|
2664
|
+
if (id === "walletconnect" && config.walletConnect?.projectId.trim()) {
|
|
2665
|
+
const { WalletConnectAdapter } = await import('@partylayer/adapter-walletconnect');
|
|
2666
|
+
return new WalletConnectAdapter({
|
|
2667
|
+
...config.walletConnect,
|
|
2668
|
+
metadata: config.walletConnect.metadata ?? {
|
|
2669
|
+
name: config.app.name,
|
|
2670
|
+
description: config.app.name,
|
|
2671
|
+
url: origin === "unknown" ? "" : origin,
|
|
2672
|
+
icons: []
|
|
2673
|
+
}
|
|
2674
|
+
});
|
|
2675
|
+
}
|
|
2676
|
+
if (id === "bron" && config.bron) {
|
|
2677
|
+
const { BronAdapter: BronAdapter2 } = await import('@partylayer/adapter-bron');
|
|
2678
|
+
return new BronAdapter2(config.bron);
|
|
2679
|
+
}
|
|
2680
|
+
return Object.prototype.hasOwnProperty.call(loaders, id) ? loaders[id](config.network) : void 0;
|
|
2681
|
+
}
|
|
2682
|
+
var SILENT_PROBE_TIMEOUT_MS = 1e4;
|
|
2683
|
+
function disconnected() {
|
|
2684
|
+
return new Error("Native wallet connection is no longer active");
|
|
2685
|
+
}
|
|
2686
|
+
function teardown(source) {
|
|
2687
|
+
try {
|
|
2688
|
+
source?.teardown?.();
|
|
2689
|
+
} catch {
|
|
2690
|
+
}
|
|
2691
|
+
}
|
|
2692
|
+
function abortable(promise, signal) {
|
|
2693
|
+
return new Promise((resolve, reject) => {
|
|
2694
|
+
const abort = () => reject(disconnected());
|
|
2695
|
+
if (signal.aborted) {
|
|
2696
|
+
reject(disconnected());
|
|
2697
|
+
return;
|
|
2698
|
+
}
|
|
2699
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
2700
|
+
promise.then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
2701
|
+
});
|
|
2702
|
+
}
|
|
2703
|
+
async function readIdentity(provider, registration, signal) {
|
|
2704
|
+
const account = await abortable(
|
|
2705
|
+
provider.request({ method: "getPrimaryAccount" }),
|
|
2706
|
+
signal
|
|
2707
|
+
);
|
|
2708
|
+
assertPrimaryParty(account);
|
|
2709
|
+
let status;
|
|
2710
|
+
try {
|
|
2711
|
+
status = await abortable(provider.request({ method: "status" }), signal);
|
|
2712
|
+
} catch (error) {
|
|
2713
|
+
if (signal.aborted) throw error;
|
|
2714
|
+
}
|
|
2715
|
+
if (status?.isConnected === false || status?.connection?.isConnected === false)
|
|
2716
|
+
throw disconnected();
|
|
2717
|
+
const network = [status?.network?.networkId, account.networkId, registration.network].find(
|
|
2718
|
+
(value) => typeof value === "string" && isRecognizedNetwork(value)
|
|
2719
|
+
) ?? registration.network;
|
|
2720
|
+
if (registration.expected && (account.partyId !== registration.expected.partyId || detectNetworkMismatch(registration.expected.network, network)))
|
|
2721
|
+
throw disconnected();
|
|
2722
|
+
if (signal.aborted) throw disconnected();
|
|
2723
|
+
return { account, network, status };
|
|
2724
|
+
}
|
|
2725
|
+
var NativeConnectionOwner = class {
|
|
2726
|
+
constructor(changed = () => {
|
|
2727
|
+
}) {
|
|
2728
|
+
this.changed = changed;
|
|
2729
|
+
}
|
|
2730
|
+
client = null;
|
|
2731
|
+
initialization = null;
|
|
2732
|
+
registrations = /* @__PURE__ */ new Map();
|
|
2733
|
+
operation = null;
|
|
2734
|
+
pending = null;
|
|
2735
|
+
closing = null;
|
|
2736
|
+
projection = null;
|
|
2737
|
+
unsubscribers = [];
|
|
2738
|
+
disposed = false;
|
|
2739
|
+
refreshRevision = 0;
|
|
2740
|
+
identityRefreshing = false;
|
|
2741
|
+
probes = /* @__PURE__ */ new Set();
|
|
2742
|
+
get busy() {
|
|
2743
|
+
return this.operation !== null || this.closing !== null;
|
|
2744
|
+
}
|
|
2745
|
+
assertAvailable() {
|
|
2746
|
+
if (this.disposed) throw disconnected();
|
|
2747
|
+
if (this.busy) throw new Error("A native wallet operation is already in progress");
|
|
2748
|
+
}
|
|
2749
|
+
/** Run during picker preparation, never between a click and provider.connect. */
|
|
2750
|
+
async prepare(id, source, network) {
|
|
2751
|
+
if (this.disposed) throw disconnected();
|
|
2752
|
+
this.registrations.set(id, { id, source, network });
|
|
2753
|
+
if (!this.initialization) {
|
|
2754
|
+
this.initialization = (async () => {
|
|
2755
|
+
const { DiscoveryClient } = await import('@canton-network/core-wallet-discovery');
|
|
2756
|
+
const client = await DiscoveryClient.create({ adapters: [] });
|
|
2757
|
+
if (this.disposed) {
|
|
2758
|
+
client.destroy();
|
|
2759
|
+
return;
|
|
2760
|
+
}
|
|
2761
|
+
this.client = client;
|
|
2762
|
+
})();
|
|
2763
|
+
}
|
|
2764
|
+
await this.initialization;
|
|
2765
|
+
if (this.disposed || !this.client) throw disconnected();
|
|
2766
|
+
for (const registration of this.registrations.values())
|
|
2767
|
+
this.client.registerAdapter(this.wrap(registration));
|
|
2768
|
+
}
|
|
2769
|
+
wrap(registration) {
|
|
2770
|
+
const source = registration.source;
|
|
2771
|
+
return {
|
|
2772
|
+
providerId: registration.id,
|
|
2773
|
+
name: source.name,
|
|
2774
|
+
type: source.type === "desktop" || source.type === "mobile" || source.type === "remote" ? source.type : "browser",
|
|
2775
|
+
icon: source.icon,
|
|
2776
|
+
getInfo: () => ({
|
|
2777
|
+
providerId: registration.id,
|
|
2778
|
+
name: source.name,
|
|
2779
|
+
type: "browser",
|
|
2780
|
+
icon: source.icon
|
|
2781
|
+
}),
|
|
2782
|
+
detect: () => source.detect(),
|
|
2783
|
+
provider: () => this.guard(source.provider(), registration),
|
|
2784
|
+
teardown: () => teardown(source),
|
|
2785
|
+
restore: async () => {
|
|
2786
|
+
const operation = this.operation;
|
|
2787
|
+
if (!operation) throw disconnected();
|
|
2788
|
+
try {
|
|
2789
|
+
const raw = source.restore ? await abortable(source.restore(), operation.abort.signal) : source.provider();
|
|
2790
|
+
if (!raw) return null;
|
|
2791
|
+
const provider = raw;
|
|
2792
|
+
operation.identity = await readIdentity(provider, registration, operation.abort.signal);
|
|
2793
|
+
return this.guard(provider, registration);
|
|
2794
|
+
} catch (error) {
|
|
2795
|
+
teardown(source);
|
|
2796
|
+
throw error;
|
|
2797
|
+
}
|
|
2798
|
+
}
|
|
2799
|
+
};
|
|
2800
|
+
}
|
|
2801
|
+
guard(raw, registration) {
|
|
2802
|
+
const provider = {
|
|
2803
|
+
request: async (args) => {
|
|
2804
|
+
if (this.disposed) throw disconnected();
|
|
2805
|
+
if (args.method !== "connect") {
|
|
2806
|
+
if (this.client?.getActiveSession()?.provider !== provider) throw disconnected();
|
|
2807
|
+
if (!this.projection && args.method !== "disconnect") throw disconnected();
|
|
2808
|
+
if (this.identityRefreshing && ![
|
|
2809
|
+
"status",
|
|
2810
|
+
"getPrimaryAccount",
|
|
2811
|
+
"listAccounts",
|
|
2812
|
+
"getActiveNetwork",
|
|
2813
|
+
"isConnected"
|
|
2814
|
+
].includes(args.method)) {
|
|
2815
|
+
throw new Error("Native wallet identity is being refreshed");
|
|
2816
|
+
}
|
|
2817
|
+
const result = await (args.method === "disconnect" && this.operation ? abortable(raw.request(args), this.operation.abort.signal) : raw.request(args));
|
|
2818
|
+
if (args.method === "status") {
|
|
2819
|
+
const status = result;
|
|
2820
|
+
if (status?.isConnected === false || status?.connection?.isConnected === false) {
|
|
2821
|
+
this.invalidate(registration.id);
|
|
2822
|
+
}
|
|
2823
|
+
}
|
|
2824
|
+
return result;
|
|
2825
|
+
}
|
|
2826
|
+
const operation = this.operation;
|
|
2827
|
+
if (!operation || operation.abort.signal.aborted) throw disconnected();
|
|
2828
|
+
const response = await abortable(raw.request(args), operation.abort.signal);
|
|
2829
|
+
assertConnectionAccepted(response);
|
|
2830
|
+
operation.identity = await readIdentity(raw, registration, operation.abort.signal);
|
|
2831
|
+
return response;
|
|
2832
|
+
},
|
|
2833
|
+
on: (event, listener) => {
|
|
2834
|
+
raw.on(event, listener);
|
|
2835
|
+
return provider;
|
|
2836
|
+
},
|
|
2837
|
+
emit: (event, ...args) => raw.emit(event, ...args),
|
|
2838
|
+
removeListener: (event, listener) => {
|
|
2839
|
+
raw.removeListener(event, listener);
|
|
2840
|
+
return provider;
|
|
2841
|
+
}
|
|
2842
|
+
};
|
|
2843
|
+
return provider;
|
|
2844
|
+
}
|
|
2845
|
+
/** No await precedes the official client's connect call. */
|
|
2846
|
+
connect(id, signal) {
|
|
2847
|
+
this.assertAvailable();
|
|
2848
|
+
if (!this.client || !this.registrations.has(id))
|
|
2849
|
+
return Promise.reject(new Error("Native wallet was not prepared"));
|
|
2850
|
+
const operation = { abort: new AbortController(), id };
|
|
2851
|
+
this.operation = operation;
|
|
2852
|
+
this.detach();
|
|
2853
|
+
const previous = this.client.getActiveSession();
|
|
2854
|
+
teardown(previous?.adapter);
|
|
2855
|
+
const previousId = this.projection?.id;
|
|
2856
|
+
this.projection = null;
|
|
2857
|
+
if (previousId) this.changed(previousId, null);
|
|
2858
|
+
const abort = () => operation.abort.abort();
|
|
2859
|
+
if (signal?.aborted) abort();
|
|
2860
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
2861
|
+
const task = (async () => {
|
|
2862
|
+
try {
|
|
2863
|
+
await this.client.connect(id);
|
|
2864
|
+
if (operation.abort.signal.aborted || this.disposed || !operation.identity)
|
|
2865
|
+
throw disconnected();
|
|
2866
|
+
this.projection = { id, identity: operation.identity };
|
|
2867
|
+
this.attach(id);
|
|
2868
|
+
return operation.identity;
|
|
2869
|
+
} catch (error) {
|
|
2870
|
+
this.client?.destroy();
|
|
2871
|
+
this.client = null;
|
|
2872
|
+
this.initialization = null;
|
|
2873
|
+
teardown(this.registrations.get(id)?.source);
|
|
2874
|
+
throw error;
|
|
2875
|
+
} finally {
|
|
2876
|
+
signal?.removeEventListener("abort", abort);
|
|
2877
|
+
if (this.operation === operation) this.operation = null;
|
|
2878
|
+
}
|
|
2879
|
+
})();
|
|
2880
|
+
this.pending = task;
|
|
2881
|
+
void task.then(
|
|
2882
|
+
() => {
|
|
2883
|
+
if (this.pending === task) this.pending = null;
|
|
2884
|
+
},
|
|
2885
|
+
() => {
|
|
2886
|
+
if (this.pending === task) this.pending = null;
|
|
2887
|
+
}
|
|
2888
|
+
);
|
|
2889
|
+
return task;
|
|
2890
|
+
}
|
|
2891
|
+
async restore(expected) {
|
|
2892
|
+
if (!this.client || this.projection || this.busy || this.disposed) return this.getSession();
|
|
2893
|
+
const registration = expected && this.registrations.get(expected.id);
|
|
2894
|
+
if (registration) registration.expected = expected;
|
|
2895
|
+
const operation = { abort: new AbortController() };
|
|
2896
|
+
this.operation = operation;
|
|
2897
|
+
const timeout = setTimeout(() => operation.abort.abort(), SILENT_PROBE_TIMEOUT_MS);
|
|
2898
|
+
const task = (async () => {
|
|
2899
|
+
try {
|
|
2900
|
+
await this.client.restorePersistedSessionIfNeeded();
|
|
2901
|
+
const active = this.client?.getActiveSession();
|
|
2902
|
+
if (!active || !operation.identity || operation.abort.signal.aborted || this.disposed)
|
|
2903
|
+
return null;
|
|
2904
|
+
if (expected && active.providerId !== expected.id) {
|
|
2905
|
+
teardown(active.adapter);
|
|
2906
|
+
this.client.destroy();
|
|
2907
|
+
this.client = null;
|
|
2908
|
+
this.initialization = null;
|
|
2909
|
+
return null;
|
|
2910
|
+
}
|
|
2911
|
+
const id = String(active.providerId);
|
|
2912
|
+
this.projection = { id, identity: operation.identity };
|
|
2913
|
+
this.attach(id);
|
|
2914
|
+
return this.getSession();
|
|
2915
|
+
} finally {
|
|
2916
|
+
clearTimeout(timeout);
|
|
2917
|
+
if (registration) delete registration.expected;
|
|
2918
|
+
if (this.operation === operation) this.operation = null;
|
|
2919
|
+
}
|
|
2920
|
+
})();
|
|
2921
|
+
this.pending = task;
|
|
2922
|
+
try {
|
|
2923
|
+
return await task;
|
|
2924
|
+
} finally {
|
|
2925
|
+
if (this.pending === task) this.pending = null;
|
|
2926
|
+
}
|
|
2927
|
+
}
|
|
2928
|
+
getSession() {
|
|
2929
|
+
if (this.disposed || !this.client?.getActiveSession() || !this.projection) return null;
|
|
2930
|
+
return this.projection;
|
|
2931
|
+
}
|
|
2932
|
+
getProvider(id) {
|
|
2933
|
+
if (!this.getSession() || id && this.projection?.id !== id) throw disconnected();
|
|
2934
|
+
return this.client.getProvider();
|
|
2935
|
+
}
|
|
2936
|
+
/** A bare signature must not be attributed to an old, eventless primary party. */
|
|
2937
|
+
async validateIdentity(id, partyId, network) {
|
|
2938
|
+
const provider = this.getProvider(id);
|
|
2939
|
+
const revision = ++this.refreshRevision;
|
|
2940
|
+
this.identityRefreshing = true;
|
|
2941
|
+
let verified = false;
|
|
2942
|
+
try {
|
|
2943
|
+
const identity2 = await this.probe(provider, this.registrations.get(id));
|
|
2944
|
+
if (revision !== this.refreshRevision || this.getProvider(id) !== provider)
|
|
2945
|
+
throw disconnected();
|
|
2946
|
+
verified = true;
|
|
2947
|
+
this.projection = { id, identity: identity2 };
|
|
2948
|
+
if (identity2.account.partyId !== partyId || detectNetworkMismatch(network, identity2.network)) {
|
|
2949
|
+
this.changed(id, identity2);
|
|
2950
|
+
throw new Error("Wallet identity changed during message signing");
|
|
2951
|
+
}
|
|
2952
|
+
} catch (error) {
|
|
2953
|
+
if (!verified && revision === this.refreshRevision) this.invalidate(id);
|
|
2954
|
+
throw error;
|
|
2955
|
+
} finally {
|
|
2956
|
+
if (revision === this.refreshRevision) this.identityRefreshing = false;
|
|
2957
|
+
}
|
|
2958
|
+
}
|
|
2959
|
+
async probe(provider, registration) {
|
|
2960
|
+
const abort = new AbortController();
|
|
2961
|
+
this.probes.add(abort);
|
|
2962
|
+
const timeout = setTimeout(() => abort.abort(), SILENT_PROBE_TIMEOUT_MS);
|
|
2963
|
+
try {
|
|
2964
|
+
return await readIdentity(provider, registration, abort.signal);
|
|
2965
|
+
} finally {
|
|
2966
|
+
clearTimeout(timeout);
|
|
2967
|
+
this.probes.delete(abort);
|
|
2968
|
+
}
|
|
2969
|
+
}
|
|
2970
|
+
detach() {
|
|
2971
|
+
++this.refreshRevision;
|
|
2972
|
+
this.identityRefreshing = false;
|
|
2973
|
+
for (const probe of this.probes) probe.abort();
|
|
2974
|
+
this.probes.clear();
|
|
2975
|
+
this.unsubscribers.splice(0).forEach((unsubscribe) => {
|
|
2976
|
+
try {
|
|
2977
|
+
unsubscribe();
|
|
2978
|
+
} catch {
|
|
2979
|
+
}
|
|
2980
|
+
});
|
|
2981
|
+
}
|
|
2982
|
+
attach(id) {
|
|
2983
|
+
const provider = this.getProvider(id);
|
|
2984
|
+
const update = (payload) => {
|
|
2985
|
+
const status = payload;
|
|
2986
|
+
if (status?.connection?.isConnected === false || status?.isConnected === false || Array.isArray(payload) && payload.length === 0) {
|
|
2987
|
+
this.invalidate(id);
|
|
2988
|
+
return;
|
|
2989
|
+
}
|
|
2990
|
+
const revision = ++this.refreshRevision;
|
|
2991
|
+
this.identityRefreshing = true;
|
|
2992
|
+
const registration = this.registrations.get(id);
|
|
2993
|
+
void this.probe(provider, registration).then(
|
|
2994
|
+
(identity2) => {
|
|
2995
|
+
if (revision !== this.refreshRevision || this.projection?.id !== id || this.disposed)
|
|
2996
|
+
return;
|
|
2997
|
+
this.identityRefreshing = false;
|
|
2998
|
+
this.projection = { id, identity: identity2 };
|
|
2999
|
+
this.changed(id, identity2);
|
|
3000
|
+
},
|
|
3001
|
+
() => {
|
|
3002
|
+
if (revision === this.refreshRevision) this.invalidate(id);
|
|
3003
|
+
}
|
|
3004
|
+
);
|
|
3005
|
+
};
|
|
3006
|
+
for (const event of ["accountsChanged", "statusChanged"]) {
|
|
3007
|
+
provider.on(event, update);
|
|
3008
|
+
this.unsubscribers.push(() => provider.removeListener(event, update));
|
|
3009
|
+
}
|
|
3010
|
+
}
|
|
3011
|
+
invalidate(id) {
|
|
3012
|
+
if (this.projection?.id !== id) return;
|
|
3013
|
+
this.detach();
|
|
3014
|
+
this.projection = null;
|
|
3015
|
+
teardown(this.client?.getActiveSession()?.adapter);
|
|
3016
|
+
this.client?.destroy();
|
|
3017
|
+
this.client = null;
|
|
3018
|
+
this.initialization = null;
|
|
3019
|
+
this.changed(id, null);
|
|
3020
|
+
}
|
|
3021
|
+
/** Local transport cleanup when switching to a proprietary integration. */
|
|
3022
|
+
release() {
|
|
3023
|
+
this.assertAvailable();
|
|
3024
|
+
const id = this.projection?.id;
|
|
3025
|
+
this.detach();
|
|
3026
|
+
this.projection = null;
|
|
3027
|
+
teardown(this.client?.getActiveSession()?.adapter);
|
|
3028
|
+
this.client?.destroy();
|
|
3029
|
+
this.client = null;
|
|
3030
|
+
this.initialization = null;
|
|
3031
|
+
if (id) this.changed(id, null);
|
|
3032
|
+
}
|
|
3033
|
+
disconnect() {
|
|
3034
|
+
if (this.closing) return this.closing;
|
|
3035
|
+
this.operation?.abort.abort();
|
|
3036
|
+
const task = (async () => {
|
|
3037
|
+
await this.pending?.catch(() => {
|
|
3038
|
+
});
|
|
3039
|
+
this.detach();
|
|
3040
|
+
const id = this.projection?.id;
|
|
3041
|
+
this.projection = null;
|
|
3042
|
+
const operation = { abort: new AbortController() };
|
|
3043
|
+
this.operation = operation;
|
|
3044
|
+
const timeout = setTimeout(() => operation.abort.abort(), SILENT_PROBE_TIMEOUT_MS);
|
|
3045
|
+
try {
|
|
3046
|
+
await this.client?.disconnect();
|
|
3047
|
+
} finally {
|
|
3048
|
+
clearTimeout(timeout);
|
|
3049
|
+
if (this.client?.getActiveSession()) {
|
|
3050
|
+
this.client.destroy();
|
|
3051
|
+
this.client = null;
|
|
3052
|
+
this.initialization = null;
|
|
3053
|
+
}
|
|
3054
|
+
this.operation = null;
|
|
3055
|
+
if (id) this.changed(id, null);
|
|
3056
|
+
}
|
|
3057
|
+
})();
|
|
3058
|
+
this.closing = task;
|
|
3059
|
+
void task.then(
|
|
3060
|
+
() => {
|
|
3061
|
+
if (this.closing === task) this.closing = null;
|
|
3062
|
+
},
|
|
3063
|
+
() => {
|
|
3064
|
+
if (this.closing === task) this.closing = null;
|
|
3065
|
+
}
|
|
3066
|
+
);
|
|
3067
|
+
return task;
|
|
3068
|
+
}
|
|
3069
|
+
destroy() {
|
|
3070
|
+
if (this.disposed) return;
|
|
3071
|
+
this.disposed = true;
|
|
3072
|
+
this.operation?.abort.abort();
|
|
3073
|
+
this.detach();
|
|
3074
|
+
this.projection = null;
|
|
3075
|
+
teardown(this.client?.getActiveSession()?.adapter);
|
|
3076
|
+
if (!this.client?.getActiveSession() && this.operation?.id)
|
|
3077
|
+
teardown(this.registrations.get(this.operation.id)?.source);
|
|
3078
|
+
this.client?.destroy();
|
|
3079
|
+
this.client = null;
|
|
3080
|
+
}
|
|
3081
|
+
};
|
|
3082
|
+
|
|
3083
|
+
// src/client.ts
|
|
3084
|
+
var PartyLayerClient = class extends PartyLayerClientBase {
|
|
3085
|
+
constructor(config) {
|
|
3086
|
+
super(config, {
|
|
3087
|
+
getBuiltinAdapters,
|
|
3088
|
+
loadDefaultWallet: (id, _sharedConfig, origin) => loadDefaultWallet(id, config, origin),
|
|
3089
|
+
createOwner: (onIdentityChanged) => new NativeConnectionOwner(onIdentityChanged),
|
|
3090
|
+
browserDiscovery: true
|
|
3091
|
+
});
|
|
3092
|
+
}
|
|
3093
|
+
};
|
|
3074
3094
|
function createPartyLayer(config) {
|
|
3075
3095
|
return new PartyLayerClient(config);
|
|
3076
3096
|
}
|