@partylayer/provider 0.1.6 → 0.2.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/README.md +5 -2
- package/dist/index.d.mts +83 -1
- package/dist/index.d.ts +83 -1
- package/dist/index.js +191 -1
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +189 -2
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -42,12 +42,15 @@ npm install @partylayer/provider
|
|
|
42
42
|
```typescript
|
|
43
43
|
import { PartyLayerProvider, discoverProviders } from '@partylayer/provider';
|
|
44
44
|
|
|
45
|
-
// Discover
|
|
46
|
-
|
|
45
|
+
// Discover CIP-0103 wallets: the synchronous window.canton scan PLUS the
|
|
46
|
+
// canton:announceProvider handshake (async), merged + deduped by provider id.
|
|
47
|
+
const wallets = await discoverProviders();
|
|
47
48
|
|
|
48
49
|
// Create provider with first discovered wallet
|
|
49
50
|
const provider = new PartyLayerProvider({ walletProvider: wallets[0] });
|
|
50
51
|
|
|
52
|
+
// (For the synchronous window.canton-only scan, use discoverInjectedProviders().)
|
|
53
|
+
|
|
51
54
|
// Connect
|
|
52
55
|
const result = await provider.request({ method: 'connect' });
|
|
53
56
|
|
package/dist/index.d.mts
CHANGED
|
@@ -21,6 +21,8 @@ interface DiscoveredProvider {
|
|
|
21
21
|
isAsync?: boolean;
|
|
22
22
|
/** Display name (if discoverable from status) */
|
|
23
23
|
name?: string;
|
|
24
|
+
/** Icon (data: URI or URL) — populated for announce-discovered wallets. */
|
|
25
|
+
icon?: string;
|
|
24
26
|
}
|
|
25
27
|
/**
|
|
26
28
|
* Check if an object implements the CIP-0103 Provider interface.
|
|
@@ -47,6 +49,54 @@ declare function discoverInjectedProviders(): DiscoveredProvider[];
|
|
|
47
49
|
* @param timeoutMs - Maximum wait time (default 3000ms)
|
|
48
50
|
*/
|
|
49
51
|
declare function waitForProvider(id: string, timeoutMs?: number): Promise<DiscoveredProvider | null>;
|
|
52
|
+
/** Metadata carried by a `canton:announceProvider` event. */
|
|
53
|
+
interface AnnouncedWallet {
|
|
54
|
+
/** Stable provider id (extension id), e.g. "ldmoh…" for Send. */
|
|
55
|
+
id: string;
|
|
56
|
+
/** Display name. */
|
|
57
|
+
name?: string;
|
|
58
|
+
/** Icon (data: URI or URL). */
|
|
59
|
+
icon?: string;
|
|
60
|
+
/** Routing key for the extension postMessage channel. */
|
|
61
|
+
target?: string;
|
|
62
|
+
}
|
|
63
|
+
interface AnnounceDiscoveryOptions {
|
|
64
|
+
/** How long to collect announce replies after the request (ms). Default 300. */
|
|
65
|
+
timeoutMs?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Build a CIP-0103 provider from an announced wallet. Defaults to the
|
|
68
|
+
* self-contained `createExtensionChannelProvider` (splice postMessage over
|
|
69
|
+
* the `target` channel). Injectable so apps can substitute the official
|
|
70
|
+
* `@canton-network/dapp-sdk` `ExtensionAdapter`, and tests a mock.
|
|
71
|
+
*/
|
|
72
|
+
createProvider?: (announced: AnnouncedWallet) => CIP0103Provider | Promise<CIP0103Provider>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Discover wallets that advertise via `canton:announceProvider` (EIP-6963-style).
|
|
76
|
+
*
|
|
77
|
+
* Works regardless of who owns `window.canton` — this is how Send (and
|
|
78
|
+
* Console-via-announce) are found. Each result is a working CIP-0103 provider.
|
|
79
|
+
* Announce replies are deduped by id within a single call.
|
|
80
|
+
*/
|
|
81
|
+
declare function discoverAnnouncedProviders(options?: AnnounceDiscoveryOptions): Promise<DiscoveredProvider[]>;
|
|
82
|
+
/**
|
|
83
|
+
* Discover ALL CIP-0103 wallets: the synchronous `window.canton` scan PLUS the
|
|
84
|
+
* `canton:announceProvider` handshake, MERGED and deduped by stable provider id.
|
|
85
|
+
*
|
|
86
|
+
* Dedup keys:
|
|
87
|
+
* - INJECTED entries: resolved via {@link resolveInjectedKey} (sync id →
|
|
88
|
+
* capped read-only status() probe → path id). Resolved in PARALLEL.
|
|
89
|
+
* - ANNOUNCE entries: their `d.id` (== announce id == target == the wallet's
|
|
90
|
+
* `provider.id`). NOT status-probed — an offline announce wallet (e.g. Send)
|
|
91
|
+
* would otherwise hang up to the channel timeout.
|
|
92
|
+
*
|
|
93
|
+
* INJECTED entries are processed FIRST so the direct `window.canton` provider
|
|
94
|
+
* wins over the announce postMessage shim for a wallet reachable both ways
|
|
95
|
+
* (e.g. Console announces AND owns `window.canton` → appears exactly once).
|
|
96
|
+
*
|
|
97
|
+
* Backward-compatible superset of `discoverInjectedProviders()` (left unchanged).
|
|
98
|
+
*/
|
|
99
|
+
declare function discoverProviders(options?: AnnounceDiscoveryOptions): Promise<DiscoveredProvider[]>;
|
|
50
100
|
|
|
51
101
|
/**
|
|
52
102
|
* PartyLayerProvider — CIP-0103 Native Provider Implementation
|
|
@@ -261,6 +311,38 @@ declare function toProviderRpcError(err: unknown): ProviderRpcError;
|
|
|
261
311
|
*/
|
|
262
312
|
declare function toPartyLayerError(err: ProviderRpcError): PartyLayerError;
|
|
263
313
|
|
|
314
|
+
/**
|
|
315
|
+
* Extension target-channel CIP-0103 provider.
|
|
316
|
+
*
|
|
317
|
+
* Builds a working CIP-0103 provider for a wallet discovered via
|
|
318
|
+
* `canton:announceProvider` (see discovery.ts). Announce wallets route requests
|
|
319
|
+
* over the splice-wallet postMessage protocol — the dApp posts a
|
|
320
|
+
* `SPLICE_WALLET_REQUEST` on `window` and the extension replies with a
|
|
321
|
+
* `SPLICE_WALLET_RESPONSE`, correlated by JSON-RPC id and routed by `target`.
|
|
322
|
+
*
|
|
323
|
+
* This is a self-contained, dependency-free implementation of that wire
|
|
324
|
+
* protocol. We deliberately do NOT depend on `@canton-network/dapp-sdk`'s
|
|
325
|
+
* `ExtensionAdapter`: its single bundled entry statically imports
|
|
326
|
+
* `@walletconnect/sign-client` (an optional peer used only by its
|
|
327
|
+
* WalletConnectAdapter), which is not installed and breaks every downstream
|
|
328
|
+
* webpack/Next build that pulls `@partylayer/provider` into its graph
|
|
329
|
+
* (confirmed against the live demo's `next build`). The protocol constants and
|
|
330
|
+
* message shapes below mirror `@canton-network/core-types` (`WalletEvent`,
|
|
331
|
+
* `SpliceMessage`) verbatim, so behaviour matches the official adapter.
|
|
332
|
+
*/
|
|
333
|
+
|
|
334
|
+
interface ExtensionChannelOptions {
|
|
335
|
+
/** Routing key (announce `target`) included on every request message. */
|
|
336
|
+
target?: string;
|
|
337
|
+
/** Per-request timeout in ms. Default 30000. */
|
|
338
|
+
timeoutMs?: number;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Create a CIP-0103 provider that talks the splice-wallet postMessage protocol
|
|
342
|
+
* over the page `window`, routed by `target`.
|
|
343
|
+
*/
|
|
344
|
+
declare function createExtensionChannelProvider(options?: ExtensionChannelOptions): CIP0103Provider;
|
|
345
|
+
|
|
264
346
|
/**
|
|
265
347
|
* Async Wallet Handling
|
|
266
348
|
*
|
|
@@ -386,4 +468,4 @@ declare class MethodRouter {
|
|
|
386
468
|
private registerDefaults;
|
|
387
469
|
}
|
|
388
470
|
|
|
389
|
-
export { type AsyncConnectOptions, type AsyncPrepareExecuteOptions, CANTON_NETWORKS, CIP0103EventBus, type DiscoveredProvider, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, chainDisconnected, createProviderBridge, disconnected, discoverInjectedProviders, fromCAIP2Network, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, isValidCAIP2, methodNotFound, resourceNotFound, resourceUnavailable, toCAIP2Network, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForProvider };
|
|
471
|
+
export { type AnnounceDiscoveryOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CANTON_NETWORKS, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, fromCAIP2Network, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, isValidCAIP2, methodNotFound, resourceNotFound, resourceUnavailable, toCAIP2Network, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForProvider };
|
package/dist/index.d.ts
CHANGED
|
@@ -21,6 +21,8 @@ interface DiscoveredProvider {
|
|
|
21
21
|
isAsync?: boolean;
|
|
22
22
|
/** Display name (if discoverable from status) */
|
|
23
23
|
name?: string;
|
|
24
|
+
/** Icon (data: URI or URL) — populated for announce-discovered wallets. */
|
|
25
|
+
icon?: string;
|
|
24
26
|
}
|
|
25
27
|
/**
|
|
26
28
|
* Check if an object implements the CIP-0103 Provider interface.
|
|
@@ -47,6 +49,54 @@ declare function discoverInjectedProviders(): DiscoveredProvider[];
|
|
|
47
49
|
* @param timeoutMs - Maximum wait time (default 3000ms)
|
|
48
50
|
*/
|
|
49
51
|
declare function waitForProvider(id: string, timeoutMs?: number): Promise<DiscoveredProvider | null>;
|
|
52
|
+
/** Metadata carried by a `canton:announceProvider` event. */
|
|
53
|
+
interface AnnouncedWallet {
|
|
54
|
+
/** Stable provider id (extension id), e.g. "ldmoh…" for Send. */
|
|
55
|
+
id: string;
|
|
56
|
+
/** Display name. */
|
|
57
|
+
name?: string;
|
|
58
|
+
/** Icon (data: URI or URL). */
|
|
59
|
+
icon?: string;
|
|
60
|
+
/** Routing key for the extension postMessage channel. */
|
|
61
|
+
target?: string;
|
|
62
|
+
}
|
|
63
|
+
interface AnnounceDiscoveryOptions {
|
|
64
|
+
/** How long to collect announce replies after the request (ms). Default 300. */
|
|
65
|
+
timeoutMs?: number;
|
|
66
|
+
/**
|
|
67
|
+
* Build a CIP-0103 provider from an announced wallet. Defaults to the
|
|
68
|
+
* self-contained `createExtensionChannelProvider` (splice postMessage over
|
|
69
|
+
* the `target` channel). Injectable so apps can substitute the official
|
|
70
|
+
* `@canton-network/dapp-sdk` `ExtensionAdapter`, and tests a mock.
|
|
71
|
+
*/
|
|
72
|
+
createProvider?: (announced: AnnouncedWallet) => CIP0103Provider | Promise<CIP0103Provider>;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Discover wallets that advertise via `canton:announceProvider` (EIP-6963-style).
|
|
76
|
+
*
|
|
77
|
+
* Works regardless of who owns `window.canton` — this is how Send (and
|
|
78
|
+
* Console-via-announce) are found. Each result is a working CIP-0103 provider.
|
|
79
|
+
* Announce replies are deduped by id within a single call.
|
|
80
|
+
*/
|
|
81
|
+
declare function discoverAnnouncedProviders(options?: AnnounceDiscoveryOptions): Promise<DiscoveredProvider[]>;
|
|
82
|
+
/**
|
|
83
|
+
* Discover ALL CIP-0103 wallets: the synchronous `window.canton` scan PLUS the
|
|
84
|
+
* `canton:announceProvider` handshake, MERGED and deduped by stable provider id.
|
|
85
|
+
*
|
|
86
|
+
* Dedup keys:
|
|
87
|
+
* - INJECTED entries: resolved via {@link resolveInjectedKey} (sync id →
|
|
88
|
+
* capped read-only status() probe → path id). Resolved in PARALLEL.
|
|
89
|
+
* - ANNOUNCE entries: their `d.id` (== announce id == target == the wallet's
|
|
90
|
+
* `provider.id`). NOT status-probed — an offline announce wallet (e.g. Send)
|
|
91
|
+
* would otherwise hang up to the channel timeout.
|
|
92
|
+
*
|
|
93
|
+
* INJECTED entries are processed FIRST so the direct `window.canton` provider
|
|
94
|
+
* wins over the announce postMessage shim for a wallet reachable both ways
|
|
95
|
+
* (e.g. Console announces AND owns `window.canton` → appears exactly once).
|
|
96
|
+
*
|
|
97
|
+
* Backward-compatible superset of `discoverInjectedProviders()` (left unchanged).
|
|
98
|
+
*/
|
|
99
|
+
declare function discoverProviders(options?: AnnounceDiscoveryOptions): Promise<DiscoveredProvider[]>;
|
|
50
100
|
|
|
51
101
|
/**
|
|
52
102
|
* PartyLayerProvider — CIP-0103 Native Provider Implementation
|
|
@@ -261,6 +311,38 @@ declare function toProviderRpcError(err: unknown): ProviderRpcError;
|
|
|
261
311
|
*/
|
|
262
312
|
declare function toPartyLayerError(err: ProviderRpcError): PartyLayerError;
|
|
263
313
|
|
|
314
|
+
/**
|
|
315
|
+
* Extension target-channel CIP-0103 provider.
|
|
316
|
+
*
|
|
317
|
+
* Builds a working CIP-0103 provider for a wallet discovered via
|
|
318
|
+
* `canton:announceProvider` (see discovery.ts). Announce wallets route requests
|
|
319
|
+
* over the splice-wallet postMessage protocol — the dApp posts a
|
|
320
|
+
* `SPLICE_WALLET_REQUEST` on `window` and the extension replies with a
|
|
321
|
+
* `SPLICE_WALLET_RESPONSE`, correlated by JSON-RPC id and routed by `target`.
|
|
322
|
+
*
|
|
323
|
+
* This is a self-contained, dependency-free implementation of that wire
|
|
324
|
+
* protocol. We deliberately do NOT depend on `@canton-network/dapp-sdk`'s
|
|
325
|
+
* `ExtensionAdapter`: its single bundled entry statically imports
|
|
326
|
+
* `@walletconnect/sign-client` (an optional peer used only by its
|
|
327
|
+
* WalletConnectAdapter), which is not installed and breaks every downstream
|
|
328
|
+
* webpack/Next build that pulls `@partylayer/provider` into its graph
|
|
329
|
+
* (confirmed against the live demo's `next build`). The protocol constants and
|
|
330
|
+
* message shapes below mirror `@canton-network/core-types` (`WalletEvent`,
|
|
331
|
+
* `SpliceMessage`) verbatim, so behaviour matches the official adapter.
|
|
332
|
+
*/
|
|
333
|
+
|
|
334
|
+
interface ExtensionChannelOptions {
|
|
335
|
+
/** Routing key (announce `target`) included on every request message. */
|
|
336
|
+
target?: string;
|
|
337
|
+
/** Per-request timeout in ms. Default 30000. */
|
|
338
|
+
timeoutMs?: number;
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Create a CIP-0103 provider that talks the splice-wallet postMessage protocol
|
|
342
|
+
* over the page `window`, routed by `target`.
|
|
343
|
+
*/
|
|
344
|
+
declare function createExtensionChannelProvider(options?: ExtensionChannelOptions): CIP0103Provider;
|
|
345
|
+
|
|
264
346
|
/**
|
|
265
347
|
* Async Wallet Handling
|
|
266
348
|
*
|
|
@@ -386,4 +468,4 @@ declare class MethodRouter {
|
|
|
386
468
|
private registerDefaults;
|
|
387
469
|
}
|
|
388
470
|
|
|
389
|
-
export { type AsyncConnectOptions, type AsyncPrepareExecuteOptions, CANTON_NETWORKS, CIP0103EventBus, type DiscoveredProvider, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, chainDisconnected, createProviderBridge, disconnected, discoverInjectedProviders, fromCAIP2Network, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, isValidCAIP2, methodNotFound, resourceNotFound, resourceUnavailable, toCAIP2Network, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForProvider };
|
|
471
|
+
export { type AnnounceDiscoveryOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CANTON_NETWORKS, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, fromCAIP2Network, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, isValidCAIP2, methodNotFound, resourceNotFound, resourceUnavailable, toCAIP2Network, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForProvider };
|
package/dist/index.js
CHANGED
|
@@ -540,7 +540,7 @@ function isValidCAIP2(networkId) {
|
|
|
540
540
|
}
|
|
541
541
|
|
|
542
542
|
// src/bridge.ts
|
|
543
|
-
var PROVIDER_VERSION = "0.
|
|
543
|
+
var PROVIDER_VERSION = "0.2.0" ;
|
|
544
544
|
function createProviderBridge(client) {
|
|
545
545
|
const eventBus = new CIP0103EventBus();
|
|
546
546
|
const pendingEmitted = /* @__PURE__ */ new Set();
|
|
@@ -819,6 +819,96 @@ function sessionToAccount(session) {
|
|
|
819
819
|
};
|
|
820
820
|
}
|
|
821
821
|
|
|
822
|
+
// src/extension-channel.ts
|
|
823
|
+
var SPLICE_WALLET_REQUEST = "SPLICE_WALLET_REQUEST";
|
|
824
|
+
var SPLICE_WALLET_RESPONSE = "SPLICE_WALLET_RESPONSE";
|
|
825
|
+
var channelSeq = 0;
|
|
826
|
+
function createExtensionChannelProvider(options = {}) {
|
|
827
|
+
const target = options.target;
|
|
828
|
+
const timeoutMs = options.timeoutMs ?? 3e4;
|
|
829
|
+
const eventBus = new CIP0103EventBus();
|
|
830
|
+
const channelId = `pl-${++channelSeq}-${target ?? "ext"}`;
|
|
831
|
+
let requestSeq = 0;
|
|
832
|
+
const pending = /* @__PURE__ */ new Map();
|
|
833
|
+
function onMessage(event) {
|
|
834
|
+
if (event.source !== window) return;
|
|
835
|
+
const selfOrigin = typeof window !== "undefined" ? window.location?.origin : void 0;
|
|
836
|
+
if (selfOrigin && selfOrigin !== "null" && event.origin !== selfOrigin) return;
|
|
837
|
+
const data = event.data;
|
|
838
|
+
if (!data || data.type !== SPLICE_WALLET_RESPONSE || !data.response) return;
|
|
839
|
+
const { id, result, error } = data.response;
|
|
840
|
+
if (id === void 0 || id === null) return;
|
|
841
|
+
const entry = pending.get(String(id));
|
|
842
|
+
if (!entry) return;
|
|
843
|
+
pending.delete(String(id));
|
|
844
|
+
clearTimeout(entry.timer);
|
|
845
|
+
if (error) {
|
|
846
|
+
entry.reject(
|
|
847
|
+
new ProviderRpcError(
|
|
848
|
+
error.message || "Wallet request failed",
|
|
849
|
+
typeof error.code === "number" ? error.code : JSON_RPC_ERRORS.INTERNAL_ERROR,
|
|
850
|
+
error.data
|
|
851
|
+
)
|
|
852
|
+
);
|
|
853
|
+
} else {
|
|
854
|
+
entry.resolve(result);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
if (typeof window !== "undefined") {
|
|
858
|
+
window.addEventListener("message", onMessage);
|
|
859
|
+
}
|
|
860
|
+
function postTargetOrigin() {
|
|
861
|
+
const origin = typeof window !== "undefined" ? window.location?.origin : void 0;
|
|
862
|
+
return origin && origin !== "null" ? origin : "*";
|
|
863
|
+
}
|
|
864
|
+
const provider = {
|
|
865
|
+
request(args) {
|
|
866
|
+
if (typeof window === "undefined") {
|
|
867
|
+
return Promise.reject(
|
|
868
|
+
new ProviderRpcError("No window: extension channel unavailable", JSON_RPC_ERRORS.INTERNAL_ERROR)
|
|
869
|
+
);
|
|
870
|
+
}
|
|
871
|
+
const id = `${channelId}-${++requestSeq}`;
|
|
872
|
+
return new Promise((resolve, reject) => {
|
|
873
|
+
const timer = setTimeout(() => {
|
|
874
|
+
pending.delete(id);
|
|
875
|
+
reject(
|
|
876
|
+
new ProviderRpcError(
|
|
877
|
+
`Wallet request "${args.method}" timed out after ${timeoutMs}ms`,
|
|
878
|
+
JSON_RPC_ERRORS.INTERNAL_ERROR
|
|
879
|
+
)
|
|
880
|
+
);
|
|
881
|
+
}, timeoutMs);
|
|
882
|
+
pending.set(id, { resolve, reject, timer });
|
|
883
|
+
const message = {
|
|
884
|
+
type: SPLICE_WALLET_REQUEST,
|
|
885
|
+
request: {
|
|
886
|
+
jsonrpc: "2.0",
|
|
887
|
+
id,
|
|
888
|
+
method: args.method,
|
|
889
|
+
...args.params !== void 0 ? { params: args.params } : {}
|
|
890
|
+
},
|
|
891
|
+
...target ? { target } : {}
|
|
892
|
+
};
|
|
893
|
+
window.postMessage(message, postTargetOrigin());
|
|
894
|
+
});
|
|
895
|
+
},
|
|
896
|
+
on(event, listener) {
|
|
897
|
+
eventBus.on(event, listener);
|
|
898
|
+
return provider;
|
|
899
|
+
},
|
|
900
|
+
emit(event, ...args) {
|
|
901
|
+
return eventBus.emit(event, ...args);
|
|
902
|
+
},
|
|
903
|
+
removeListener(event, listener) {
|
|
904
|
+
eventBus.removeListener(event, listener);
|
|
905
|
+
return provider;
|
|
906
|
+
}
|
|
907
|
+
};
|
|
908
|
+
eventBus.setOwner(provider);
|
|
909
|
+
return provider;
|
|
910
|
+
}
|
|
911
|
+
|
|
822
912
|
// src/discovery.ts
|
|
823
913
|
var KNOWN_INJECTION_PATHS = [
|
|
824
914
|
"canton",
|
|
@@ -892,6 +982,103 @@ function findById(id) {
|
|
|
892
982
|
(p) => p.id === id || p.id.endsWith(`.${id}`)
|
|
893
983
|
);
|
|
894
984
|
}
|
|
985
|
+
var CANTON_REQUEST_PROVIDER_EVENT = "canton:requestProvider";
|
|
986
|
+
var CANTON_ANNOUNCE_PROVIDER_EVENT = "canton:announceProvider";
|
|
987
|
+
function defaultAnnounceProvider(announced) {
|
|
988
|
+
return createExtensionChannelProvider({ target: announced.target });
|
|
989
|
+
}
|
|
990
|
+
async function discoverAnnouncedProviders(options = {}) {
|
|
991
|
+
if (typeof window === "undefined") return [];
|
|
992
|
+
const timeoutMs = options.timeoutMs ?? 300;
|
|
993
|
+
const make = options.createProvider ?? defaultAnnounceProvider;
|
|
994
|
+
const announced = /* @__PURE__ */ new Map();
|
|
995
|
+
const onAnnounce = (event) => {
|
|
996
|
+
const detail = event.detail;
|
|
997
|
+
if (!detail) return;
|
|
998
|
+
const rawId = detail.providerId ?? detail.id;
|
|
999
|
+
if (typeof rawId !== "string" || rawId.length === 0) return;
|
|
1000
|
+
if (announced.has(rawId)) return;
|
|
1001
|
+
announced.set(rawId, {
|
|
1002
|
+
id: rawId,
|
|
1003
|
+
name: typeof detail.name === "string" ? detail.name : void 0,
|
|
1004
|
+
icon: typeof detail.icon === "string" ? detail.icon : void 0,
|
|
1005
|
+
target: typeof detail.target === "string" ? detail.target : void 0
|
|
1006
|
+
});
|
|
1007
|
+
};
|
|
1008
|
+
window.addEventListener(
|
|
1009
|
+
CANTON_ANNOUNCE_PROVIDER_EVENT,
|
|
1010
|
+
onAnnounce
|
|
1011
|
+
);
|
|
1012
|
+
try {
|
|
1013
|
+
window.dispatchEvent(new CustomEvent(CANTON_REQUEST_PROVIDER_EVENT));
|
|
1014
|
+
await new Promise((resolve) => setTimeout(resolve, timeoutMs));
|
|
1015
|
+
} finally {
|
|
1016
|
+
window.removeEventListener(
|
|
1017
|
+
CANTON_ANNOUNCE_PROVIDER_EVENT,
|
|
1018
|
+
onAnnounce
|
|
1019
|
+
);
|
|
1020
|
+
}
|
|
1021
|
+
const results = [];
|
|
1022
|
+
for (const wallet of announced.values()) {
|
|
1023
|
+
let provider;
|
|
1024
|
+
try {
|
|
1025
|
+
provider = await make(wallet);
|
|
1026
|
+
} catch {
|
|
1027
|
+
continue;
|
|
1028
|
+
}
|
|
1029
|
+
if (!isCIP0103Provider(provider)) continue;
|
|
1030
|
+
results.push({
|
|
1031
|
+
id: wallet.id,
|
|
1032
|
+
provider,
|
|
1033
|
+
source: "injected",
|
|
1034
|
+
name: wallet.name,
|
|
1035
|
+
icon: wallet.icon
|
|
1036
|
+
});
|
|
1037
|
+
}
|
|
1038
|
+
return results;
|
|
1039
|
+
}
|
|
1040
|
+
var INJECTED_ID_PROBE_TIMEOUT_MS = 1500;
|
|
1041
|
+
function raceTimeout(promise, ms) {
|
|
1042
|
+
return Promise.race([
|
|
1043
|
+
promise,
|
|
1044
|
+
new Promise(
|
|
1045
|
+
(_, reject) => setTimeout(() => reject(new Error("id-probe timeout")), ms)
|
|
1046
|
+
)
|
|
1047
|
+
]);
|
|
1048
|
+
}
|
|
1049
|
+
async function resolveInjectedKey(d) {
|
|
1050
|
+
const sync = d.provider.id;
|
|
1051
|
+
if (typeof sync === "string" && sync.length > 0) return sync;
|
|
1052
|
+
try {
|
|
1053
|
+
const status = await raceTimeout(
|
|
1054
|
+
d.provider.request({ method: "status" }),
|
|
1055
|
+
INJECTED_ID_PROBE_TIMEOUT_MS
|
|
1056
|
+
);
|
|
1057
|
+
const pid = status?.provider?.id;
|
|
1058
|
+
if (typeof pid === "string" && pid.length > 0) return pid;
|
|
1059
|
+
} catch {
|
|
1060
|
+
}
|
|
1061
|
+
return d.id;
|
|
1062
|
+
}
|
|
1063
|
+
async function discoverProviders(options = {}) {
|
|
1064
|
+
const injected = discoverInjectedProviders();
|
|
1065
|
+
const announcedResults = await discoverAnnouncedProviders(options);
|
|
1066
|
+
const injectedKeys = await Promise.all(injected.map(resolveInjectedKey));
|
|
1067
|
+
const out = [];
|
|
1068
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1069
|
+
injected.forEach((d, i) => {
|
|
1070
|
+
const key = injectedKeys[i];
|
|
1071
|
+
if (seen.has(key)) return;
|
|
1072
|
+
seen.add(key);
|
|
1073
|
+
out.push(d);
|
|
1074
|
+
});
|
|
1075
|
+
for (const d of announcedResults) {
|
|
1076
|
+
if (seen.has(d.id)) continue;
|
|
1077
|
+
seen.add(d.id);
|
|
1078
|
+
out.push(d);
|
|
1079
|
+
}
|
|
1080
|
+
return out;
|
|
1081
|
+
}
|
|
895
1082
|
|
|
896
1083
|
Object.defineProperty(exports, "CIP0103_EVENTS", {
|
|
897
1084
|
enumerable: true,
|
|
@@ -913,9 +1100,12 @@ exports.PartyLayerProvider = PartyLayerProvider;
|
|
|
913
1100
|
exports.ProviderRpcError = ProviderRpcError;
|
|
914
1101
|
exports.RPC_ERRORS = RPC_ERRORS;
|
|
915
1102
|
exports.chainDisconnected = chainDisconnected;
|
|
1103
|
+
exports.createExtensionChannelProvider = createExtensionChannelProvider;
|
|
916
1104
|
exports.createProviderBridge = createProviderBridge;
|
|
917
1105
|
exports.disconnected = disconnected;
|
|
1106
|
+
exports.discoverAnnouncedProviders = discoverAnnouncedProviders;
|
|
918
1107
|
exports.discoverInjectedProviders = discoverInjectedProviders;
|
|
1108
|
+
exports.discoverProviders = discoverProviders;
|
|
919
1109
|
exports.fromCAIP2Network = fromCAIP2Network;
|
|
920
1110
|
exports.handleAsyncConnect = handleAsyncConnect;
|
|
921
1111
|
exports.handleAsyncPrepareExecute = handleAsyncPrepareExecute;
|