@cartridge/controller 0.5.7 → 0.5.9

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.
Files changed (49) hide show
  1. package/.turbo/turbo-build$colon$deps.log +72 -67
  2. package/.turbo/turbo-build.log +73 -68
  3. package/dist/__tests__/parseChainId.test.d.ts +2 -0
  4. package/dist/__tests__/parseChainId.test.js +89 -0
  5. package/dist/__tests__/parseChainId.test.js.map +1 -0
  6. package/dist/account.d.ts +2 -2
  7. package/dist/account.js +4 -2
  8. package/dist/account.js.map +1 -1
  9. package/dist/controller.d.ts +8 -3
  10. package/dist/controller.js +111 -36
  11. package/dist/controller.js.map +1 -1
  12. package/dist/iframe/base.d.ts +1 -1
  13. package/dist/iframe/index.d.ts +1 -1
  14. package/dist/iframe/keychain.d.ts +1 -1
  15. package/dist/iframe/profile.d.ts +1 -1
  16. package/dist/index.d.ts +1 -1
  17. package/dist/index.js +128 -37
  18. package/dist/index.js.map +1 -1
  19. package/dist/provider.d.ts +5 -6
  20. package/dist/provider.js +28 -19
  21. package/dist/provider.js.map +1 -1
  22. package/dist/session/account.d.ts +1 -1
  23. package/dist/session/account.js +2 -0
  24. package/dist/session/account.js.map +1 -1
  25. package/dist/session/index.d.ts +1 -1
  26. package/dist/session/index.js +40 -22
  27. package/dist/session/index.js.map +1 -1
  28. package/dist/session/provider.d.ts +4 -3
  29. package/dist/session/provider.js +40 -22
  30. package/dist/session/provider.js.map +1 -1
  31. package/dist/telegram/provider.d.ts +4 -3
  32. package/dist/telegram/provider.js +40 -24
  33. package/dist/telegram/provider.js.map +1 -1
  34. package/dist/{types-BReKRAuh.d.ts → types-CVnDQVqD.d.ts} +10 -11
  35. package/dist/types.d.ts +1 -1
  36. package/dist/types.js.map +1 -1
  37. package/dist/utils.d.ts +3 -2
  38. package/dist/utils.js +25 -0
  39. package/dist/utils.js.map +1 -1
  40. package/jest.config.ts +13 -0
  41. package/package.json +7 -3
  42. package/src/__tests__/parseChainId.test.ts +60 -0
  43. package/src/account.ts +2 -1
  44. package/src/controller.ts +85 -15
  45. package/src/provider.ts +37 -23
  46. package/src/session/provider.ts +14 -4
  47. package/src/telegram/provider.ts +14 -5
  48. package/src/types.ts +11 -12
  49. package/src/utils.ts +28 -0
