@cartridge/controller 0.10.4 → 0.10.6
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 +15 -15
- package/.turbo/turbo-build.log +14 -14
- package/dist/index.js +2 -2
- package/dist/node/index.cjs +8 -1
- package/dist/node/index.cjs.map +1 -1
- package/dist/node/index.js +9 -2
- package/dist/node/index.js.map +1 -1
- package/dist/{provider-PftcmETC.js → provider-BgBI_LQl.js} +2 -2
- package/dist/{provider-PftcmETC.js.map → provider-BgBI_LQl.js.map} +1 -1
- package/dist/session/provider.d.ts +5 -2
- package/dist/session.js +138 -106
- package/dist/session.js.map +1 -1
- package/dist/stats.html +1 -1
- package/package.json +2 -2
- package/src/node/provider.ts +8 -1
- package/src/session/provider.ts +79 -39
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cartridge/controller",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.6",
|
|
4
4
|
"description": "Cartridge Controller",
|
|
5
5
|
"module": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"vite-plugin-node-polyfills": "^0.23.0",
|
|
51
51
|
"vite-plugin-top-level-await": "^1.4.4",
|
|
52
52
|
"vite-plugin-wasm": "^3.4.1",
|
|
53
|
-
"@cartridge/tsconfig": "0.10.
|
|
53
|
+
"@cartridge/tsconfig": "0.10.6"
|
|
54
54
|
},
|
|
55
55
|
"scripts": {
|
|
56
56
|
"build:deps": "pnpm build",
|
package/src/node/provider.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { ec, stark, WalletAccount } from "starknet";
|
|
1
|
+
import { ec, encode, stark, WalletAccount } from "starknet";
|
|
2
2
|
import { SessionPolicies } from "@cartridge/presets";
|
|
3
3
|
import { AddStarknetChainParameters } from "@starknet-io/types-js";
|
|
4
|
+
import { signerToGuid } from "@cartridge/controller-wasm";
|
|
4
5
|
|
|
5
6
|
import SessionAccount from "./account";
|
|
6
7
|
import { KEYCHAIN_URL } from "../constants";
|
|
@@ -153,10 +154,16 @@ export default class SessionProvider extends BaseProvider {
|
|
|
153
154
|
const sessionData = await this._backend.waitForCallback();
|
|
154
155
|
if (sessionData) {
|
|
155
156
|
const sessionRegistration = JSON.parse(atob(sessionData));
|
|
157
|
+
const formattedPk = encode.addHexPrefix(publicKey);
|
|
156
158
|
// Ensure addresses are properly formatted
|
|
157
159
|
sessionRegistration.address = sessionRegistration.address.toLowerCase();
|
|
158
160
|
sessionRegistration.ownerGuid =
|
|
159
161
|
sessionRegistration.ownerGuid.toLowerCase();
|
|
162
|
+
sessionRegistration.guardianKeyGuid = "0x0";
|
|
163
|
+
sessionRegistration.metadataHash = "0x0";
|
|
164
|
+
sessionRegistration.sessionKeyGuid = signerToGuid({
|
|
165
|
+
starknet: { privateKey: formattedPk },
|
|
166
|
+
});
|
|
160
167
|
await this._backend.set("session", JSON.stringify(sessionRegistration));
|
|
161
168
|
return this.probe();
|
|
162
169
|
}
|
package/src/session/provider.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
import { SessionPolicies } from "@cartridge/presets";
|
|
8
8
|
import { AddStarknetChainParameters } from "@starknet-io/types-js";
|
|
9
9
|
import { encode } from "starknet";
|
|
10
|
-
import { KEYCHAIN_URL } from "../constants";
|
|
10
|
+
import { API_URL, KEYCHAIN_URL } from "../constants";
|
|
11
11
|
import { ParsedSessionPolicies } from "../policies";
|
|
12
12
|
import BaseProvider from "../provider";
|
|
13
13
|
import { toWasmPolicies } from "../utils";
|
|
@@ -43,7 +43,10 @@ export default class SessionProvider extends BaseProvider {
|
|
|
43
43
|
protected _redirectUrl: string;
|
|
44
44
|
protected _policies: ParsedSessionPolicies;
|
|
45
45
|
protected _keychainUrl: string;
|
|
46
|
-
protected _apiUrl
|
|
46
|
+
protected _apiUrl: string;
|
|
47
|
+
protected _publicKey: string;
|
|
48
|
+
protected _sessionKeyGuid: string;
|
|
49
|
+
public reopenBrowser: boolean = true;
|
|
47
50
|
|
|
48
51
|
constructor({
|
|
49
52
|
rpc,
|
|
@@ -81,7 +84,37 @@ export default class SessionProvider extends BaseProvider {
|
|
|
81
84
|
this._chainId = chainId;
|
|
82
85
|
this._redirectUrl = redirectUrl;
|
|
83
86
|
this._keychainUrl = keychainUrl || KEYCHAIN_URL;
|
|
84
|
-
this._apiUrl = apiUrl;
|
|
87
|
+
this._apiUrl = apiUrl ?? API_URL;
|
|
88
|
+
|
|
89
|
+
const account = this.tryRetrieveFromQueryOrStorage();
|
|
90
|
+
if (!account) {
|
|
91
|
+
const pk = stark.randomAddress();
|
|
92
|
+
this._publicKey = ec.starkCurve.getStarkKey(pk);
|
|
93
|
+
|
|
94
|
+
localStorage.setItem(
|
|
95
|
+
"sessionSigner",
|
|
96
|
+
JSON.stringify({
|
|
97
|
+
privKey: pk,
|
|
98
|
+
pubKey: this._publicKey,
|
|
99
|
+
}),
|
|
100
|
+
);
|
|
101
|
+
this._sessionKeyGuid = signerToGuid({
|
|
102
|
+
starknet: { privateKey: encode.addHexPrefix(pk) },
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
const pk = localStorage.getItem("sessionSigner");
|
|
106
|
+
if (!pk) throw new Error("failed to get sessionSigner");
|
|
107
|
+
|
|
108
|
+
const jsonPk: {
|
|
109
|
+
privKey: string;
|
|
110
|
+
pubKey: string;
|
|
111
|
+
} = JSON.parse(pk);
|
|
112
|
+
|
|
113
|
+
this._publicKey = jsonPk.pubKey;
|
|
114
|
+
this._sessionKeyGuid = signerToGuid({
|
|
115
|
+
starknet: { privateKey: encode.addHexPrefix(jsonPk.privKey) },
|
|
116
|
+
});
|
|
117
|
+
}
|
|
85
118
|
|
|
86
119
|
if (typeof window !== "undefined") {
|
|
87
120
|
(window as any).starknet_controller_session = this;
|
|
@@ -134,7 +167,7 @@ export default class SessionProvider extends BaseProvider {
|
|
|
134
167
|
return this.account;
|
|
135
168
|
}
|
|
136
169
|
|
|
137
|
-
this.account =
|
|
170
|
+
this.account = this.tryRetrieveFromQueryOrStorage();
|
|
138
171
|
return this.account;
|
|
139
172
|
}
|
|
140
173
|
|
|
@@ -143,46 +176,43 @@ export default class SessionProvider extends BaseProvider {
|
|
|
143
176
|
return this.account;
|
|
144
177
|
}
|
|
145
178
|
|
|
146
|
-
this.account =
|
|
179
|
+
this.account = this.tryRetrieveFromQueryOrStorage();
|
|
147
180
|
if (this.account) {
|
|
148
181
|
return this.account;
|
|
149
182
|
}
|
|
150
183
|
|
|
151
|
-
const pk = stark.randomAddress();
|
|
152
|
-
const publicKey = ec.starkCurve.getStarkKey(pk);
|
|
153
|
-
|
|
154
|
-
localStorage.setItem(
|
|
155
|
-
"sessionSigner",
|
|
156
|
-
JSON.stringify({
|
|
157
|
-
privKey: pk,
|
|
158
|
-
pubKey: publicKey,
|
|
159
|
-
}),
|
|
160
|
-
);
|
|
161
|
-
|
|
162
184
|
localStorage.setItem("sessionPolicies", JSON.stringify(this._policies));
|
|
163
|
-
|
|
164
|
-
const url = `${
|
|
165
|
-
this._keychainUrl
|
|
166
|
-
}/session?public_key=${publicKey}&redirect_uri=${
|
|
167
|
-
this._redirectUrl
|
|
168
|
-
}&redirect_query_name=startapp&policies=${JSON.stringify(
|
|
169
|
-
this._policies,
|
|
170
|
-
)}&rpc_url=${this._rpcUrl}`;
|
|
171
|
-
|
|
172
185
|
localStorage.setItem("lastUsedConnector", this.id);
|
|
173
|
-
const openedWindow = window.open(url, "_blank");
|
|
174
186
|
|
|
175
187
|
try {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
188
|
+
if (this.reopenBrowser) {
|
|
189
|
+
const pk = stark.randomAddress();
|
|
190
|
+
this._publicKey = ec.starkCurve.getStarkKey(pk);
|
|
191
|
+
|
|
192
|
+
localStorage.setItem(
|
|
193
|
+
"sessionSigner",
|
|
194
|
+
JSON.stringify({
|
|
195
|
+
privKey: pk,
|
|
196
|
+
pubKey: this._publicKey,
|
|
197
|
+
}),
|
|
198
|
+
);
|
|
199
|
+
this._sessionKeyGuid = signerToGuid({
|
|
200
|
+
starknet: { privateKey: encode.addHexPrefix(pk) },
|
|
201
|
+
});
|
|
202
|
+
const url = `${
|
|
203
|
+
this._keychainUrl
|
|
204
|
+
}/session?public_key=${this._publicKey}&redirect_uri=${
|
|
205
|
+
this._redirectUrl
|
|
206
|
+
}&redirect_query_name=startapp&policies=${JSON.stringify(
|
|
207
|
+
this._policies,
|
|
208
|
+
)}&rpc_url=${this._rpcUrl}`;
|
|
209
|
+
|
|
210
|
+
window.open(url, "_blank");
|
|
211
|
+
}
|
|
181
212
|
|
|
182
|
-
const cartridgeApiUrl = this._apiUrl ?? "https://api.cartridge.gg";
|
|
183
213
|
const sessionResult = await subscribeCreateSession(
|
|
184
|
-
|
|
185
|
-
|
|
214
|
+
this._sessionKeyGuid,
|
|
215
|
+
this._apiUrl,
|
|
186
216
|
);
|
|
187
217
|
|
|
188
218
|
// auth is: [shortstring!('authorization-by-registered'), owner_guid]
|
|
@@ -194,17 +224,16 @@ export default class SessionProvider extends BaseProvider {
|
|
|
194
224
|
expiresAt: sessionResult.expiresAt,
|
|
195
225
|
guardianKeyGuid: "0x0",
|
|
196
226
|
metadataHash: "0x0",
|
|
197
|
-
sessionKeyGuid,
|
|
227
|
+
sessionKeyGuid: this._sessionKeyGuid,
|
|
198
228
|
};
|
|
199
229
|
localStorage.setItem("session", JSON.stringify(session));
|
|
200
230
|
|
|
201
231
|
this.tryRetrieveFromQueryOrStorage();
|
|
202
232
|
|
|
203
|
-
openedWindow?.close();
|
|
204
|
-
|
|
205
233
|
return this.account;
|
|
206
234
|
} catch (e) {
|
|
207
235
|
console.log(e);
|
|
236
|
+
throw e;
|
|
208
237
|
}
|
|
209
238
|
}
|
|
210
239
|
|
|
@@ -222,10 +251,21 @@ export default class SessionProvider extends BaseProvider {
|
|
|
222
251
|
localStorage.removeItem("sessionPolicies");
|
|
223
252
|
this.account = undefined;
|
|
224
253
|
this._username = undefined;
|
|
225
|
-
|
|
254
|
+
const openedWindow = window.open(`${this._keychainUrl}/disconnect`);
|
|
255
|
+
if (openedWindow === null) return Promise.resolve();
|
|
256
|
+
|
|
257
|
+
const { resolve, promise } = Promise.withResolvers<void>();
|
|
258
|
+
function onWindowClose() {
|
|
259
|
+
if (openedWindow?.closed) {
|
|
260
|
+
resolve();
|
|
261
|
+
clearInterval(checkInterval);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
const checkInterval = setInterval(onWindowClose, 500);
|
|
265
|
+
return promise;
|
|
226
266
|
}
|
|
227
267
|
|
|
228
|
-
|
|
268
|
+
tryRetrieveFromQueryOrStorage() {
|
|
229
269
|
if (this.account) {
|
|
230
270
|
return this.account;
|
|
231
271
|
}
|