@cartridge/controller 0.5.8 → 0.5.9
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 +72 -67
- package/.turbo/turbo-build.log +73 -68
- package/dist/__tests__/parseChainId.test.d.ts +2 -0
- package/dist/__tests__/parseChainId.test.js +89 -0
- package/dist/__tests__/parseChainId.test.js.map +1 -0
- package/dist/account.js +2 -0
- package/dist/account.js.map +1 -1
- package/dist/controller.d.ts +1 -1
- package/dist/controller.js +42 -28
- package/dist/controller.js.map +1 -1
- package/dist/index.js +42 -28
- package/dist/index.js.map +1 -1
- package/dist/provider.js +5 -1
- package/dist/provider.js.map +1 -1
- package/dist/session/account.js +2 -0
- package/dist/session/account.js.map +1 -1
- package/dist/session/index.js +7 -1
- package/dist/session/index.js.map +1 -1
- package/dist/session/provider.js +7 -1
- package/dist/session/provider.js.map +1 -1
- package/dist/telegram/provider.js +7 -1
- package/dist/telegram/provider.js.map +1 -1
- package/dist/utils.d.ts +3 -2
- package/dist/utils.js +25 -0
- package/dist/utils.js.map +1 -1
- package/jest.config.ts +13 -0
- package/package.json +7 -3
- package/src/__tests__/parseChainId.test.ts +60 -0
- package/src/controller.ts +24 -29
- package/src/utils.ts +28 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { constants, shortString } from "starknet";
|
|
2
|
+
import { parseChainId } from "../utils";
|
|
3
|
+
|
|
4
|
+
describe("parseChainId", () => {
|
|
5
|
+
describe("Starknet chains", () => {
|
|
6
|
+
test("identifies mainnet", () => {
|
|
7
|
+
expect(
|
|
8
|
+
parseChainId(new URL("https://api.cartridge.gg/x/starknet/mainnet")),
|
|
9
|
+
).toBe(constants.StarknetChainId.SN_MAIN);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("identifies sepolia", () => {
|
|
13
|
+
expect(
|
|
14
|
+
parseChainId(new URL("https://api.cartridge.gg/x/starknet/sepolia")),
|
|
15
|
+
).toBe(constants.StarknetChainId.SN_SEPOLIA);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
describe("Project-specific chains", () => {
|
|
20
|
+
test("identifies slot chain", () => {
|
|
21
|
+
expect(
|
|
22
|
+
parseChainId(new URL("https://api.cartridge.gg/x/slot/katana")),
|
|
23
|
+
).toBe(shortString.encodeShortString("WP_SLOT"));
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test("identifies slot chain on localhost", () => {
|
|
27
|
+
expect(parseChainId(new URL("http://localhost:8001/x/slot/katana"))).toBe(
|
|
28
|
+
shortString.encodeShortString("WP_SLOT"),
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("identifies slot chain with hyphenated name", () => {
|
|
33
|
+
expect(
|
|
34
|
+
parseChainId(
|
|
35
|
+
new URL("https://api.cartridge.gg/x/my-slot-chain/katana"),
|
|
36
|
+
),
|
|
37
|
+
).toBe(shortString.encodeShortString("WP_MY_SLOT_CHAIN"));
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("identifies slot mainnet chain", () => {
|
|
41
|
+
expect(
|
|
42
|
+
parseChainId(new URL("https://api.cartridge.gg/x/slot/mainnet")),
|
|
43
|
+
).toBe(shortString.encodeShortString("GG_SLOT"));
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("Error cases", () => {
|
|
48
|
+
test("throws error for unsupported URL format", () => {
|
|
49
|
+
expect(() =>
|
|
50
|
+
parseChainId(new URL("https://api.example.com/unsupported")),
|
|
51
|
+
).toThrow("Chain https://api.example.com/unsupported not supported");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("throws error for URLs without proper chain identifiers", () => {
|
|
55
|
+
expect(() =>
|
|
56
|
+
parseChainId(new URL("https://api.example.com/v1/starknet")),
|
|
57
|
+
).toThrow("Chain https://api.example.com/v1/starknet not supported");
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
});
|
package/src/controller.ts
CHANGED
|
@@ -16,9 +16,10 @@ import {
|
|
|
16
16
|
Chain,
|
|
17
17
|
} from "./types";
|
|
18
18
|
import BaseProvider from "./provider";
|
|
19
|
-
import {
|
|
19
|
+
import { WalletAccount } from "starknet";
|
|
20
20
|
import { Policy } from "@cartridge/presets";
|
|
21
21
|
import { AddStarknetChainParameters, ChainId } from "@starknet-io/types-js";
|
|
22
|
+
import { parseChainId } from "./utils";
|
|
22
23
|
|
|
23
24
|
export default class ControllerProvider extends BaseProvider {
|
|
24
25
|
private keychain?: AsyncMethodReturns<Keychain>;
|
|
@@ -34,28 +35,8 @@ export default class ControllerProvider extends BaseProvider {
|
|
|
34
35
|
const chains = new Map<ChainId, Chain>();
|
|
35
36
|
|
|
36
37
|
for (const chain of options.chains) {
|
|
37
|
-
let chainId: ChainId | undefined;
|
|
38
38
|
const url = new URL(chain.rpcUrl);
|
|
39
|
-
const
|
|
40
|
-
if (parts.includes("starknet")) {
|
|
41
|
-
if (parts.includes("mainnet")) {
|
|
42
|
-
chainId = constants.StarknetChainId.SN_MAIN;
|
|
43
|
-
} else if (parts.includes("sepolia")) {
|
|
44
|
-
chainId = constants.StarknetChainId.SN_SEPOLIA;
|
|
45
|
-
}
|
|
46
|
-
} else if (parts.length >= 3) {
|
|
47
|
-
const projectName = parts[2];
|
|
48
|
-
if (parts.includes("katana")) {
|
|
49
|
-
chainId = `WP_${projectName.toUpperCase()}` as ChainId;
|
|
50
|
-
} else if (parts.includes("mainnet")) {
|
|
51
|
-
chainId = `GG_${projectName.toUpperCase()}` as ChainId;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (!chainId) {
|
|
56
|
-
throw new Error(`Chain ${chain.rpcUrl} not supported`);
|
|
57
|
-
}
|
|
58
|
-
|
|
39
|
+
const chainId = parseChainId(url);
|
|
59
40
|
chains.set(chainId, chain);
|
|
60
41
|
}
|
|
61
42
|
|
|
@@ -305,7 +286,7 @@ export default class ControllerProvider extends BaseProvider {
|
|
|
305
286
|
this.keychain.openPurchaseCredits();
|
|
306
287
|
}
|
|
307
288
|
|
|
308
|
-
async openExecute(calls: any) {
|
|
289
|
+
async openExecute(calls: any, chainId?: string) {
|
|
309
290
|
if (!this.keychain || !this.iframes.keychain) {
|
|
310
291
|
console.error(new NotReadyToConnect().message);
|
|
311
292
|
return;
|
|
@@ -314,16 +295,30 @@ export default class ControllerProvider extends BaseProvider {
|
|
|
314
295
|
console.error("Profile is not ready");
|
|
315
296
|
return;
|
|
316
297
|
}
|
|
317
|
-
if
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
this.
|
|
298
|
+
// Switch to the chain if provided
|
|
299
|
+
let currentChainId = this.selectedChain;
|
|
300
|
+
if (chainId) {
|
|
301
|
+
this.switchStarknetChain(chainId);
|
|
321
302
|
}
|
|
303
|
+
// Switch iframes
|
|
304
|
+
this.iframes.profile?.sendBackward();
|
|
322
305
|
this.iframes.keychain.open();
|
|
306
|
+
this.iframes.profile?.close();
|
|
307
|
+
// Invoke execute
|
|
323
308
|
const res = await this.keychain.execute(calls, undefined, undefined, true);
|
|
309
|
+
// Switch back iframes
|
|
310
|
+
this.iframes.profile?.open();
|
|
324
311
|
this.iframes.keychain.close();
|
|
325
|
-
this.iframes.profile?.sendForward
|
|
326
|
-
|
|
312
|
+
this.iframes.profile?.sendForward();
|
|
313
|
+
// Switch back to the original chain
|
|
314
|
+
if (chainId) {
|
|
315
|
+
this.switchStarknetChain(currentChainId);
|
|
316
|
+
}
|
|
317
|
+
return !(
|
|
318
|
+
res &&
|
|
319
|
+
((res as ConnectError).code === ResponseCodes.NOT_CONNECTED ||
|
|
320
|
+
(res as ConnectError).code === ResponseCodes.CANCELED)
|
|
321
|
+
);
|
|
327
322
|
}
|
|
328
323
|
|
|
329
324
|
async delegateAccount() {
|
package/src/utils.ts
CHANGED
|
@@ -2,13 +2,16 @@ import {
|
|
|
2
2
|
addAddressPadding,
|
|
3
3
|
Call,
|
|
4
4
|
CallData,
|
|
5
|
+
constants,
|
|
5
6
|
getChecksumAddress,
|
|
6
7
|
hash,
|
|
8
|
+
shortString,
|
|
7
9
|
typedData,
|
|
8
10
|
TypedDataRevision,
|
|
9
11
|
} from "starknet";
|
|
10
12
|
import wasm from "@cartridge/account-wasm/controller";
|
|
11
13
|
import { Policies, SessionPolicies } from "@cartridge/presets";
|
|
14
|
+
import { ChainId } from "@starknet-io/types-js";
|
|
12
15
|
|
|
13
16
|
// Whitelist of allowed property names to prevent prototype pollution
|
|
14
17
|
const ALLOWED_PROPERTIES = new Set([
|
|
@@ -129,3 +132,28 @@ export function humanizeString(str: string): string {
|
|
|
129
132
|
.replace(/^\w/, (c) => c.toUpperCase())
|
|
130
133
|
);
|
|
131
134
|
}
|
|
135
|
+
|
|
136
|
+
export function parseChainId(url: URL): ChainId {
|
|
137
|
+
const parts = url.pathname.split("/");
|
|
138
|
+
|
|
139
|
+
if (parts.includes("starknet")) {
|
|
140
|
+
if (parts.includes("mainnet")) {
|
|
141
|
+
return constants.StarknetChainId.SN_MAIN;
|
|
142
|
+
} else if (parts.includes("sepolia")) {
|
|
143
|
+
return constants.StarknetChainId.SN_SEPOLIA;
|
|
144
|
+
}
|
|
145
|
+
} else if (parts.length >= 3) {
|
|
146
|
+
const projectName = parts[2];
|
|
147
|
+
if (parts.includes("katana")) {
|
|
148
|
+
return shortString.encodeShortString(
|
|
149
|
+
`WP_${projectName.toUpperCase().replace(/-/g, "_")}`,
|
|
150
|
+
) as ChainId;
|
|
151
|
+
} else if (parts.includes("mainnet")) {
|
|
152
|
+
return shortString.encodeShortString(
|
|
153
|
+
`GG_${projectName.toUpperCase().replace(/-/g, "_")}`,
|
|
154
|
+
) as ChainId;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
throw new Error(`Chain ${url.toString()} not supported`);
|
|
159
|
+
}
|