@cartridge/controller 0.10.2 → 0.10.3

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/session.js CHANGED
@@ -1,196 +1,39 @@
1
- import { WalletAccount as N, stark as T, ec as F } from "starknet";
2
- import * as C from "../../../node_modules/.pnpm/@cartridge+controller-wasm@0.3.4/node_modules/@cartridge/controller-wasm/pkg-session/session_wasm_bg.wasm";
3
- import { n as J, B as z, K, b as E } from "./provider-D-5qL7QC.js";
4
- import { F as Y, N as X, R as Z, S as ee } from "./provider-D-5qL7QC.js";
5
- let a;
6
- function j(n) {
7
- a = n;
8
- }
9
- let b = null;
10
- function O() {
11
- return (b === null || b.byteLength === 0) && (b = new Uint8Array(a.memory.buffer)), b;
12
- }
13
- const D = typeof TextDecoder > "u" ? (0, module.require)("util").TextDecoder : TextDecoder;
14
- let P = new D("utf-8", { ignoreBOM: !0, fatal: !0 });
15
- P.decode();
16
- const g = new Array(128).fill(void 0);
17
- g.push(void 0, null, !0, !1);
18
- let y = g.length;
19
- function l(n) {
20
- y === g.length && g.push(g.length + 1);
21
- const e = y;
22
- return y = g[e], g[e] = n, e;
23
- }
24
- function M(n) {
25
- return g[n];
26
- }
27
- let _ = 0;
28
- const V = typeof TextEncoder > "u" ? (0, module.require)("util").TextEncoder : TextEncoder, x = new V("utf-8"), $ = typeof x.encodeInto == "function" ? function(n, e) {
29
- return x.encodeInto(n, e);
30
- } : function(n, e) {
31
- const r = x.encode(n);
32
- return e.set(r), {
33
- read: n.length,
34
- written: r.length
35
- };
36
- };
37
- function k(n, e, r) {
38
- if (r === void 0) {
39
- const i = x.encode(n), u = e(i.length, 1) >>> 0;
40
- return O().subarray(u, u + i.length).set(i), _ = i.length, u;
41
- }
42
- let t = n.length, s = e(t, 1) >>> 0;
43
- const c = O();
44
- let o = 0;
45
- for (; o < t; o++) {
46
- const i = n.charCodeAt(o);
47
- if (i > 127) break;
48
- c[s + o] = i;
49
- }
50
- if (o !== t) {
51
- o !== 0 && (n = n.slice(o)), s = r(s, t, t = o + n.length * 3, 1) >>> 0;
52
- const i = O().subarray(s + o, s + t), u = $(n, i);
53
- o += u.written, s = r(s, t, o, 1) >>> 0;
54
- }
55
- return _ = o, s;
56
- }
57
- let h = null;
58
- function f() {
59
- return (h === null || h.buffer.detached === !0 || h.buffer.detached === void 0 && h.buffer !== a.memory.buffer) && (h = new DataView(a.memory.buffer)), h;
60
- }
61
- function G(n) {
62
- n < 132 || (g[n] = y, y = n);
63
- }
64
- function w(n) {
65
- const e = M(n);
66
- return G(n), e;
67
- }
68
- typeof FinalizationRegistry > "u" || new FinalizationRegistry((n) => {
69
- a.__wbindgen_export_4.get(n.dtor)(n.a, n.b);
70
- });
71
- function v(n, e) {
72
- const r = e(n.length * 4, 4) >>> 0, t = f();
73
- for (let s = 0; s < n.length; s++)
74
- t.setUint32(r + 4 * s, l(n[s]), !0);
75
- return _ = n.length, r;
76
- }
77
- const R = typeof FinalizationRegistry > "u" ? { register: () => {
78
- }, unregister: () => {
79
- } } : new FinalizationRegistry((n) => a.__wbg_cartridgesessionaccount_free(n >>> 0, 1));
80
- class S {
81
- static __wrap(e) {
82
- e = e >>> 0;
83
- const r = Object.create(S.prototype);
84
- return r.__wbg_ptr = e, R.register(r, r.__wbg_ptr, r), r;
85
- }
86
- __destroy_into_raw() {
87
- const e = this.__wbg_ptr;
88
- return this.__wbg_ptr = 0, R.unregister(this), e;
89
- }
90
- free() {
91
- const e = this.__destroy_into_raw();
92
- a.__wbg_cartridgesessionaccount_free(e, 0);
93
- }
94
- /**
95
- * @param {string} rpc_url
96
- * @param {JsFelt} signer
97
- * @param {JsFelt} address
98
- * @param {JsFelt} chain_id
99
- * @param {JsFelt[]} session_authorization
100
- * @param {Session} session
101
- * @returns {CartridgeSessionAccount}
102
- */
103
- static new(e, r, t, s, c, o) {
104
- try {
105
- const d = a.__wbindgen_add_to_stack_pointer(-16), m = k(e, a.__wbindgen_export_0, a.__wbindgen_export_1), I = _, U = v(c, a.__wbindgen_export_0), A = _;
106
- a.cartridgesessionaccount_new(d, m, I, l(r), l(t), l(s), U, A, l(o));
107
- var i = f().getInt32(d + 4 * 0, !0), u = f().getInt32(d + 4 * 1, !0), p = f().getInt32(d + 4 * 2, !0);
108
- if (p)
109
- throw w(u);
110
- return S.__wrap(i);
111
- } finally {
112
- a.__wbindgen_add_to_stack_pointer(16);
113
- }
114
- }
115
- /**
116
- * @param {string} rpc_url
117
- * @param {JsFelt} signer
118
- * @param {JsFelt} address
119
- * @param {JsFelt} owner_guid
120
- * @param {JsFelt} chain_id
121
- * @param {Session} session
122
- * @returns {CartridgeSessionAccount}
123
- */
124
- static newAsRegistered(e, r, t, s, c, o) {
125
- try {
126
- const d = a.__wbindgen_add_to_stack_pointer(-16), m = k(e, a.__wbindgen_export_0, a.__wbindgen_export_1), I = _;
127
- a.cartridgesessionaccount_newAsRegistered(d, m, I, l(r), l(t), l(s), l(c), l(o));
128
- var i = f().getInt32(d + 4 * 0, !0), u = f().getInt32(d + 4 * 1, !0), p = f().getInt32(d + 4 * 2, !0);
129
- if (p)
130
- throw w(u);
131
- return S.__wrap(i);
132
- } finally {
133
- a.__wbindgen_add_to_stack_pointer(16);
134
- }
135
- }
136
- /**
137
- * @param {JsFelt} hash
138
- * @param {JsCall[]} calls
139
- * @returns {Promise<Felts>}
140
- */
141
- sign(e, r) {
142
- const t = v(r, a.__wbindgen_export_0), s = _, c = a.cartridgesessionaccount_sign(this.__wbg_ptr, l(e), t, s);
143
- return w(c);
144
- }
145
- /**
146
- * @param {JsCall[]} calls
147
- * @returns {Promise<any>}
148
- */
149
- execute(e) {
150
- const r = v(e, a.__wbindgen_export_0), t = _, s = a.cartridgesessionaccount_execute(this.__wbg_ptr, r, t);
151
- return w(s);
152
- }
153
- /**
154
- * @param {JsCall[]} calls
155
- * @returns {Promise<any>}
156
- */
157
- executeFromOutside(e) {
158
- const r = v(e, a.__wbindgen_export_0), t = _, s = a.cartridgesessionaccount_executeFromOutside(this.__wbg_ptr, r, t);
159
- return w(s);
160
- }
161
- }
162
- typeof FinalizationRegistry > "u" || new FinalizationRegistry((n) => a.__wbg_jscontrollererror_free(n >>> 0, 1));
163
- j(C);
164
- class W extends N {
1
+ import { WalletAccount as h, stark as m, ec as p, encode as S } from "starknet";
2
+ import { signerToGuid as g, subscribeCreateSession as f } from "@cartridge/controller-wasm";
3
+ export * from "@cartridge/controller-wasm";
4
+ import { n as y, B as _, K as w, b as v } from "./provider-S-IvFw23.js";
5
+ import { F as J, N as R, R as P, S as G } from "./provider-S-IvFw23.js";
6
+ import { CartridgeSessionAccount as I } from "@cartridge/controller-wasm/session";
7
+ class O extends h {
165
8
  controller;
166
- constructor(e, {
9
+ constructor(t, {
167
10
  rpcUrl: r,
168
- privateKey: t,
169
- address: s,
170
- ownerGuid: c,
171
- chainId: o,
172
- expiresAt: i,
173
- policies: u,
174
- guardianKeyGuid: p,
175
- metadataHash: d,
176
- sessionKeyGuid: m
11
+ privateKey: e,
12
+ address: n,
13
+ ownerGuid: i,
14
+ chainId: a,
15
+ expiresAt: s,
16
+ policies: o,
17
+ guardianKeyGuid: c,
18
+ metadataHash: l,
19
+ sessionKeyGuid: u
177
20
  }) {
178
21
  super({
179
22
  provider: { nodeUrl: r },
180
- walletProvider: e,
181
- address: s
182
- }), this.controller = S.newAsRegistered(
23
+ walletProvider: t,
24
+ address: n
25
+ }), this.controller = I.newAsRegistered(
183
26
  r,
184
- t,
185
- s,
186
- c,
187
- o,
27
+ e,
28
+ n,
29
+ i,
30
+ a,
188
31
  {
189
- expiresAt: i,
190
- policies: u,
191
- guardianKeyGuid: p,
192
- metadataHash: d,
193
- sessionKeyGuid: m
32
+ expiresAt: s,
33
+ policies: o,
34
+ guardianKeyGuid: c,
35
+ metadataHash: l,
36
+ sessionKeyGuid: u
194
37
  }
195
38
  );
196
39
  }
@@ -206,11 +49,11 @@ class W extends N {
206
49
  *
207
50
  * @returns response from addTransaction
208
51
  */
209
- async execute(e) {
210
- return this.controller.execute(J(e));
52
+ async execute(t) {
53
+ return this.controller.execute(y(t));
211
54
  }
212
55
  }
213
- class Q extends z {
56
+ class k extends _ {
214
57
  id = "controller_session";
215
58
  name = "Controller Session";
216
59
  _chainId;
@@ -219,54 +62,56 @@ class Q extends z {
219
62
  _redirectUrl;
220
63
  _policies;
221
64
  _keychainUrl;
65
+ _apiUrl;
222
66
  constructor({
223
- rpc: e,
67
+ rpc: t,
224
68
  chainId: r,
225
- policies: t,
226
- redirectUrl: s,
227
- keychainUrl: c
69
+ policies: e,
70
+ redirectUrl: n,
71
+ keychainUrl: i,
72
+ apiUrl: a
228
73
  }) {
229
74
  super(), this._policies = {
230
75
  verified: !1,
231
- contracts: t.contracts ? Object.fromEntries(
232
- Object.entries(t.contracts).map(([o, i]) => [
233
- o,
76
+ contracts: e.contracts ? Object.fromEntries(
77
+ Object.entries(e.contracts).map(([s, o]) => [
78
+ s,
234
79
  {
235
- ...i,
236
- methods: i.methods.map((u) => ({
237
- ...u,
80
+ ...o,
81
+ methods: o.methods.map((c) => ({
82
+ ...c,
238
83
  authorized: !0
239
84
  }))
240
85
  }
241
86
  ])
242
87
  ) : void 0,
243
- messages: t.messages?.map((o) => ({
244
- ...o,
88
+ messages: e.messages?.map((s) => ({
89
+ ...s,
245
90
  authorized: !0
246
91
  }))
247
- }, this._rpcUrl = e, this._chainId = r, this._redirectUrl = s, this._keychainUrl = c || K, typeof window < "u" && (window.starknet_controller_session = this);
92
+ }, this._rpcUrl = t, this._chainId = r, this._redirectUrl = n, this._keychainUrl = i || w, this._apiUrl = a, typeof window < "u" && (window.starknet_controller_session = this);
248
93
  }
249
- validatePoliciesSubset(e, r) {
250
- if (e.contracts) {
94
+ validatePoliciesSubset(t, r) {
95
+ if (t.contracts) {
251
96
  if (!r.contracts) return !1;
252
- for (const [t, s] of Object.entries(e.contracts)) {
253
- const c = r.contracts[t];
254
- if (!c) return !1;
255
- for (const o of s.methods) {
256
- const i = c.methods.find(
257
- (u) => u.entrypoint === o.entrypoint
97
+ for (const [e, n] of Object.entries(t.contracts)) {
98
+ const i = r.contracts[e];
99
+ if (!i) return !1;
100
+ for (const a of n.methods) {
101
+ const s = i.methods.find(
102
+ (o) => o.entrypoint === a.entrypoint
258
103
  );
259
- if (!i || !i.authorized) return !1;
104
+ if (!s || !s.authorized) return !1;
260
105
  }
261
106
  }
262
107
  }
263
- if (e.messages) {
108
+ if (t.messages) {
264
109
  if (!r.messages) return !1;
265
- for (const t of e.messages) {
266
- const s = r.messages.find(
267
- (c) => JSON.stringify(c.domain) === JSON.stringify(t.domain) && JSON.stringify(c.types) === JSON.stringify(t.types)
110
+ for (const e of t.messages) {
111
+ const n = r.messages.find(
112
+ (i) => JSON.stringify(i.domain) === JSON.stringify(e.domain) && JSON.stringify(i.types) === JSON.stringify(e.types)
268
113
  );
269
- if (!s || !s.authorized) return !1;
114
+ if (!n || !n.authorized) return !1;
270
115
  }
271
116
  }
272
117
  return !0;
@@ -282,23 +127,43 @@ class Q extends z {
282
127
  return this.account;
283
128
  if (this.account = await this.tryRetrieveFromQueryOrStorage(), this.account)
284
129
  return this.account;
285
- const e = T.randomAddress(), r = F.starkCurve.getStarkKey(e);
130
+ const t = m.randomAddress(), r = p.starkCurve.getStarkKey(t);
286
131
  localStorage.setItem(
287
132
  "sessionSigner",
288
133
  JSON.stringify({
289
- privKey: e,
134
+ privKey: t,
290
135
  pubKey: r
291
136
  })
292
137
  ), localStorage.setItem("sessionPolicies", JSON.stringify(this._policies));
293
- const t = `${this._keychainUrl}/session?public_key=${r}&redirect_uri=${this._redirectUrl}&redirect_query_name=startapp&policies=${JSON.stringify(
138
+ const e = `${this._keychainUrl}/session?public_key=${r}&redirect_uri=${this._redirectUrl}&redirect_query_name=startapp&policies=${JSON.stringify(
294
139
  this._policies
295
140
  )}&rpc_url=${this._rpcUrl}`;
296
- return localStorage.setItem("lastUsedConnector", this.id), window.open(t, "_blank"), this.account;
141
+ localStorage.setItem("lastUsedConnector", this.id);
142
+ const n = window.open(e, "_blank");
143
+ try {
144
+ const i = S.addHexPrefix(t), a = g({
145
+ starknet: { privateKey: i }
146
+ }), s = this._apiUrl ?? "https://api.cartridge.gg", o = await f(
147
+ a,
148
+ s
149
+ ), c = o.authorization[1], l = {
150
+ username: o.controller.accountID,
151
+ address: o.controller.address,
152
+ ownerGuid: c,
153
+ expiresAt: o.expiresAt,
154
+ guardianKeyGuid: "0x0",
155
+ metadataHash: "0x0",
156
+ sessionKeyGuid: a
157
+ };
158
+ return localStorage.setItem("session", JSON.stringify(l)), this.tryRetrieveFromQueryOrStorage(), n?.close(), this.account;
159
+ } catch (i) {
160
+ console.log(i);
161
+ }
297
162
  }
298
- switchStarknetChain(e) {
163
+ switchStarknetChain(t) {
299
164
  throw new Error("switchStarknetChain not implemented");
300
165
  }
301
- addStarknetChain(e) {
166
+ addStarknetChain(t) {
302
167
  throw new Error("addStarknetChain not implemented");
303
168
  }
304
169
  disconnect() {
@@ -307,51 +172,51 @@ class Q extends z {
307
172
  async tryRetrieveFromQueryOrStorage() {
308
173
  if (this.account)
309
174
  return this.account;
310
- const e = localStorage.getItem("sessionSigner"), r = e ? JSON.parse(e) : null;
311
- let t = null;
312
- if (window.location.search.includes("startapp")) {
313
- const o = new URLSearchParams(window.location.search), i = o.get("startapp");
314
- if (i) {
315
- t = JSON.parse(atob(i)), localStorage.setItem("session", JSON.stringify(t)), o.delete("startapp");
316
- const u = window.location.pathname + (o.toString() ? `?${o.toString()}` : "") + window.location.hash;
317
- window.history.replaceState({}, document.title, u);
175
+ const t = localStorage.getItem("sessionSigner"), r = t ? JSON.parse(t) : null;
176
+ let e = null;
177
+ const n = localStorage.getItem("session");
178
+ if (n && (e = JSON.parse(n)), window.location.search.includes("startapp")) {
179
+ const s = new URLSearchParams(window.location.search), o = s.get("startapp");
180
+ if (o) {
181
+ const c = JSON.parse(
182
+ atob(o)
183
+ );
184
+ Number(c.expiresAt) !== Number(e?.expiresAt) && (e = c, localStorage.setItem("session", JSON.stringify(e))), s.delete("startapp");
185
+ const l = window.location.pathname + (s.toString() ? `?${s.toString()}` : "") + window.location.hash;
186
+ window.history.replaceState({}, document.title, l);
318
187
  }
319
188
  }
320
- if (!t) {
321
- const o = localStorage.getItem("session");
322
- o && (t = JSON.parse(o));
323
- }
324
- if (!t || !r)
189
+ if (!e || !r)
325
190
  return;
326
- const s = parseInt(t.expiresAt) * 1e3;
327
- if (Date.now() >= s) {
191
+ const i = parseInt(e.expiresAt) * 1e3;
192
+ if (Date.now() >= i) {
328
193
  this.clearStoredSession();
329
194
  return;
330
195
  }
331
- const c = localStorage.getItem("sessionPolicies");
332
- if (c) {
333
- const o = JSON.parse(
334
- c
196
+ const a = localStorage.getItem("sessionPolicies");
197
+ if (a) {
198
+ const s = JSON.parse(
199
+ a
335
200
  );
336
201
  if (!this.validatePoliciesSubset(
337
202
  this._policies,
338
- o
203
+ s
339
204
  )) {
340
205
  this.clearStoredSession();
341
206
  return;
342
207
  }
343
208
  }
344
- return this._username = t.username, this.account = new W(this, {
209
+ return this._username = e.username, this.account = new O(this, {
345
210
  rpcUrl: this._rpcUrl,
346
211
  privateKey: r.privKey,
347
- address: t.address,
348
- ownerGuid: t.ownerGuid,
212
+ address: e.address,
213
+ ownerGuid: e.ownerGuid,
349
214
  chainId: this._chainId,
350
- expiresAt: parseInt(t.expiresAt),
351
- policies: E(this._policies),
352
- guardianKeyGuid: t.guardianKeyGuid,
353
- metadataHash: t.metadataHash,
354
- sessionKeyGuid: t.sessionKeyGuid
215
+ expiresAt: parseInt(e.expiresAt),
216
+ policies: v(this._policies),
217
+ guardianKeyGuid: e.guardianKeyGuid,
218
+ metadataHash: e.metadataHash,
219
+ sessionKeyGuid: e.sessionKeyGuid
355
220
  }), this.account;
356
221
  }
357
222
  clearStoredSession() {
@@ -359,10 +224,10 @@ class Q extends z {
359
224
  }
360
225
  }
361
226
  export {
362
- Y as FeeSource,
363
- X as NotReadyToConnect,
364
- Z as ResponseCodes,
365
- ee as StarterPackItemType,
366
- Q as default
227
+ J as FeeSource,
228
+ R as NotReadyToConnect,
229
+ P as ResponseCodes,
230
+ G as StarterPackItemType,
231
+ k as default
367
232
  };
368
233
  //# sourceMappingURL=session.js.map