@partylayer/sdk 0.6.0 → 0.8.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/dist/index.d.mts +229 -4
- package/dist/index.d.ts +229 -4
- package/dist/index.js +463 -122
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +459 -126
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -11,6 +11,155 @@ var adapterSend = require('@partylayer/adapter-send');
|
|
|
11
11
|
var adapterBron = require('@partylayer/adapter-bron');
|
|
12
12
|
|
|
13
13
|
// src/client.ts
|
|
14
|
+
var ANNOUNCED_WALLET_ID_PREFIX = "browser:ext:";
|
|
15
|
+
function announcedWalletId(announceId) {
|
|
16
|
+
return core.toWalletId(`${ANNOUNCED_WALLET_ID_PREFIX}${announceId}`);
|
|
17
|
+
}
|
|
18
|
+
var GenericAnnounceAdapter = class {
|
|
19
|
+
walletId;
|
|
20
|
+
name;
|
|
21
|
+
/** Icon surfaced to the picker (announce detail). */
|
|
22
|
+
icon;
|
|
23
|
+
provider;
|
|
24
|
+
constructor(args) {
|
|
25
|
+
this.walletId = announcedWalletId(args.announceId);
|
|
26
|
+
this.name = args.name && args.name.length > 0 ? args.name : `Canton Wallet (${args.announceId.slice(0, 6)}\u2026)`;
|
|
27
|
+
this.icon = args.icon;
|
|
28
|
+
this.provider = args.provider;
|
|
29
|
+
}
|
|
30
|
+
/** Baseline CIP-0103 capabilities every announced wallet must implement. */
|
|
31
|
+
getCapabilities() {
|
|
32
|
+
return ["connect", "signMessage", "submitTransaction"];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The adapter is only ever constructed for a wallet that just announced, so
|
|
36
|
+
* its presence is established by the announce handshake itself.
|
|
37
|
+
*/
|
|
38
|
+
async detectInstalled() {
|
|
39
|
+
return { installed: true, reason: "Announced via canton:announceProvider" };
|
|
40
|
+
}
|
|
41
|
+
async connect(ctx) {
|
|
42
|
+
await this.provider.request({ method: "connect" });
|
|
43
|
+
const account = await this.provider.request({
|
|
44
|
+
method: "getPrimaryAccount"
|
|
45
|
+
});
|
|
46
|
+
let reportedNetwork;
|
|
47
|
+
try {
|
|
48
|
+
const status = await this.provider.request({
|
|
49
|
+
method: "status"
|
|
50
|
+
});
|
|
51
|
+
reportedNetwork = status?.network?.networkId;
|
|
52
|
+
} catch {
|
|
53
|
+
}
|
|
54
|
+
const partyId = core.toPartyId(account.partyId);
|
|
55
|
+
const session = {
|
|
56
|
+
walletId: this.walletId,
|
|
57
|
+
partyId,
|
|
58
|
+
network: reportedNetwork ?? account.networkId ?? ctx.network
|
|
59
|
+
};
|
|
60
|
+
return { partyId, session, capabilities: this.getCapabilities() };
|
|
61
|
+
}
|
|
62
|
+
async disconnect() {
|
|
63
|
+
try {
|
|
64
|
+
await this.provider.request({ method: "disconnect" });
|
|
65
|
+
} catch {
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async signMessage(_ctx, _session, params) {
|
|
69
|
+
return this.provider.request({
|
|
70
|
+
method: "signMessage",
|
|
71
|
+
params: { message: params.message }
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
async submitTransaction(_ctx, _session, params) {
|
|
75
|
+
return this.provider.request({
|
|
76
|
+
method: "prepareExecute",
|
|
77
|
+
params
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var GenericDiscoveryAdapter = class {
|
|
82
|
+
walletId;
|
|
83
|
+
name;
|
|
84
|
+
icon;
|
|
85
|
+
official;
|
|
86
|
+
/** Lazily resolved from `official.provider()` — NOT at construction (SSR-safe). */
|
|
87
|
+
providerInstance = null;
|
|
88
|
+
constructor(args) {
|
|
89
|
+
this.official = args.official;
|
|
90
|
+
this.walletId = args.walletId ?? core.toWalletId(args.official.providerId);
|
|
91
|
+
this.name = args.name && args.name.length > 0 ? args.name : args.official.name;
|
|
92
|
+
this.icon = args.icon ?? args.official.icon;
|
|
93
|
+
}
|
|
94
|
+
/** Lazily obtain (and cache) the official provider — deferred off the init/SSR path.
|
|
95
|
+
* `official.provider()` is typed `OfficialProvider` (loose `request` so real
|
|
96
|
+
* official adapters are assignable); it is call-compatible with CIP0103Provider
|
|
97
|
+
* (the bridge only ever calls `request({ method, params })`). */
|
|
98
|
+
get provider() {
|
|
99
|
+
if (this.providerInstance === null) {
|
|
100
|
+
this.providerInstance = this.official.provider();
|
|
101
|
+
}
|
|
102
|
+
return this.providerInstance;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Baseline CIP-0103 capabilities. NEVER includes `'events'`: popup/remote
|
|
106
|
+
* wallets expose the event surface but do not emit (truthfulness doctrine).
|
|
107
|
+
*/
|
|
108
|
+
getCapabilities() {
|
|
109
|
+
return ["connect", "disconnect", "signMessage", "submitTransaction"];
|
|
110
|
+
}
|
|
111
|
+
/** Install/availability probe — delegated to the official adapter (popup-free). */
|
|
112
|
+
async detectInstalled() {
|
|
113
|
+
try {
|
|
114
|
+
const installed = await this.official.detect();
|
|
115
|
+
return {
|
|
116
|
+
installed,
|
|
117
|
+
reason: installed ? void 0 : "Wallet reported not available"
|
|
118
|
+
};
|
|
119
|
+
} catch {
|
|
120
|
+
return { installed: false, reason: "Detection failed" };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
async connect(ctx) {
|
|
124
|
+
await this.provider.request({ method: "connect" });
|
|
125
|
+
const account = await this.provider.request({
|
|
126
|
+
method: "getPrimaryAccount"
|
|
127
|
+
});
|
|
128
|
+
let reportedNetwork;
|
|
129
|
+
try {
|
|
130
|
+
const status = await this.provider.request({
|
|
131
|
+
method: "status"
|
|
132
|
+
});
|
|
133
|
+
reportedNetwork = status?.network?.networkId;
|
|
134
|
+
} catch {
|
|
135
|
+
}
|
|
136
|
+
const partyId = core.toPartyId(account.partyId);
|
|
137
|
+
const session = {
|
|
138
|
+
walletId: this.walletId,
|
|
139
|
+
partyId,
|
|
140
|
+
network: reportedNetwork ?? account.networkId ?? ctx.network
|
|
141
|
+
};
|
|
142
|
+
return { partyId, session, capabilities: this.getCapabilities() };
|
|
143
|
+
}
|
|
144
|
+
async disconnect() {
|
|
145
|
+
try {
|
|
146
|
+
await this.provider.request({ method: "disconnect" });
|
|
147
|
+
} catch {
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
async signMessage(_ctx, _session, params) {
|
|
151
|
+
return this.provider.request({
|
|
152
|
+
method: "signMessage",
|
|
153
|
+
params: { message: params.message }
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
async submitTransaction(_ctx, _session, params) {
|
|
157
|
+
return this.provider.request({
|
|
158
|
+
method: "prepareExecute",
|
|
159
|
+
params
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
};
|
|
14
163
|
|
|
15
164
|
// src/config.ts
|
|
16
165
|
var DEFAULT_REGISTRY_URL = "https://registry.partylayer.xyz";
|
|
@@ -365,6 +514,13 @@ var SESSION_STORAGE_KEY = "active_session";
|
|
|
365
514
|
var PartyLayerClient = class {
|
|
366
515
|
config;
|
|
367
516
|
adapters = /* @__PURE__ */ new Map();
|
|
517
|
+
/**
|
|
518
|
+
* Pre-resolved connect plans for popup/remote (GenericDiscoveryAdapter)
|
|
519
|
+
* wallets, warmed on `listWallets()` (which the modal calls on open) so a
|
|
520
|
+
* subsequent click can connect gesture-synchronously. Consumed (deleted) on
|
|
521
|
+
* use and cleared on disconnect; a cold miss falls back to the normal path.
|
|
522
|
+
*/
|
|
523
|
+
warmPlans = /* @__PURE__ */ new Map();
|
|
368
524
|
eventHandlers = /* @__PURE__ */ new Map();
|
|
369
525
|
activeSession = null;
|
|
370
526
|
registryClient;
|
|
@@ -373,6 +529,8 @@ var PartyLayerClient = class {
|
|
|
373
529
|
crypto;
|
|
374
530
|
storage;
|
|
375
531
|
telemetry;
|
|
532
|
+
/** Cached announce-discovery picker entries (one-shot; cleared by refreshDiscovery). */
|
|
533
|
+
announceEntriesCache = null;
|
|
376
534
|
origin;
|
|
377
535
|
constructor(config) {
|
|
378
536
|
this.config = config;
|
|
@@ -393,6 +551,8 @@ var PartyLayerClient = class {
|
|
|
393
551
|
let adapter;
|
|
394
552
|
if (typeof adapterOrClass === "function") {
|
|
395
553
|
adapter = new adapterOrClass();
|
|
554
|
+
} else if (core.isOfficialProviderAdapter(adapterOrClass)) {
|
|
555
|
+
adapter = new GenericDiscoveryAdapter({ official: adapterOrClass });
|
|
396
556
|
} else {
|
|
397
557
|
adapter = adapterOrClass;
|
|
398
558
|
}
|
|
@@ -477,6 +637,9 @@ var PartyLayerClient = class {
|
|
|
477
637
|
channel: "stable"
|
|
478
638
|
});
|
|
479
639
|
}
|
|
640
|
+
registryWallets = await this.gateDiscoveryAdapterEntries(registryWallets);
|
|
641
|
+
registryWallets = await this.aggregateAnnouncedWallets(registryWallets);
|
|
642
|
+
void this.warmDiscoveryPlans(registryWallets);
|
|
480
643
|
if (filter?.requiredCapabilities) {
|
|
481
644
|
return registryWallets.filter(
|
|
482
645
|
(walletInfo) => filter.requiredCapabilities.every(
|
|
@@ -489,6 +652,108 @@ var PartyLayerClient = class {
|
|
|
489
652
|
}
|
|
490
653
|
return registryWallets;
|
|
491
654
|
}
|
|
655
|
+
/**
|
|
656
|
+
* Hide `transport: 'discovery-adapter'` registry entries whose matching
|
|
657
|
+
* adapter is NOT registered. A discovery-adapter wallet's provider is supplied
|
|
658
|
+
* by the app (an official ProviderAdapter the SDK bridges under
|
|
659
|
+
* `toWalletId(providerId)`); without it, clicking the entry can only fail. So
|
|
660
|
+
* the entry surfaces only when its adapter is present. No-op when the registry
|
|
661
|
+
* is unavailable (list is already adapters-only) or has no such entries.
|
|
662
|
+
*/
|
|
663
|
+
async gateDiscoveryAdapterEntries(wallets) {
|
|
664
|
+
let hidden = null;
|
|
665
|
+
try {
|
|
666
|
+
const registry = await this.registryClient.getRegistry();
|
|
667
|
+
for (const entry of registry.wallets) {
|
|
668
|
+
if (entry.adapter?.transport !== "discovery-adapter") continue;
|
|
669
|
+
const walletId = core.toWalletId(entry.id);
|
|
670
|
+
if (!this.adapters.has(walletId)) {
|
|
671
|
+
(hidden ??= /* @__PURE__ */ new Set()).add(String(walletId));
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
} catch {
|
|
675
|
+
return wallets;
|
|
676
|
+
}
|
|
677
|
+
if (!hidden || hidden.size === 0) return wallets;
|
|
678
|
+
return wallets.filter((w) => !hidden.has(String(w.walletId)));
|
|
679
|
+
}
|
|
680
|
+
/**
|
|
681
|
+
* Whether announce discovery is active: explicit `discovery.announce`, else
|
|
682
|
+
* ON in the browser and OFF under SSR (no `window`). Canonical: provider.md.
|
|
683
|
+
*/
|
|
684
|
+
get announceEnabled() {
|
|
685
|
+
if (typeof window === "undefined") return false;
|
|
686
|
+
return this.config.discovery?.announce ?? true;
|
|
687
|
+
}
|
|
688
|
+
/**
|
|
689
|
+
* Clear the cached announce round-trip so the next `listWallets()` re-runs the
|
|
690
|
+
* `canton:requestProvider` handshake (e.g. after a wallet is installed).
|
|
691
|
+
*/
|
|
692
|
+
refreshDiscovery() {
|
|
693
|
+
this.announceEntriesCache = null;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Aggregate `canton:announceProvider` wallets into the picker list (A2).
|
|
697
|
+
*
|
|
698
|
+
* One-shot cached per client (refresh via {@link refreshDiscovery}); SSR-skipped.
|
|
699
|
+
* For each discovered provider (announce ∪ `window.canton` scan, deduped):
|
|
700
|
+
* - if its id matches a known wallet's `providerDetection` (provider.id) it
|
|
701
|
+
* IS that wallet — no new entry (identity bridge: Console's `lpnf…` → the
|
|
702
|
+
* `console` entry, Send's `ldmo…` → the `send` entry);
|
|
703
|
+
* - otherwise it is surfaced as a dynamic `browser:ext:<id>` entry routed to
|
|
704
|
+
* its own extension `target`, with a {@link GenericAnnounceAdapter}
|
|
705
|
+
* registered so a click connects through that target ONLY (no shared slot).
|
|
706
|
+
* With zero announcers this returns `base` unchanged.
|
|
707
|
+
*/
|
|
708
|
+
async aggregateAnnouncedWallets(base) {
|
|
709
|
+
if (!this.announceEnabled) return base;
|
|
710
|
+
if (this.announceEntriesCache === null) {
|
|
711
|
+
try {
|
|
712
|
+
const discovered = await provider.discoverProviders({
|
|
713
|
+
timeoutMs: this.config.discovery?.announceTimeoutMs,
|
|
714
|
+
// Working provider over the announce target channel; G4 (provider.md):
|
|
715
|
+
// target defaults to id when omitted — never a shared slot.
|
|
716
|
+
createProvider: (a) => provider.createExtensionChannelProvider({ target: a.target ?? a.id })
|
|
717
|
+
});
|
|
718
|
+
const entries = [];
|
|
719
|
+
for (const d of discovered) {
|
|
720
|
+
if (d.identityResolved === false) continue;
|
|
721
|
+
const known = core.findMatchingWalletInfo(
|
|
722
|
+
{ provider: { id: d.id } },
|
|
723
|
+
base
|
|
724
|
+
);
|
|
725
|
+
if (known) continue;
|
|
726
|
+
const adapter = new GenericAnnounceAdapter({
|
|
727
|
+
announceId: d.id,
|
|
728
|
+
name: d.name,
|
|
729
|
+
icon: d.icon,
|
|
730
|
+
provider: d.provider
|
|
731
|
+
});
|
|
732
|
+
this.adapters.set(adapter.walletId, adapter);
|
|
733
|
+
entries.push({
|
|
734
|
+
walletId: adapter.walletId,
|
|
735
|
+
name: adapter.name,
|
|
736
|
+
website: "",
|
|
737
|
+
icons: d.icon ? { sm: d.icon, md: d.icon, lg: d.icon } : {},
|
|
738
|
+
capabilities: adapter.getCapabilities(),
|
|
739
|
+
adapter: { packageName: "announced", versionRange: "*" },
|
|
740
|
+
docs: [],
|
|
741
|
+
networks: [this.config.network || "devnet"],
|
|
742
|
+
channel: "stable"
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
this.announceEntriesCache = entries;
|
|
746
|
+
} catch {
|
|
747
|
+
this.announceEntriesCache = [];
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if (this.announceEntriesCache.length === 0) return base;
|
|
751
|
+
const ids = new Set(base.map((w) => String(w.walletId)));
|
|
752
|
+
return [
|
|
753
|
+
...base,
|
|
754
|
+
...this.announceEntriesCache.filter((w) => !ids.has(String(w.walletId)))
|
|
755
|
+
];
|
|
756
|
+
}
|
|
492
757
|
/** Active network-enforcement policy (default 'guard'). */
|
|
493
758
|
get enforcement() {
|
|
494
759
|
return this.config.networkEnforcement ?? "guard";
|
|
@@ -518,128 +783,10 @@ var PartyLayerClient = class {
|
|
|
518
783
|
async connect(options) {
|
|
519
784
|
this.telemetry?.increment?.(core.METRICS.WALLET_CONNECT_ATTEMPTS);
|
|
520
785
|
try {
|
|
521
|
-
const
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
let availableWallets = wallets;
|
|
526
|
-
if (options?.allowWallets) {
|
|
527
|
-
availableWallets = wallets.filter(
|
|
528
|
-
(w) => options.allowWallets.includes(w.walletId)
|
|
529
|
-
);
|
|
530
|
-
}
|
|
531
|
-
let selectedWallet;
|
|
532
|
-
let isNativeWallet = false;
|
|
533
|
-
if (options?.walletId) {
|
|
534
|
-
const found = availableWallets.find(
|
|
535
|
-
(w) => w.walletId === options.walletId
|
|
536
|
-
);
|
|
537
|
-
if (found) {
|
|
538
|
-
selectedWallet = found;
|
|
539
|
-
} else {
|
|
540
|
-
const nativeAdapter = this.adapters.get(options.walletId);
|
|
541
|
-
if (nativeAdapter) {
|
|
542
|
-
isNativeWallet = true;
|
|
543
|
-
selectedWallet = {
|
|
544
|
-
walletId: options.walletId,
|
|
545
|
-
name: nativeAdapter.name,
|
|
546
|
-
website: "",
|
|
547
|
-
icons: {},
|
|
548
|
-
capabilities: nativeAdapter.getCapabilities(),
|
|
549
|
-
adapter: { packageName: "native-cip0103", versionRange: "*" },
|
|
550
|
-
docs: [],
|
|
551
|
-
networks: [this.config.network],
|
|
552
|
-
channel: "stable",
|
|
553
|
-
metadata: { source: "native-cip0103" }
|
|
554
|
-
};
|
|
555
|
-
} else {
|
|
556
|
-
throw new core.WalletNotFoundError(String(options.walletId));
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
} else if (availableWallets.length === 0) {
|
|
560
|
-
throw new core.WalletNotFoundError("No wallets available");
|
|
561
|
-
} else {
|
|
562
|
-
selectedWallet = availableWallets[0];
|
|
563
|
-
}
|
|
564
|
-
const adapter = this.adapters.get(selectedWallet.walletId);
|
|
565
|
-
if (!adapter) {
|
|
566
|
-
throw new core.WalletNotFoundError(String(selectedWallet.walletId));
|
|
567
|
-
}
|
|
568
|
-
if (!isNativeWallet) {
|
|
569
|
-
try {
|
|
570
|
-
const walletEntry = await this.registryClient.getWalletEntry(String(selectedWallet.walletId));
|
|
571
|
-
if (walletEntry.originAllowlist && walletEntry.originAllowlist.length > 0) {
|
|
572
|
-
if (!walletEntry.originAllowlist.includes(this.origin)) {
|
|
573
|
-
const { OriginNotAllowedError: OriginNotAllowedError2 } = await import('@partylayer/core');
|
|
574
|
-
throw new OriginNotAllowedError2(
|
|
575
|
-
this.origin,
|
|
576
|
-
walletEntry.originAllowlist
|
|
577
|
-
);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
} catch (e) {
|
|
581
|
-
if (!(e instanceof core.WalletNotFoundError)) {
|
|
582
|
-
throw e;
|
|
583
|
-
}
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
if (options?.requiredCapabilities) {
|
|
587
|
-
core.capabilityGuard(adapter, options.requiredCapabilities);
|
|
588
|
-
}
|
|
589
|
-
await core.installGuard(adapter);
|
|
590
|
-
const ctx = this.createAdapterContext();
|
|
591
|
-
const timeoutMs = options?.timeoutMs || 12e4;
|
|
592
|
-
const connectPromise = adapter.connect(ctx, {
|
|
593
|
-
timeoutMs,
|
|
594
|
-
partyId: void 0,
|
|
595
|
-
preferInstalled: options?.preferInstalled,
|
|
596
|
-
onDisplayUri: options?.onDisplayUri
|
|
597
|
-
});
|
|
598
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
599
|
-
setTimeout(() => {
|
|
600
|
-
reject(new Error(`Connection timed out after ${timeoutMs}ms - user did not complete wallet connection`));
|
|
601
|
-
}, timeoutMs);
|
|
602
|
-
});
|
|
603
|
-
const result = await Promise.race([connectPromise, timeoutPromise]);
|
|
604
|
-
const session = {
|
|
605
|
-
sessionId: core.toSessionId(`session_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`),
|
|
606
|
-
walletId: selectedWallet.walletId,
|
|
607
|
-
partyId: result.partyId,
|
|
608
|
-
// The wallet's reported network (adapters that read the live wallet —
|
|
609
|
-
// e.g. Console via getActiveNetwork — surface the actual network here;
|
|
610
|
-
// echo-only adapters report ctx.network === config.network). Used for
|
|
611
|
-
// mismatch detection; falls back to the configured network.
|
|
612
|
-
network: result.session.network ?? this.config.network,
|
|
613
|
-
createdAt: Date.now(),
|
|
614
|
-
expiresAt: result.session.expiresAt,
|
|
615
|
-
origin: this.origin,
|
|
616
|
-
capabilitiesSnapshot: result.capabilities,
|
|
617
|
-
metadata: result.session.metadata
|
|
618
|
-
};
|
|
619
|
-
const mismatch = this.networkMismatch(session);
|
|
620
|
-
if (mismatch) {
|
|
621
|
-
session.networkMismatch = mismatch;
|
|
622
|
-
this.emit("session:networkMismatch", {
|
|
623
|
-
type: "session:networkMismatch",
|
|
624
|
-
sessionId: session.sessionId,
|
|
625
|
-
expected: mismatch.expected,
|
|
626
|
-
actual: mismatch.actual,
|
|
627
|
-
enforced: this.enforcement !== "off"
|
|
628
|
-
});
|
|
629
|
-
if (this.enforcement === "strict") {
|
|
630
|
-
throw new core.NetworkMismatchError(mismatch.expected, mismatch.actual);
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
await this.persistSession(session);
|
|
634
|
-
this.activeSession = session;
|
|
635
|
-
this.updateRegistryStatus();
|
|
636
|
-
this.telemetry?.increment?.(core.METRICS.WALLET_CONNECT_SUCCESS);
|
|
637
|
-
this.telemetry?.increment?.(core.METRICS.SESSIONS_CREATED);
|
|
638
|
-
this.emit("session:connected", {
|
|
639
|
-
type: "session:connected",
|
|
640
|
-
session
|
|
641
|
-
});
|
|
642
|
-
return session;
|
|
786
|
+
const fast = this.tryFastConnect(options);
|
|
787
|
+
if (fast) return await fast;
|
|
788
|
+
const plan = await this.resolveConnectPlan(options);
|
|
789
|
+
return await this.completeConnect(plan, options);
|
|
643
790
|
} catch (err) {
|
|
644
791
|
const timeoutMs = options?.timeoutMs || 3e4;
|
|
645
792
|
const error = core.mapUnknownErrorToPartyLayerError(err, {
|
|
@@ -651,6 +798,191 @@ var PartyLayerClient = class {
|
|
|
651
798
|
throw error;
|
|
652
799
|
}
|
|
653
800
|
}
|
|
801
|
+
/**
|
|
802
|
+
* Pre-resolve everything `adapter.connect()` needs WITHOUT calling it: wallet
|
|
803
|
+
* selection, origin-allowlist + capability + install guards, and the adapter
|
|
804
|
+
* context. `prefetchedWallets` (passed during warm-up from `listWallets`)
|
|
805
|
+
* avoids a recursive `listWallets()` call.
|
|
806
|
+
*/
|
|
807
|
+
async resolveConnectPlan(options, prefetchedWallets) {
|
|
808
|
+
const wallets = prefetchedWallets ?? await this.listWallets({
|
|
809
|
+
requiredCapabilities: options?.requiredCapabilities,
|
|
810
|
+
includeExperimental: true
|
|
811
|
+
});
|
|
812
|
+
let availableWallets = wallets;
|
|
813
|
+
if (options?.allowWallets) {
|
|
814
|
+
availableWallets = wallets.filter(
|
|
815
|
+
(w) => options.allowWallets.includes(w.walletId)
|
|
816
|
+
);
|
|
817
|
+
}
|
|
818
|
+
let selectedWallet;
|
|
819
|
+
let isNativeWallet = false;
|
|
820
|
+
if (options?.walletId) {
|
|
821
|
+
const found = availableWallets.find(
|
|
822
|
+
(w) => w.walletId === options.walletId
|
|
823
|
+
);
|
|
824
|
+
if (found) {
|
|
825
|
+
selectedWallet = found;
|
|
826
|
+
} else {
|
|
827
|
+
const nativeAdapter = this.adapters.get(options.walletId);
|
|
828
|
+
if (nativeAdapter) {
|
|
829
|
+
isNativeWallet = true;
|
|
830
|
+
selectedWallet = {
|
|
831
|
+
walletId: options.walletId,
|
|
832
|
+
name: nativeAdapter.name,
|
|
833
|
+
website: "",
|
|
834
|
+
icons: {},
|
|
835
|
+
capabilities: nativeAdapter.getCapabilities(),
|
|
836
|
+
adapter: { packageName: "native-cip0103", versionRange: "*" },
|
|
837
|
+
docs: [],
|
|
838
|
+
networks: [this.config.network],
|
|
839
|
+
channel: "stable",
|
|
840
|
+
metadata: { source: "native-cip0103" }
|
|
841
|
+
};
|
|
842
|
+
} else {
|
|
843
|
+
throw new core.WalletNotFoundError(String(options.walletId));
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
} else if (availableWallets.length === 0) {
|
|
847
|
+
throw new core.WalletNotFoundError("No wallets available");
|
|
848
|
+
} else {
|
|
849
|
+
selectedWallet = availableWallets[0];
|
|
850
|
+
}
|
|
851
|
+
const adapter = this.adapters.get(selectedWallet.walletId);
|
|
852
|
+
if (!adapter) {
|
|
853
|
+
throw new core.WalletNotFoundError(String(selectedWallet.walletId));
|
|
854
|
+
}
|
|
855
|
+
if (!isNativeWallet) {
|
|
856
|
+
try {
|
|
857
|
+
const walletEntry = await this.registryClient.getWalletEntry(String(selectedWallet.walletId));
|
|
858
|
+
if (walletEntry.originAllowlist && walletEntry.originAllowlist.length > 0) {
|
|
859
|
+
if (!walletEntry.originAllowlist.includes(this.origin)) {
|
|
860
|
+
const { OriginNotAllowedError: OriginNotAllowedError2 } = await import('@partylayer/core');
|
|
861
|
+
throw new OriginNotAllowedError2(
|
|
862
|
+
this.origin,
|
|
863
|
+
walletEntry.originAllowlist
|
|
864
|
+
);
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
} catch (e) {
|
|
868
|
+
if (!(e instanceof core.WalletNotFoundError)) {
|
|
869
|
+
throw e;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
if (options?.requiredCapabilities) {
|
|
874
|
+
core.capabilityGuard(adapter, options.requiredCapabilities);
|
|
875
|
+
}
|
|
876
|
+
await core.installGuard(adapter);
|
|
877
|
+
const ctx = this.createAdapterContext();
|
|
878
|
+
return { selectedWallet, adapter, ctx, isNativeWallet };
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Invoke `adapter.connect()` (its FIRST statement — no await precedes it) and
|
|
882
|
+
* build / persist / emit the session. Separated from plan resolution so the
|
|
883
|
+
* popup-safe fast-path can call it gesture-synchronously.
|
|
884
|
+
*/
|
|
885
|
+
async completeConnect(plan, options) {
|
|
886
|
+
const { selectedWallet, adapter, ctx } = plan;
|
|
887
|
+
const timeoutMs = options?.timeoutMs || 12e4;
|
|
888
|
+
const connectPromise = adapter.connect(ctx, {
|
|
889
|
+
timeoutMs,
|
|
890
|
+
partyId: void 0,
|
|
891
|
+
preferInstalled: options?.preferInstalled,
|
|
892
|
+
onDisplayUri: options?.onDisplayUri
|
|
893
|
+
});
|
|
894
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
895
|
+
setTimeout(() => {
|
|
896
|
+
reject(new Error(`Connection timed out after ${timeoutMs}ms - user did not complete wallet connection`));
|
|
897
|
+
}, timeoutMs);
|
|
898
|
+
});
|
|
899
|
+
const result = await Promise.race([connectPromise, timeoutPromise]);
|
|
900
|
+
const session = {
|
|
901
|
+
sessionId: core.toSessionId(`session_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`),
|
|
902
|
+
walletId: selectedWallet.walletId,
|
|
903
|
+
partyId: result.partyId,
|
|
904
|
+
// The wallet's reported network (adapters that read the live wallet —
|
|
905
|
+
// e.g. Console via getActiveNetwork — surface the actual network here;
|
|
906
|
+
// echo-only adapters report ctx.network === config.network). Used for
|
|
907
|
+
// mismatch detection; falls back to the configured network.
|
|
908
|
+
network: result.session.network ?? this.config.network,
|
|
909
|
+
createdAt: Date.now(),
|
|
910
|
+
expiresAt: result.session.expiresAt,
|
|
911
|
+
origin: this.origin,
|
|
912
|
+
capabilitiesSnapshot: result.capabilities,
|
|
913
|
+
metadata: result.session.metadata
|
|
914
|
+
};
|
|
915
|
+
const mismatch = this.networkMismatch(session);
|
|
916
|
+
if (mismatch) {
|
|
917
|
+
session.networkMismatch = mismatch;
|
|
918
|
+
this.emit("session:networkMismatch", {
|
|
919
|
+
type: "session:networkMismatch",
|
|
920
|
+
sessionId: session.sessionId,
|
|
921
|
+
expected: mismatch.expected,
|
|
922
|
+
actual: mismatch.actual,
|
|
923
|
+
enforced: this.enforcement !== "off"
|
|
924
|
+
});
|
|
925
|
+
if (this.enforcement === "strict") {
|
|
926
|
+
throw new core.NetworkMismatchError(mismatch.expected, mismatch.actual);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
await this.persistSession(session);
|
|
930
|
+
this.activeSession = session;
|
|
931
|
+
this.updateRegistryStatus();
|
|
932
|
+
this.telemetry?.increment?.(core.METRICS.WALLET_CONNECT_SUCCESS);
|
|
933
|
+
this.telemetry?.increment?.(core.METRICS.SESSIONS_CREATED);
|
|
934
|
+
this.emit("session:connected", {
|
|
935
|
+
type: "session:connected",
|
|
936
|
+
session
|
|
937
|
+
});
|
|
938
|
+
return session;
|
|
939
|
+
}
|
|
940
|
+
/**
|
|
941
|
+
* PUBLIC popup-safe primitive: pre-resolve a connect plan ahead of the user
|
|
942
|
+
* gesture; the returned `connect()` invokes `adapter.connect()` as its first
|
|
943
|
+
* statement (no await precedes it) so a popup/remote wallet's `window.open`
|
|
944
|
+
* survives the gesture. For consumers driving such a wallet outside the modal
|
|
945
|
+
* (the modal gets this for free via the `listWallets` warm-up + `connect()`).
|
|
946
|
+
*/
|
|
947
|
+
async prepareConnect(options) {
|
|
948
|
+
const plan = await this.resolveConnectPlan(options);
|
|
949
|
+
return {
|
|
950
|
+
walletId: plan.selectedWallet.walletId,
|
|
951
|
+
connect: (o) => this.completeConnect(plan, o ?? options)
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Background warm-up of connect plans for popup/remote
|
|
956
|
+
* (`GenericDiscoveryAdapter`) wallets. Popup-free — `resolveConnectPlan` only
|
|
957
|
+
* runs read/guard probes. Skips already-warm entries; never throws.
|
|
958
|
+
*/
|
|
959
|
+
async warmDiscoveryPlans(wallets) {
|
|
960
|
+
for (const [walletId, adapter] of this.adapters) {
|
|
961
|
+
if (!(adapter instanceof GenericDiscoveryAdapter)) continue;
|
|
962
|
+
if (this.warmPlans.has(walletId)) continue;
|
|
963
|
+
try {
|
|
964
|
+
const plan = await this.resolveConnectPlan({ walletId }, wallets);
|
|
965
|
+
this.warmPlans.set(walletId, plan);
|
|
966
|
+
} catch {
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
/**
|
|
971
|
+
* SYNCHRONOUS fast-path: if a fresh warm plan exists for a popup/remote wallet
|
|
972
|
+
* — and `options` carries no plan-affecting guards — consume it and START
|
|
973
|
+
* `completeConnect` immediately, reaching `adapter.connect()` with no awaits.
|
|
974
|
+
* Returns the in-flight Session promise, or null to fall back to the normal
|
|
975
|
+
* path (e.g. cold cache, or a non-discovery wallet).
|
|
976
|
+
*/
|
|
977
|
+
tryFastConnect(options) {
|
|
978
|
+
const walletId = options?.walletId;
|
|
979
|
+
if (!walletId) return null;
|
|
980
|
+
if (options?.requiredCapabilities || options?.allowWallets) return null;
|
|
981
|
+
const plan = this.warmPlans.get(walletId);
|
|
982
|
+
if (!plan) return null;
|
|
983
|
+
this.warmPlans.delete(walletId);
|
|
984
|
+
return this.completeConnect(plan, options);
|
|
985
|
+
}
|
|
654
986
|
/**
|
|
655
987
|
* Disconnect from wallet
|
|
656
988
|
*/
|
|
@@ -667,6 +999,7 @@ var PartyLayerClient = class {
|
|
|
667
999
|
const sessionId = this.activeSession.sessionId;
|
|
668
1000
|
await this.removeSession(sessionId);
|
|
669
1001
|
this.activeSession = null;
|
|
1002
|
+
this.warmPlans.clear();
|
|
670
1003
|
this.emit("session:disconnected", {
|
|
671
1004
|
type: "session:disconnected",
|
|
672
1005
|
sessionId
|
|
@@ -1116,6 +1449,10 @@ Object.defineProperty(exports, "errorMetricName", {
|
|
|
1116
1449
|
enumerable: true,
|
|
1117
1450
|
get: function () { return core.errorMetricName; }
|
|
1118
1451
|
});
|
|
1452
|
+
Object.defineProperty(exports, "isOfficialProviderAdapter", {
|
|
1453
|
+
enumerable: true,
|
|
1454
|
+
get: function () { return core.isOfficialProviderAdapter; }
|
|
1455
|
+
});
|
|
1119
1456
|
Object.defineProperty(exports, "deriveGenericWalletName", {
|
|
1120
1457
|
enumerable: true,
|
|
1121
1458
|
get: function () { return registryClient.deriveGenericWalletName; }
|
|
@@ -1224,10 +1561,14 @@ Object.defineProperty(exports, "BronAdapter", {
|
|
|
1224
1561
|
enumerable: true,
|
|
1225
1562
|
get: function () { return adapterBron.BronAdapter; }
|
|
1226
1563
|
});
|
|
1564
|
+
exports.ANNOUNCED_WALLET_ID_PREFIX = ANNOUNCED_WALLET_ID_PREFIX;
|
|
1227
1565
|
exports.CantonConnectClient = PartyLayerClient;
|
|
1228
1566
|
exports.DEFAULT_REGISTRY_URL = DEFAULT_REGISTRY_URL;
|
|
1567
|
+
exports.GenericAnnounceAdapter = GenericAnnounceAdapter;
|
|
1568
|
+
exports.GenericDiscoveryAdapter = GenericDiscoveryAdapter;
|
|
1229
1569
|
exports.MetricsTelemetryAdapter = MetricsTelemetryAdapter;
|
|
1230
1570
|
exports.PartyLayerClient = PartyLayerClient;
|
|
1571
|
+
exports.announcedWalletId = announcedWalletId;
|
|
1231
1572
|
exports.createCantonConnect = createPartyLayer;
|
|
1232
1573
|
exports.createPartyLayer = createPartyLayer;
|
|
1233
1574
|
exports.createTelemetryAdapter = createTelemetryAdapter;
|