package/dist/index.js CHANGED
@@ -17,8 +17,10 @@ var ResponseCodes = /* @__PURE__ */ ((ResponseCodes2) => {
17
17
  import {
18
18
  addAddressPadding,
19
19
  CallData,
20
+ constants,
20
21
  getChecksumAddress,
21
22
  hash,
23
+ shortString,
22
24
  typedData,
23
25
  TypedDataRevision
24
26
  } from "starknet";
@@ -110,11 +112,33 @@ function toArray(val) {
110
112
  function humanizeString(str) {
111
113
  return str.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/_/g, " ").toLowerCase().replace(/^\w/, (c) => c.toUpperCase());
112
114
  }
115
+ function parseChainId(url) {
116
+ const parts = url.pathname.split("/");
117
+ if (parts.includes("starknet")) {
118
+ if (parts.includes("mainnet")) {
119
+ return constants.StarknetChainId.SN_MAIN;
120
+ } else if (parts.includes("sepolia")) {
121
+ return constants.StarknetChainId.SN_SEPOLIA;
122
+ }
123
+ } else if (parts.length >= 3) {
124
+ const projectName = parts[2];
125
+ if (parts.includes("katana")) {
126
+ return shortString.encodeShortString(
127
+ `WP_${projectName.toUpperCase().replace(/-/g, "_")}`
128
+ );
129
+ } else if (parts.includes("mainnet")) {
130
+ return shortString.encodeShortString(
131
+ `GG_${projectName.toUpperCase().replace(/-/g, "_")}`
132
+ );
133
+ }
134
+ }
135
+ throw new Error(`Chain ${url.toString()} not supported`);
136
+ }
113
137
 
114
138
  // src/account.ts
115
139
  var ControllerAccount = class extends WalletAccount {
116
- constructor(provider, address, keychain, options, modal) {
117
- super({ nodeUrl: provider.rpc.toString() }, provider);
140
+ constructor(provider, rpcUrl, address, keychain, options, modal) {
141
+ super({ nodeUrl: rpcUrl }, provider);
118
142
  this.address = address;
119
143
  this.keychain = keychain;
120
144
  this.options = options;
@@ -390,7 +414,7 @@ import {
390
414
  // package.json
391
415
  var package_default = {
392
416
  name: "@cartridge/controller",
393
- version: "0.5.7",
417
+ version: "0.5.9",
394
418
  description: "Cartridge Controller",
395
419
  module: "dist/index.js",
396
420
  types: "dist/index.d.ts",
@@ -400,6 +424,7 @@ var package_default = {
400
424
  build: "pnpm build:deps",
401
425
  format: 'prettier --write "src/**/*.ts"',
402
426
  "format:check": 'prettier --check "src/**/*.ts"',
427
+ test: "jest",
403
428
  version: "pnpm pkg get version"
404
429
  },
405
430
  exports: {
@@ -432,7 +457,10 @@ var package_default = {
432
457
  },
433
458
  devDependencies: {
434
459
  "@cartridge/tsconfig": "workspace:*",
460
+ "@types/jest": "^29.5.14",
435
461
  "@types/node": "^20.6.0",
462
+ jest: "^29.7.0",
463
+ "ts-jest": "^29.2.5",
436
464
  typescript: "^5.4.5"
437
465
  }
438
466
  };
@@ -442,7 +470,7 @@ var icon = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AA
442
470
 
443
471
  // src/provider.ts
444
472
  var BaseProvider = class {
445
- constructor(options) {
473
+ constructor() {
446
474
  this.id = "controller";
447
475
  this.name = "Controller";
448
476
  this.version = package_default.version;
@@ -460,8 +488,9 @@ var BaseProvider = class {
460
488
  if (this.account) {
461
489
  return [this.account.address];
462
490
  }
491
+ const silentMode = call.params && call.params.silent_mode;
463
492
  this.account = await this.probe();
464
- if (!this.account) {
493
+ if (!this.account && !silentMode) {
465
494
  this.account = await this.connect();
466
495
  }
467
496
  if (this.account) {
@@ -475,24 +504,20 @@ var BaseProvider = class {
475
504
  message: "An unexpected error occurred",
476
505
  data: "wallet_watchAsset not implemented"
477
506
  };
478
- case "wallet_addStarknetChain":
479
- throw {
480
- code: 63,
481
- message: "An unexpected error occurred",
482
- data: "wallet_addStarknetChain not implemented"
483
- };
484
- case "wallet_switchStarknetChain":
485
- throw {
486
- code: 63,
487
- message: "An unexpected error occurred",
488
- data: "wallet_switchStarknetChain not implemented"
489
- };
507
+ case "wallet_addStarknetChain": {
508
+ let params2 = call.params;
509
+ return this.addStarknetChain(params2);
510
+ }
511
+ case "wallet_switchStarknetChain": {
512
+ let params2 = call.params;
513
+ return this.switchStarknetChain(params2.chainId);
514
+ }
490
515
  case "wallet_requestChainId":
491
516
  if (!this.account) {
492
517
  throw {
493
518
  code: 63,
494
519
  message: "An unexpected error occurred",
495
- data: "wallet_deploymentData not implemented"
520
+ data: "Account not initialized"
496
521
  };
497
522
  }
498
523
  return await this.account.getChainId();
@@ -507,7 +532,7 @@ var BaseProvider = class {
507
532
  throw {
508
533
  code: 63,
509
534
  message: "An unexpected error occurred",
510
- data: "wallet_deploymentData not implemented"
535
+ data: "Account not initialized"
511
536
  };
512
537
  }
513
538
  let params = call.params;
@@ -563,16 +588,41 @@ var BaseProvider = class {
563
588
  this.subscriptions.splice(idx, 1);
564
589
  }
565
590
  };
566
- const { rpc } = options;
567
- this.rpc = new URL(rpc);
591
+ }
592
+ emitNetworkChanged(chainId) {
593
+ this.subscriptions.filter((sub) => sub.type === "networkChanged").forEach((sub) => {
594
+ sub.handler(chainId);
595
+ });
596
+ }
597
+ emitAccountsChanged(accounts) {
598
+ this.subscriptions.filter((sub) => sub.type === "accountsChanged").forEach((sub) => {
599
+ sub.handler(accounts);
600
+ });
568
601
  }
569
602
  };
570
603
 
571
604
  // src/controller.ts
572
605
  var ControllerProvider = class extends BaseProvider {
573
606
  constructor(options) {
574
- const { rpc } = options;
575
- super({ rpc });
607
+ super();
608
+ const chains = /* @__PURE__ */ new Map();
609
+ for (const chain of options.chains) {
610
+ const url = new URL(chain.rpcUrl);
611
+ const chainId = parseChainId(url);
612
+ chains.set(chainId, chain);
613
+ }
614
+ if (options.policies?.messages?.length && options.policies.messages.length !== chains.size) {
615
+ console.warn(
616
+ "Each message policy is associated with a specific chain. The number of message policies does not match the number of chains specified - session message signing may not work on some chains."
617
+ );
618
+ }
619
+ this.chains = chains;
620
+ this.selectedChain = options.defaultChainId;
621
+ if (!this.chains.has(this.selectedChain)) {
622
+ throw new Error(
623
+ `Chain ${this.selectedChain} not found in configured chains`
624
+ );
625
+ }
576
626
  this.iframes = {
577
627
  keychain: new KeychainIFrame({
578
628
  ...options,
@@ -594,11 +644,10 @@ var ControllerProvider = class extends BaseProvider {
594
644
  console.error(new NotReadyToConnect().message);
595
645
  return;
596
646
  }
597
- const response = await this.keychain.probe(
598
- this.rpc.toString()
599
- );
647
+ const response = await this.keychain.probe(this.rpcUrl());
600
648
  this.account = new account_default(
601
649
  this,
650
+ this.rpcUrl(),
602
651
  response.address,
603
652
  this.keychain,
604
653
  this.options,
@@ -620,7 +669,7 @@ var ControllerProvider = class extends BaseProvider {
620
669
  openPurchaseCredits: () => this.openPurchaseCredits.bind(this),
621
670
  openExecute: () => this.openExecute.bind(this)
622
671
  },
623
- rpcUrl: this.rpc.toString(),
672
+ rpcUrl: this.rpcUrl(),
624
673
  username,
625
674
  version: this.version
626
675
  });
@@ -644,8 +693,8 @@ var ControllerProvider = class extends BaseProvider {
644
693
  this.iframes.keychain.open();
645
694
  try {
646
695
  let response = await this.keychain.connect(
647
- this.options.policies || [],
648
- this.rpc.toString()
696
+ this.options.policies || {},
697
+ this.rpcUrl()
649
698
  );
650
699
  if (response.code !== "SUCCESS" /* SUCCESS */) {
651
700
  throw new Error(response.message);
@@ -653,6 +702,7 @@ var ControllerProvider = class extends BaseProvider {
653
702
  response = response;
654
703
  this.account = new account_default(
655
704
  this,
705
+ this.rpcUrl(),
656
706
  response.address,
657
707
  this.keychain,
658
708
  this.options,
@@ -665,6 +715,23 @@ var ControllerProvider = class extends BaseProvider {
665
715
  this.iframes.keychain.close();
666
716
  }
667
717
  }
718
+ async switchStarknetChain(chainId) {
719
+ try {
720
+ this.selectedChain = chainId;
721
+ this.account = await this.probe();
722
+ if (!this.account) {
723
+ this.account = await this.connect();
724
+ }
725
+ } catch (e) {
726
+ console.error(e);
727
+ return false;
728
+ }
729
+ this.emitNetworkChanged(chainId);
730
+ return true;
731
+ }
732
+ addStarknetChain(_chain) {
733
+ return Promise.resolve(true);
734
+ }
668
735
  async disconnect() {
669
736
  if (!this.keychain) {
670
737
  console.error(new NotReadyToConnect().message);
@@ -717,6 +784,9 @@ var ControllerProvider = class extends BaseProvider {
717
784
  }
718
785
  return this.keychain.revoke(origin);
719
786
  }
787
+ rpcUrl() {
788
+ return this.chains.get(this.selectedChain).rpcUrl;
789
+ }
720
790
  username() {
721
791
  if (!this.keychain) {
722
792
  console.error(new NotReadyToConnect().message);
@@ -743,7 +813,7 @@ var ControllerProvider = class extends BaseProvider {
743
813
  this.iframes.keychain.open();
744
814
  this.keychain.openPurchaseCredits();
745
815
  }
746
- async openExecute(calls) {
816
+ async openExecute(calls, chainId) {
747
817
  if (!this.keychain || !this.iframes.keychain) {
748
818
  console.error(new NotReadyToConnect().message);
749
819
  return;
@@ -752,16 +822,21 @@ var ControllerProvider = class extends BaseProvider {
752
822
  console.error("Profile is not ready");
753
823
  return;
754
824
  }
755
- if (this.iframes.profile?.sendBackward) {
756
- this.iframes.profile?.sendBackward();
757
- } else {
758
- this.iframes.profile?.close();
825
+ let currentChainId = this.selectedChain;
826
+ if (chainId) {
827
+ this.switchStarknetChain(chainId);
759
828
  }
829
+ this.iframes.profile?.sendBackward();
760
830
  this.iframes.keychain.open();
831
+ this.iframes.profile?.close();
761
832
  const res = await this.keychain.execute(calls, void 0, void 0, true);
833
+ this.iframes.profile?.open();
762
834
  this.iframes.keychain.close();
763
- this.iframes.profile?.sendForward?.();
764
- return !(res && res.code === "NOT_CONNECTED" /* NOT_CONNECTED */);
835
+ this.iframes.profile?.sendForward();
836
+ if (chainId) {
837
+ this.switchStarknetChain(currentChainId);
838
+ }
839
+ return !(res && (res.code === "NOT_CONNECTED" /* NOT_CONNECTED */ || res.code === "CANCELED" /* CANCELED */));
765
840
  }
766
841
  async delegateAccount() {
767
842
  if (!this.keychain) {
@@ -837,7 +912,7 @@ async function lookupAddresses(addresses) {
837
912
  );
838
913
  }
839
914
 
840
- // ../../node_modules/.pnpm/@cartridge+presets@https+++codeload.github.com+cartridge-gg+presets+tar.gz+570ecfe/node_modules/@cartridge/presets/dist/index.js
915
+ // ../../node_modules/.pnpm/@cartridge+presets@https+++codeload.github.com+cartridge-gg+presets+tar.gz+b0def0f/node_modules/@cartridge/presets/dist/index.js
841
916
  var configs = {
842
917
  "blob-arena": {
843
918
  origin: "blobarena.xyz",
@@ -1343,6 +1418,22 @@ var configs = {
1343
1418
  entrypoint: "set_approval_for_all"
1344
1419
  }
1345
1420
  ]
1421
+ },
1422
+ "0x4b5e65a9617c7ba3c7ea64324ff4338a400adb1a3cfe903b3f8b647cbb59fb7": {
1423
+ name: "Season Systems",
1424
+ description: "Register and claim",
1425
+ methods: [
1426
+ {
1427
+ name: "Register",
1428
+ description: "Registers to leaderboard",
1429
+ entrypoint: "register_to_leaderboard"
1430
+ },
1431
+ {
1432
+ name: "Claim",
1433
+ description: "Claim",
1434
+ entrypoint: "claim_leaderboard_rewards"
1435
+ }
1436
+ ]
1346
1437
  }
1347
1438
  },
1348
1439
  messages: [