@partylayer/sdk 0.11.0 → 0.12.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/dist/index.mjs CHANGED
@@ -21,21 +21,54 @@ var ANNOUNCED_WALLET_ID_PREFIX = "browser:ext:";
21
21
  function announcedWalletId(announceId) {
22
22
  return toWalletId(`${ANNOUNCED_WALLET_ID_PREFIX}${announceId}`);
23
23
  }
24
+ function mapTxStatus(status) {
25
+ switch (status) {
26
+ case "pending":
27
+ return "pending";
28
+ case "signed":
29
+ return "submitted";
30
+ case "executed":
31
+ return "committed";
32
+ case "failed":
33
+ default:
34
+ return "failed";
35
+ }
36
+ }
24
37
  var GenericAnnounceAdapter = class {
25
38
  walletId;
26
39
  name;
27
40
  /** Icon surfaced to the picker (announce detail). */
28
41
  icon;
29
42
  provider;
43
+ metadataEnabled;
44
+ mapError;
45
+ // Optional WalletAdapter surface — assigned in the ctor ONLY when configured,
46
+ // so `getCapabilities()` and `'x' in adapter` feature-detection stay honest.
47
+ restore;
48
+ on;
49
+ ledgerApi;
30
50
  constructor(args) {
31
- this.walletId = announcedWalletId(args.announceId);
51
+ this.walletId = args.walletId ?? announcedWalletId(args.announceId);
32
52
  this.name = args.name && args.name.length > 0 ? args.name : `Canton Wallet (${args.announceId.slice(0, 6)}\u2026)`;
33
53
  this.icon = args.icon;
34
54
  this.provider = args.provider;
55
+ const config = args.config;
56
+ this.metadataEnabled = config?.metadata === true;
57
+ this.mapError = config?.mapError;
58
+ if (config?.events) this.on = this.makeOn();
59
+ if (config?.restore) this.restore = this.makeRestore();
60
+ if (config?.ledgerApi) this.ledgerApi = this.makeLedgerApi();
35
61
  }
36
- /** Baseline CIP-0103 capabilities every announced wallet must implement. */
62
+ /**
63
+ * Capabilities: the baseline three plus whatever optional methods were
64
+ * configured (derived from actual presence, never advertised when absent).
65
+ */
37
66
  getCapabilities() {
38
- return ["connect", "signMessage", "submitTransaction"];
67
+ const caps = ["connect", "signMessage", "submitTransaction"];
68
+ if (this.restore) caps.push("restore");
69
+ if (this.ledgerApi) caps.push("ledgerApi");
70
+ if (this.on) caps.push("events");
71
+ return caps;
39
72
  }
40
73
  /**
41
74
  * The adapter is only ever constructed for a wallet that just announced, so
@@ -45,25 +78,28 @@ var GenericAnnounceAdapter = class {
45
78
  return { installed: true, reason: "Announced via canton:announceProvider" };
46
79
  }
47
80
  async connect(ctx) {
48
- await this.provider.request({ method: "connect" });
49
- const account = await this.provider.request({
50
- method: "getPrimaryAccount"
51
- });
52
- let reportedNetwork;
53
- try {
54
- const status = await this.provider.request({
55
- method: "status"
81
+ return this.guarded(async () => {
82
+ await this.provider.request({ method: "connect" });
83
+ const account = await this.provider.request({
84
+ method: "getPrimaryAccount"
56
85
  });
57
- reportedNetwork = status?.network?.networkId;
58
- } catch {
59
- }
60
- const partyId = toPartyId(account.partyId);
61
- const session = {
62
- walletId: this.walletId,
63
- partyId,
64
- network: reportedNetwork ?? account.networkId ?? ctx.network
65
- };
66
- return { partyId, session, capabilities: this.getCapabilities() };
86
+ let status;
87
+ try {
88
+ status = await this.provider.request({ method: "status" });
89
+ } catch {
90
+ }
91
+ const reportedNetwork = status?.network?.networkId;
92
+ const partyId = toPartyId(account.partyId);
93
+ const session = {
94
+ walletId: this.walletId,
95
+ partyId,
96
+ network: reportedNetwork ?? account.networkId ?? ctx.network
97
+ };
98
+ if (this.metadataEnabled) {
99
+ session.metadata = buildMetadata(status, account);
100
+ }
101
+ return { partyId, session, capabilities: this.getCapabilities() };
102
+ });
67
103
  }
68
104
  async disconnect() {
69
105
  try {
@@ -72,18 +108,109 @@ var GenericAnnounceAdapter = class {
72
108
  }
73
109
  }
74
110
  async signMessage(_ctx, _session, params) {
75
- return this.provider.request({
76
- method: "signMessage",
77
- params: { message: params.message }
78
- });
111
+ return this.guarded(
112
+ () => this.provider.request({
113
+ method: "signMessage",
114
+ params: { message: params.message }
115
+ })
116
+ );
79
117
  }
80
118
  async submitTransaction(_ctx, _session, params) {
81
- return this.provider.request({
82
- method: "prepareExecute",
83
- params
119
+ return this.guarded(
120
+ () => this.provider.request({
121
+ method: "prepareExecute",
122
+ params
123
+ })
124
+ );
125
+ }
126
+ // ── Optional-method factories (assigned only when configured) ──────────────
127
+ /** events: bridge the provider's CIP-0103 `txChanged` push event → `txStatus`. */
128
+ makeOn() {
129
+ return (event, handler) => {
130
+ if (event !== "txStatus") return () => {
131
+ };
132
+ const listener = (...args) => {
133
+ const tx = args[0];
134
+ if (!tx) return;
135
+ handler({ status: mapTxStatus(tx.status), commandId: tx.commandId, raw: tx });
136
+ };
137
+ try {
138
+ this.provider.on("txChanged", listener);
139
+ } catch {
140
+ return () => {
141
+ };
142
+ }
143
+ return () => this.provider.removeListener("txChanged", listener);
144
+ };
145
+ }
146
+ /** restore: silent status()/getPrimaryAccount() probe + expiry + party-match. */
147
+ makeRestore() {
148
+ return (_ctx, persisted) => this.guarded(async () => {
149
+ if (persisted.expiresAt && Date.now() >= persisted.expiresAt) return null;
150
+ let status;
151
+ try {
152
+ status = await this.provider.request({ method: "status" });
153
+ } catch {
154
+ }
155
+ const connected = status?.isConnected ?? status?.connection?.isConnected;
156
+ if (connected === false) return null;
157
+ const account = await this.provider.request({
158
+ method: "getPrimaryAccount"
159
+ });
160
+ if (account.partyId !== persisted.partyId) return null;
161
+ return {
162
+ ...persisted,
163
+ walletId: this.walletId,
164
+ metadata: { ...persisted.metadata ?? {}, ...buildMetadata(status, account) }
165
+ };
166
+ });
167
+ }
168
+ /** ledgerApi: proxy the standard CIP-0103 ledgerApi call; shape to {response:string}. */
169
+ makeLedgerApi() {
170
+ return (_ctx, _session, params) => this.guarded(async () => {
171
+ const result = await this.provider.request({
172
+ method: "ledgerApi",
173
+ params: {
174
+ requestMethod: params.requestMethod,
175
+ resource: params.resource,
176
+ body: params.body
177
+ }
178
+ });
179
+ if (result && typeof result.response === "string") return { response: result.response };
180
+ return { response: JSON.stringify(result ?? null) };
84
181
  });
85
182
  }
183
+ /**
184
+ * Run an operation, giving the optional `mapError` hook first crack at any
185
+ * thrown error. With NO hook configured this is a direct pass-through (no
186
+ * try/catch) — byte-identical to the baseline, and the SDK's built-in standard
187
+ * error mapping still applies downstream.
188
+ */
189
+ async guarded(op) {
190
+ if (!this.mapError) return op();
191
+ try {
192
+ return await op();
193
+ } catch (err) {
194
+ const mapped = this.mapError(err);
195
+ throw mapped ?? err;
196
+ }
197
+ }
86
198
  };
