@partylayer/sdk 0.8.0 → 0.10.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 +57 -12
- package/dist/index.d.ts +57 -12
- package/dist/index.js +114 -5
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +112 -7
- 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, 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, isRecognizedNetwork, 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
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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"
|
|
@@ -140,10 +205,13 @@ var GenericDiscoveryAdapter = class {
|
|
|
140
205
|
} catch {
|
|
141
206
|
}
|
|
142
207
|
const partyId = toPartyId(account.partyId);
|
|
208
|
+
const network = [reportedNetwork, account.networkId, ctx.network].find(
|
|
209
|
+
(n) => typeof n === "string" && isRecognizedNetwork(n)
|
|
210
|
+
) ?? ctx.network;
|
|
143
211
|
const session = {
|
|
144
212
|
walletId: this.walletId,
|
|
145
213
|
partyId,
|
|
146
|
-
network
|
|
214
|
+
network
|
|
147
215
|
};
|
|
148
216
|
return { partyId, session, capabilities: this.getCapabilities() };
|
|
149
217
|
}
|
|
@@ -557,6 +625,8 @@ var PartyLayerClient = class {
|
|
|
557
625
|
let adapter;
|
|
558
626
|
if (typeof adapterOrClass === "function") {
|
|
559
627
|
adapter = new adapterOrClass();
|
|
628
|
+
} else if (isOfficialAdapterFactory(adapterOrClass)) {
|
|
629
|
+
adapter = new GenericDiscoveryAdapter({ factory: adapterOrClass });
|
|
560
630
|
} else if (isOfficialProviderAdapter(adapterOrClass)) {
|
|
561
631
|
adapter = new GenericDiscoveryAdapter({ official: adapterOrClass });
|
|
562
632
|
} else {
|
|
@@ -881,6 +951,18 @@ var PartyLayerClient = class {
|
|
|
881
951
|
}
|
|
882
952
|
await installGuard(adapter);
|
|
883
953
|
const ctx = this.createAdapterContext();
|
|
954
|
+
if (adapter instanceof GenericDiscoveryAdapter && adapter.usesFactory()) {
|
|
955
|
+
let networkHosts = {};
|
|
956
|
+
try {
|
|
957
|
+
const entry = await this.registryClient.getWalletEntry(
|
|
958
|
+
String(selectedWallet.walletId)
|
|
959
|
+
);
|
|
960
|
+
networkHosts = entry.adapter?.networkHosts ?? {};
|
|
961
|
+
} catch {
|
|
962
|
+
}
|
|
963
|
+
adapter.setNetworkHosts(networkHosts);
|
|
964
|
+
adapter.resolveOfficial(ctx.network);
|
|
965
|
+
}
|
|
884
966
|
return { selectedWallet, adapter, ctx, isNativeWallet };
|
|
885
967
|
}
|
|
886
968
|
/**
|
|
@@ -1285,6 +1367,29 @@ var PartyLayerClient = class {
|
|
|
1285
1367
|
if (session.origin !== this.origin) {
|
|
1286
1368
|
return null;
|
|
1287
1369
|
}
|
|
1370
|
+
const mismatch = this.networkMismatch(session);
|
|
1371
|
+
if (mismatch) {
|
|
1372
|
+
this.emit("session:networkMismatch", {
|
|
1373
|
+
type: "session:networkMismatch",
|
|
1374
|
+
sessionId: session.sessionId,
|
|
1375
|
+
expected: mismatch.expected,
|
|
1376
|
+
actual: mismatch.actual,
|
|
1377
|
+
enforced: this.enforcement !== "off"
|
|
1378
|
+
});
|
|
1379
|
+
if (this.enforcement !== "off") {
|
|
1380
|
+
this.logger.warn("Refused session restore \u2014 network mismatch", {
|
|
1381
|
+
expected: mismatch.expected,
|
|
1382
|
+
actual: mismatch.actual
|
|
1383
|
+
});
|
|
1384
|
+
await this.removeSession(session.sessionId);
|
|
1385
|
+
this.emit("session:expired", {
|
|
1386
|
+
type: "session:expired",
|
|
1387
|
+
sessionId: session.sessionId
|
|
1388
|
+
});
|
|
1389
|
+
return null;
|
|
1390
|
+
}
|
|
1391
|
+
session.networkMismatch = mismatch;
|
|
1392
|
+
}
|
|
1288
1393
|
const adapter = this.adapters.get(session.walletId);
|
|
1289
1394
|
if (adapter?.restore) {
|
|
1290
1395
|
const ctx = this.createAdapterContext();
|