@cartridge/controller 0.1.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.js ADDED
@@ -0,0 +1,696 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var cuid = require('cuid');
6
+ var starknet = require('starknet');
7
+ var qs = require('query-string');
8
+
9
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
10
+
11
+ var cuid__default = /*#__PURE__*/_interopDefaultLegacy(cuid);
12
+ var qs__default = /*#__PURE__*/_interopDefaultLegacy(qs);
13
+
14
+ /*! *****************************************************************************
15
+ Copyright (c) Microsoft Corporation.
16
+
17
+ Permission to use, copy, modify, and/or distribute this software for any
18
+ purpose with or without fee is hereby granted.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
21
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
22
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
23
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
24
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
25
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
26
+ PERFORMANCE OF THIS SOFTWARE.
27
+ ***************************************************************************** */
28
+ /* global Reflect, Promise */
29
+
30
+ var extendStatics = function(d, b) {
31
+ extendStatics = Object.setPrototypeOf ||
32
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
33
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
34
+ return extendStatics(d, b);
35
+ };
36
+
37
+ function __extends(d, b) {
38
+ if (typeof b !== "function" && b !== null)
39
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
40
+ extendStatics(d, b);
41
+ function __() { this.constructor = d; }
42
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
43
+ }
44
+
45
+ var __assign = function() {
46
+ __assign = Object.assign || function __assign(t) {
47
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
48
+ s = arguments[i];
49
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
50
+ }
51
+ return t;
52
+ };
53
+ return __assign.apply(this, arguments);
54
+ };
55
+
56
+ function __awaiter(thisArg, _arguments, P, generator) {
57
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
58
+ return new (P || (P = Promise))(function (resolve, reject) {
59
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
60
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
61
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
62
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
63
+ });
64
+ }
65
+
66
+ function __generator(thisArg, body) {
67
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
68
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
69
+ function verb(n) { return function (v) { return step([n, v]); }; }
70
+ function step(op) {
71
+ if (f) throw new TypeError("Generator is already executing.");
72
+ while (_) try {
73
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
74
+ if (y = 0, t) op = [op[0] & 2, t.value];
75
+ switch (op[0]) {
76
+ case 0: case 1: t = op; break;
77
+ case 4: _.label++; return { value: op[1], done: false };
78
+ case 5: _.label++; y = op[1]; op = [0]; continue;
79
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
80
+ default:
81
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
82
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
83
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
84
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
85
+ if (t[2]) _.ops.pop();
86
+ _.trys.pop(); continue;
87
+ }
88
+ op = body.call(thisArg, _);
89
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
90
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
91
+ }
92
+ }
93
+
94
+ var Signer = /** @class */ (function () {
95
+ function Signer(messenger, options) {
96
+ this.url = "https://cartridge.gg";
97
+ this.messenger = messenger;
98
+ if (options === null || options === void 0 ? void 0 : options.url) {
99
+ this.url = options.url;
100
+ }
101
+ }
102
+ /**
103
+ * Method to get the public key of the signer
104
+ *
105
+ * @returns public key of signer as hex string with 0x prefix
106
+ */
107
+ Signer.prototype.getPubKey = function () {
108
+ return Promise.resolve("");
109
+ };
110
+ /**
111
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
112
+ * This adds a message prefix so it cant be interchanged with transactions
113
+ *
114
+ * @param typedData - JSON object to be signed
115
+ * @param accountAddress - account
116
+ * @returns the signature of the JSON object
117
+ * @throws {Error} if the JSON object is not a valid JSON
118
+ */
119
+ Signer.prototype.signMessage = function (typedData, account) {
120
+ return __awaiter(this, void 0, void 0, function () {
121
+ var id, response;
122
+ return __generator(this, function (_a) {
123
+ switch (_a.label) {
124
+ case 0:
125
+ id = cuid__default["default"]();
126
+ window.open("".concat(this.url, "/sign?").concat(qs__default["default"].stringify({
127
+ id: id,
128
+ origin: window.origin,
129
+ message: JSON.stringify(typedData.message),
130
+ })), "_blank", "height=650,width=400");
131
+ return [4 /*yield*/, this.messenger.send({
132
+ method: "sign-message",
133
+ params: {
134
+ id: id,
135
+ account: account,
136
+ typedData: typedData,
137
+ },
138
+ })];
139
+ case 1:
140
+ response = _a.sent();
141
+ if (response.error) {
142
+ throw new Error(response.error);
143
+ }
144
+ return [2 /*return*/, response.result];
145
+ }
146
+ });
147
+ });
148
+ };
149
+ /**
150
+ * Signs a transaction with the starknet private key and returns the signature
151
+ *
152
+ * @param invocation the invocation object containing:
153
+ * - contractAddress - the address of the contract
154
+ * - entrypoint - the entrypoint of the contract
155
+ * - calldata - (defaults to []) the calldata
156
+ * - signature - (defaults to []) the signature
157
+ * @param abi (optional) the abi of the contract for better displaying
158
+ *
159
+ * @returns signature
160
+ */
161
+ Signer.prototype.signTransaction = function (transactions, transactionsDetail, abis) {
162
+ return __awaiter(this, void 0, void 0, function () {
163
+ var id, calls, response;
164
+ return __generator(this, function (_a) {
165
+ switch (_a.label) {
166
+ case 0:
167
+ id = cuid__default["default"]();
168
+ calls = Array.isArray(transactions) ? transactions : [transactions];
169
+ window.open("".concat(this.url, "/sign?").concat(qs__default["default"].stringify({
170
+ id: id,
171
+ origin: window.origin,
172
+ calls: JSON.stringify(calls),
173
+ })), "_blank", "height=650,width=400");
174
+ return [4 /*yield*/, this.messenger.send({
175
+ method: "sign-transaction",
176
+ params: {
177
+ id: id,
178
+ transactions: transactions,
179
+ abis: abis,
180
+ transactionsDetail: transactionsDetail,
181
+ },
182
+ })];
183
+ case 1:
184
+ response = _a.sent();
185
+ if (response.error) {
186
+ throw new Error(response.error);
187
+ }
188
+ return [2 /*return*/, response.result];
189
+ }
190
+ });
191
+ });
192
+ };
193
+ return Signer;
194
+ }());
195
+
196
+ var Account = /** @class */ (function (_super) {
197
+ __extends(Account, _super);
198
+ function Account(address, scopes, messenger, options) {
199
+ if (scopes === void 0) { scopes = []; }
200
+ var _this = _super.call(this) || this;
201
+ _this.url = "https://cartridge.gg";
202
+ _this._scopes = [];
203
+ _this.address = address;
204
+ _this.messenger = messenger;
205
+ _this._scopes = scopes;
206
+ _this.signer = new Signer(messenger, options);
207
+ if (options === null || options === void 0 ? void 0 : options.url) {
208
+ _this.url = options.url;
209
+ }
210
+ return _this;
211
+ }
212
+ /**
213
+ * Deploys a given compiled contract (json) to starknet
214
+ *
215
+ * @param payload payload to be deployed containing:
216
+ * - compiled contract code
217
+ * - constructor calldata
218
+ * - address salt
219
+ * @param abi the abi of the contract
220
+ * @returns a confirmation of sending a transaction on the starknet contract
221
+ */
222
+ Account.prototype.deployContract = function (payload, abi) {
223
+ return __awaiter(this, void 0, void 0, function () {
224
+ var id, response;
225
+ return __generator(this, function (_a) {
226
+ switch (_a.label) {
227
+ case 0:
228
+ id = cuid__default["default"]();
229
+ window.open("".concat(this.url, "/deploy?origin=").concat(encodeURIComponent(window.origin), "&id=").concat(id), "_blank", "height=650,width=400");
230
+ return [4 /*yield*/, this.messenger.send({
231
+ method: "deploy-contract",
232
+ params: {
233
+ id: id,
234
+ payload: payload,
235
+ abi: abi,
236
+ },
237
+ })];
238
+ case 1:
239
+ response = _a.sent();
240
+ if (response.error) {
241
+ throw new Error(response.error);
242
+ }
243
+ return [2 /*return*/, response.result];
244
+ }
245
+ });
246
+ });
247
+ };
248
+ /**
249
+ * Estimate Fee for a method on starknet
250
+ *
251
+ * @param invocation the invocation object containing:
252
+ * - contractAddress - the address of the contract
253
+ * - entrypoint - the entrypoint of the contract
254
+ * - calldata - (defaults to []) the calldata
255
+ * - signature - (defaults to []) the signature
256
+ *
257
+ * @returns response from addTransaction
258
+ */
259
+ Account.prototype.estimateFee = function (invocation) {
260
+ return __awaiter(this, void 0, void 0, function () {
261
+ var response;
262
+ return __generator(this, function (_a) {
263
+ switch (_a.label) {
264
+ case 0: return [4 /*yield*/, this.messenger.send({
265
+ method: "estimate-fee",
266
+ params: {
267
+ invocation: invocation,
268
+ },
269
+ })];
270
+ case 1:
271
+ response = _a.sent();
272
+ if (response.error) {
273
+ throw new Error(response.error);
274
+ }
275
+ return [2 /*return*/, response.result];
276
+ }
277
+ });
278
+ });
279
+ };
280
+ /**
281
+ * Invoke execute function in account contract
282
+ *
283
+ * @param transactions the invocation object or an array of them, containing:
284
+ * - contractAddress - the address of the contract
285
+ * - entrypoint - the entrypoint of the contract
286
+ * - calldata - (defaults to []) the calldata
287
+ * - signature - (defaults to []) the signature
288
+ * @param abi (optional) the abi of the contract for better displaying
289
+ *
290
+ * @returns response from addTransaction
291
+ */
292
+ Account.prototype.execute = function (transactions, abis, transactionsDetail) {
293
+ return __awaiter(this, void 0, void 0, function () {
294
+ var response, id, calls;
295
+ return __generator(this, function (_a) {
296
+ switch (_a.label) {
297
+ case 0: return [4 /*yield*/, this.messenger.send({
298
+ method: "execute",
299
+ params: {
300
+ transactions: transactions,
301
+ abis: abis,
302
+ transactionsDetail: transactionsDetail,
303
+ },
304
+ })];
305
+ case 1:
306
+ response = _a.sent();
307
+ if (response.result) {
308
+ return [2 /*return*/, response.result];
309
+ }
310
+ if (response.error && response.error !== "missing scopes") {
311
+ throw new Error(response.error);
312
+ }
313
+ id = cuid__default["default"]();
314
+ calls = Array.isArray(transactions) ? transactions : [transactions];
315
+ window.open("".concat(this.url, "/execute?").concat(qs__default["default"].stringify({
316
+ id: id,
317
+ origin: window.origin,
318
+ calls: JSON.stringify(calls),
319
+ })), "_blank", "height=650,width=400");
320
+ return [4 /*yield*/, this.messenger.send({
321
+ method: "execute",
322
+ params: {
323
+ id: id,
324
+ transactions: transactions,
325
+ abis: abis,
326
+ transactionsDetail: transactionsDetail,
327
+ },
328
+ })];
329
+ case 2:
330
+ response = _a.sent();
331
+ if (response.error) {
332
+ throw new Error(response.error);
333
+ }
334
+ return [2 /*return*/, response.result];
335
+ }
336
+ });
337
+ });
338
+ };
339
+ /**
340
+ * Sign an JSON object for off-chain usage with the starknet private key and return the signature
341
+ * This adds a message prefix so it cant be interchanged with transactions
342
+ *
343
+ * @param json - JSON object to be signed
344
+ * @returns the signature of the JSON object
345
+ * @throws {Error} if the JSON object is not a valid JSON
346
+ */
347
+ Account.prototype.signMessage = function (typedData) {
348
+ return __awaiter(this, void 0, void 0, function () {
349
+ var id, response;
350
+ return __generator(this, function (_a) {
351
+ switch (_a.label) {
352
+ case 0:
353
+ id = cuid__default["default"]();
354
+ window.open("".concat(this.url, "/sign?").concat(qs__default["default"].stringify({
355
+ id: id,
356
+ origin: window.origin,
357
+ typedData: JSON.stringify(typedData),
358
+ })), "_blank", "height=650,width=400");
359
+ return [4 /*yield*/, this.messenger.send({
360
+ method: "sign-message",
361
+ params: {
362
+ id: id,
363
+ typedData: typedData,
364
+ },
365
+ })];
366
+ case 1:
367
+ response = _a.sent();
368
+ if (response.error) {
369
+ throw new Error(response.error);
370
+ }
371
+ return [2 /*return*/, response.result];
372
+ }
373
+ });
374
+ });
375
+ };
376
+ /**
377
+ * Hash a JSON object with pederson hash and return the hash
378
+ * This adds a message prefix so it cant be interchanged with transactions
379
+ *
380
+ * @param json - JSON object to be hashed
381
+ * @returns the hash of the JSON object
382
+ * @throws {Error} if the JSON object is not a valid JSON
383
+ */
384
+ Account.prototype.hashMessage = function (typedData) {
385
+ return __awaiter(this, void 0, void 0, function () {
386
+ var response;
387
+ return __generator(this, function (_a) {
388
+ switch (_a.label) {
389
+ case 0: return [4 /*yield*/, this.messenger.send({
390
+ method: "hash-message",
391
+ params: {
392
+ typedData: typedData,
393
+ },
394
+ })];
395
+ case 1:
396
+ response = _a.sent();
397
+ if (response.error) {
398
+ throw new Error(response.error);
399
+ }
400
+ return [2 /*return*/, response.result];
401
+ }
402
+ });
403
+ });
404
+ };
405
+ /**
406
+ * Verify a signature of a JSON object
407
+ *
408
+ * @param json - JSON object to be verified
409
+ * @param signature - signature of the JSON object
410
+ * @returns true if the signature is valid, false otherwise
411
+ * @throws {Error} if the JSON object is not a valid JSON or the signature is not a valid signature
412
+ */
413
+ Account.prototype.verifyMessage = function (typedData, signature) {
414
+ return __awaiter(this, void 0, void 0, function () {
415
+ var response;
416
+ return __generator(this, function (_a) {
417
+ switch (_a.label) {
418
+ case 0: return [4 /*yield*/, this.messenger.send({
419
+ method: "verify-message",
420
+ params: {
421
+ typedData: typedData,
422
+ signature: signature,
423
+ },
424
+ })];
425
+ case 1:
426
+ response = _a.sent();
427
+ if (response.error) {
428
+ throw new Error(response.error);
429
+ }
430
+ return [2 /*return*/, response.result];
431
+ }
432
+ });
433
+ });
434
+ };
435
+ /**
436
+ * Verify a signature of a given hash
437
+ * @warning This method is not recommended, use verifyMessage instead
438
+ *
439
+ * @param hash - hash to be verified
440
+ * @param signature - signature of the hash
441
+ * @returns true if the signature is valid, false otherwise
442
+ * @throws {Error} if the signature is not a valid signature
443
+ */
444
+ Account.prototype.verifyMessageHash = function (hash, signature) {
445
+ return __awaiter(this, void 0, void 0, function () {
446
+ var response;
447
+ return __generator(this, function (_a) {
448
+ switch (_a.label) {
449
+ case 0: return [4 /*yield*/, this.messenger.send({
450
+ method: "verify-message-hash",
451
+ params: {
452
+ hash: hash,
453
+ signature: signature,
454
+ },
455
+ })];
456
+ case 1:
457
+ response = _a.sent();
458
+ if (response.error) {
459
+ throw new Error(response.error);
460
+ }
461
+ return [2 /*return*/, response.result];
462
+ }
463
+ });
464
+ });
465
+ };
466
+ Account.prototype.getNonce = function () {
467
+ return __awaiter(this, void 0, void 0, function () {
468
+ var response;
469
+ return __generator(this, function (_a) {
470
+ switch (_a.label) {
471
+ case 0: return [4 /*yield*/, this.messenger.send({
472
+ method: "get-nonce",
473
+ })];
474
+ case 1:
475
+ response = _a.sent();
476
+ if (response.error) {
477
+ throw new Error(response.error);
478
+ }
479
+ return [2 /*return*/, response.result];
480
+ }
481
+ });
482
+ });
483
+ };
484
+ return Account;
485
+ }(starknet.Provider));
486
+
487
+ var Messenger = /** @class */ (function () {
488
+ function Messenger(target, origin) {
489
+ if (origin === void 0) { origin = "https://cartridge.gg"; }
490
+ var _this = this;
491
+ this.pending = [];
492
+ this.defaultHandler = function (e) {
493
+ if (e.data.target === "cartridge" &&
494
+ e.data.type === "request") {
495
+ _this.pending.push(e);
496
+ }
497
+ };
498
+ this.target = target;
499
+ this.origin = origin;
500
+ if (typeof document !== "undefined" &&
501
+ document.body.getAttribute("cartridge") !== "true") {
502
+ document.body.setAttribute("cartridge", "true");
503
+ }
504
+ window.addEventListener("message", this.defaultHandler);
505
+ }
506
+ Messenger.prototype.onRequest = function (cb) {
507
+ window.removeEventListener("message", this.defaultHandler);
508
+ var onResponse = function (_a) {
509
+ var origin = _a.origin, source = _a.source, id = _a.data.id;
510
+ return function (response) {
511
+ source.postMessage({
512
+ id: id,
513
+ target: "cartridge",
514
+ type: "response",
515
+ payload: __assign({ origin: window.origin }, response),
516
+ }, { targetOrigin: origin });
517
+ };
518
+ };
519
+ for (var i = 0; i < this.pending.length; i++) {
520
+ var payload = this.pending[i].data.payload;
521
+ cb(payload, onResponse(this.pending[i]));
522
+ }
523
+ window.addEventListener("message", function (e) {
524
+ var _a = e.data, type = _a.type, target = _a.target, payload = _a.payload;
525
+ if (target === "cartridge" &&
526
+ type === "request") {
527
+ cb(payload, onResponse(e));
528
+ }
529
+ });
530
+ };
531
+ Messenger.prototype.send = function (request) {
532
+ var id = cuid__default["default"]();
533
+ if (!this.target) {
534
+ throw new Error("read only");
535
+ }
536
+ this.target.postMessage({
537
+ id: id,
538
+ target: "cartridge",
539
+ type: "request",
540
+ payload: __assign({ origin: window.origin }, request),
541
+ }, this.origin);
542
+ return new Promise(function (resolve, reject) {
543
+ var handler = function (_a) {
544
+ var data = _a.data;
545
+ if (data.target === "cartridge" &&
546
+ data.type === "response" &&
547
+ id === data.id) {
548
+ resolve(data.payload);
549
+ window.removeEventListener("message", handler);
550
+ }
551
+ };
552
+ window.addEventListener("message", handler);
553
+ });
554
+ };
555
+ return Messenger;
556
+ }());
557
+
558
+ var Cartridge = /** @class */ (function () {
559
+ function Cartridge(scopes, options) {
560
+ var _this = this;
561
+ this.selector = "cartridge-messenger";
562
+ this.scopes = [];
563
+ this.url = "https://cartridge.gg";
564
+ this.origin = "https://cartridge.gg";
565
+ this.loading = true;
566
+ if (scopes) {
567
+ this.scopes = scopes;
568
+ }
569
+ if (options === null || options === void 0 ? void 0 : options.url) {
570
+ this.url = options.url;
571
+ }
572
+ if (options === null || options === void 0 ? void 0 : options.origin) {
573
+ this.origin = options.origin;
574
+ }
575
+ if (typeof document !== "undefined") {
576
+ this.ready_ = new Promise(function (resolve, reject) {
577
+ window.addEventListener("message", function (e) { return __awaiter(_this, void 0, void 0, function () {
578
+ return __generator(this, function (_a) {
579
+ switch (_a.label) {
580
+ case 0:
581
+ if (!(e.data.target === "cartridge" &&
582
+ e.data.payload.method === "ready")) return [3 /*break*/, 2];
583
+ return [4 /*yield*/, this.probe()];
584
+ case 1:
585
+ _a.sent();
586
+ this.loading = false;
587
+ resolve(true);
588
+ _a.label = 2;
589
+ case 2: return [2 /*return*/];
590
+ }
591
+ });
592
+ }); });
593
+ });
594
+ }
595
+ if (typeof document !== "undefined" && !this.messenger) {
596
+ var iframe = document.getElementById(this.selector);
597
+ if (!!iframe) {
598
+ if (!this.messenger) {
599
+ this.messenger = new Messenger(iframe.contentWindow, this.origin);
600
+ }
601
+ }
602
+ else {
603
+ iframe = document.createElement("iframe");
604
+ iframe.id = this.selector;
605
+ iframe.src = "".concat(this.url, "/iframe");
606
+ iframe.style.opacity = "0";
607
+ iframe.style.height = "0";
608
+ iframe.style.width = "0";
609
+ iframe.sandbox.add("allow-scripts");
610
+ iframe.sandbox.add("allow-same-origin");
611
+ if (!!document.hasStorageAccess) {
612
+ iframe.sandbox.add("allow-storage-access-by-user-activation");
613
+ }
614
+ document.body.appendChild(iframe);
615
+ this.messenger = new Messenger(iframe.contentWindow, this.origin);
616
+ }
617
+ }
618
+ }
619
+ Cartridge.prototype.ready = function () {
620
+ return __awaiter(this, void 0, void 0, function () {
621
+ return __generator(this, function (_a) {
622
+ if (!this.loading)
623
+ return [2 /*return*/, Promise.resolve(true)];
624
+ return [2 /*return*/, this.ready_];
625
+ });
626
+ });
627
+ };
628
+ Cartridge.prototype.probe = function () {
629
+ var _a;
630
+ return __awaiter(this, void 0, void 0, function () {
631
+ var prob;
632
+ return __generator(this, function (_b) {
633
+ switch (_b.label) {
634
+ case 0: return [4 /*yield*/, this.messenger.send({
635
+ method: "probe",
636
+ })];
637
+ case 1:
638
+ prob = _b.sent();
639
+ if ((_a = prob.result) === null || _a === void 0 ? void 0 : _a.address) {
640
+ this.account = new Account(prob.result.address, prob.result.scopes, this.messenger, {
641
+ url: this.url,
642
+ });
643
+ return [2 /*return*/, this.account];
644
+ }
645
+ return [2 /*return*/];
646
+ }
647
+ });
648
+ });
649
+ };
650
+ Cartridge.prototype.connect = function () {
651
+ return __awaiter(this, void 0, void 0, function () {
652
+ var id, ok, response;
653
+ return __generator(this, function (_a) {
654
+ switch (_a.label) {
655
+ case 0:
656
+ id = cuid__default["default"]();
657
+ if (this.account) {
658
+ return [2 /*return*/, this.account];
659
+ }
660
+ if (!!!document.hasStorageAccess) return [3 /*break*/, 3];
661
+ return [4 /*yield*/, document.hasStorageAccess()];
662
+ case 1:
663
+ ok = _a.sent();
664
+ if (!!ok) return [3 /*break*/, 3];
665
+ return [4 /*yield*/, document.requestStorageAccess()];
666
+ case 2:
667
+ _a.sent();
668
+ _a.label = 3;
669
+ case 3:
670
+ window.open("".concat(this.url, "/connect?").concat(qs__default["default"].stringify({
671
+ id: id,
672
+ origin: window.origin,
673
+ scopes: JSON.stringify(this.scopes),
674
+ })), "_blank", "height=650,width=400");
675
+ return [4 /*yield*/, this.messenger.send({
676
+ method: "connect",
677
+ params: {
678
+ id: id,
679
+ scopes: this.scopes,
680
+ },
681
+ })];
682
+ case 4:
683
+ response = _a.sent();
684
+ this.account = new Account(response.result.address, response.result.scopes, this.messenger, {
685
+ url: this.url,
686
+ });
687
+ return [2 /*return*/, this.account];
688
+ }
689
+ });
690
+ });
691
+ };
692
+ return Cartridge;
693
+ }());
694
+
695
+ exports.Cartridge = Cartridge;
696
+ exports.Messenger = Messenger;