199
+ function buildMetadata(status, account) {
200
+ const meta = {};
201
+ const put = (k, v) => {
202
+ if (typeof v === "string" && v.length > 0) meta[k] = v;
203
+ };
204
+ put("kernelId", status?.kernel?.id);
205
+ put("publicKey", account.publicKey);
206
+ put("namespace", account.namespace);
207
+ put("networkId", account.networkId);
208
+ put("signingProviderId", account.signingProviderId);
209
+ put("hint", account.hint);
210
+ put("ledgerApiBaseUrl", status?.network?.ledgerApi?.baseUrl);
211
+ put("userId", status?.session?.userId);
212
+ return meta;
213
+ }
87
214
  var GenericDiscoveryAdapter = class {
88
215
  walletId;
89
216
  name;
@@ -586,6 +713,15 @@ function createTelemetryAdapter(config) {
586
713
  }
587
714
  var SESSION_STORAGE_KEY = "active_session";
588
715
  var WALLETS_CHANGED_DEBOUNCE_MS = 50;
716
+ function deriveAnnounceConfig(entry) {
717
+ const cfg = entry.adapter?.config ?? {};
718
+ return {
719
+ events: entry.capabilities?.events === true,
720
+ restore: cfg.restore === true,
721
+ ledgerApi: cfg.ledgerApi === true,
722
+ metadata: cfg.metadata === true
723
+ };
724
+ }
589
725
  var PartyLayerClient = class {
590
726
  config;
591
727
  adapters = /* @__PURE__ */ new Map();
@@ -863,7 +999,25 @@ var PartyLayerClient = class {
863
999
  { provider: { id: d.id } },
864
1000
  base
865
1001
  );
866
- if (known) continue;
1002
+ if (known) {
1003
+ if (this.adapters.has(known.walletId)) continue;
1004
+ try {
1005
+ const entry = await this.registryClient.getWalletEntry(String(known.walletId));
1006
+ if (entry?.adapter?.transport === "announce") {
1007
+ const configured = new GenericAnnounceAdapter({
1008
+ announceId: d.id,
1009
+ walletId: known.walletId,
1010
+ name: d.name ?? known.name,
1011
+ icon: d.icon,
1012
+ provider: d.provider,
1013
+ config: deriveAnnounceConfig(entry)
1014
+ });
1015
+ this.adapters.set(configured.walletId, configured);
1016
+ }
1017
+ } catch {
1018
+ }
1019
+ continue;
1020
+ }
867
1021
  const adapter = new GenericAnnounceAdapter({
868
1022
  announceId: d.id,
869
1023
  name: d.name,