@cartridge/controller 0.5.0 → 0.5.2

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 (67) hide show
  1. package/.turbo/turbo-build$colon$deps.log +116 -115
  2. package/.turbo/turbo-build.log +120 -0
  3. package/.turbo/turbo-format.log +25 -0
  4. package/dist/account.d.ts +2 -2
  5. package/dist/account.js +19 -4
  6. package/dist/account.js.map +1 -1
  7. package/dist/constants.d.ts +2 -1
  8. package/dist/constants.js +2 -0
  9. package/dist/constants.js.map +1 -1
  10. package/dist/controller.d.ts +2 -2
  11. package/dist/controller.js +28 -188
  12. package/dist/controller.js.map +1 -1
  13. package/dist/iframe/base.d.ts +2 -2
  14. package/dist/iframe/base.js +3 -177
  15. package/dist/iframe/base.js.map +1 -1
  16. package/dist/iframe/index.d.ts +2 -2
  17. package/dist/iframe/index.js +8 -180
  18. package/dist/iframe/index.js.map +1 -1
  19. package/dist/iframe/keychain.d.ts +2 -2
  20. package/dist/iframe/keychain.js +3 -177
  21. package/dist/iframe/keychain.js.map +1 -1
  22. package/dist/iframe/profile.d.ts +2 -2
  23. package/dist/iframe/profile.js +8 -180
  24. package/dist/iframe/profile.js.map +1 -1
  25. package/dist/index.d.ts +4 -2
  26. package/dist/index.js +167 -189
  27. package/dist/index.js.map +1 -1
  28. package/dist/lookup.d.ts +4 -0
  29. package/dist/lookup.js +56 -0
  30. package/dist/lookup.js.map +1 -0
  31. package/dist/provider.d.ts +2 -2
  32. package/dist/session/account.d.ts +2 -3
  33. package/dist/session/account.js +13 -171
  34. package/dist/session/account.js.map +1 -1
  35. package/dist/session/index.d.ts +2 -3
  36. package/dist/session/index.js +40 -172
  37. package/dist/session/index.js.map +1 -1
  38. package/dist/session/provider.d.ts +4 -4
  39. package/dist/session/provider.js +39 -3
  40. package/dist/session/provider.js.map +1 -1
  41. package/dist/telegram/provider.d.ts +7 -4
  42. package/dist/telegram/provider.js +39 -3
  43. package/dist/telegram/provider.js.map +1 -1
  44. package/dist/{types-BkXOAuzX.d.ts → types-1WsOoNO2.d.ts} +17 -30
  45. package/dist/types.d.ts +2 -2
  46. package/dist/types.js.map +1 -1
  47. package/dist/utils.d.ts +6 -1
  48. package/dist/utils.js +101 -3
  49. package/dist/utils.js.map +1 -1
  50. package/package.json +5 -4
  51. package/src/account.ts +2 -1
  52. package/src/constants.ts +1 -0
  53. package/src/controller.ts +2 -3
  54. package/src/iframe/base.ts +3 -9
  55. package/src/iframe/profile.ts +5 -3
  56. package/src/index.ts +3 -1
  57. package/src/lookup.ts +68 -0
  58. package/src/session/account.ts +0 -1
  59. package/src/session/index.ts +0 -2
  60. package/src/session/provider.ts +5 -4
  61. package/src/telegram/provider.ts +8 -7
  62. package/src/types.ts +25 -34
  63. package/src/utils.ts +122 -2
  64. package/dist/presets.d.ts +0 -9
  65. package/dist/presets.js +0 -170
  66. package/dist/presets.js.map +0 -1
  67. package/src/presets.ts +0 -167
package/dist/index.js CHANGED
@@ -1,6 +1,3 @@
1
- // src/controller.ts
2
- import { normalize } from "@cartridge/utils";
3
-
4
1
  // src/account.ts
