@partylayer/sdk 0.8.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.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { toWalletId, toPartyId, hashForPrivacy, createMetricsPayload, 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, isOfficialProviderAdapter } 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';
@@ -88,14 +88,70 @@ var GenericDiscoveryAdapter = class {
88
88
  walletId;
89
89
  name;
90
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
+ */
91
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;
92
101
  /** Lazily resolved from `official.provider()` — NOT at construction (SSR-safe). */
93
102
  providerInstance = null;
94
103
  constructor(args) {
95
- this.official = args.official;
96
- this.walletId = args.walletId ?? toWalletId(args.official.providerId);
97
- this.name = args.name && args.name.length > 0 ? args.name : args.official.name;
98
- this.icon = args.icon ?? args.official.icon;
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);
99
155
  }
100
156
  /** Lazily obtain (and cache) the official provider — deferred off the init/SSR path.
101
157
  * `official.provider()` is typed `OfficialProvider` (loose `request` so real
@@ -103,6 +159,11 @@ var GenericDiscoveryAdapter = class {
103
159
  * (the bridge only ever calls `request({ method, params })`). */
104
160
  get provider() {
105
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
+ }
106
167
  this.providerInstance = this.official.provider();
107
168
  }
108
169
  return this.providerInstance;
@@ -116,6 +177,9 @@ var GenericDiscoveryAdapter = class {
116
177
  }
117
178
  /** Install/availability probe — delegated to the official adapter (popup-free). */
118
179
  async detectInstalled() {
180
+ if (!this.official) {
181
+ return { installed: true, reason: "Popup/remote wallet (resolved at connect)" };
182
+ }
119
183
  try {
120
184
  const installed = await this.official.detect();
121
185
  return {
@@ -127,6 +191,7 @@ var GenericDiscoveryAdapter = class {
127
191
  }
128
192
  }
129
193
  async connect(ctx) {
194
+ this.resolveOfficial(ctx.network);
130
195
  await this.provider.request({ method: "connect" });
131
196
  const account = await this.provider.request({
132
197
  method: "getPrimaryAccount"
@@ -557,6 +622,8 @@ var PartyLayerClient = class {
557
622
  let adapter;
558
623
  if (typeof adapterOrClass === "function") {
559
624
  adapter = new adapterOrClass();
625
+ } else if (isOfficialAdapterFactory(adapterOrClass)) {
626
+ adapter = new GenericDiscoveryAdapter({ factory: adapterOrClass });
560
627
  } else if (isOfficialProviderAdapter(adapterOrClass)) {
561
628
  adapter = new GenericDiscoveryAdapter({ official: adapterOrClass });
562
629
  } else {
@@ -881,6 +948,18 @@ var PartyLayerClient = class {
881
948
  }
882
949
  await installGuard(adapter);
883
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 {
959
+ }
960
+ adapter.setNetworkHosts(networkHosts);
961
+ adapter.resolveOfficial(ctx.network);
962
+ }
884
963
  return { selectedWallet, adapter, ctx, isNativeWallet };
885
964
  }
886
965
  /**
@@ -1285,6 +1364,29 @@ var PartyLayerClient = class {
1285
1364
  if (session.origin !== this.origin) {
1286
1365
  return null;
1287
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
+ }
1288
1390
  const adapter = this.adapters.get(session.walletId);
1289
1391
  if (adapter?.restore) {
1290
1392
  const ctx = this.createAdapterContext();