@partylayer/sdk 0.5.0 → 0.7.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.js CHANGED
@@ -11,6 +11,73 @@ var adapterSend = require('@partylayer/adapter-send');
11
11
  var adapterBron = require('@partylayer/adapter-bron');
12
12
 
13
13
  // src/client.ts
14
+ var ANNOUNCED_WALLET_ID_PREFIX = "browser:ext:";
15
+ function announcedWalletId(announceId) {
16
+ return core.toWalletId(`${ANNOUNCED_WALLET_ID_PREFIX}${announceId}`);
17
+ }
18
+ var GenericAnnounceAdapter = class {
19
+ walletId;
20
+ name;
21
+ /** Icon surfaced to the picker (announce detail). */
22
+ icon;
23
+ provider;
24
+ constructor(args) {
25
+ this.walletId = announcedWalletId(args.announceId);
26
+ this.name = args.name && args.name.length > 0 ? args.name : `Canton Wallet (${args.announceId.slice(0, 6)}\u2026)`;
27
+ this.icon = args.icon;
28
+ this.provider = args.provider;
29
+ }
30
+ /** Baseline CIP-0103 capabilities every announced wallet must implement. */
31
+ getCapabilities() {
32
+ return ["connect", "signMessage", "submitTransaction"];
33
+ }
34
+ /**
35
+ * The adapter is only ever constructed for a wallet that just announced, so
36
+ * its presence is established by the announce handshake itself.
37
+ */
38
+ async detectInstalled() {
39
+ return { installed: true, reason: "Announced via canton:announceProvider" };
40
+ }
41
+ async connect(ctx) {
42
+ await this.provider.request({ method: "connect" });
43
+ const account = await this.provider.request({
44
+ method: "getPrimaryAccount"
45
+ });
46
+ let reportedNetwork;
47
+ try {
48
+ const status = await this.provider.request({
49
+ method: "status"
50
+ });
51
+ reportedNetwork = status?.network?.networkId;
52
+ } catch {
53
+ }
54
+ const partyId = core.toPartyId(account.partyId);
55
+ const session = {
56
+ walletId: this.walletId,
57
+ partyId,
58
+ network: reportedNetwork ?? account.networkId ?? ctx.network
59
+ };
60
+ return { partyId, session, capabilities: this.getCapabilities() };
61
+ }
62
+ async disconnect() {
63
+ try {
64
+ await this.provider.request({ method: "disconnect" });
65
+ } catch {
66
+ }
67
+ }
68
+ async signMessage(_ctx, _session, params) {
69
+ return this.provider.request({
70
+ method: "signMessage",
71
+ params: { message: params.message }
72
+ });
73
+ }
74
+ async submitTransaction(_ctx, _session, params) {
75
+ return this.provider.request({
76
+ method: "prepareExecute",
77
+ params
78
+ });
79
+ }
80
+ };
14
81
 
15
82
  // src/config.ts
16
83
  var DEFAULT_REGISTRY_URL = "https://registry.partylayer.xyz";
@@ -373,6 +440,8 @@ var PartyLayerClient = class {
373
440
  crypto;
374
441
  storage;
375
442
  telemetry;
443
+ /** Cached announce-discovery picker entries (one-shot; cleared by refreshDiscovery). */
444
+ announceEntriesCache = null;
376
445
  origin;
377
446
  constructor(config) {
378
447
  this.config = config;
@@ -477,6 +546,7 @@ var PartyLayerClient = class {
477
546
  channel: "stable"
478
547
  });
479
548
  }
