@partylayer/provider 0.2.5 → 0.3.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 CHANGED
@@ -97,6 +97,40 @@ interface AnnounceDiscoveryOptions {
97
97
  * Announce replies are deduped by id within a single call.
98
98
  */
99
99
  declare function discoverAnnouncedProviders(options?: AnnounceDiscoveryOptions): Promise<DiscoveredProvider[]>;
100
+ /** Options for {@link subscribeAnnouncedProviders}. */
101
+ interface AnnounceSubscribeOptions {
102
+ /** Build a CIP-0103 provider from an announce. Defaults to the channel provider. */
103
+ createProvider?: (announced: AnnouncedWallet) => CIP0103Provider | Promise<CIP0103Provider>;
104
+ /** Dispatch `canton:requestProvider` on subscribe to elicit replies (default true). */
105
+ requestOnSubscribe?: boolean;
106
+ }
107
+ /**
108
+ * PERSISTENT (EIP-6963-style) announce subscription. Mounts a `window` listener
109
+ * for `canton:announceProvider` that stays up until the returned unsubscribe runs.
110
+ * Unlike {@link discoverAnnouncedProviders} (a one-shot snapshot that stops
111
+ * listening after `timeoutMs`), this captures announces that arrive AT ANY TIME —
112
+ * both LATE (slow/late extension injection, after the request window) and announces
113
+ * fired on inject BEFORE this subscription. Dispatches `canton:requestProvider`
114
+ * once on subscribe (unless disabled) to elicit replies. Calls `onProvider` once
115
+ * per newly-announced id (deduped) whose provider builds + passes the CIP-0103
116
+ * shape check.
117
+ *
118
+ * The caller MUST call the returned unsubscribe to remove the listener (no leak).
119
+ */
120
+ declare function subscribeAnnouncedProviders(onProvider: (provider: DiscoveredProvider) => void, options?: AnnounceSubscribeOptions): () => void;
121
+ /** Options for {@link waitForAnnouncedProvider}. */
122
+ interface WaitForAnnouncedOptions extends AnnounceSubscribeOptions {
123
+ /** Max wait before resolving null (default 3000ms). Resolves EARLY on a match. */
124
+ timeoutMs?: number;
125
+ }
126
+ /**
127
+ * Resolve the FIRST announced provider matching `predicate` the MOMENT it arrives,
128
+ * or `null` after `timeoutMs`. Built on {@link subscribeAnnouncedProviders}, so a
129
+ * LATE announce within the bound is captured (vs a fixed window that returns its
130
+ * snapshot at a fixed time and misses anything later). Auto-unsubscribes on
131
+ * resolve/timeout — no lingering listener.
132
+ */
133
+ declare function waitForAnnouncedProvider(predicate: (provider: DiscoveredProvider) => boolean, options?: WaitForAnnouncedOptions): Promise<DiscoveredProvider | null>;
100
134
  /**
101
135
  * Discover ALL CIP-0103 wallets: the synchronous `window.canton` scan PLUS the
102
136
  * `canton:announceProvider` handshake, MERGED and deduped by stable provider id.
@@ -455,4 +489,4 @@ declare class MethodRouter {
455
489
  private registerDefaults;
456
490
  }
457
491
 
458
- export { type AnnounceDiscoveryOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, methodNotFound, resourceNotFound, resourceUnavailable, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForProvider };
492
+ export { type AnnounceDiscoveryOptions, type AnnounceSubscribeOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, type WaitForAnnouncedOptions, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, methodNotFound, resourceNotFound, resourceUnavailable, subscribeAnnouncedProviders, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForAnnouncedProvider, waitForProvider };
package/dist/index.d.ts CHANGED
@@ -97,6 +97,40 @@ interface AnnounceDiscoveryOptions {
97
97
  * Announce replies are deduped by id within a single call.
98
98
  */
99
99
  declare function discoverAnnouncedProviders(options?: AnnounceDiscoveryOptions): Promise<DiscoveredProvider[]>;
100
+ /** Options for {@link subscribeAnnouncedProviders}. */
101
+ interface AnnounceSubscribeOptions {
102
+ /** Build a CIP-0103 provider from an announce. Defaults to the channel provider. */
103
+ createProvider?: (announced: AnnouncedWallet) => CIP0103Provider | Promise<CIP0103Provider>;
104
+ /** Dispatch `canton:requestProvider` on subscribe to elicit replies (default true). */
105
+ requestOnSubscribe?: boolean;
106
+ }
107
+ /**
108
+ * PERSISTENT (EIP-6963-style) announce subscription. Mounts a `window` listener
109
+ * for `canton:announceProvider` that stays up until the returned unsubscribe runs.
110
+ * Unlike {@link discoverAnnouncedProviders} (a one-shot snapshot that stops
111
+ * listening after `timeoutMs`), this captures announces that arrive AT ANY TIME —
112
+ * both LATE (slow/late extension injection, after the request window) and announces
113
+ * fired on inject BEFORE this subscription. Dispatches `canton:requestProvider`
114
+ * once on subscribe (unless disabled) to elicit replies. Calls `onProvider` once
115
+ * per newly-announced id (deduped) whose provider builds + passes the CIP-0103
116
+ * shape check.
117
+ *
118
+ * The caller MUST call the returned unsubscribe to remove the listener (no leak).
119
+ */
120
+ declare function subscribeAnnouncedProviders(onProvider: (provider: DiscoveredProvider) => void, options?: AnnounceSubscribeOptions): () => void;
121
+ /** Options for {@link waitForAnnouncedProvider}. */
122
+ interface WaitForAnnouncedOptions extends AnnounceSubscribeOptions {
123
+ /** Max wait before resolving null (default 3000ms). Resolves EARLY on a match. */
124
+ timeoutMs?: number;
125
+ }
126
+ /**
127
+ * Resolve the FIRST announced provider matching `predicate` the MOMENT it arrives,
128
+ * or `null` after `timeoutMs`. Built on {@link subscribeAnnouncedProviders}, so a
129
+ * LATE announce within the bound is captured (vs a fixed window that returns its
130
+ * snapshot at a fixed time and misses anything later). Auto-unsubscribes on
131
+ * resolve/timeout — no lingering listener.
132
+ */
133
+ declare function waitForAnnouncedProvider(predicate: (provider: DiscoveredProvider) => boolean, options?: WaitForAnnouncedOptions): Promise<DiscoveredProvider | null>;
100
134
  /**
101
135
  * Discover ALL CIP-0103 wallets: the synchronous `window.canton` scan PLUS the
102
136
  * `canton:announceProvider` handshake, MERGED and deduped by stable provider id.
@@ -455,4 +489,4 @@ declare class MethodRouter {
455
489
  private registerDefaults;
456
490
  }
457
491
 
458
- export { type AnnounceDiscoveryOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, methodNotFound, resourceNotFound, resourceUnavailable, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForProvider };
492
+ export { type AnnounceDiscoveryOptions, type AnnounceSubscribeOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, type WaitForAnnouncedOptions, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, methodNotFound, resourceNotFound, resourceUnavailable, subscribeAnnouncedProviders, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForAnnouncedProvider, waitForProvider };
package/dist/index.js CHANGED
@@ -156,6 +156,9 @@ function transactionRejected(message = "Transaction rejected", data) {
156
156
  var ERROR_CODE_TO_RPC = {
157
157
  USER_REJECTED: RPC_ERRORS.USER_REJECTED,
158
158
  WALLET_NOT_FOUND: JSON_RPC_ERRORS.RESOURCE_NOT_FOUND,
159
+ // Client-side config gap (the walletId refers to an unwired discovery adapter),
160
+ // not a missing resource → INVALID_PARAMS, distinct from WALLET_NOT_FOUND.
161
+ ADAPTER_NOT_REGISTERED: JSON_RPC_ERRORS.INVALID_PARAMS,
159
162
  WALLET_NOT_INSTALLED: JSON_RPC_ERRORS.RESOURCE_UNAVAILABLE,
160
163
  ORIGIN_NOT_ALLOWED: RPC_ERRORS.UNAUTHORIZED,
161
164
  SESSION_EXPIRED: RPC_ERRORS.DISCONNECTED,
@@ -506,7 +509,7 @@ var PartyLayerProvider = class {
506
509
  };
507
510
 
508
511
  // src/bridge.ts
509
- var PROVIDER_VERSION = "0.2.5" ;
512
+ var PROVIDER_VERSION = "0.3.0" ;
510
513
  function createProviderBridge(client) {
511
514
  const eventBus = new CIP0103EventBus();
512
515
  const pendingEmitted = /* @__PURE__ */ new Set();
@@ -1003,6 +1006,70 @@ async function discoverAnnouncedProviders(options = {}) {
1003
1006
  }
1004
1007
  return results;
1005
1008
  }
