@cartridge/controller 0.6.0 → 0.7.1
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/.turbo/turbo-build$colon$deps.log +52 -76
- package/.turbo/turbo-build.log +53 -77
- package/dist/controller.cjs +860 -0
- package/dist/controller.cjs.map +1 -0
- package/dist/controller.d.cts +33 -0
- package/dist/controller.d.ts +4 -4
- package/dist/controller.js +84 -68
- package/dist/controller.js.map +1 -1
- package/dist/index.cjs +2200 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +17 -0
- package/dist/index.d.ts +12 -7
- package/dist/index.js +161 -7601
- package/dist/index.js.map +1 -1
- package/dist/lookup.cjs +59 -0
- package/dist/lookup.cjs.map +1 -0
- package/dist/lookup.d.cts +4 -0
- package/dist/lookup.js +7 -7
- package/dist/lookup.js.map +1 -1
- package/dist/{provider.js → node/index.cjs} +499 -25
- package/dist/node/index.cjs.map +1 -0
- package/dist/node/index.d.cts +56 -0
- package/dist/node/index.d.ts +56 -0
- package/dist/{telegram/provider.js → node/index.js} +354 -135
- package/dist/node/index.js.map +1 -0
- package/dist/{policies.d.ts → policies-DD1aPjQ4.d.cts} +6 -4
- package/dist/policies-DD1aPjQ4.d.ts +21 -0
- package/dist/{types-CVnDQVqD.d.ts → provider-ap1C1ypF.d.cts} +27 -10
- package/dist/provider-ap1C1ypF.d.ts +201 -0
- package/dist/session/{provider.js → index.cjs} +82 -73
- package/dist/session/index.cjs.map +1 -0
- package/dist/session/{provider.d.ts → index.d.cts} +6 -3
- package/dist/session/index.d.ts +37 -8
- package/dist/session/index.js +47 -64
- package/dist/session/index.js.map +1 -1
- package/package.json +35 -16
- package/src/controller.ts +43 -15
- package/src/iframe/base.ts +1 -11
- package/src/node/account.ts +72 -0
- package/src/node/backend.ts +159 -0
- package/src/node/index.ts +4 -0
- package/src/node/provider.ts +178 -0
- package/src/node/server.ts +89 -0
- package/src/session/account.ts +1 -1
- package/src/session/provider.ts +0 -16
- package/src/types.ts +3 -6
- package/tsconfig.json +2 -1
- package/dist/__tests__/parseChainId.test.d.ts +0 -2
- package/dist/__tests__/parseChainId.test.js +0 -89
- package/dist/__tests__/parseChainId.test.js.map +0 -1
- package/dist/account.d.ts +0 -38
- package/dist/account.js +0 -110
- package/dist/account.js.map +0 -1
- package/dist/constants.d.ts +0 -5
- package/dist/constants.js +0 -10
- package/dist/constants.js.map +0 -1
- package/dist/errors.d.ts +0 -5
- package/dist/errors.js +0 -11
- package/dist/errors.js.map +0 -1
- package/dist/icon.d.ts +0 -3
- package/dist/icon.js +0 -6
- package/dist/icon.js.map +0 -1
- package/dist/iframe/base.d.ts +0 -5
- package/dist/iframe/base.js +0 -126
- package/dist/iframe/base.js.map +0 -1
- package/dist/iframe/index.d.ts +0 -5
- package/dist/iframe/index.js +0 -188
- package/dist/iframe/index.js.map +0 -1
- package/dist/iframe/keychain.d.ts +0 -5
- package/dist/iframe/keychain.js +0 -147
- package/dist/iframe/keychain.js.map +0 -1
- package/dist/iframe/profile.d.ts +0 -5
- package/dist/iframe/profile.js +0 -167
- package/dist/iframe/profile.js.map +0 -1
- package/dist/index.d-BbTUPBeO.d.ts +0 -68
- package/dist/mutex.d.ts +0 -14
- package/dist/mutex.js +0 -22
- package/dist/mutex.js.map +0 -1
- package/dist/policies.js +0 -26
- package/dist/policies.js.map +0 -1
- package/dist/provider.d.ts +0 -24
- package/dist/provider.js.map +0 -1
- package/dist/session/account.d.ts +0 -37
- package/dist/session/account.js +0 -94
- package/dist/session/account.js.map +0 -1
- package/dist/session/backend.d.ts +0 -60
- package/dist/session/backend.js +0 -39
- package/dist/session/backend.js.map +0 -1
- package/dist/session/provider.js.map +0 -1
- package/dist/telegram/backend.d.ts +0 -33
- package/dist/telegram/backend.js +0 -40
- package/dist/telegram/backend.js.map +0 -1
- package/dist/telegram/provider.d.ts +0 -27
- package/dist/telegram/provider.js.map +0 -1
- package/dist/types.d.ts +0 -5
- package/dist/types.js +0 -13
- package/dist/types.js.map +0 -1
- package/dist/utils.d.ts +0 -19
- package/dist/utils.js +0 -141
- package/dist/utils.js.map +0 -1
|
@@ -1,30 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from "@telegram-apps/sdk";
|
|
8
|
-
import { ec, stark } from "starknet";
|
|
1
|
+
import { stark, ec, typedData, TypedDataRevision, hash, WalletAccount, addAddressPadding, CallData } from 'starknet';
|
|
2
|
+
import { CartridgeSessionAccount } from '@cartridge/account-wasm/session';
|
|
3
|
+
import { Permission } from '@starknet-io/types-js';
|
|
4
|
+
import * as fs from 'fs/promises';
|
|
5
|
+
import * as path from 'path';
|
|
6
|
+
import * as http from 'http';
|
|
9
7
|
|
|
10
|
-
// src/
|
|
11
|
-
var KEYCHAIN_URL = "https://x.cartridge.gg";
|
|
12
|
-
|
|
13
|
-
// src/session/account.ts
|
|
14
|
-
import { CartridgeSessionAccount } from "@cartridge/account-wasm/session";
|
|
15
|
-
import { WalletAccount } from "starknet";
|
|
16
|
-
|
|
17
|
-
// src/utils.ts
|
|
18
|
-
import {
|
|
19
|
-
addAddressPadding,
|
|
20
|
-
CallData,
|
|
21
|
-
constants,
|
|
22
|
-
getChecksumAddress,
|
|
23
|
-
hash,
|
|
24
|
-
shortString,
|
|
25
|
-
typedData,
|
|
26
|
-
TypedDataRevision
|
|
27
|
-
} from "starknet";
|
|
8
|
+
// src/node/provider.ts
|
|
28
9
|
function normalizeCalls(calls) {
|
|
29
10
|
return toArray(calls).map((call) => {
|
|
30
11
|
return {
|
|
@@ -66,7 +47,25 @@ function toArray(val) {
|
|
|
66
47
|
return Array.isArray(val) ? val : [val];
|
|
67
48
|
}
|
|
68
49
|
|
|
69
|
-
// src/
|
|
50
|
+
// src/errors.ts
|
|
51
|
+
var NotReadyToConnect = class _NotReadyToConnect extends Error {
|
|
52
|
+
constructor() {
|
|
53
|
+
super("Not ready to connect");
|
|
54
|
+
Object.setPrototypeOf(this, _NotReadyToConnect.prototype);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// src/types.ts
|
|
59
|
+
var ResponseCodes = /* @__PURE__ */ ((ResponseCodes2) => {
|
|
60
|
+
ResponseCodes2["SUCCESS"] = "SUCCESS";
|
|
61
|
+
ResponseCodes2["NOT_CONNECTED"] = "NOT_CONNECTED";
|
|
62
|
+
ResponseCodes2["ERROR"] = "ERROR";
|
|
63
|
+
ResponseCodes2["CANCELED"] = "CANCELED";
|
|
64
|
+
ResponseCodes2["USER_INTERACTION_REQUIRED"] = "USER_INTERACTION_REQUIRED";
|
|
65
|
+
return ResponseCodes2;
|
|
66
|
+
})(ResponseCodes || {});
|
|
67
|
+
|
|
68
|
+
// src/node/account.ts
|
|
70
69
|
var SessionAccount = class extends WalletAccount {
|
|
71
70
|
controller;
|
|
72
71
|
constructor(provider, {
|
|
@@ -80,7 +79,7 @@ var SessionAccount = class extends WalletAccount {
|
|
|
80
79
|
}) {
|
|
81
80
|
super({ nodeUrl: rpcUrl }, provider);
|
|
82
81
|
this.address = address;
|
|
83
|
-
this.controller = CartridgeSessionAccount.
|
|
82
|
+
this.controller = CartridgeSessionAccount.newAsRegistered(
|
|
84
83
|
rpcUrl,
|
|
85
84
|
privateKey,
|
|
86
85
|
address,
|
|
@@ -100,30 +99,34 @@ var SessionAccount = class extends WalletAccount {
|
|
|
100
99
|
* - entrypoint - the entrypoint of the contract
|
|
101
100
|
* - calldata - (defaults to []) the calldata
|
|
102
101
|
* - signature - (defaults to []) the signature
|
|
103
|
-
* @param abis (optional) the abi of the contract for better displaying
|
|
104
102
|
*
|
|
105
103
|
* @returns response from addTransaction
|
|
106
104
|
*/
|
|
107
105
|
async execute(calls) {
|
|
108
|
-
|
|
106
|
+
try {
|
|
107
|
+
const res = await this.controller.executeFromOutside(
|
|
108
|
+
normalizeCalls(calls)
|
|
109
|
+
);
|
|
110
|
+
return res;
|
|
111
|
+
} catch (e) {
|
|
112
|
+
return this.controller.execute(normalizeCalls(calls));
|
|
113
|
+
}
|
|
109
114
|
}
|
|
110
115
|
};
|
|
111
116
|
|
|
112
|
-
// src/
|
|
113
|
-
|
|
114
|
-
Permission
|
|
115
|
-
} from "@starknet-io/types-js";
|
|
117
|
+
// src/constants.ts
|
|
118
|
+
var KEYCHAIN_URL = "https://x.cartridge.gg";
|
|
116
119
|
|
|
117
120
|
// package.json
|
|
118
121
|
var package_default = {
|
|
119
122
|
name: "@cartridge/controller",
|
|
120
|
-
version: "0.
|
|
123
|
+
version: "0.7.1",
|
|
121
124
|
description: "Cartridge Controller",
|
|
122
125
|
module: "dist/index.js",
|
|
123
126
|
types: "dist/index.d.ts",
|
|
124
127
|
type: "module",
|
|
125
128
|
scripts: {
|
|
126
|
-
"build:deps": "tsup
|
|
129
|
+
"build:deps": "tsup",
|
|
127
130
|
build: "pnpm build:deps",
|
|
128
131
|
format: 'prettier --write "src/**/*.ts"',
|
|
129
132
|
"format:check": 'prettier --check "src/**/*.ts"',
|
|
@@ -133,52 +136,71 @@ var package_default = {
|
|
|
133
136
|
exports: {
|
|
134
137
|
".": {
|
|
135
138
|
types: "./dist/index.d.ts",
|
|
136
|
-
|
|
139
|
+
import: "./dist/index.js",
|
|
140
|
+
require: "./dist/index.cjs"
|
|
137
141
|
},
|
|
138
142
|
"./session": {
|
|
139
143
|
types: "./dist/session/index.d.ts",
|
|
140
|
-
|
|
144
|
+
import: "./dist/session/index.js",
|
|
145
|
+
require: "./dist/session/index.cjs"
|
|
146
|
+
},
|
|
147
|
+
"./session/node": {
|
|
148
|
+
types: "./dist/node/index.d.ts",
|
|
149
|
+
import: "./dist/node/index.js",
|
|
150
|
+
require: "./dist/node/index.cjs"
|
|
141
151
|
},
|
|
142
152
|
"./provider": {
|
|
143
153
|
types: "./dist/provider/index.d.ts",
|
|
144
|
-
|
|
154
|
+
import: "./dist/provider/index.js"
|
|
145
155
|
},
|
|
146
156
|
"./types": {
|
|
147
157
|
types: "./dist/types/index.d.ts",
|
|
148
|
-
|
|
158
|
+
import: "./dist/types/index.js"
|
|
149
159
|
}
|
|
150
160
|
},
|
|
151
161
|
tsup: {
|
|
152
162
|
entry: [
|
|
153
|
-
"src
|
|
163
|
+
"src/index.ts",
|
|
164
|
+
"src/controller.ts",
|
|
165
|
+
"src/lookup.ts",
|
|
166
|
+
"src/session/index.ts",
|
|
167
|
+
"src/node/index.ts"
|
|
154
168
|
],
|
|
155
169
|
format: [
|
|
156
|
-
"esm"
|
|
170
|
+
"esm",
|
|
171
|
+
"cjs"
|
|
157
172
|
],
|
|
158
173
|
splitting: false,
|
|
159
174
|
sourcemap: true,
|
|
160
|
-
clean: true
|
|
175
|
+
clean: true,
|
|
176
|
+
dts: true,
|
|
177
|
+
treeshake: {
|
|
178
|
+
preset: "recommended"
|
|
179
|
+
},
|
|
180
|
+
exports: "named"
|
|
161
181
|
},
|
|
162
182
|
peerDependencies: {
|
|
163
|
-
starknet: "
|
|
183
|
+
starknet: "catalog:",
|
|
184
|
+
open: "^10.1.0"
|
|
164
185
|
},
|
|
165
186
|
dependencies: {
|
|
166
187
|
"@cartridge/account-wasm": "workspace:*",
|
|
167
|
-
"@cartridge/penpal": "
|
|
168
|
-
"@starknet-io/types-js": "
|
|
188
|
+
"@cartridge/penpal": "catalog:",
|
|
189
|
+
"@starknet-io/types-js": "catalog:",
|
|
169
190
|
"@telegram-apps/sdk": "^2.4.0",
|
|
170
|
-
base64url: "
|
|
191
|
+
base64url: "catalog:",
|
|
171
192
|
"cbor-x": "^1.5.0",
|
|
172
|
-
"fast-deep-equal": "
|
|
173
|
-
"query-string": "^7.1.1"
|
|
193
|
+
"fast-deep-equal": "catalog:"
|
|
174
194
|
},
|
|
175
195
|
devDependencies: {
|
|
176
196
|
"@cartridge/tsconfig": "workspace:*",
|
|
177
197
|
"@types/jest": "^29.5.14",
|
|
178
|
-
"@types/node": "
|
|
198
|
+
"@types/node": "catalog:",
|
|
179
199
|
jest: "^29.7.0",
|
|
200
|
+
prettier: "catalog:",
|
|
180
201
|
"ts-jest": "^29.2.5",
|
|
181
|
-
|
|
202
|
+
tsup: "catalog:",
|
|
203
|
+
typescript: "catalog:"
|
|
182
204
|
}
|
|
183
205
|
};
|
|
184
206
|
|
|
@@ -357,77 +379,311 @@ var BaseProvider = class {
|
|
|
357
379
|
});
|
|
358
380
|
}
|
|
359
381
|
};
|
|
382
|
+
var CallbackServer = class {
|
|
383
|
+
server;
|
|
384
|
+
resolveCallback;
|
|
385
|
+
rejectCallback;
|
|
386
|
+
timeoutId;
|
|
387
|
+
constructor() {
|
|
388
|
+
this.server = http.createServer(this.handleRequest.bind(this));
|
|
389
|
+
this.server.on("error", (error) => {
|
|
390
|
+
console.error("Server error:", error);
|
|
391
|
+
if (this.rejectCallback) {
|
|
392
|
+
this.rejectCallback(error);
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
cleanup() {
|
|
397
|
+
if (this.timeoutId) {
|
|
398
|
+
clearTimeout(this.timeoutId);
|
|
399
|
+
}
|
|
400
|
+
this.server.close();
|
|
401
|
+
}
|
|
402
|
+
handleRequest(req, res) {
|
|
403
|
+
if (!req.url?.startsWith("/callback")) {
|
|
404
|
+
res.writeHead(404);
|
|
405
|
+
res.end();
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
const params = new URLSearchParams(req.url.split("?")[1]);
|
|
409
|
+
const session = params.get("startapp");
|
|
410
|
+
if (!session) {
|
|
411
|
+
console.warn("Received callback without session data");
|
|
412
|
+
res.writeHead(400);
|
|
413
|
+
res.end("Missing session data");
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (this.resolveCallback) {
|
|
417
|
+
this.resolveCallback(session);
|
|
418
|
+
}
|
|
419
|
+
res.writeHead(200, { "Content-Type": "text/html" });
|
|
420
|
+
res.end(
|
|
421
|
+
"<html><body><script>window.close();</script>Session registered successfully. You can close this window.</body></html>"
|
|
422
|
+
);
|
|
423
|
+
this.cleanup();
|
|
424
|
+
}
|
|
425
|
+
async listen() {
|
|
426
|
+
return new Promise((resolve, reject) => {
|
|
427
|
+
this.server.listen(0, "localhost", () => {
|
|
428
|
+
const address = this.server.address();
|
|
429
|
+
const url = `http://localhost:${address.port}/callback`;
|
|
430
|
+
resolve(url);
|
|
431
|
+
});
|
|
432
|
+
this.server.on("error", reject);
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
async waitForCallback() {
|
|
436
|
+
return new Promise((resolve, reject) => {
|
|
437
|
+
this.resolveCallback = resolve;
|
|
438
|
+
this.rejectCallback = reject;
|
|
439
|
+
this.timeoutId = setTimeout(
|
|
440
|
+
() => {
|
|
441
|
+
console.warn("Callback timeout reached");
|
|
442
|
+
reject(new Error("Callback timeout after 5 minutes"));
|
|
443
|
+
this.cleanup();
|
|
444
|
+
},
|
|
445
|
+
5 * 60 * 1e3
|
|
446
|
+
);
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
};
|
|
360
450
|
|
|
361
|
-
// src/
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
451
|
+
// src/node/backend.ts
|
|
452
|
+
var NodeBackend = class {
|
|
453
|
+
basePath;
|
|
454
|
+
sessionFile;
|
|
455
|
+
data = {};
|
|
456
|
+
callbackServer;
|
|
457
|
+
constructor(basePath) {
|
|
458
|
+
if (!basePath) {
|
|
459
|
+
throw new Error("basePath is required for NodeBackend");
|
|
460
|
+
}
|
|
461
|
+
this.basePath = basePath;
|
|
462
|
+
this.sessionFile = path.join(this.basePath, "session.json");
|
|
463
|
+
}
|
|
464
|
+
async ensureDirectoryExists() {
|
|
465
|
+
try {
|
|
466
|
+
await fs.access(this.basePath);
|
|
467
|
+
} catch {
|
|
468
|
+
try {
|
|
469
|
+
await fs.mkdir(this.basePath, { recursive: true });
|
|
470
|
+
} catch (error) {
|
|
471
|
+
throw new Error(
|
|
472
|
+
`Failed to create directory ${this.basePath}: ${error.message}`
|
|
473
|
+
);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
async loadData() {
|
|
478
|
+
try {
|
|
479
|
+
const content = await fs.readFile(this.sessionFile, "utf-8");
|
|
480
|
+
const parsed = JSON.parse(content);
|
|
481
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
482
|
+
throw new Error("Invalid session data format");
|
|
483
|
+
}
|
|
484
|
+
this.data = parsed;
|
|
485
|
+
} catch (error) {
|
|
486
|
+
if (error instanceof Error) {
|
|
487
|
+
if (error.code !== "ENOENT") {
|
|
488
|
+
throw new Error(`Failed to load session data: ${error.message}`);
|
|
374
489
|
}
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
490
|
+
}
|
|
491
|
+
this.data = {};
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
async saveData() {
|
|
495
|
+
try {
|
|
496
|
+
await this.ensureDirectoryExists();
|
|
497
|
+
await fs.writeFile(
|
|
498
|
+
this.sessionFile,
|
|
499
|
+
JSON.stringify(this.data, null, 2),
|
|
500
|
+
"utf-8"
|
|
501
|
+
);
|
|
502
|
+
} catch (error) {
|
|
503
|
+
if (error instanceof Error) {
|
|
504
|
+
throw new Error(`Failed to save session data: ${error.message}`);
|
|
505
|
+
}
|
|
506
|
+
throw error;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
async get(key) {
|
|
510
|
+
if (!key) {
|
|
511
|
+
throw new Error("Key is required");
|
|
512
|
+
}
|
|
513
|
+
await this.loadData();
|
|
514
|
+
return this.data[key] ? JSON.stringify(this.data[key]) : null;
|
|
515
|
+
}
|
|
516
|
+
async set(key, value) {
|
|
517
|
+
if (!key) {
|
|
518
|
+
throw new Error("Key is required");
|
|
519
|
+
}
|
|
520
|
+
if (!value) {
|
|
521
|
+
throw new Error("Value is required");
|
|
522
|
+
}
|
|
523
|
+
await this.loadData();
|
|
524
|
+
try {
|
|
525
|
+
this.data[key] = JSON.parse(value);
|
|
526
|
+
await this.saveData();
|
|
527
|
+
} catch (error) {
|
|
528
|
+
if (error instanceof Error) {
|
|
529
|
+
throw new Error(`Failed to set ${key}: ${error.message}`);
|
|
530
|
+
}
|
|
531
|
+
throw error;
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
async delete(key) {
|
|
535
|
+
if (!key) {
|
|
536
|
+
throw new Error("Key is required");
|
|
537
|
+
}
|
|
538
|
+
await this.loadData();
|
|
539
|
+
delete this.data[key];
|
|
540
|
+
await this.saveData();
|
|
541
|
+
}
|
|
542
|
+
async getRedirectUri() {
|
|
543
|
+
try {
|
|
544
|
+
this.callbackServer = new CallbackServer();
|
|
545
|
+
return await this.callbackServer.listen();
|
|
546
|
+
} catch (error) {
|
|
547
|
+
if (error instanceof Error) {
|
|
548
|
+
throw new Error(`Failed to start callback server: ${error.message}`);
|
|
549
|
+
}
|
|
550
|
+
throw error;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
async waitForCallback() {
|
|
554
|
+
if (!this.callbackServer) {
|
|
555
|
+
throw new Error("Callback server not initialized");
|
|
556
|
+
}
|
|
557
|
+
return await this.callbackServer.waitForCallback();
|
|
558
|
+
}
|
|
559
|
+
openLink(url) {
|
|
560
|
+
if (!url) {
|
|
561
|
+
throw new Error("URL is required");
|
|
562
|
+
}
|
|
563
|
+
console.log(`
|
|
564
|
+
Open url to authorize session: ${url}`);
|
|
565
|
+
}
|
|
566
|
+
};
|
|
383
567
|
|
|
384
|
-
// src/
|
|
385
|
-
var
|
|
386
|
-
|
|
568
|
+
// src/node/provider.ts
|
|
569
|
+
var SessionProvider = class extends BaseProvider {
|
|
570
|
+
id = "controller_session";
|
|
571
|
+
name = "Controller Session";
|
|
387
572
|
_chainId;
|
|
573
|
+
_rpcUrl;
|
|
388
574
|
_username;
|
|
389
575
|
_policies;
|
|
390
|
-
|
|
576
|
+
_keychainUrl;
|
|
577
|
+
_backend;
|
|
391
578
|
constructor({
|
|
392
579
|
rpc,
|
|
393
580
|
chainId,
|
|
394
581
|
policies,
|
|
395
|
-
|
|
582
|
+
basePath,
|
|
583
|
+
keychainUrl
|
|
396
584
|
}) {
|
|
397
585
|
super();
|
|
586
|
+
this._policies = {
|
|
587
|
+
verified: false,
|
|
588
|
+
contracts: policies.contracts ? Object.fromEntries(
|
|
589
|
+
Object.entries(policies.contracts).map(([address, contract]) => [
|
|
590
|
+
address,
|
|
591
|
+
{
|
|
592
|
+
...contract,
|
|
593
|
+
methods: contract.methods.map((method) => ({
|
|
594
|
+
...method,
|
|
595
|
+
authorized: true
|
|
596
|
+
}))
|
|
597
|
+
}
|
|
598
|
+
])
|
|
599
|
+
) : undefined,
|
|
600
|
+
messages: policies.messages?.map((message) => ({
|
|
601
|
+
...message,
|
|
602
|
+
authorized: true
|
|
603
|
+
}))
|
|
604
|
+
};
|
|
398
605
|
this._rpcUrl = rpc;
|
|
399
|
-
this._tmaUrl = tmaUrl;
|
|
400
606
|
this._chainId = chainId;
|
|
401
|
-
this.
|
|
402
|
-
|
|
403
|
-
|
|
607
|
+
this._keychainUrl = keychainUrl || KEYCHAIN_URL;
|
|
608
|
+
this._backend = new NodeBackend(basePath);
|
|
609
|
+
}
|
|
610
|
+
async username() {
|
|
611
|
+
const sessionStr = await this._backend.get("session");
|
|
612
|
+
if (sessionStr) {
|
|
613
|
+
const session = JSON.parse(sessionStr);
|
|
614
|
+
return session.username;
|
|
404
615
|
}
|
|
616
|
+
return undefined;
|
|
405
617
|
}
|
|
406
618
|
async probe() {
|
|
407
|
-
|
|
408
|
-
|
|
619
|
+
if (this.account) {
|
|
620
|
+
return this.account;
|
|
621
|
+
}
|
|
622
|
+
const [sessionStr, signerStr] = await Promise.all([
|
|
623
|
+
this._backend.get("session"),
|
|
624
|
+
this._backend.get("signer")
|
|
625
|
+
]);
|
|
626
|
+
if (!sessionStr || !signerStr) {
|
|
627
|
+
return undefined;
|
|
628
|
+
}
|
|
629
|
+
const session = JSON.parse(sessionStr);
|
|
630
|
+
const signer = JSON.parse(signerStr);
|
|
631
|
+
const expirationTime = parseInt(session.expiresAt) * 1e3;
|
|
632
|
+
if (Date.now() >= expirationTime) {
|
|
633
|
+
await this.disconnect();
|
|
634
|
+
return undefined;
|
|
635
|
+
}
|
|
636
|
+
this._username = session.username;
|
|
637
|
+
this.account = new SessionAccount(this, {
|
|
638
|
+
rpcUrl: this._rpcUrl,
|
|
639
|
+
privateKey: signer.privKey,
|
|
640
|
+
address: session.address,
|
|
641
|
+
ownerGuid: session.ownerGuid,
|
|
642
|
+
chainId: this._chainId,
|
|
643
|
+
expiresAt: parseInt(session.expiresAt),
|
|
644
|
+
policies: toWasmPolicies(this._policies)
|
|
645
|
+
});
|
|
646
|
+
return this.account;
|
|
409
647
|
}
|
|
410
648
|
async connect() {
|
|
411
|
-
await this.tryRetrieveFromQueryOrStorage();
|
|
412
649
|
if (this.account) {
|
|
413
|
-
return;
|
|
650
|
+
return this.account;
|
|
651
|
+
}
|
|
652
|
+
const account = await this.probe();
|
|
653
|
+
if (account) {
|
|
654
|
+
return account;
|
|
414
655
|
}
|
|
415
656
|
const pk = stark.randomAddress();
|
|
416
657
|
const publicKey = ec.starkCurve.getStarkKey(pk);
|
|
417
|
-
|
|
418
|
-
"
|
|
658
|
+
await this._backend.set(
|
|
659
|
+
"signer",
|
|
419
660
|
JSON.stringify({
|
|
420
661
|
privKey: pk,
|
|
421
662
|
pubKey: publicKey
|
|
422
663
|
})
|
|
423
664
|
);
|
|
424
|
-
const
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
665
|
+
const redirectUri = await this._backend.getRedirectUri();
|
|
666
|
+
const url = `${this._keychainUrl}/session?public_key=${encodeURIComponent(publicKey)}&redirect_uri=${encodeURIComponent(
|
|
667
|
+
redirectUri
|
|
668
|
+
)}&redirect_query_name=startapp&policies=${encodeURIComponent(
|
|
669
|
+
JSON.stringify(this._policies)
|
|
670
|
+
)}&rpc_url=${encodeURIComponent(this._rpcUrl)}`;
|
|
671
|
+
this._backend.openLink(url);
|
|
672
|
+
const sessionData = await this._backend.waitForCallback();
|
|
673
|
+
if (sessionData) {
|
|
674
|
+
const sessionRegistration = JSON.parse(atob(sessionData));
|
|
675
|
+
sessionRegistration.address = sessionRegistration.address.toLowerCase();
|
|
676
|
+
sessionRegistration.ownerGuid = sessionRegistration.ownerGuid.toLowerCase();
|
|
677
|
+
await this._backend.set("session", JSON.stringify(sessionRegistration));
|
|
678
|
+
return this.probe();
|
|
679
|
+
}
|
|
680
|
+
return undefined;
|
|
681
|
+
}
|
|
682
|
+
async disconnect() {
|
|
683
|
+
await this._backend.delete("signer");
|
|
684
|
+
await this._backend.delete("session");
|
|
685
|
+
this.account = undefined;
|
|
686
|
+
this._username = undefined;
|
|
431
687
|
}
|
|
432
688
|
switchStarknetChain(_chainId) {
|
|
433
689
|
throw new Error("switchStarknetChain not implemented");
|
|
@@ -435,45 +691,8 @@ var TelegramProvider = class extends BaseProvider {
|
|
|
435
691
|
addStarknetChain(_chain) {
|
|
436
692
|
throw new Error("addStarknetChain not implemented");
|
|
437
693
|
}
|
|
438
|
-
disconnect() {
|
|
439
|
-
cloudStorage.deleteItem("sessionSigner");
|
|
440
|
-
cloudStorage.deleteItem("session");
|
|
441
|
-
this.account = void 0;
|
|
442
|
-
this._username = void 0;
|
|
443
|
-
return Promise.resolve();
|
|
444
|
-
}
|
|
445
|
-
async tryRetrieveFromQueryOrStorage() {
|
|
446
|
-
const signer = JSON.parse(await cloudStorage.getItem("sessionSigner"));
|
|
447
|
-
let sessionRegistration = null;
|
|
448
|
-
const launchParams = retrieveLaunchParams();
|
|
449
|
-
const session = launchParams.startParam;
|
|
450
|
-
if (session) {
|
|
451
|
-
sessionRegistration = JSON.parse(atob(session));
|
|
452
|
-
cloudStorage.setItem("session", JSON.stringify(sessionRegistration));
|
|
453
|
-
}
|
|
454
|
-
if (!sessionRegistration) {
|
|
455
|
-
const session2 = await cloudStorage.getItem("session");
|
|
456
|
-
if (session2) {
|
|
457
|
-
sessionRegistration = JSON.parse(session2);
|
|
458
|
-
}
|
|
459
|
-
}
|
|
460
|
-
if (!sessionRegistration) {
|
|
461
|
-
return;
|
|
462
|
-
}
|
|
463
|
-
this._username = sessionRegistration.username;
|
|
464
|
-
this.account = new SessionAccount(this, {
|
|
465
|
-
rpcUrl: this._rpcUrl,
|
|
466
|
-
privateKey: signer.privKey,
|
|
467
|
-
address: sessionRegistration.address,
|
|
468
|
-
ownerGuid: sessionRegistration.ownerGuid,
|
|
469
|
-
chainId: this._chainId,
|
|
470
|
-
expiresAt: parseInt(sessionRegistration.expiresAt),
|
|
471
|
-
policies: toWasmPolicies(this._policies)
|
|
472
|
-
});
|
|
473
|
-
return this.account;
|
|
474
|
-
}
|
|
475
|
-
};
|
|
476
|
-
export {
|
|
477
|
-
TelegramProvider as default
|
|
478
694
|
};
|
|
479
|
-
|
|
695
|
+
|
|
696
|
+
export { NotReadyToConnect, ResponseCodes, SessionProvider as default };
|
|
697
|
+
//# sourceMappingURL=index.js.map
|
|
698
|
+
//# sourceMappingURL=index.js.map
|