5
2
  import {
6
3
  WalletAccount
@@ -16,6 +13,104 @@ var ResponseCodes = /* @__PURE__ */ ((ResponseCodes2) => {
16
13
  return ResponseCodes2;
17
14
  })(ResponseCodes || {});
18
15
 
16
+ // src/utils.ts
17
+ import {
18
+ addAddressPadding,
19
+ CallData,
20
+ getChecksumAddress,
21
+ hash,
22
+ typedData,
23
+ TypedDataRevision
24
+ } from "starknet";
25
+ var ALLOWED_PROPERTIES = /* @__PURE__ */ new Set([
26
+ "contracts",
27
+ "messages",
28
+ "target",
29
+ "method",
30
+ "name",
31
+ "description",
32
+ "types",
33
+ "domain",
34
+ "primaryType"
35
+ ]);
36
+ function validatePropertyName(prop) {
37
+ if (!ALLOWED_PROPERTIES.has(prop)) {
38
+ throw new Error(`Invalid property name: ${prop}`);
39
+ }
40
+ }
41
+ function safeObjectAccess(obj, prop) {
42
+ validatePropertyName(prop);
43
+ return obj[prop];
44
+ }
45
+ function toSessionPolicies(policies) {
46
+ return Array.isArray(policies) ? policies.reduce(
47
+ (prev, p) => {
48
+ if (safeObjectAccess(p, "target")) {
49
+ const target = getChecksumAddress(
50
+ safeObjectAccess(p, "target")
51
+ );
52
+ const entrypoint = safeObjectAccess(p, "method");
53
+ const contracts = safeObjectAccess(
54
+ prev,
55
+ "contracts"
56
+ );
57
+ const item = {
58
+ name: humanizeString(entrypoint),
59
+ entrypoint,
60
+ description: safeObjectAccess(p, "description")
61
+ };
62
+ if (target in contracts) {
63
+ const methods = toArray(contracts[target].methods);
64
+ contracts[target] = {
65
+ methods: [...methods, item]
66
+ };
67
+ } else {
68
+ contracts[target] = {
69
+ methods: [item]
70
+ };
71
+ }
72
+ } else {
73
+ const messages = safeObjectAccess(prev, "messages");
74
+ messages.push(p);
75
+ }
76
+ return prev;
77
+ },
78
+ { contracts: {}, messages: [] }
79
+ ) : policies;
80
+ }
81
+ function toWasmPolicies(policies) {
82
+ return [
83
+ ...Object.entries(policies.contracts ?? {}).flatMap(
84
+ ([target, { methods }]) => toArray(methods).map((m) => ({
85
+ target,
86
+ method: m.entrypoint
87
+ }))
88
+ ),
89
+ ...(policies.messages ?? []).map((p) => {
90
+ const domainHash = typedData.getStructHash(
91
+ p.types,
92
+ "StarknetDomain",
93
+ p.domain,
94
+ TypedDataRevision.ACTIVE
95
+ );
96
+ const typeHash = typedData.getTypeHash(
97
+ p.types,
98
+ p.primaryType,
99
+ TypedDataRevision.ACTIVE
100
+ );
101
+ return {
102
+ scope_hash: hash.computePoseidonHash(domainHash, typeHash)
103
+ };
104
+ })
105
+ ];
106
+ }
107
+ function toArray(val) {
108
+ return Array.isArray(val) ? val : [val];
109
+ }
110
+ function humanizeString(str) {
111
+ return str.replace(/([a-z])([A-Z])/g, "$1 $2").replace(/_/g, " ").toLowerCase().replace(/^\w/, (c) => c.toUpperCase());
112
+ }
113
+
19
114
  // src/account.ts
20
115
  var ControllerAccount = class extends WalletAccount {
21
116
  constructor(provider, address, keychain, options, modal) {
@@ -38,7 +133,7 @@ var ControllerAccount = class extends WalletAccount {
38
133
  * @returns response from addTransaction
39
134
  */
40
135
  async execute(calls) {
41
- calls = Array.isArray(calls) ? calls : [calls];
136
+ calls = toArray(calls);
42
137
  return new Promise(async (resolve, reject) => {
43
138
  const sessionExecute = await this.keychain.execute(
44
139
  calls,
@@ -79,15 +174,15 @@ var ControllerAccount = class extends WalletAccount {
79
174
  * @returns the signature of the JSON object
80
175
  * @throws {Error} if the JSON object is not a valid JSON
81
176
  */
82
- async signMessage(typedData) {
177
+ async signMessage(typedData2) {
83
178
  return new Promise(async (resolve, reject) => {
84
- const sessionSign = await this.keychain.signMessage(typedData, "", true);
179
+ const sessionSign = await this.keychain.signMessage(typedData2, "", true);
85
180
  if (!("code" in sessionSign)) {
86
181
  resolve(sessionSign);
87
182
  return;
88
183
  }
89
184
  this.modal.open();
90
- const manualSign = await this.keychain.signMessage(typedData, "", false);
185
+ const manualSign = await this.keychain.signMessage(typedData2, "", false);
91
186
  if (!("code" in manualSign)) {
92
187
  resolve(manualSign);
93
188
  } else {
@@ -101,175 +196,6 @@ var account_default = ControllerAccount;
101
196
 
102
197
  // src/iframe/base.ts
103
198
  import { connectToChild } from "@cartridge/penpal";
104
-
105
- // src/presets.ts
106
- var defaultPresets = {
107
- cartridge: {
108
- id: "cartridge",
109
- name: "Cartridge",
110
- icon: "/whitelabel/cartridge/icon.svg",
111
- cover: {
112
- light: "/whitelabel/cartridge/cover-light.png",
113
- dark: "/whitelabel/cartridge/cover-dark.png"
114
- }
115
- },
116
- "force-prime": {
117
- id: "force-prime",
118
- name: "Force Prime",
119
- icon: "/whitelabel/force-prime/icon.png",
120
- cover: "/whitelabel/force-prime/cover.png",
121
- colors: {
122
- primary: "#E1CC89"
123
- }
124
- },
125
- paved: {
126
- id: "paved",
127
- name: "Paved",
128
- icon: "/whitelabel/paved/icon.svg",
129
- cover: "/whitelabel/paved/cover.png",
130
- colors: {
131
- primary: "#B0CAF8"
132
- }
133
- },
134
- eternum: {
135
- id: "eternum",
136
- name: "Eternum",
137
- icon: "/whitelabel/eternum/icon.gif",
138
- cover: "/whitelabel/eternum/cover.png",
139
- colors: {
140
- primary: "#CE9822"
141
- }
142
- },
143
- pistols: {
144
- id: "pistols",
145
- name: "Pistols at Ten Blocks",
146
- icon: "/whitelabel/pistols/icon.png",
147
- cover: "/whitelabel/pistols/cover.png",
148
- colors: {
149
- primary: "#EF9758"
150
- }
151
- },
152
- pixelaw: {
153
- id: "pixelaw",
154
- name: "Pixelaw",
155
- icon: "/whitelabel/pixelaw/icon.svg",
156
- cover: "/whitelabel/pixelaw/cover.png",
157
- colors: {
158
- primary: "#7C00B1",
159
- primaryForeground: "white"
160
- }
161
- },
162
- "dope-wars": {
163
- id: "dope-wars",
164
- name: "Dope Wars",
165
- icon: "/whitelabel/dope-wars/icon.png",
166
- cover: "/whitelabel/dope-wars/cover.png",
167
- colors: {
168
- primary: "#11ED83"
169
- }
170
- },
171
- zkastle: {
172
- id: "zkastle",
173
- name: "zKastle",
174
- icon: "/whitelabel/zkastle/icon.svg",
175
- cover: "/whitelabel/zkastle/cover.png",
176
- colors: {
177
- primary: "#E50D2C"
178
- }
179
- },
180
- "loot-survivor": {
181
- id: "loot-survivor",
182
- name: "Loot Survivor",
183
- icon: "/whitelabel/loot-survivor/icon.png",
184
- cover: "/whitelabel/loot-survivor/cover.png",
185
- colors: {
186
- primary: "#33FF33"
187
- }
188
- },
189
- zktt: {
190
- id: "zktt",
191
- name: "zKTT",
192
- icon: "/whitelabel/zktt/icon.png",
193
- cover: "/whitelabel/zktt/cover.png",
194
- colors: {
195
- primary: "#FFFFFF"
196
- }
197
- },
198
- "tale-weaver": {
199
- id: "tale-weaver",
200
- name: "Tale Weaver",
201
- icon: "/whitelabel/tale-weaver/icon.png",
202
- cover: "/whitelabel/tale-weaver/cover.png",
203
- colors: {
204
- primary: "#fce377"
205
- }
206
- },
207
- "realm-of-ra": {
208
- id: "realm-of-ra",
209
- name: "Realm of Ra",
210
- icon: "/whitelabel/realm-of-ra/icon.png",
211
- cover: "/whitelabel/realm-of-ra/cover.png",
212
- colors: {
213
- primary: "#de9534"
214
- }
215
- },
216
- "jokers-of-neon": {
217
- id: "jokers-of-neon",
218
- name: "Jokers of Neon",
219
- icon: "/whitelabel/jokers-of-neon/icon.png",
220
- cover: "/whitelabel/jokers-of-neon/cover.png",
221
- colors: {
222
- primary: "#A144B2"
223
- }
224
- },
225
- flippyflop: {
226
- id: "flippyflop",
227
- name: "FlippyFlop",
228
- icon: "/whitelabel/flippyflop/icon.png",
229
- cover: "/whitelabel/flippyflop/cover.png",
230
- colors: {
231
- primary: "#F38332"
232
- }
233
- },
234
- "savage-summit": {
235
- id: "savage-summit",
236
- name: "Savage Summit",
237
- icon: "/whitelabel/savage-summit/icon.png",
238
- cover: "/whitelabel/savage-summit/cover.png",
239
- colors: {
240
- primary: "#fbf7da"
241
- }
242
- },
243
- "dark-shuffle": {
244
- id: "dark-shuffle",
245
- name: "Dark Shuffle",
246
- icon: "/whitelabel/dark-shuffle/icon.svg",
247
- cover: "/whitelabel/dark-shuffle/cover.png",
248
- colors: {
249
- primary: "#F59100"
250
- }
251
- },
252
- "blob-arena": {
253
- id: "blob-arena",
254
- name: "Blob Arena",
255
- icon: "/whitelabel/blob-arena/icon.png",
256
- cover: "/whitelabel/blob-arena/cover.png",
257
- colors: {
258
- primary: "#980f06"
259
- }
260
- },
261
- zkube: {
262
- id: "zkube",
263
- name: "zKube",
264
- icon: "/whitelabel/zkube/icon.png",
265
- cover: "/whitelabel/zkube/cover.png",
266
- colors: {
267
- primary: "#5bc3e6"
268
- }
269
- }
270
- };
271
-
272
- // src/iframe/base.ts
273
199
  var IFrame = class {
274
200
  constructor({
275
201
  id,
@@ -283,14 +209,9 @@ var IFrame = class {
283
209
  if (typeof document === "undefined") {
284
210
  return;
285
211
  }
286
- url.searchParams.set(
287
- "theme",
288
- encodeURIComponent(
289
- JSON.stringify(
290
- defaultPresets[theme ?? "cartridge"] ?? defaultPresets.cartridge
291
- )
292
- )
293
- );
212
+ if (theme) {
213
+ url.searchParams.set("theme", encodeURIComponent(theme));
214
+ }
294
215
  if (colorMode) {
295
216
  url.searchParams.set("colorMode", colorMode);
296
217
  }
@@ -383,6 +304,7 @@ var IFrame = class {
383
304
  // src/constants.ts
384
305
  var KEYCHAIN_URL = "https://x.cartridge.gg";
385
306
  var PROFILE_URL = "https://profile.cartridge.gg";
307
+ var API_URL = "https://api.cartridge.gg";
386
308
 
387
309
  // src/iframe/keychain.ts
388
310
  var KeychainIFrame = class extends IFrame {
@@ -415,9 +337,11 @@ var ProfileIFrame = class extends IFrame {
415
337
  }) {
416
338
  const _profileUrl = (profileUrl || PROFILE_URL).replace(/\/$/, "");
417
339
  const _url = new URL(
418
- slot ? namespace ? `${_profileUrl}/account/${username}/slot/${slot}?ns=${encodeURIComponent(
419
- namespace
420
- )}` : `${_profileUrl}/account/${username}/slot/${slot}` : `${_profileUrl}/account/${username}`
340
+ slot ? namespace ? `${_profileUrl}/account/${username}/slot/${slot}?ps=${encodeURIComponent(
341
+ slot
342
+ )}&ns=${encodeURIComponent(namespace)}` : `${_profileUrl}/account/${username}/slot/${slot}?ps=${encodeURIComponent(
343
+ slot
344
+ )}` : `${_profileUrl}/account/${username}`
421
345
  );
422
346
  _url.searchParams.set("rpcUrl", encodeURIComponent(rpcUrl));
423
347
  if (tokens?.erc20) {
@@ -628,7 +552,7 @@ var ControllerProvider = class extends BaseProvider {
628
552
  methods: {
629
553
  openSettings: this.openSettings.bind(this),
630
554
  openPurchaseCredits: this.openPurchaseCredits.bind(this),
631
- openExecute: normalize(() => this.openExecute.bind(this))
555
+ openExecute: this.openExecute.bind(this)
632
556
  },
633
557
  rpcUrl: this.rpc.toString(),
634
558
  username
@@ -786,10 +710,64 @@ var ControllerProvider = class extends BaseProvider {
786
710
  });
787
711
  }
788
712
  };
713
+
714
+ // src/lookup.ts
715
+ import { num } from "starknet";
716
+ var cache = /* @__PURE__ */ new Map();
717
+ async function lookup(request) {
718
+ if (!request.addresses?.length && !request.usernames?.length) {
719
+ return { results: [] };
720
+ }
721
+ const response = await fetch(`${API_URL}/lookup`, {
722
+ method: "POST",
723
+ headers: {
724
+ "Content-Type": "application/json"
725
+ },
726
+ body: JSON.stringify(request)
727
+ });
728
+ if (!response.ok) {
729
+ throw new Error(`HTTP error! status: ${response.status}`);
730
+ }
731
+ return response.json();
732
+ }
733
+ async function lookupUsernames(usernames) {
734
+ const uncachedUsernames = usernames.filter((name) => !cache.has(name));
735
+ if (uncachedUsernames.length > 0) {
736
+ const response = await lookup({ usernames: uncachedUsernames });
737
+ response.results.forEach((result) => {
738
+ cache.set(result.username, result.addresses[0]);
739
+ });
740
+ }
741
+ return new Map(
742
+ usernames.map((name) => [name, cache.get(name)]).filter((entry) => entry[1] !== void 0)
743
+ );
744
+ }
745
+ async function lookupAddresses(addresses) {
746
+ addresses = addresses.map(num.toHex);
747
+ const uncachedAddresses = addresses.filter((addr) => !cache.has(addr));
748
+ if (uncachedAddresses.length > 0) {
749
+ const response = await lookup({
750
+ addresses: uncachedAddresses
751
+ });
752
+ response.results.forEach((result) => {
753
+ cache.set(result.addresses[0], result.username);
754
+ });
755
+ }
756
+ return new Map(
757
+ addresses.map((addr) => [addr, cache.get(addr)]).filter((entry) => entry[1] !== void 0)
758
+ );
759
+ }
760
+
761
+ // src/index.ts
762
+ export * from "@cartridge/presets";
789
763
  export {
790
764
  NotReadyToConnect,
791
765
  ResponseCodes,
792
766
  ControllerProvider as default,
793
- defaultPresets
767
+ lookupAddresses,
768
+ lookupUsernames,
769
+ toArray,
770
+ toSessionPolicies,
771
+ toWasmPolicies
794
772
  };
795
773
  //# sourceMappingURL=index.js.map