549
+ registryWallets = await this.aggregateAnnouncedWallets(registryWallets);
480
550
  if (filter?.requiredCapabilities) {
481
551
  return registryWallets.filter(
482
552
  (walletInfo) => filter.requiredCapabilities.every(
@@ -489,6 +559,106 @@ var PartyLayerClient = class {
489
559
  }
490
560
  return registryWallets;
491
561
  }
562
+ /**
563
+ * Whether announce discovery is active: explicit `discovery.announce`, else
564
+ * ON in the browser and OFF under SSR (no `window`). Canonical: provider.md.
565
+ */
566
+ get announceEnabled() {
567
+ if (typeof window === "undefined") return false;
568
+ return this.config.discovery?.announce ?? true;
569
+ }
570
+ /**
571
+ * Clear the cached announce round-trip so the next `listWallets()` re-runs the
572
+ * `canton:requestProvider` handshake (e.g. after a wallet is installed).
573
+ */
574
+ refreshDiscovery() {
575
+ this.announceEntriesCache = null;
576
+ }
577
+ /**
578
+ * Aggregate `canton:announceProvider` wallets into the picker list (A2).
579
+ *
580
+ * One-shot cached per client (refresh via {@link refreshDiscovery}); SSR-skipped.
581
+ * For each discovered provider (announce ∪ `window.canton` scan, deduped):
582
+ * - if its id matches a known wallet's `providerDetection` (provider.id) it
583
+ * IS that wallet — no new entry (identity bridge: Console's `lpnf…` → the
584
+ * `console` entry, Send's `ldmo…` → the `send` entry);
585
+ * - otherwise it is surfaced as a dynamic `browser:ext:<id>` entry routed to
586
+ * its own extension `target`, with a {@link GenericAnnounceAdapter}
587
+ * registered so a click connects through that target ONLY (no shared slot).
588
+ * With zero announcers this returns `base` unchanged.
589
+ */
590
+ async aggregateAnnouncedWallets(base) {
591
+ if (!this.announceEnabled) return base;
592
+ if (this.announceEntriesCache === null) {
593
+ try {
594
+ const discovered = await provider.discoverProviders({
595
+ timeoutMs: this.config.discovery?.announceTimeoutMs,
596
+ // Working provider over the announce target channel; G4 (provider.md):
597
+ // target defaults to id when omitted — never a shared slot.
598
+ createProvider: (a) => provider.createExtensionChannelProvider({ target: a.target ?? a.id })
599
+ });
600
+ const entries = [];
601
+ for (const d of discovered) {
602
+ if (d.identityResolved === false) continue;
603
+ const known = core.findMatchingWalletInfo(
604
+ { provider: { id: d.id } },
605
+ base
606
+ );
607
+ if (known) continue;
608
+ const adapter = new GenericAnnounceAdapter({
609
+ announceId: d.id,
610
+ name: d.name,
611
+ icon: d.icon,
612
+ provider: d.provider
613
+ });
614
+ this.adapters.set(adapter.walletId, adapter);
615
+ entries.push({
616
+ walletId: adapter.walletId,
617
+ name: adapter.name,
618
+ website: "",
619
+ icons: d.icon ? { sm: d.icon, md: d.icon, lg: d.icon } : {},
620
+ capabilities: adapter.getCapabilities(),
621
+ adapter: { packageName: "announced", versionRange: "*" },
622
+ docs: [],
623
+ networks: [this.config.network || "devnet"],
624
+ channel: "stable"
625
+ });
626
+ }
627
+ this.announceEntriesCache = entries;
628
+ } catch {
629
+ this.announceEntriesCache = [];
630
+ }
631
+ }
632
+ if (this.announceEntriesCache.length === 0) return base;
633
+ const ids = new Set(base.map((w) => String(w.walletId)));
634
+ return [
635
+ ...base,
636
+ ...this.announceEntriesCache.filter((w) => !ids.has(String(w.walletId)))
637
+ ];
638
+ }
639
+ /** Active network-enforcement policy (default 'guard'). */
640
+ get enforcement() {
641
+ return this.config.networkEnforcement ?? "guard";
642
+ }
643
+ /**
644
+ * Detect a confident network mismatch between the dApp's configured network
645
+ * and the session's (wallet-reported) network. Returns null when they match
646
+ * or the comparison isn't confident (conservative — never a false positive).
647
+ */
648
+ networkMismatch(session) {
649
+ return core.detectNetworkMismatch(this.config.network, session.network);
650
+ }
651
+ /**
652
+ * Guard a transaction-class operation: throw `NetworkMismatchError` when the
653
+ * session is on the wrong network AND the policy enforces it ('guard' |
654
+ * 'strict'). Also protects restored sessions and mid-session network switches.
655
+ */
656
+ assertNetworkOk(session) {
657
+ const mm = this.networkMismatch(session);
658
+ if (mm && this.enforcement !== "off") {
659
+ throw new core.NetworkMismatchError(mm.expected, mm.actual);
660
+ }
661
+ }
492
662
  /**
493
663
  * Connect to a wallet
494
664
  */
@@ -582,13 +752,31 @@ var PartyLayerClient = class {
582
752
  sessionId: core.toSessionId(`session_${Date.now()}_${Math.random().toString(36).substring(2, 15)}`),
583
753
  walletId: selectedWallet.walletId,
584
754
  partyId: result.partyId,
585
- network: this.config.network,
755
+ // The wallet's reported network (adapters that read the live wallet —
756
+ // e.g. Console via getActiveNetwork — surface the actual network here;
757
+ // echo-only adapters report ctx.network === config.network). Used for
758
+ // mismatch detection; falls back to the configured network.
759
+ network: result.session.network ?? this.config.network,
586
760
  createdAt: Date.now(),
587
761
  expiresAt: result.session.expiresAt,
588
762
  origin: this.origin,
589
763
  capabilitiesSnapshot: result.capabilities,
590
764
  metadata: result.session.metadata
591
765
  };
766
+ const mismatch = this.networkMismatch(session);
767
+ if (mismatch) {
768
+ session.networkMismatch = mismatch;
769
+ this.emit("session:networkMismatch", {
770
+ type: "session:networkMismatch",
771
+ sessionId: session.sessionId,
772
+ expected: mismatch.expected,
773
+ actual: mismatch.actual,
774
+ enforced: this.enforcement !== "off"
775
+ });
776
+ if (this.enforcement === "strict") {
777
+ throw new core.NetworkMismatchError(mismatch.expected, mismatch.actual);
778
+ }
779
+ }
592
780
  await this.persistSession(session);
593
781
  this.activeSession = session;
594
782
  this.updateRegistryStatus();
@@ -672,6 +860,7 @@ var PartyLayerClient = class {
672
860
  );
673
861
  }
674
862
  try {
863
+ this.assertNetworkOk(session);
675
864
  const ctx = this.createAdapterContext();
676
865
  return await adapter.signMessage(ctx, session, params);
677
866
  } catch (err) {
@@ -699,6 +888,7 @@ var PartyLayerClient = class {
699
888
  );
700
889
  }
701
890
  try {
891
+ this.assertNetworkOk(session);
702
892
  const ctx = this.createAdapterContext();
703
893
  const result = await adapter.signTransaction(ctx, session, params);
704
894
  this.emit("tx:status", {
@@ -734,6 +924,7 @@ var PartyLayerClient = class {
734
924
  );
735
925
  }
736
926
  try {
927
+ this.assertNetworkOk(session);
737
928
  const ctx = this.createAdapterContext();
738
929
  const result = await adapter.submitTransaction(ctx, session, params);
739
930
  this.emit("tx:status", {
@@ -769,6 +960,7 @@ var PartyLayerClient = class {
769
960
  );
770
961
  }
771
962
  try {
963
+ this.assertNetworkOk(session);
772
964
  const ctx = this.createAdapterContext();
773
965
  return await adapter.ledgerApi(ctx, session, params);
774
966
  } catch (err) {
@@ -1179,10 +1371,13 @@ Object.defineProperty(exports, "BronAdapter", {
1179
1371
  enumerable: true,
1180
1372
  get: function () { return adapterBron.BronAdapter; }
1181
1373
  });
1374
+ exports.ANNOUNCED_WALLET_ID_PREFIX = ANNOUNCED_WALLET_ID_PREFIX;
1182
1375
  exports.CantonConnectClient = PartyLayerClient;
1183
1376
  exports.DEFAULT_REGISTRY_URL = DEFAULT_REGISTRY_URL;
1377
+ exports.GenericAnnounceAdapter = GenericAnnounceAdapter;
1184
1378
  exports.MetricsTelemetryAdapter = MetricsTelemetryAdapter;
1185
1379
  exports.PartyLayerClient = PartyLayerClient;
1380
+ exports.announcedWalletId = announcedWalletId;
1186
1381
  exports.createCantonConnect = createPartyLayer;
1187
1382
  exports.createPartyLayer = createPartyLayer;
1188
1383
  exports.createTelemetryAdapter = createTelemetryAdapter;