@partylayer/sdk 0.7.0 → 0.9.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 +173 -4
- package/dist/index.d.ts +173 -4
- package/dist/index.js +418 -121
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +412 -124
- package/dist/index.mjs.map +1 -1
- package/package.json +10 -10
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { toWalletId, toPartyId, hashForPrivacy, createMetricsPayload, mapUnknownErrorToPartyLayerError, findMatchingWalletInfo, detectNetworkMismatch, NetworkMismatchError, METRICS, WalletNotFoundError, capabilityGuard, installGuard, toSessionId, CapabilityNotSupportedError, errorMetricName } from '@partylayer/core';
|
|
2
|
-
export { PartyLayerError as CantonConnectError, CapabilityNotSupportedError, ENABLEMENT_METRICS, ERROR_METRICS, InternalError, METRICS, OriginNotAllowedError, PartyLayerError, REGISTRY_METRICS, RegistryFetchFailedError, RegistrySchemaInvalidError, RegistryVerificationFailedError, SessionExpiredError, TimeoutError, TransportError, UserRejectedError, WalletNotFoundError, WalletNotInstalledError, errorMetricName } from '@partylayer/core';
|
|
1
|
+
import { toWalletId, toPartyId, hashForPrivacy, createMetricsPayload, isOfficialAdapterFactory, isOfficialProviderAdapter, mapUnknownErrorToPartyLayerError, findMatchingWalletInfo, detectNetworkMismatch, NetworkMismatchError, METRICS, WalletNotFoundError, capabilityGuard, installGuard, toSessionId, CapabilityNotSupportedError, errorMetricName } from '@partylayer/core';
|
|
2
|
+
export { PartyLayerError as CantonConnectError, CapabilityNotSupportedError, ENABLEMENT_METRICS, ERROR_METRICS, InternalError, METRICS, OriginNotAllowedError, PartyLayerError, REGISTRY_METRICS, RegistryFetchFailedError, RegistrySchemaInvalidError, RegistryVerificationFailedError, SessionExpiredError, TimeoutError, TransportError, UserRejectedError, WalletNotFoundError, WalletNotInstalledError, errorMetricName, isOfficialAdapterFactory, isOfficialProviderAdapter } from '@partylayer/core';
|
|
3
3
|
import { RegistryClient } from '@partylayer/registry-client';
|
|
4
4
|
export { deriveGenericWalletName, findMatchingWallet, findMatchingWalletInfo, isCip0103Native, matchesProviderDetection } from '@partylayer/registry-client';
|
|
5
5
|
import { discoverProviders, createExtensionChannelProvider, createProviderBridge } from '@partylayer/provider';
|
|
@@ -84,6 +84,153 @@ var GenericAnnounceAdapter = class {
|
|
|
84
84
|
});
|
|
85
85
|
}
|
|
86
86
|
};
|
|
87
|
+
var GenericDiscoveryAdapter = class {
|
|
88
|
+
walletId;
|
|
89
|
+
name;
|
|
90
|
+
icon;
|
|
91
|
+
/**
|
|
92
|
+
* Resolved official adapter. Set at construction for the instance form; for
|
|
93
|
+
* the factory form it stays null until {@link resolveOfficial} constructs it
|
|
94
|
+
* with the network-resolved host.
|
|
95
|
+
*/
|
|
96
|
+
official;
|
|
97
|
+
/** Factory form (null for the instance form). */
|
|
98
|
+
factory;
|
|
99
|
+
/** Network→host map for the factory form; injected by the SDK post-registry. */
|
|
100
|
+
networkHosts;
|
|
101
|
+
/** Lazily resolved from `official.provider()` — NOT at construction (SSR-safe). */
|
|
102
|
+
providerInstance = null;
|
|
103
|
+
constructor(args) {
|
|
104
|
+
if (args.official) {
|
|
105
|
+
this.official = args.official;
|
|
106
|
+
this.factory = null;
|
|
107
|
+
this.networkHosts = {};
|
|
108
|
+
this.walletId = args.walletId ?? toWalletId(args.official.providerId);
|
|
109
|
+
this.name = args.name && args.name.length > 0 ? args.name : args.official.name;
|
|
110
|
+
this.icon = args.icon ?? args.official.icon;
|
|
111
|
+
} else if (args.factory) {
|
|
112
|
+
this.official = null;
|
|
113
|
+
this.factory = args.factory;
|
|
114
|
+
this.networkHosts = args.networkHosts ?? {};
|
|
115
|
+
this.walletId = args.walletId ?? toWalletId(args.factory.providerId);
|
|
116
|
+
this.name = args.name && args.name.length > 0 ? args.name : args.factory.name ?? args.factory.providerId;
|
|
117
|
+
this.icon = args.icon ?? args.factory.icon;
|
|
118
|
+
} else {
|
|
119
|
+
throw new Error(
|
|
120
|
+
"GenericDiscoveryAdapter requires either `official` (instance) or `factory`."
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** Whether this bridge resolves its host from `networkHosts` (factory form). */
|
|
125
|
+
usesFactory() {
|
|
126
|
+
return this.factory !== null;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Inject the registry's network→host map (factory form only). SYNCHRONOUS —
|
|
130
|
+
* the SDK calls this during the async warm phase once the registry entry is
|
|
131
|
+
* loaded, BEFORE the (possibly gesture-synchronous) connect.
|
|
132
|
+
*/
|
|
133
|
+
setNetworkHosts(networkHosts) {
|
|
134
|
+
if (this.factory) this.networkHosts = networkHosts ?? {};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Resolve + construct the official adapter for `network` — SYNCHRONOUS, so the
|
|
138
|
+
* popup-safe gesture path reaches `window.open` with NO awaits. Idempotent.
|
|
139
|
+
* The SDK pre-calls it during warm-up; `connect()` also calls it as its first
|
|
140
|
+
* statement so a cold (un-warmed) connect is still gesture-safe.
|
|
141
|
+
*
|
|
142
|
+
* Failure is CLEAR, never a silent wrong-network host: a factory wallet with
|
|
143
|
+
* no host for `network` in `networkHosts` throws, naming the network.
|
|
144
|
+
*/
|
|
145
|
+
resolveOfficial(network) {
|
|
146
|
+
if (this.official) return;
|
|
147
|
+
if (!this.factory) return;
|
|
148
|
+
const host = this.networkHosts[network];
|
|
149
|
+
if (!host) {
|
|
150
|
+
throw new Error(
|
|
151
|
+
`Wallet "${String(this.walletId)}" has no host for network "${network}". Add it to the registry entry's adapter.networkHosts, or supply a pre-constructed official adapter with an explicit host.`
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
this.official = this.factory.create(host);
|
|
155
|
+
}
|
|
156
|
+
/** Lazily obtain (and cache) the official provider — deferred off the init/SSR path.
|
|
157
|
+
* `official.provider()` is typed `OfficialProvider` (loose `request` so real
|
|
158
|
+
* official adapters are assignable); it is call-compatible with CIP0103Provider
|
|
159
|
+
* (the bridge only ever calls `request({ method, params })`). */
|
|
160
|
+
get provider() {
|
|
161
|
+
if (this.providerInstance === null) {
|
|
162
|
+
if (!this.official) {
|
|
163
|
+
throw new Error(
|
|
164
|
+
`Wallet "${String(this.walletId)}" provider requested before host resolution \u2014 call resolveOfficial(network) first.`
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
this.providerInstance = this.official.provider();
|
|
168
|
+
}
|
|
169
|
+
return this.providerInstance;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Baseline CIP-0103 capabilities. NEVER includes `'events'`: popup/remote
|
|
173
|
+
* wallets expose the event surface but do not emit (truthfulness doctrine).
|
|
174
|
+
*/
|
|
175
|
+
getCapabilities() {
|
|
176
|
+
return ["connect", "disconnect", "signMessage", "submitTransaction"];
|
|
177
|
+
}
|
|
178
|
+
/** Install/availability probe — delegated to the official adapter (popup-free). */
|
|
179
|
+
async detectInstalled() {
|
|
180
|
+
if (!this.official) {
|
|
181
|
+
return { installed: true, reason: "Popup/remote wallet (resolved at connect)" };
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
const installed = await this.official.detect();
|
|
185
|
+
return {
|
|
186
|
+
installed,
|
|
187
|
+
reason: installed ? void 0 : "Wallet reported not available"
|
|
188
|
+
};
|
|
189
|
+
} catch {
|
|
190
|
+
return { installed: false, reason: "Detection failed" };
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
async connect(ctx) {
|
|
194
|
+
this.resolveOfficial(ctx.network);
|
|
195
|
+
await this.provider.request({ method: "connect" });
|
|
196
|
+
const account = await this.provider.request({
|
|
197
|
+
method: "getPrimaryAccount"
|
|
198
|
+
});
|
|
199
|
+
let reportedNetwork;
|
|
200
|
+
try {
|
|
201
|
+
const status = await this.provider.request({
|
|
202
|
+
method: "status"
|
|
203
|
+
});
|
|
204
|
+
reportedNetwork = status?.network?.networkId;
|
|
205
|
+
} catch {
|
|
206
|
+
}
|
|
207
|
+
const partyId = toPartyId(account.partyId);
|
|
208
|
+
const session = {
|
|
209
|
+
walletId: this.walletId,
|
|
210
|
+
partyId,
|
|
211
|
+
network: reportedNetwork ?? account.networkId ?? ctx.network
|
|
212
|
+
};
|
|
213
|
+
return { partyId, session, capabilities: this.getCapabilities() };
|
|
214
|
+
}
|
|
215
|
+
async disconnect() {
|
|
216
|
+
try {
|
|
217
|
+
await this.provider.request({ method: "disconnect" });
|
|
218
|
+
} catch {
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
async signMessage(_ctx, _session, params) {
|
|
222
|
+
return this.provider.request({
|
|
223
|
+
method: "signMessage",
|
|
224
|
+
params: { message: params.message }
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
async submitTransaction(_ctx, _session, params) {
|
|
228
|
+
return this.provider.request({
|
|
229
|
+
method: "prepareExecute",
|
|
230
|
+
params
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
};
|
|
87
234
|
|
|
88
235
|
// src/config.ts
|
|
89
236
|
var DEFAULT_REGISTRY_URL = "https://registry.partylayer.xyz";
|
|
@@ -438,6 +585,13 @@ var SESSION_STORAGE_KEY = "active_session";
|
|
|
438
585
|
var PartyLayerClient = class {
|
|
439
586
|
config;
|
|
440
587
|
adapters = /* @__PURE__ */ new Map();
|
|
588
|
+
/**
|
|
589
|
+
* Pre-resolved connect plans for popup/remote (GenericDiscoveryAdapter)
|
|
590
|
+
* wallets, warmed on `listWallets()` (which the modal calls on open) so a
|
|
591
|
+
* subsequent click can connect gesture-synchronously. Consumed (deleted) on
|
|
592
|
+
* use and cleared on disconnect; a cold miss falls back to the normal path.
|
|
593
|
+
*/
|
|
594
|
+
warmPlans = /* @__PURE__ */ new Map();
|
|
441
595
|
eventHandlers = /* @__PURE__ */ new Map();
|
|
442
596
|
activeSession = null;
|
|
443
597
|
registryClient;
|
|
@@ -468,6 +622,10 @@ var PartyLayerClient = class {
|
|
|
468
622
|
let adapter;
|
|
469
623
|
if (typeof adapterOrClass === "function") {
|
|
470
624
|
adapter = new adapterOrClass();
|
|
625
|
+
} else if (isOfficialAdapterFactory(adapterOrClass)) {
|
|
626
|
+
adapter = new GenericDiscoveryAdapter({ factory: adapterOrClass });
|
|
627
|
+
} else if (isOfficialProviderAdapter(adapterOrClass)) {
|
|
628
|
+
adapter = new GenericDiscoveryAdapter({ official: adapterOrClass });
|
|
471
629
|
} else {
|
|
472
630
|
adapter = adapterOrClass;
|
|
473
631
|
}
|
|
@@ -552,7 +710,9 @@ var PartyLayerClient = class {
|
|
|
552
710
|
channel: "stable"
|
|
553
711
|
});
|
|
554
712
|
}
|
|
713
|
+
registryWallets = await this.gateDiscoveryAdapterEntries(registryWallets);
|
|
555
714
|
registryWallets = await this.aggregateAnnouncedWallets(registryWallets);
|
|
715
|
+
void this.warmDiscoveryPlans(registryWallets);
|
|
556
716
|
if (filter?.requiredCapabilities) {
|
|
557
717
|
return registryWallets.filter(
|
|
558
718
|
(walletInfo) => filter.requiredCapabilities.every(
|
|
@@ -565,6 +725,31 @@ var PartyLayerClient = class {
|
|
|
565
725
|
}
|
|
566
726
|
return registryWallets;
|
|
567
727
|
}
|
|
728
|
+
/**
|
|
729
|
+
* Hide `transport: 'discovery-adapter'` registry entries whose matching
|
|
730
|
+
* adapter is NOT registered. A discovery-adapter wallet's provider is supplied
|
|
731
|
+
* by the app (an official ProviderAdapter the SDK bridges under
|
|
732
|
+
* `toWalletId(providerId)`); without it, clicking the entry can only fail. So
|
|
733
|
+
* the entry surfaces only when its adapter is present. No-op when the registry
|
|
734
|
+
* is unavailable (list is already adapters-only) or has no such entries.
|
|
735
|
+
*/
|
|
736
|
+
async gateDiscoveryAdapterEntries(wallets) {
|
|
737
|
+
let hidden = null;
|
|
738
|
+
try {
|
|
739
|
+
const registry = await this.registryClient.getRegistry();
|
|
740
|
+
for (const entry of registry.wallets) {
|
|
741
|
+
if (entry.adapter?.transport !== "discovery-adapter") continue;
|
|
742
|
+
const walletId = toWalletId(entry.id);
|
|
743
|
+
if (!this.adapters.has(walletId)) {
|
|
744
|
+
(hidden ??= /* @__PURE__ */ new Set()).add(String(walletId));
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
} catch {
|
|
748
|
+
return wallets;
|
|
749
|
+
}
|
|
750
|
+
if (!hidden || hidden.size === 0) return wallets;
|
|
751
|
+
return wallets.filter((w) => !hidden.has(String(w.walletId)));
|
|
752
|
+
}
|
|
568
753
|
/**
|
|
569
754
|
* Whether announce discovery is active: explicit `discovery.announce`, else
|
|
570
755
|
* ON in the browser and OFF under SSR (no `window`). Canonical: provider.md.
|
|
@@ -671,139 +856,218 @@ var PartyLayerClient = class {
|
|
|
671
856
|
async connect(options) {
|
|
672
857
|
this.telemetry?.increment?.(METRICS.WALLET_CONNECT_ATTEMPTS);
|
|
673
858
|
try {
|
|
674
|
-
const
|
|
675
|
-
|
|
676
|
-
|
|
859
|
+
const fast = this.tryFastConnect(options);
|
|
860
|
+
if (fast) return await fast;
|
|
861
|
+
const plan = await this.resolveConnectPlan(options);
|
|
862
|
+
return await this.completeConnect(plan, options);
|
|
863
|
+
} catch (err) {
|
|
864
|
+
const timeoutMs = options?.timeoutMs || 3e4;
|
|
865
|
+
const error = mapUnknownErrorToPartyLayerError(err, {
|
|
866
|
+
phase: "connect",
|
|
867
|
+
walletId: options?.walletId ? String(options.walletId) : void 0,
|
|
868
|
+
timeoutMs
|
|
677
869
|
});
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
870
|
+
this.emit("error", { type: "error", error });
|
|
871
|
+
throw error;
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Pre-resolve everything `adapter.connect()` needs WITHOUT calling it: wallet
|
|
876
|
+
* selection, origin-allowlist + capability + install guards, and the adapter
|
|
877
|
+
* context. `prefetchedWallets` (passed during warm-up from `listWallets`)
|
|
878
|
+
* avoids a recursive `listWallets()` call.
|
|
879
|
+
*/
|
|
880
|
+
async resolveConnectPlan(options, prefetchedWallets) {
|
|
881
|
+
const wallets = prefetchedWallets ?? await this.listWallets({
|
|
882
|
+
requiredCapabilities: options?.requiredCapabilities,
|
|
883
|
+
includeExperimental: true
|
|
884
|
+
});
|
|
885
|
+
let availableWallets = wallets;
|
|
886
|
+
if (options?.allowWallets) {
|
|
887
|
+
availableWallets = wallets.filter(
|
|
888
|
+
(w) => options.allowWallets.includes(w.walletId)
|
|
889
|
+
);
|
|
890
|
+
}
|
|
891
|
+
let selectedWallet;
|
|
892
|
+
let isNativeWallet = false;
|
|
893
|
+
if (options?.walletId) {
|
|
894
|
+
const found = availableWallets.find(
|
|
895
|
+
(w) => w.walletId === options.walletId
|
|
896
|
+
);
|
|
897
|
+
if (found) {
|
|
898
|
+
selectedWallet = found;
|
|
899
|
+
} else {
|
|
900
|
+
const nativeAdapter = this.adapters.get(options.walletId);
|
|
901
|
+
if (nativeAdapter) {
|
|
902
|
+
isNativeWallet = true;
|
|
903
|
+
selectedWallet = {
|
|
904
|
+
walletId: options.walletId,
|
|
905
|
+
name: nativeAdapter.name,
|
|
906
|
+
website: "",
|
|
907
|
+
icons: {},
|
|
908
|
+
capabilities: nativeAdapter.getCapabilities(),
|
|
909
|
+
adapter: { packageName: "native-cip0103", versionRange: "*" },
|
|
910
|
+
docs: [],
|
|
911
|
+
networks: [this.config.network],
|
|
912
|
+
channel: "stable",
|
|
913
|
+
metadata: { source: "native-cip0103" }
|
|
914
|
+
};
|
|
692
915
|
} else {
|
|
693
|
-
|
|
694
|
-
if (nativeAdapter) {
|
|
695
|
-
isNativeWallet = true;
|
|
696
|
-
selectedWallet = {
|
|
697
|
-
walletId: options.walletId,
|
|
698
|
-
name: nativeAdapter.name,
|
|
699
|
-
website: "",
|
|
700
|
-
icons: {},
|
|
701
|
-
capabilities: nativeAdapter.getCapabilities(),
|
|
702
|
-
adapter: { packageName: "native-cip0103", versionRange: "*" },
|
|
703
|
-
docs: [],
|
|
704
|
-
networks: [this.config.network],
|
|
705
|
-
channel: "stable",
|
|
706
|
-
metadata: { source: "native-cip0103" }
|
|
707
|
-
};
|
|
708
|
-
} else {
|
|
709
|
-
throw new WalletNotFoundError(String(options.walletId));
|
|
710
|
-
}
|
|
916
|
+
throw new WalletNotFoundError(String(options.walletId));
|
|
711
917
|
}
|
|
712
|
-
} else if (availableWallets.length === 0) {
|
|
713
|
-
throw new WalletNotFoundError("No wallets available");
|
|
714
|
-
} else {
|
|
715
|
-
selectedWallet = availableWallets[0];
|
|
716
|
-
}
|
|
717
|
-
const adapter = this.adapters.get(selectedWallet.walletId);
|
|
718
|
-
if (!adapter) {
|
|
719
|
-
throw new WalletNotFoundError(String(selectedWallet.walletId));
|
|
720
918
|
}
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
if (!(
|
|
735
|
-
|
|
919
|
+
} else if (availableWallets.length === 0) {
|
|
920
|
+
throw new WalletNotFoundError("No wallets available");
|
|
921
|
+
} else {
|
|
922
|
+
selectedWallet = availableWallets[0];
|
|
923
|
+
}
|
|
924
|
+
const adapter = this.adapters.get(selectedWallet.walletId);
|
|
925
|
+
if (!adapter) {
|
|
926
|
+
throw new WalletNotFoundError(String(selectedWallet.walletId));
|
|
927
|
+
}
|
|
928
|
+
if (!isNativeWallet) {
|
|
929
|
+
try {
|
|
930
|
+
const walletEntry = await this.registryClient.getWalletEntry(String(selectedWallet.walletId));
|
|
931
|
+
if (walletEntry.originAllowlist && walletEntry.originAllowlist.length > 0) {
|
|
932
|
+
if (!walletEntry.originAllowlist.includes(this.origin)) {
|
|
933
|
+
const { OriginNotAllowedError: OriginNotAllowedError2 } = await import('@partylayer/core');
|
|
934
|
+
throw new OriginNotAllowedError2(
|
|
935
|
+
this.origin,
|
|
936
|
+
walletEntry.originAllowlist
|
|
937
|
+
);
|
|
736
938
|
}
|
|
737
939
|
}
|
|
940
|
+
} catch (e) {
|
|
941
|
+
if (!(e instanceof WalletNotFoundError)) {
|
|
942
|
+
throw e;
|
|
943
|
+
}
|
|
738
944
|
}
|
|
739
|
-
|
|
740
|
-
|
|
945
|
+
}
|
|
946
|
+
if (options?.requiredCapabilities) {
|
|
947
|
+
capabilityGuard(adapter, options.requiredCapabilities);
|
|
948
|
+
}
|
|
949
|
+
await installGuard(adapter);
|
|
950
|
+
const ctx = this.createAdapterContext();
|
|
951
|
+
if (adapter instanceof GenericDiscoveryAdapter && adapter.usesFactory()) {
|
|
952
|
+
let networkHosts = {};
|
|
953
|
+
try {
|
|
954
|
+
const entry = await this.registryClient.getWalletEntry(
|
|
955
|
+
String(selectedWallet.walletId)
|
|
956
|
+
);
|
|
957
|
+
networkHosts = entry.adapter?.networkHosts ?? {};
|
|
958
|
+
} catch {
|
|
741
959
|
}
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
960
|
+
adapter.setNetworkHosts(networkHosts);
|
|
961
|
+
adapter.resolveOfficial(ctx.network);
|
|
962
|
+
}
|
|
963
|
+
return { selectedWallet, adapter, ctx, isNativeWallet };
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* Invoke `adapter.connect()` (its FIRST statement — no await precedes it) and
|
|
967
|
+
* build / persist / emit the session. Separated from plan resolution so the
|
|
968
|
+
* popup-safe fast-path can call it gesture-synchronously.
|
|
969
|
+
*/
|
|
970
|
+
async completeConnect(plan, options) {
|
|
971
|
+
const { selectedWallet, adapter, ctx } = plan;
|
|
972
|
+
const timeoutMs = options?.timeoutMs || 12e4;
|
|
973
|
+
const connectPromise = adapter.connect(ctx, {
|
|
974
|
+
timeoutMs,
|
|
975
|
+
partyId: void 0,
|
|
976
|
+
preferInstalled: options?.preferInstalled,
|
|
977
|
+
onDisplayUri: options?.onDisplayUri
|
|
978
|
+
});
|
|
979
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
980
|
+
setTimeout(() => {
|
|
981
|
+
reject(new Error(`Connection timed out after ${timeoutMs}ms - user did not complete wallet connection`));
|
|
982
|
+
}, timeoutMs);
|
|
983
|
+
});
|
|
984
|
+
const result = await Promise.race([connectPromise, timeoutPromise]);
|
|
985
|
+
const session = {
|
|
986
|
+
sessionId: toSessionId(`session_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`),
|
|
987
|
+
walletId: selectedWallet.walletId,
|
|
988
|
+
partyId: result.partyId,
|
|
989
|
+
// The wallet's reported network (adapters that read the live wallet —
|
|
990
|
+
// e.g. Console via getActiveNetwork — surface the actual network here;
|
|
991
|
+
// echo-only adapters report ctx.network === config.network). Used for
|
|
992
|
+
// mismatch detection; falls back to the configured network.
|
|
993
|
+
network: result.session.network ?? this.config.network,
|
|
994
|
+
createdAt: Date.now(),
|
|
995
|
+
expiresAt: result.session.expiresAt,
|
|
996
|
+
origin: this.origin,
|
|
997
|
+
capabilitiesSnapshot: result.capabilities,
|
|
998
|
+
metadata: result.session.metadata
|
|
999
|
+
};
|
|
1000
|
+
const mismatch = this.networkMismatch(session);
|
|
1001
|
+
if (mismatch) {
|
|
1002
|
+
session.networkMismatch = mismatch;
|
|
1003
|
+
this.emit("session:networkMismatch", {
|
|
1004
|
+
type: "session:networkMismatch",
|
|
1005
|
+
sessionId: session.sessionId,
|
|
1006
|
+
expected: mismatch.expected,
|
|
1007
|
+
actual: mismatch.actual,
|
|
1008
|
+
enforced: this.enforcement !== "off"
|
|
755
1009
|
});
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
1010
|
+
if (this.enforcement === "strict") {
|
|
1011
|
+
throw new NetworkMismatchError(mismatch.expected, mismatch.actual);
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
await this.persistSession(session);
|
|
1015
|
+
this.activeSession = session;
|
|
1016
|
+
this.updateRegistryStatus();
|
|
1017
|
+
this.telemetry?.increment?.(METRICS.WALLET_CONNECT_SUCCESS);
|
|
1018
|
+
this.telemetry?.increment?.(METRICS.SESSIONS_CREATED);
|
|
1019
|
+
this.emit("session:connected", {
|
|
1020
|
+
type: "session:connected",
|
|
1021
|
+
session
|
|
1022
|
+
});
|
|
1023
|
+
return session;
|
|
1024
|
+
}
|
|
1025
|
+
/**
|
|
1026
|
+
* PUBLIC popup-safe primitive: pre-resolve a connect plan ahead of the user
|
|
1027
|
+
* gesture; the returned `connect()` invokes `adapter.connect()` as its first
|
|
1028
|
+
* statement (no await precedes it) so a popup/remote wallet's `window.open`
|
|
1029
|
+
* survives the gesture. For consumers driving such a wallet outside the modal
|
|
1030
|
+
* (the modal gets this for free via the `listWallets` warm-up + `connect()`).
|
|
1031
|
+
*/
|
|
1032
|
+
async prepareConnect(options) {
|
|
1033
|
+
const plan = await this.resolveConnectPlan(options);
|
|
1034
|
+
return {
|
|
1035
|
+
walletId: plan.selectedWallet.walletId,
|
|
1036
|
+
connect: (o) => this.completeConnect(plan, o ?? options)
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
/**
|
|
1040
|
+
* Background warm-up of connect plans for popup/remote
|
|
1041
|
+
* (`GenericDiscoveryAdapter`) wallets. Popup-free — `resolveConnectPlan` only
|
|
1042
|
+
* runs read/guard probes. Skips already-warm entries; never throws.
|
|
1043
|
+
*/
|
|
1044
|
+
async warmDiscoveryPlans(wallets) {
|
|
1045
|
+
for (const [walletId, adapter] of this.adapters) {
|
|
1046
|
+
if (!(adapter instanceof GenericDiscoveryAdapter)) continue;
|
|
1047
|
+
if (this.warmPlans.has(walletId)) continue;
|
|
1048
|
+
try {
|
|
1049
|
+
const plan = await this.resolveConnectPlan({ walletId }, wallets);
|
|
1050
|
+
this.warmPlans.set(walletId, plan);
|
|
1051
|
+
} catch {
|
|
785
1052
|
}
|
|
786
|
-
await this.persistSession(session);
|
|
787
|
-
this.activeSession = session;
|
|
788
|
-
this.updateRegistryStatus();
|
|
789
|
-
this.telemetry?.increment?.(METRICS.WALLET_CONNECT_SUCCESS);
|
|
790
|
-
this.telemetry?.increment?.(METRICS.SESSIONS_CREATED);
|
|
791
|
-
this.emit("session:connected", {
|
|
792
|
-
type: "session:connected",
|
|
793
|
-
session
|
|
794
|
-
});
|
|
795
|
-
return session;
|
|
796
|
-
} catch (err) {
|
|
797
|
-
const timeoutMs = options?.timeoutMs || 3e4;
|
|
798
|
-
const error = mapUnknownErrorToPartyLayerError(err, {
|
|
799
|
-
phase: "connect",
|
|
800
|
-
walletId: options?.walletId ? String(options.walletId) : void 0,
|
|
801
|
-
timeoutMs
|
|
802
|
-
});
|
|
803
|
-
this.emit("error", { type: "error", error });
|
|
804
|
-
throw error;
|
|
805
1053
|
}
|
|
806
1054
|
}
|
|
1055
|
+
/**
|
|
1056
|
+
* SYNCHRONOUS fast-path: if a fresh warm plan exists for a popup/remote wallet
|
|
1057
|
+
* — and `options` carries no plan-affecting guards — consume it and START
|
|
1058
|
+
* `completeConnect` immediately, reaching `adapter.connect()` with no awaits.
|
|
1059
|
+
* Returns the in-flight Session promise, or null to fall back to the normal
|
|
1060
|
+
* path (e.g. cold cache, or a non-discovery wallet).
|
|
1061
|
+
*/
|
|
1062
|
+
tryFastConnect(options) {
|
|
1063
|
+
const walletId = options?.walletId;
|
|
1064
|
+
if (!walletId) return null;
|
|
1065
|
+
if (options?.requiredCapabilities || options?.allowWallets) return null;
|
|
1066
|
+
const plan = this.warmPlans.get(walletId);
|
|
1067
|
+
if (!plan) return null;
|
|
1068
|
+
this.warmPlans.delete(walletId);
|
|
1069
|
+
return this.completeConnect(plan, options);
|
|
1070
|
+
}
|
|
807
1071
|
/**
|
|
808
1072
|
* Disconnect from wallet
|
|
809
1073
|
*/
|
|
@@ -820,6 +1084,7 @@ var PartyLayerClient = class {
|
|
|
820
1084
|
const sessionId = this.activeSession.sessionId;
|
|
821
1085
|
await this.removeSession(sessionId);
|
|
822
1086
|
this.activeSession = null;
|
|
1087
|
+
this.warmPlans.clear();
|
|
823
1088
|
this.emit("session:disconnected", {
|
|
824
1089
|
type: "session:disconnected",
|
|
825
1090
|
sessionId
|
|
@@ -1099,6 +1364,29 @@ var PartyLayerClient = class {
|
|
|
1099
1364
|
if (session.origin !== this.origin) {
|
|
1100
1365
|
return null;
|
|
1101
1366
|
}
|
|
1367
|
+
const mismatch = this.networkMismatch(session);
|
|
1368
|
+
if (mismatch) {
|
|
1369
|
+
this.emit("session:networkMismatch", {
|
|
1370
|
+
type: "session:networkMismatch",
|
|
1371
|
+
sessionId: session.sessionId,
|
|
1372
|
+
expected: mismatch.expected,
|
|
1373
|
+
actual: mismatch.actual,
|
|
1374
|
+
enforced: this.enforcement !== "off"
|
|
1375
|
+
});
|
|
1376
|
+
if (this.enforcement !== "off") {
|
|
1377
|
+
this.logger.warn("Refused session restore \u2014 network mismatch", {
|
|
1378
|
+
expected: mismatch.expected,
|
|
1379
|
+
actual: mismatch.actual
|
|
1380
|
+
});
|
|
1381
|
+
await this.removeSession(session.sessionId);
|
|
1382
|
+
this.emit("session:expired", {
|
|
1383
|
+
type: "session:expired",
|
|
1384
|
+
sessionId: session.sessionId
|
|
1385
|
+
});
|
|
1386
|
+
return null;
|
|
1387
|
+
}
|
|
1388
|
+
session.networkMismatch = mismatch;
|
|
1389
|
+
}
|
|
1102
1390
|
const adapter = this.adapters.get(session.walletId);
|
|
1103
1391
|
if (adapter?.restore) {
|
|
1104
1392
|
const ctx = this.createAdapterContext();
|
|
@@ -1193,6 +1481,6 @@ function createPartyLayer(config) {
|
|
|
1193
1481
|
return new PartyLayerClient(config);
|
|
1194
1482
|
}
|
|
1195
1483
|
|
|
1196
|
-
export { ANNOUNCED_WALLET_ID_PREFIX, PartyLayerClient as CantonConnectClient, DEFAULT_REGISTRY_URL, GenericAnnounceAdapter, MetricsTelemetryAdapter, PartyLayerClient, announcedWalletId, createPartyLayer as createCantonConnect, createPartyLayer, createTelemetryAdapter, getBuiltinAdapters, isTelemetryConfig };
|
|
1484
|
+
export { ANNOUNCED_WALLET_ID_PREFIX, PartyLayerClient as CantonConnectClient, DEFAULT_REGISTRY_URL, GenericAnnounceAdapter, GenericDiscoveryAdapter, MetricsTelemetryAdapter, PartyLayerClient, announcedWalletId, createPartyLayer as createCantonConnect, createPartyLayer, createTelemetryAdapter, getBuiltinAdapters, isTelemetryConfig };
|
|
1197
1485
|
//# sourceMappingURL=index.mjs.map
|
|
1198
1486
|
//# sourceMappingURL=index.mjs.map
|