@canton-network/dapp-sdk 1.1.0 → 1.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/dist/index.cjs CHANGED
@@ -6,6 +6,7 @@ var coreWalletUiComponents = require('@canton-network/core-wallet-ui-components'
6
6
  var coreWalletDiscovery = require('@canton-network/core-wallet-discovery');
7
7
  var coreRpcTransport = require('@canton-network/core-rpc-transport');
8
8
  var SignClient = require('@walletconnect/sign-client');
9
+ var uuid = require('uuid');
9
10
  var coreWalletDappRpcClient = require('@canton-network/core-wallet-dapp-rpc-client');
10
11
 
11
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -87,6 +88,31 @@ var clearAllLocalState = ({
87
88
  coreWalletUiComponents.popup.close();
88
89
  }
89
90
  };
91
+ var composeSIWXMessage = (params) => {
92
+ {
93
+ const networkName = "Canton";
94
+ return [
95
+ `${params.domain} wants you to sign in with your ${networkName} account:`,
96
+ params.accountAddress,
97
+ params.statement ? `
98
+ ${params.statement}
99
+ ` : "",
100
+ `URI: ${params.uri}`,
101
+ `Version: ${params.version}`,
102
+ `Chain ID: ${params.chainId}`,
103
+ `Nonce: ${params.nonce}`,
104
+ params.issuedAt && `Issued At: ${params.issuedAt}`,
105
+ params.expirationTime && `Expiration Time: ${params.expirationTime}`,
106
+ params.notBefore && `Not Before: ${params.notBefore}`,
107
+ params.requestId && `Request ID: ${params.requestId}`,
108
+ params.resources?.length && params.resources.reduce(
109
+ (acc, resource) => `${acc}
110
+ - ${resource}`,
111
+ "Resources:"
112
+ )
113
+ ].filter((line) => typeof line === "string").join("\n").trim();
114
+ }
115
+ };
90
116
 
91
117
  // src/client.ts
92
118
  var DappClient = class {
@@ -95,9 +121,6 @@ var DappClient = class {
95
121
  __publicField(this, "options");
96
122
  this.provider = provider;
97
123
  this.options = options;
98
- if (options.injectGlobal !== false) {
99
- coreProviderDapp.injectProvider(provider);
100
- }
101
124
  }
102
125
  // ── Provider access ───────────────────────────────────
103
126
  getProvider() {
@@ -122,6 +145,9 @@ var DappClient = class {
122
145
  params
123
146
  });
124
147
  }
148
+ async signMessage(params) {
149
+ return this.provider.request({ method: "signMessage", params });
150
+ }
125
151
  async ledgerApi(params) {
126
152
  return this.provider.request({ method: "ledgerApi", params });
127
153
  }
@@ -138,6 +164,9 @@ var DappClient = class {
138
164
  onTxChanged(listener) {
139
165
  this.provider.on("txChanged", listener);
140
166
  }
167
+ onMessageSignature(listener) {
168
+ this.provider.on("messageSignature", listener);
169
+ }
141
170
  removeOnStatusChanged(listener) {
142
171
  this.provider.removeListener("statusChanged", listener);
143
172
  }
@@ -153,6 +182,12 @@ var DappClient = class {
153
182
  removeOnTxChanged(listener) {
154
183
  this.provider.removeListener("txChanged", listener);
155
184
  }
185
+ removeOnMessageSignature(listener) {
186
+ this.provider.removeListener(
187
+ "messageSignature",
188
+ listener
189
+ );
190
+ }
156
191
  // ── Open wallet UI ─────────────────────────────────────
157
192
  async open() {
158
193
  const statusResult = await this.status();
@@ -266,52 +301,6 @@ var ExtensionAdapter = class {
266
301
  }
267
302
  };
268
303
 
269
- // src/adapter/injected-adapter.ts
270
- var InjectedAdapter = class {
271
- constructor(config) {
272
- __publicField(this, "providerId");
273
- __publicField(this, "name");
274
- __publicField(this, "type", "browser");
275
- __publicField(this, "icon");
276
- __publicField(this, "providerInstance");
277
- __publicField(this, "description");
278
- this.providerId = `browser:${config.id}`;
279
- this.name = config.name;
280
- this.icon = config.icon;
281
- this.providerInstance = config.provider;
282
- this.description = config.description;
283
- }
284
- getInfo() {
285
- return {
286
- providerId: this.providerId,
287
- name: this.name,
288
- type: this.type,
289
- description: this.description ?? "Connect via an injected CIP-103 provider (window namespace)",
290
- icon: this.icon
291
- };
292
- }
293
- async detect() {
294
- return true;
295
- }
296
- provider() {
297
- return this.providerInstance;
298
- }
299
- teardown() {
300
- }
301
- async restore() {
302
- try {
303
- const status2 = await this.providerInstance.request({
304
- method: "status"
305
- });
306
- if (status2.connection.isConnected) {
307
- return this.providerInstance;
308
- }
309
- } catch {
310
- }
311
- return null;
312
- }
313
- };
314
-
315
304
  // src/dapp-api/rpc-gen/index.ts
316
305
  function buildController(methods) {
317
306
  return {
@@ -327,7 +316,8 @@ function buildController(methods) {
327
316
  accountsChanged: methods.accountsChanged,
328
317
  getPrimaryAccount: methods.getPrimaryAccount,
329
318
  listAccounts: methods.listAccounts,
330
- txChanged: methods.txChanged
319
+ txChanged: methods.txChanged,
320
+ messageSignature: methods.messageSignature
331
321
  };
332
322
  }
333
323
  var rpc_gen_default = buildController;
@@ -453,13 +443,51 @@ var dappSDKController = (provider) => rpc_gen_default({
453
443
  getActiveNetwork: async () => provider.request({
454
444
  method: "getActiveNetwork"
455
445
  }),
456
- signMessage: async (params) => provider.request({
457
- method: "signMessage",
458
- params
459
- }),
446
+ signMessage: async (params) => {
447
+ const response = await provider.request({
448
+ method: "signMessage",
449
+ params
450
+ });
451
+ const { userUrl } = response;
452
+ coreWalletUiComponents.popup.open(userUrl);
453
+ const messageId = new URL(userUrl).searchParams.get("messageId");
454
+ if (!messageId) {
455
+ throw new Error(
456
+ "Remote signMessage userUrl is missing messageId query param"
457
+ );
458
+ }
459
+ return await new Promise((resolve, reject) => {
460
+ const timeout = withTimeout(
461
+ reject,
462
+ "Timed out waiting for message signing approval"
463
+ );
464
+ const listener = (event) => {
465
+ if (event.messageId !== messageId) return;
466
+ if (event.status === "pending") return;
467
+ provider.removeListener("messageSignature", listener);
468
+ clearTimeout(timeout);
469
+ if (event.status === "failed") {
470
+ reject({
471
+ status: "error",
472
+ error: 3 /* TransactionFailed */,
473
+ details: `Message signing failed for messageId ${event.messageId}.`
474
+ });
475
+ return;
476
+ }
477
+ resolve({ signature: event.signature });
478
+ };
479
+ provider.on(
480
+ "messageSignature",
481
+ listener
482
+ );
483
+ });
484
+ },
460
485
  getPrimaryAccount: async () => provider.request({
461
486
  method: "getPrimaryAccount"
462
- })
487
+ }),
488
+ messageSignature: function() {
489
+ throw new Error("Only for events.");
490
+ }
463
491
  });
464
492
 
465
493
  // src/adapter/remote-adapter.ts
@@ -621,6 +649,8 @@ var WalletConnectAdapter = class _WalletConnectAdapter {
621
649
  __publicField(this, "chainId");
622
650
  __publicField(this, "metadata");
623
651
  __publicField(this, "onUri");
652
+ __publicField(this, "onSignInWithCanton");
653
+ __publicField(this, "signInWithCanton");
624
654
  __publicField(this, "signClient", null);
625
655
  __publicField(this, "session", null);
626
656
  __publicField(this, "initPromise", null);
@@ -628,9 +658,11 @@ var WalletConnectAdapter = class _WalletConnectAdapter {
628
658
  __publicField(this, "listeners", {});
629
659
  __publicField(this, "eventBuffer", /* @__PURE__ */ new Map());
630
660
  this.projectId = config.projectId;
661
+ this.signInWithCanton = config.signInWithCanton;
631
662
  this.chainId = config.chainId ?? "canton:devnet";
632
663
  this.metadata = config.metadata;
633
664
  this.onUri = config.onUri;
665
+ this.onSignInWithCanton = config.onSignInWithCanton;
634
666
  }
635
667
  static create(config) {
636
668
  return new _WalletConnectAdapter(config);
@@ -837,6 +869,46 @@ var WalletConnectAdapter = class _WalletConnectAdapter {
837
869
  }
838
870
  this.session = await approval();
839
871
  this.setupSessionEvents();
872
+ if (this.signInWithCanton) {
873
+ const nonce = this.signInWithCanton.nonce || uuid.v4();
874
+ try {
875
+ const account = this.session?.namespaces?.canton?.accounts?.[0];
876
+ const address = decodeURIComponent(account?.split(":")[2]);
877
+ const chainId = account?.split(":")[1] ?? this.chainId;
878
+ const message = composeSIWXMessage({
879
+ ...this.signInWithCanton,
880
+ accountAddress: address,
881
+ chainId
882
+ });
883
+ const result = await this.walletConnectRequest("signMessage", {
884
+ message
885
+ });
886
+ this.onSignInWithCanton?.({
887
+ requestId: this.signInWithCanton.requestId,
888
+ nonce,
889
+ account,
890
+ chainId,
891
+ message,
892
+ signature: result.signature,
893
+ publicKey: result.publicKey
894
+ });
895
+ } catch (error) {
896
+ const err = error;
897
+ this.onSignInWithCanton?.({
898
+ requestId: this.signInWithCanton.requestId,
899
+ nonce,
900
+ account: "",
901
+ chainId: "",
902
+ message: "",
903
+ publicKey: "",
904
+ signature: "",
905
+ error: {
906
+ message: err.message,
907
+ code: -32603
908
+ }
909
+ });
910
+ }
911
+ }
840
912
  }
841
913
  async showUriInPopup(uri) {
842
914
  try {
@@ -865,48 +937,6 @@ var gateways_default = [
865
937
  rpcUrl: "http://localhost:3030/api/v0/dapp"
866
938
  }
867
939
  ];
868
-
869
- // src/injected-discovery.ts
870
- var DEFAULT_ROOTS = ["canton"];
871
- var isProviderLike = (obj) => {
872
- if (typeof obj !== "object" || obj === null) return false;
873
- const p = obj;
874
- return typeof p.request === "function" && typeof p.on === "function" && typeof p.emit === "function" && typeof p.removeListener === "function";
875
- };
876
- function discoverInjectedProviders(roots = DEFAULT_ROOTS) {
877
- if (typeof window === "undefined") return [];
878
- const discovered = [];
879
- const seen = /* @__PURE__ */ new Set();
880
- const win = window;
881
- for (const root of roots) {
882
- const candidate = win[root];
883
- if (candidate === void 0 || candidate === null) continue;
884
- if (isProviderLike(candidate) && !seen.has(candidate)) {
885
- seen.add(candidate);
886
- discovered.push({
887
- id: root,
888
- provider: candidate,
889
- sourceRoot: root
890
- });
891
- continue;
892
- }
893
- if (typeof candidate === "object") {
894
- for (const [key, value] of Object.entries(
895
- candidate
896
- )) {
897
- if (isProviderLike(value) && !seen.has(value)) {
898
- seen.add(value);
899
- discovered.push({
900
- id: `${root}.${key}`,
901
- provider: value,
902
- sourceRoot: root
903
- });
904
- }
905
- }
906
- }
907
- }
908
- return discovered;
909
- }
910
940
  async function requestAnnouncedProviders(options) {
911
941
  if (typeof window === "undefined") return [];
912
942
  const timeoutMs = 300;
@@ -967,25 +997,6 @@ var DappSDK = class {
967
997
  }
968
998
  }
969
999
  }
970
- async registerInjectedNamespaceAdapters(discovery) {
971
- const existingIds = new Set(
972
- discovery.listAdapters().map((a) => a.providerId)
973
- );
974
- const injected = discoverInjectedProviders();
975
- for (const item of injected) {
976
- const id = `browser:${item.id}`;
977
- if (existingIds.has(id)) continue;
978
- const key = `${item.id} (injected)`;
979
- const adapter = new InjectedAdapter({
980
- id: item.id,
981
- name: key,
982
- provider: item.provider,
983
- description: `Injected provider from window.${item.id}`
984
- });
985
- discovery.registerAdapter(adapter);
986
- existingIds.add(id);
987
- }
988
- }
989
1000
  async registerAnnouncedAdapters(discovery) {
990
1001
  const existingIds = new Set(
991
1002
  discovery.listAdapters().map((a) => a.providerId)
@@ -1018,7 +1029,6 @@ var DappSDK = class {
1018
1029
  } else {
1019
1030
  await this.registerAdapters(this.discovery, initAdapters);
1020
1031
  }
1021
- await this.registerInjectedNamespaceAdapters(this.discovery);
1022
1032
  await this.registerAnnouncedAdapters(this.discovery);
1023
1033
  await this.discovery.restorePersistedSessionIfNeeded();
1024
1034
  if (!this.client) {
@@ -1149,7 +1159,6 @@ var DappSDK = class {
1149
1159
  await this.init();
1150
1160
  }
1151
1161
  const discovery = this.discovery;
1152
- await this.registerInjectedNamespaceAdapters(discovery);
1153
1162
  await this.registerAnnouncedAdapters(discovery);
1154
1163
  clearAllLocalState();
1155
1164
  const entries = discovery.listAdapters().filter((a) => !this.dynamicAdapterIds.has(a.providerId)).map((a) => {
@@ -1278,6 +1287,9 @@ var DappSDK = class {
1278
1287
  async prepareExecuteAndWait(params) {
1279
1288
  return this.requireClient().prepareExecuteAndWait(params);
1280
1289
  }
1290
+ async signMessage(params) {
1291
+ return this.requireClient().signMessage(params);
1292
+ }
1281
1293
  async ledgerApi(params) {
1282
1294
  return this.requireClient().ledgerApi(params);
1283
1295
  }
@@ -1296,6 +1308,9 @@ var DappSDK = class {
1296
1308
  async onTxChanged(listener) {
1297
1309
  this.requireClient().onTxChanged(listener);
1298
1310
  }
1311
+ async onMessageSignature(listener) {
1312
+ this.requireClient().onMessageSignature(listener);
1313
+ }
1299
1314
  async removeOnStatusChanged(listener) {
1300
1315
  if (!this.client) return;
1301
1316
  this.client.removeOnStatusChanged(listener);
@@ -1312,6 +1327,10 @@ var DappSDK = class {
1312
1327
  if (!this.client) return;
1313
1328
  this.client.removeOnTxChanged(listener);
1314
1329
  }
1330
+ async removeOnMessageSignature(listener) {
1331
+ if (!this.client) return;
1332
+ this.client.removeOnMessageSignature(listener);
1333
+ }
1315
1334
  };
1316
1335
  var sdk = new DappSDK();
1317
1336
  function connect(options) {
@@ -1389,7 +1408,6 @@ exports.DappClient = DappClient;
1389
1408
  exports.DappSDK = DappSDK;
1390
1409
  exports.ErrorCode = ErrorCode;
1391
1410
  exports.ExtensionAdapter = ExtensionAdapter;
1392
- exports.InjectedAdapter = InjectedAdapter;
1393
1411
  exports.RemoteAdapter = RemoteAdapter;
1394
1412
  exports.WALLET_GATEWAY_ICON = WALLET_GATEWAY_ICON;
1395
1413
  exports.WalletConnectAdapter = WalletConnectAdapter;