@partylayer/provider 0.1.7 → 0.2.1
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 +85 -34
- package/dist/index.d.ts +85 -34
- package/dist/index.js +213 -45
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +192 -39
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -5
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { PartyLayerError, CIP0103_EVENTS } from '@partylayer/core';
|
|
2
|
-
export { CIP0103_EVENTS, CIP0103_MANDATORY_METHODS, CIP0103_METHODS } from '@partylayer/core';
|
|
1
|
+
import { PartyLayerError, CIP0103_EVENTS, toCAIP2Network } from '@partylayer/core';
|
|
2
|
+
export { CANTON_NETWORKS, CIP0103_EVENTS, CIP0103_MANDATORY_METHODS, CIP0103_METHODS, fromCAIP2Network, isValidCAIP2, toCAIP2Network } from '@partylayer/core';
|
|
3
3
|
|
|
4
4
|
// src/provider.ts
|
|
5
5
|
|
|
@@ -164,6 +164,7 @@ var ERROR_CODE_TO_RPC = {
|
|
|
164
164
|
REGISTRY_VERIFICATION_FAILED: JSON_RPC_ERRORS.INTERNAL_ERROR,
|
|
165
165
|
REGISTRY_SCHEMA_INVALID: JSON_RPC_ERRORS.INTERNAL_ERROR,
|
|
166
166
|
INTERNAL_ERROR: JSON_RPC_ERRORS.INTERNAL_ERROR,
|
|
167
|
+
NETWORK_MISMATCH: JSON_RPC_ERRORS.INVALID_INPUT,
|
|
167
168
|
TIMEOUT: JSON_RPC_ERRORS.INVALID_INPUT
|
|
168
169
|
};
|
|
169
170
|
var RPC_TO_ERROR_CODE = {
|
|
@@ -503,43 +504,8 @@ var PartyLayerProvider = class {
|
|
|
503
504
|
}
|
|
504
505
|
};
|
|
505
506
|
|
|
506
|
-
// src/network.ts
|
|
507
|
-
var CANTON_NETWORKS = {
|
|
508
|
-
mainnet: "canton:da-mainnet",
|
|
509
|
-
testnet: "canton:da-testnet",
|
|
510
|
-
devnet: "canton:da-devnet",
|
|
511
|
-
local: "canton:da-local"
|
|
512
|
-
};
|
|
513
|
-
var CAIP2_TO_SHORT = Object.fromEntries(
|
|
514
|
-
Object.entries(CANTON_NETWORKS).map(([short, full]) => [full, short])
|
|
515
|
-
);
|
|
516
|
-
function toCAIP2Network(networkId) {
|
|
517
|
-
let result;
|
|
518
|
-
if (networkId.includes(":")) {
|
|
519
|
-
result = networkId;
|
|
520
|
-
} else {
|
|
521
|
-
const caip2 = CANTON_NETWORKS[networkId];
|
|
522
|
-
result = caip2 ?? `canton:${networkId}`;
|
|
523
|
-
}
|
|
524
|
-
if (!isValidCAIP2(result)) {
|
|
525
|
-
throw new Error(
|
|
526
|
-
`Invalid CAIP-2 network identifier: "${result}". Expected format: "namespace:reference"`
|
|
527
|
-
);
|
|
528
|
-
}
|
|
529
|
-
return { networkId: result };
|
|
530
|
-
}
|
|
531
|
-
function fromCAIP2Network(caip2) {
|
|
532
|
-
const short = CAIP2_TO_SHORT[caip2];
|
|
533
|
-
if (short) return short;
|
|
534
|
-
const colonIndex = caip2.indexOf(":");
|
|
535
|
-
return colonIndex >= 0 ? caip2.slice(colonIndex + 1) : caip2;
|
|
536
|
-
}
|
|
537
|
-
function isValidCAIP2(networkId) {
|
|
538
|
-
return /^[-a-z0-9]{3,8}:[-_a-zA-Z0-9]{1,32}$/.test(networkId);
|
|
539
|
-
}
|
|
540
|
-
|
|
541
507
|
// src/bridge.ts
|
|
542
|
-
var PROVIDER_VERSION = "0.1
|
|
508
|
+
var PROVIDER_VERSION = "0.2.1" ;
|
|
543
509
|
function createProviderBridge(client) {
|
|
544
510
|
const eventBus = new CIP0103EventBus();
|
|
545
511
|
const pendingEmitted = /* @__PURE__ */ new Set();
|
|
@@ -818,6 +784,96 @@ function sessionToAccount(session) {
|
|
|
818
784
|
};
|
|
819
785
|
}
|
|
820
786
|
|
|
787
|
+
// src/extension-channel.ts
|
|
788
|
+
var SPLICE_WALLET_REQUEST = "SPLICE_WALLET_REQUEST";
|
|
789
|
+
var SPLICE_WALLET_RESPONSE = "SPLICE_WALLET_RESPONSE";
|
|
790
|
+
var channelSeq = 0;
|
|
791
|
+
function createExtensionChannelProvider(options = {}) {
|
|
792
|
+
const target = options.target;
|
|
793
|
+
const timeoutMs = options.timeoutMs ?? 3e4;
|
|
794
|
+
const eventBus = new CIP0103EventBus();
|
|
795
|
+
const channelId = `pl-${++channelSeq}-${target ?? "ext"}`;
|
|
796
|
+
let requestSeq = 0;
|
|
797
|
+
const pending = /* @__PURE__ */ new Map();
|
|
798
|
+
function onMessage(event) {
|
|
799
|
+
if (event.source !== window) return;
|
|
800
|
+
const selfOrigin = typeof window !== "undefined" ? window.location?.origin : void 0;
|
|
801
|
+
if (selfOrigin && selfOrigin !== "null" && event.origin !== selfOrigin) return;
|
|
802
|
+
const data = event.data;
|
|
803
|
+
if (!data || data.type !== SPLICE_WALLET_RESPONSE || !data.response) return;
|
|
804
|
+
const { id, result, error } = data.response;
|
|
805
|
+
if (id === void 0 || id === null) return;
|
|
806
|
+
const entry = pending.get(String(id));
|
|
807
|
+
if (!entry) return;
|
|
808
|
+
pending.delete(String(id));
|
|
809
|
+
clearTimeout(entry.timer);
|
|
810
|
+
if (error) {
|
|
811
|
+
entry.reject(
|
|
812
|
+
new ProviderRpcError(
|
|
813
|
+
error.message || "Wallet request failed",
|
|
814
|
+
typeof error.code === "number" ? error.code : JSON_RPC_ERRORS.INTERNAL_ERROR,
|
|
815
|
+
error.data
|
|
816
|
+
)
|
|
817
|
+
);
|
|
818
|
+
} else {
|
|
819
|
+
entry.resolve(result);
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
if (typeof window !== "undefined") {
|
|
823
|
+
window.addEventListener("message", onMessage);
|
|
824
|
+
}
|
|
825
|
+
function postTargetOrigin() {
|
|
826
|
+
const origin = typeof window !== "undefined" ? window.location?.origin : void 0;
|
|
827
|
+
return origin && origin !== "null" ? origin : "*";
|
|
828
|
+
}
|
|
829
|
+
const provider = {
|
|
830
|
+
request(args) {
|
|
831
|
+
if (typeof window === "undefined") {
|
|
832
|
+
return Promise.reject(
|
|
833
|
+
new ProviderRpcError("No window: extension channel unavailable", JSON_RPC_ERRORS.INTERNAL_ERROR)
|
|
834
|
+
);
|
|
835
|
+
}
|
|
836
|
+
const id = `${channelId}-${++requestSeq}`;
|
|
837
|
+
return new Promise((resolve, reject) => {
|
|
838
|
+
const timer = setTimeout(() => {
|
|
839
|
+
pending.delete(id);
|
|
840
|
+
reject(
|
|
841
|
+
new ProviderRpcError(
|
|
842
|
+
`Wallet request "${args.method}" timed out after ${timeoutMs}ms`,
|
|
843
|
+
JSON_RPC_ERRORS.INTERNAL_ERROR
|
|
844
|
+
)
|
|
845
|
+
);
|
|
846
|
+
}, timeoutMs);
|
|
847
|
+
pending.set(id, { resolve, reject, timer });
|
|
848
|
+
const message = {
|
|
849
|
+
type: SPLICE_WALLET_REQUEST,
|
|
850
|
+
request: {
|
|
851
|
+
jsonrpc: "2.0",
|
|
852
|
+
id,
|
|
853
|
+
method: args.method,
|
|
854
|
+
...args.params !== void 0 ? { params: args.params } : {}
|
|
855
|
+
},
|
|
856
|
+
...target ? { target } : {}
|
|
857
|
+
};
|
|
858
|
+
window.postMessage(message, postTargetOrigin());
|
|
859
|
+
});
|
|
860
|
+
},
|
|
861
|
+
on(event, listener) {
|
|
862
|
+
eventBus.on(event, listener);
|
|
863
|
+
return provider;
|
|
864
|
+
},
|
|
865
|
+
emit(event, ...args) {
|
|
866
|
+
return eventBus.emit(event, ...args);
|
|
867
|
+
},
|
|
868
|
+
removeListener(event, listener) {
|
|
869
|
+
eventBus.removeListener(event, listener);
|
|
870
|
+
return provider;
|
|
871
|
+
}
|
|
872
|
+
};
|
|
873
|
+
eventBus.setOwner(provider);
|
|
874
|
+
return provider;
|
|
875
|
+
}
|
|
876
|
+
|
|
821
877
|
// src/discovery.ts
|
|
822
878
|
var KNOWN_INJECTION_PATHS = [
|
|
823
879
|
"canton",
|
|
@@ -891,7 +947,104 @@ function findById(id) {
|
|
|
891
947
|
(p) => p.id === id || p.id.endsWith(`.${id}`)
|
|
892
948
|
);
|
|
893
949
|
}
|
|
950
|
+
var CANTON_REQUEST_PROVIDER_EVENT = "canton:requestProvider";
|
|
951
|
+
var CANTON_ANNOUNCE_PROVIDER_EVENT = "canton:announceProvider";
|
|
952
|
+
function defaultAnnounceProvider(announced) {
|
|
953
|
+
return createExtensionChannelProvider({ target: announced.target });
|
|
954
|
+
}
|
|
955
|
+
async function discoverAnnouncedProviders(options = {}) {
|
|
956
|
+
if (typeof window === "undefined") return [];
|
|
957
|
+
const timeoutMs = options.timeoutMs ?? 300;
|
|
958
|
+
const make = options.createProvider ?? defaultAnnounceProvider;
|
|
959
|
+
const announced = /* @__PURE__ */ new Map();
|
|
960
|
+
const onAnnounce = (event) => {
|
|
961
|
+
const detail = event.detail;
|
|
962
|
+
if (!detail) return;
|
|
963
|
+
const rawId = detail.providerId ?? detail.id;
|
|
964
|
+
if (typeof rawId !== "string" || rawId.length === 0) return;
|
|
965
|
+
if (announced.has(rawId)) return;
|
|
966
|
+
announced.set(rawId, {
|
|
967
|
+
id: rawId,
|
|
968
|
+
name: typeof detail.name === "string" ? detail.name : void 0,
|
|
969
|
+
icon: typeof detail.icon === "string" ? detail.icon : void 0,
|
|
970
|
+
target: typeof detail.target === "string" ? detail.target : void 0
|
|
971
|
+
});
|
|
972
|
+
};
|
|
973
|
+
window.addEventListener(
|
|
974
|
+
CANTON_ANNOUNCE_PROVIDER_EVENT,
|
|
975
|
+
onAnnounce
|
|
976
|
+
);
|
|
977
|
+
try {
|
|
978
|
+
window.dispatchEvent(new CustomEvent(CANTON_REQUEST_PROVIDER_EVENT));
|
|
979
|
+
await new Promise((resolve) => setTimeout(resolve, timeoutMs));
|
|
980
|
+
} finally {
|
|
981
|
+
window.removeEventListener(
|
|
982
|
+
CANTON_ANNOUNCE_PROVIDER_EVENT,
|
|
983
|
+
onAnnounce
|
|
984
|
+
);
|
|
985
|
+
}
|
|
986
|
+
const results = [];
|
|
987
|
+
for (const wallet of announced.values()) {
|
|
988
|
+
let provider;
|
|
989
|
+
try {
|
|
990
|
+
provider = await make(wallet);
|
|
991
|
+
} catch {
|
|
992
|
+
continue;
|
|
993
|
+
}
|
|
994
|
+
if (!isCIP0103Provider(provider)) continue;
|
|
995
|
+
results.push({
|
|
996
|
+
id: wallet.id,
|
|
997
|
+
provider,
|
|
998
|
+
source: "injected",
|
|
999
|
+
name: wallet.name,
|
|
1000
|
+
icon: wallet.icon
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
return results;
|
|
1004
|
+
}
|
|
1005
|
+
var INJECTED_ID_PROBE_TIMEOUT_MS = 1500;
|
|
1006
|
+
function raceTimeout(promise, ms) {
|
|
1007
|
+
return Promise.race([
|
|
1008
|
+
promise,
|
|
1009
|
+
new Promise(
|
|
1010
|
+
(_, reject) => setTimeout(() => reject(new Error("id-probe timeout")), ms)
|
|
1011
|
+
)
|
|
1012
|
+
]);
|
|
1013
|
+
}
|
|
1014
|
+
async function resolveInjectedKey(d) {
|
|
1015
|
+
const sync = d.provider.id;
|
|
1016
|
+
if (typeof sync === "string" && sync.length > 0) return sync;
|
|
1017
|
+
try {
|
|
1018
|
+
const status = await raceTimeout(
|
|
1019
|
+
d.provider.request({ method: "status" }),
|
|
1020
|
+
INJECTED_ID_PROBE_TIMEOUT_MS
|
|
1021
|
+
);
|
|
1022
|
+
const pid = status?.provider?.id;
|
|
1023
|
+
if (typeof pid === "string" && pid.length > 0) return pid;
|
|
1024
|
+
} catch {
|
|
1025
|
+
}
|
|
1026
|
+
return d.id;
|
|
1027
|
+
}
|
|
1028
|
+
async function discoverProviders(options = {}) {
|
|
1029
|
+
const injected = discoverInjectedProviders();
|
|
1030
|
+
const announcedResults = await discoverAnnouncedProviders(options);
|
|
1031
|
+
const injectedKeys = await Promise.all(injected.map(resolveInjectedKey));
|
|
1032
|
+
const out = [];
|
|
1033
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1034
|
+
injected.forEach((d, i) => {
|
|
1035
|
+
const key = injectedKeys[i];
|
|
1036
|
+
if (seen.has(key)) return;
|
|
1037
|
+
seen.add(key);
|
|
1038
|
+
out.push(d);
|
|
1039
|
+
});
|
|
1040
|
+
for (const d of announcedResults) {
|
|
1041
|
+
if (seen.has(d.id)) continue;
|
|
1042
|
+
seen.add(d.id);
|
|
1043
|
+
out.push(d);
|
|
1044
|
+
}
|
|
1045
|
+
return out;
|
|
1046
|
+
}
|
|
894
1047
|
|
|
895
|
-
export {
|
|
1048
|
+
export { CIP0103EventBus, JSON_RPC_ERRORS, MethodRouter, PartyLayerProvider, 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 };
|
|
896
1049
|
//# sourceMappingURL=index.mjs.map
|
|
897
1050
|
//# sourceMappingURL=index.mjs.map
|