1009
+ function subscribeAnnouncedProviders(onProvider, options = {}) {
1010
+ if (typeof window === "undefined") return () => {
1011
+ };
1012
+ const make = options.createProvider ?? defaultAnnounceProvider;
1013
+ const seen = /* @__PURE__ */ new Set();
1014
+ const onAnnounce = (event) => {
1015
+ const detail = event.detail;
1016
+ if (!detail) return;
1017
+ const rawId = detail.providerId ?? detail.id;
1018
+ if (typeof rawId !== "string" || rawId.length === 0) return;
1019
+ if (seen.has(rawId)) return;
1020
+ seen.add(rawId);
1021
+ const wallet = {
1022
+ id: rawId,
1023
+ name: typeof detail.name === "string" ? detail.name : void 0,
1024
+ icon: typeof detail.icon === "string" ? detail.icon : void 0,
1025
+ target: typeof detail.target === "string" ? detail.target : void 0
1026
+ };
1027
+ void Promise.resolve().then(() => make(wallet)).then((provider) => {
1028
+ if (!isCIP0103Provider(provider)) return;
1029
+ onProvider({
1030
+ id: wallet.id,
1031
+ provider,
1032
+ source: "injected",
1033
+ name: wallet.name,
1034
+ icon: wallet.icon
1035
+ });
1036
+ }).catch(() => {
1037
+ });
1038
+ };
1039
+ window.addEventListener(
1040
+ CANTON_ANNOUNCE_PROVIDER_EVENT,
1041
+ onAnnounce
1042
+ );
1043
+ if (options.requestOnSubscribe !== false) {
1044
+ window.dispatchEvent(new CustomEvent(CANTON_REQUEST_PROVIDER_EVENT));
1045
+ }
1046
+ return () => window.removeEventListener(
1047
+ CANTON_ANNOUNCE_PROVIDER_EVENT,
1048
+ onAnnounce
1049
+ );
1050
+ }
1051
+ function waitForAnnouncedProvider(predicate, options = {}) {
1052
+ if (typeof window === "undefined") return Promise.resolve(null);
1053
+ const timeoutMs = options.timeoutMs ?? 3e3;
1054
+ return new Promise((resolve) => {
1055
+ const h = {
1056
+ done: false,
1057
+ unsubscribe: () => {
1058
+ }
1059
+ };
1060
+ const finish = (value) => {
1061
+ if (h.done) return;
1062
+ h.done = true;
1063
+ if (h.timer) clearTimeout(h.timer);
1064
+ h.unsubscribe();
1065
+ resolve(value);
1066
+ };
1067
+ h.unsubscribe = subscribeAnnouncedProviders((p) => {
1068
+ if (predicate(p)) finish(p);
1069
+ }, { createProvider: options.createProvider, requestOnSubscribe: options.requestOnSubscribe });
1070
+ h.timer = setTimeout(() => finish(null), timeoutMs);
1071
+ });
1072
+ }
1006
1073
  var INJECTED_ID_PROBE_TIMEOUT_MS = 1500;
1007
1074
  function raceTimeout(promise, ms) {
1008
1075
  return Promise.race([
@@ -1095,12 +1162,14 @@ exports.isCIP0103Provider = isCIP0103Provider;
1095
1162
  exports.methodNotFound = methodNotFound;
1096
1163
  exports.resourceNotFound = resourceNotFound;
1097
1164
  exports.resourceUnavailable = resourceUnavailable;
1165
+ exports.subscribeAnnouncedProviders = subscribeAnnouncedProviders;
1098
1166
  exports.toPartyLayerError = toPartyLayerError;
1099
1167
  exports.toProviderRpcError = toProviderRpcError;
1100
1168
  exports.transactionRejected = transactionRejected;
1101
1169
  exports.unauthorized = unauthorized;
1102
1170
  exports.unsupportedMethod = unsupportedMethod;
1103
1171
  exports.userRejected = userRejected;
1172
+ exports.waitForAnnouncedProvider = waitForAnnouncedProvider;
1104
1173
  exports.waitForProvider = waitForProvider;
1105
1174
  //# sourceMappingURL=index.js.map
1106
1175
  //# sourceMappingURL=index.js.map