@partylayer/sdk 0.11.0 → 0.12.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
@@ -614,12 +614,43 @@ declare function createPartyLayer(config: PartyLayerConfig): PartyLayerClient;
614
614
  *
615
615
  * KNOWN wallets (Console, Send, …) never use this class — the SDK's identity
616
616
  * bridge maps their announce id to their existing adapter via `providerDetection`.
617
+ *
618
+ * OPTIONAL CAPABILITIES (additive; wagmi's optional-method model): a registry
619
+ * announce entry (`adapter.transport: 'announce'`) can opt a wallet into the
620
+ * optional CIP-0103 surface via {@link AnnounceAdapterConfig}. Each optional
621
+ * method is assigned in the constructor ONLY when configured, so it stays
622
+ * `undefined` otherwise (feature-detection honest) and `getCapabilities()`
623
+ * derives from what is actually present. With NO config the behavior is exactly
624
+ * the baseline: 3 capabilities + a minimal session.
617
625
  */
618
626
 
619
627
  /** Canonical providerId prefix for an announced extension (provider.md: `browser:ext:<id>`). */
620
628
  declare const ANNOUNCED_WALLET_ID_PREFIX = "browser:ext:";
621
629
  /** Build the canonical SDK walletId for an announced extension id. */
622
630
  declare function announcedWalletId(announceId: string): WalletId;
631
+ /**
632
+ * Opt-in configuration for an announced wallet's OPTIONAL capabilities. Sourced
633
+ * from its registry entry (`adapter.transport: 'announce'`): `events` from
634
+ * `capabilities.events`, the rest from `adapter.config`. Every field is opt-in;
635
+ * an absent config is byte-identical to the baseline 3-capability adapter.
636
+ */
637
+ interface AnnounceAdapterConfig {
638
+ /** Enable `on()` — bridge the provider's CIP-0103 `txChanged` → adapter `txStatus`. */
639
+ events?: boolean;
640
+ /** Enable `restore()` — silent `status()`/`getPrimaryAccount()` probe + party-match. */
641
+ restore?: boolean;
642
+ /** Enable `ledgerApi()` — proxy the standard CIP-0103 `ledgerApi` call. */
643
+ ledgerApi?: boolean;
644
+ /** Populate the richer `session.metadata` on connect when the provider returns it. */
645
+ metadata?: boolean;
646
+ /**
647
+ * Optional error-translation hook. Given first crack at a thrown error: return
648
+ * an `Error` to surface it, or `undefined` to fall through to the SDK's
649
+ * built-in standard EIP-1193/-1474 mapping. A generic mechanism — not
650
+ * wallet-specific code.
651
+ */
652
+ mapError?: (err: unknown) => Error | undefined;
653
+ }
623
654
  interface GenericAnnounceAdapterArgs {
624
655
  /** The announced extension id (== announce `detail.id`). */
625
656
  announceId: string;
@@ -629,6 +660,14 @@ interface GenericAnnounceAdapterArgs {
629
660
  icon?: string;
630
661
  /** Target-scoped CIP-0103 provider built from the announce (the discovery result). */
631
662
  provider: CIP0103Provider;
663
+ /**
664
+ * Override the adapter's walletId. Defaults to `browser:ext:<announceId>` (the
665
+ * UNKNOWN-wallet case). For a KNOWN registry announce wallet, pass its registry
666
+ * walletId so the adapter registers under the id its picker entry resolves by.
667
+ */
668
+ walletId?: WalletId;
669
+ /** Optional opt-in capabilities (registry-driven). Absent ⇒ baseline behavior. */
670
+ config?: AnnounceAdapterConfig;
632
671
  }
633
672
  declare class GenericAnnounceAdapter implements WalletAdapter {
634
673
  readonly walletId: WalletId;
@@ -636,8 +675,16 @@ declare class GenericAnnounceAdapter implements WalletAdapter {
636
675
  /** Icon surfaced to the picker (announce detail). */
637
676
  readonly icon?: string;
638
677
  private readonly provider;
678
+ private readonly metadataEnabled;
679
+ private readonly mapError?;
680
+ restore?: WalletAdapter['restore'];
681
+ on?: WalletAdapter['on'];
682
+ ledgerApi?: WalletAdapter['ledgerApi'];
639
683
  constructor(args: GenericAnnounceAdapterArgs);
640
- /** Baseline CIP-0103 capabilities every announced wallet must implement. */
684
+ /**
685
+ * Capabilities: the baseline three plus whatever optional methods were
686
+ * configured (derived from actual presence, never advertised when absent).
687
+ */
641
688
  getCapabilities(): CapabilityKey[];
642
689
  /**
643
690
  * The adapter is only ever constructed for a wallet that just announced, so
@@ -648,6 +695,19 @@ declare class GenericAnnounceAdapter implements WalletAdapter {
648
695
  disconnect(): Promise<void>;
649
696
  signMessage(_ctx: AdapterContext, _session: Session, params: SignMessageParams): Promise<SignedMessage>;
650
697
  submitTransaction(_ctx: AdapterContext, _session: Session, params: SubmitTransactionParams): Promise<TxReceipt>;
698
+ /** events: bridge the provider's CIP-0103 `txChanged` push event → `txStatus`. */
699
+ private makeOn;
700
+ /** restore: silent status()/getPrimaryAccount() probe + expiry + party-match. */
701
+ private makeRestore;
702
+ /** ledgerApi: proxy the standard CIP-0103 ledgerApi call; shape to {response:string}. */
703
+ private makeLedgerApi;
704
+ /**
705
+ * Run an operation, giving the optional `mapError` hook first crack at any
706
+ * thrown error. With NO hook configured this is a direct pass-through (no
707
+ * try/catch) — byte-identical to the baseline, and the SDK's built-in standard
708
+ * error mapping still applies downstream.
709
+ */
710
+ private guarded;
651
711
  }
652
712
 
653
713
  /**
package/dist/index.d.ts CHANGED
@@ -614,12 +614,43 @@ declare function createPartyLayer(config: PartyLayerConfig): PartyLayerClient;
614
614
  *
615
615
  * KNOWN wallets (Console, Send, …) never use this class — the SDK's identity
616
616
  * bridge maps their announce id to their existing adapter via `providerDetection`.
617
+ *
618
+ * OPTIONAL CAPABILITIES (additive; wagmi's optional-method model): a registry
619
+ * announce entry (`adapter.transport: 'announce'`) can opt a wallet into the
620
+ * optional CIP-0103 surface via {@link AnnounceAdapterConfig}. Each optional
621
+ * method is assigned in the constructor ONLY when configured, so it stays
622
+ * `undefined` otherwise (feature-detection honest) and `getCapabilities()`
623
+ * derives from what is actually present. With NO config the behavior is exactly
624
+ * the baseline: 3 capabilities + a minimal session.
617
625
  */
618
626
 
619
627
  /** Canonical providerId prefix for an announced extension (provider.md: `browser:ext:<id>`). */
620
628
  declare const ANNOUNCED_WALLET_ID_PREFIX = "browser:ext:";
621
629
  /** Build the canonical SDK walletId for an announced extension id. */
622
630
  declare function announcedWalletId(announceId: string): WalletId;
631
+ /**
632
+ * Opt-in configuration for an announced wallet's OPTIONAL capabilities. Sourced
633
+ * from its registry entry (`adapter.transport: 'announce'`): `events` from
634
+ * `capabilities.events`, the rest from `adapter.config`. Every field is opt-in;
635
+ * an absent config is byte-identical to the baseline 3-capability adapter.
636
+ */
637
+ interface AnnounceAdapterConfig {
638
+ /** Enable `on()` — bridge the provider's CIP-0103 `txChanged` → adapter `txStatus`. */
639
+ events?: boolean;
640
+ /** Enable `restore()` — silent `status()`/`getPrimaryAccount()` probe + party-match. */
641
+ restore?: boolean;
642
+ /** Enable `ledgerApi()` — proxy the standard CIP-0103 `ledgerApi` call. */
643
+ ledgerApi?: boolean;
644
+ /** Populate the richer `session.metadata` on connect when the provider returns it. */
645
+ metadata?: boolean;
646
+ /**
647
+ * Optional error-translation hook. Given first crack at a thrown error: return
648
+ * an `Error` to surface it, or `undefined` to fall through to the SDK's
649
+ * built-in standard EIP-1193/-1474 mapping. A generic mechanism — not
650
+ * wallet-specific code.
651
+ */
652
+ mapError?: (err: unknown) => Error | undefined;
653
+ }
623
654
  interface GenericAnnounceAdapterArgs {
624
655
  /** The announced extension id (== announce `detail.id`). */
625
656
  announceId: string;
@@ -629,6 +660,14 @@ interface GenericAnnounceAdapterArgs {
629
660
  icon?: string;
630
661
  /** Target-scoped CIP-0103 provider built from the announce (the discovery result). */
631
662
  provider: CIP0103Provider;
663
+ /**
664
+ * Override the adapter's walletId. Defaults to `browser:ext:<announceId>` (the
665
+ * UNKNOWN-wallet case). For a KNOWN registry announce wallet, pass its registry
666
+ * walletId so the adapter registers under the id its picker entry resolves by.
667
+ */
668
+ walletId?: WalletId;
669
+ /** Optional opt-in capabilities (registry-driven). Absent ⇒ baseline behavior. */
670
+ config?: AnnounceAdapterConfig;
632
671
  }
633
672
  declare class GenericAnnounceAdapter implements WalletAdapter {
634
673
  readonly walletId: WalletId;
@@ -636,8 +675,16 @@ declare class GenericAnnounceAdapter implements WalletAdapter {
636
675
  /** Icon surfaced to the picker (announce detail). */
637
676
  readonly icon?: string;
638
677
  private readonly provider;
678
+ private readonly metadataEnabled;
679
+ private readonly mapError?;
680
+ restore?: WalletAdapter['restore'];
681
+ on?: WalletAdapter['on'];
682
+ ledgerApi?: WalletAdapter['ledgerApi'];
639
683
  constructor(args: GenericAnnounceAdapterArgs);
640
- /** Baseline CIP-0103 capabilities every announced wallet must implement. */
684
+ /**
685
+ * Capabilities: the baseline three plus whatever optional methods were
686
+ * configured (derived from actual presence, never advertised when absent).
687
+ */
641
688
  getCapabilities(): CapabilityKey[];
642
689
  /**
643
690
  * The adapter is only ever constructed for a wallet that just announced, so
@@ -648,6 +695,19 @@ declare class GenericAnnounceAdapter implements WalletAdapter {
648
695
  disconnect(): Promise<void>;
649
696
  signMessage(_ctx: AdapterContext, _session: Session, params: SignMessageParams): Promise<SignedMessage>;
650
697
  submitTransaction(_ctx: AdapterContext, _session: Session, params: SubmitTransactionParams): Promise<TxReceipt>;
698
+ /** events: bridge the provider's CIP-0103 `txChanged` push event → `txStatus`. */
699
+ private makeOn;
700
+ /** restore: silent status()/getPrimaryAccount() probe + expiry + party-match. */
701
+ private makeRestore;
702
+ /** ledgerApi: proxy the standard CIP-0103 ledgerApi call; shape to {response:string}. */
703
+ private makeLedgerApi;
704
+ /**
705
+ * Run an operation, giving the optional `mapError` hook first crack at any
706
+ * thrown error. With NO hook configured this is a direct pass-through (no
707
+ * try/catch) — byte-identical to the baseline, and the SDK's built-in standard
708
+ * error mapping still applies downstream.
709
+ */
710
+ private guarded;
651
711
  }
652
712
 
653
713
  /**
package/dist/index.js CHANGED
@@ -15,21 +15,54 @@ var ANNOUNCED_WALLET_ID_PREFIX = "browser:ext:";
15
15
  function announcedWalletId(announceId) {
16
16
  return core.toWalletId(`${ANNOUNCED_WALLET_ID_PREFIX}${announceId}`);
17
17
  }
18
+ function mapTxStatus(status) {
19
+ switch (status) {
20
+ case "pending":
21
+ return "pending";
22
+ case "signed":
23
+ return "submitted";
24
+ case "executed":
25
+ return "committed";
26
+ case "failed":
27
+ default:
28
+ return "failed";
29
+ }
30
+ }
18
31
  var GenericAnnounceAdapter = class {
19
32
  walletId;
20
33
  name;
21
34
  /** Icon surfaced to the picker (announce detail). */
22
35
  icon;
23
36
  provider;
37
+ metadataEnabled;
38
+ mapError;
39
+ // Optional WalletAdapter surface — assigned in the ctor ONLY when configured,
40
+ // so `getCapabilities()` and `'x' in adapter` feature-detection stay honest.
41
+ restore;
42
+ on;
43
+ ledgerApi;
24
44
  constructor(args) {
25
- this.walletId = announcedWalletId(args.announceId);
45
+ this.walletId = args.walletId ?? announcedWalletId(args.announceId);
26
46
  this.name = args.name && args.name.length > 0 ? args.name : `Canton Wallet (${args.announceId.slice(0, 6)}\u2026)`;
27
47
  this.icon = args.icon;
28
48
  this.provider = args.provider;
49
+ const config = args.config;
50
+ this.metadataEnabled = config?.metadata === true;
51
+ this.mapError = config?.mapError;
52
+ if (config?.events) this.on = this.makeOn();
53
+ if (config?.restore) this.restore = this.makeRestore();
54
+ if (config?.ledgerApi) this.ledgerApi = this.makeLedgerApi();
29
55
  }
30
- /** Baseline CIP-0103 capabilities every announced wallet must implement. */
56
+ /**
57
+ * Capabilities: the baseline three plus whatever optional methods were
58
+ * configured (derived from actual presence, never advertised when absent).
59
+ */
31
60
  getCapabilities() {
32
- return ["connect", "signMessage", "submitTransaction"];
61
+ const caps = ["connect", "signMessage", "submitTransaction"];
62
+ if (this.restore) caps.push("restore");
63
+ if (this.ledgerApi) caps.push("ledgerApi");
64
+ if (this.on) caps.push("events");
65
+ return caps;
33
66
  }
34
67
  /**
35
68
  * The adapter is only ever constructed for a wallet that just announced, so
@@ -39,25 +72,28 @@ var GenericAnnounceAdapter = class {
39
72
  return { installed: true, reason: "Announced via canton:announceProvider" };
40
73
  }
41
74
  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"
75
+ return this.guarded(async () => {
76
+ await this.provider.request({ method: "connect" });
77
+ const account = await this.provider.request({
78
+ method: "getPrimaryAccount"
50
79
  });
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() };
80
+ let status;
81
+ try {
82
+ status = await this.provider.request({ method: "status" });
83
+ } catch {
84
+ }
85
+ const reportedNetwork = status?.network?.networkId;
86
+ const partyId = core.toPartyId(account.partyId);
87
+ const session = {
88
+ walletId: this.walletId,
89
+ partyId,
90
+ network: reportedNetwork ?? account.networkId ?? ctx.network
91
+ };
92
+ if (this.metadataEnabled) {
93
+ session.metadata = buildMetadata(status, account);
94
+ }
95
+ return { partyId, session, capabilities: this.getCapabilities() };
96
+ });
61
97
  }
62
98
  async disconnect() {
63
99
  try {
@@ -66,18 +102,108 @@ var GenericAnnounceAdapter = class {
66
102
  }
67
103
  }
68
104
  async signMessage(_ctx, _session, params) {
69
- return this.provider.request({
70
- method: "signMessage",
71
- params: { message: params.message }
72
- });
105
+ return this.guarded(
106
+ () => this.provider.request({
107
+ method: "signMessage",
108
+ params: { message: params.message }
109
+ })
110
+ );
73
111
  }
74
112
  async submitTransaction(_ctx, _session, params) {
75
- return this.provider.request({
76
- method: "prepareExecute",
77
- params
113
+ return this.guarded(
114
+ () => this.provider.request({
115
+ method: "prepareExecute",
116
+ params
117
+ })
118
+ );
119
+ }
120
+ // ── Optional-method factories (assigned only when configured) ──────────────
121
+ /** events: bridge the provider's CIP-0103 `txChanged` push event → `txStatus`. */
122
+ makeOn() {
123
+ return (event, handler) => {
124
+ if (event !== "txStatus") return () => {
125
+ };
126
+ const listener = (...args) => {
127
+ const tx = args[0];
128
+ if (!tx) return;
129
+ handler({ status: mapTxStatus(tx.status), commandId: tx.commandId, raw: tx });
130
+ };
131
+ try {
132
+ this.provider.on("txChanged", listener);
133
+ } catch {
134
+ return () => {
135
+ };
136
+ }
137
+ return () => this.provider.removeListener("txChanged", listener);
138
+ };
139
+ }
140
+ /** restore: silent status()/getPrimaryAccount() probe + expiry + party-match. */
141
+ makeRestore() {
142
+ return (_ctx, persisted) => this.guarded(async () => {
143
+ if (persisted.expiresAt && Date.now() >= persisted.expiresAt) return null;
144
+ let status;
145
+ try {
146
+ status = await this.provider.request({ method: "status" });
147
+ } catch {
148
+ }
149
+ const connected = status?.isConnected ?? status?.connection?.isConnected;
150
+ if (connected === false) return null;
151
+ const account = await this.provider.request({
152
+ method: "getPrimaryAccount"
153
+ });
154
+ if (account.partyId !== persisted.partyId) return null;
155
+ return {
156
+ ...persisted,
157
+ walletId: this.walletId,
158
+ metadata: { ...persisted.metadata ?? {}, ...buildMetadata(status, account) }
159
+ };
160
+ });
161
+ }
162
+ /** ledgerApi: proxy the standard CIP-0103 ledgerApi call; shape to {response:string}. */
163
+ makeLedgerApi() {
164
+ return (_ctx, _session, params) => this.guarded(async () => {
165
+ const result = await this.provider.request({
166
+ method: "ledgerApi",
167
+ params: {
168
+ requestMethod: params.requestMethod,
169
+ resource: params.resource,
170
+ body: params.body
171
+ }
172
+ });
173
+ if (result && typeof result.response === "string") return { response: result.response };
174
+ return { response: JSON.stringify(result ?? null) };
78
175
  });
79
176
  }
177
+ /**
178
+ * Run an operation, giving the optional `mapError` hook first crack at any
179
+ * thrown error. With NO hook configured this is a direct pass-through (no
180
+ * try/catch) — byte-identical to the baseline, and the SDK's built-in standard
181
+ * error mapping still applies downstream.
182
+ */
183
+ async guarded(op) {
184
+ if (!this.mapError) return op();
185
+ try {
186
+ return await op();
187
+ } catch (err) {
188
+ const mapped = this.mapError(err);
189
+ throw mapped ?? err;
190
+ }
191
+ }
80
192
  };
193
+ function buildMetadata(status, account) {
194
+ const meta = {};
195
+ const put = (k, v) => {
196
+ if (typeof v === "string" && v.length > 0) meta[k] = v;
197
+ };
198
+ put("publicKey", account.publicKey);
199
+ put("namespace", account.namespace);
200
+ put("networkId", account.networkId);
201
+ put("signingProviderId", account.signingProviderId);
202
+ put("hint", account.hint);
203
+ put("ledgerApiBaseUrl", status?.network?.ledgerApi?.baseUrl);
204
+ put("userId", status?.session?.userId);
205
+ return meta;
206
+ }
81
207
  var GenericDiscoveryAdapter = class {
82
208
  walletId;
83
209
  name;
@@ -580,6 +706,15 @@ function createTelemetryAdapter(config) {
580
706
  }
581
707
  var SESSION_STORAGE_KEY = "active_session";
582
708
  var WALLETS_CHANGED_DEBOUNCE_MS = 50;
709
+ function deriveAnnounceConfig(entry) {
710
+ const cfg = entry.adapter?.config ?? {};
711
+ return {
712
+ events: entry.capabilities?.events === true,
713
+ restore: cfg.restore === true,
714
+ ledgerApi: cfg.ledgerApi === true,
715
+ metadata: cfg.metadata === true
716
+ };
717
+ }
583
718
  var PartyLayerClient = class {
584
719
  config;
585
720
  adapters = /* @__PURE__ */ new Map();
@@ -857,7 +992,25 @@ var PartyLayerClient = class {
857
992
  { provider: { id: d.id } },
858
993
  base
859
994
  );
860
- if (known) continue;
995
+ if (known) {
996
+ if (this.adapters.has(known.walletId)) continue;
997
+ try {
998
+ const entry = await this.registryClient.getWalletEntry(String(known.walletId));
999
+ if (entry?.adapter?.transport === "announce") {
1000
+ const configured = new GenericAnnounceAdapter({
1001
+ announceId: d.id,
1002
+ walletId: known.walletId,
1003
+ name: d.name ?? known.name,
1004
+ icon: d.icon,
1005
+ provider: d.provider,
1006
+ config: deriveAnnounceConfig(entry)
1007
+ });
1008
+ this.adapters.set(configured.walletId, configured);
1009
+ }
1010
+ } catch {
1011
+ }
1012
+ continue;
1013
+ }
861
1014
  const adapter = new GenericAnnounceAdapter({
862
1015
  announceId: d.id,
863
1016
  name: d.name,