Skip to content

Commit 6689201

Browse files
committed
fix: support non-Cartridge chainId lookup
1 parent f29bf3c commit 6689201

File tree

4 files changed

+24
-33
lines changed

4 files changed

+24
-33
lines changed

packages/controller/src/__tests__/controllerDefaults.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,30 @@ describe("ControllerProvider defaults", () => {
4141
);
4242
});
4343

44-
test("should throw error when using non-Cartridge RPC for mainnet", async () => {
45-
const invalidChains = [
44+
test("should allow non-Cartridge RPC for mainnet", async () => {
45+
const customChains = [
4646
{ rpcUrl: "https://some-other-provider.com/starknet/mainnet/rpc/v0_9" },
4747
];
4848

4949
expect(() => {
5050
new ControllerProvider({
51-
chains: invalidChains,
51+
chains: customChains,
5252
defaultChainId: constants.StarknetChainId.SN_MAIN,
5353
});
54-
}).toThrow("Only Cartridge RPC providers are allowed for mainnet");
54+
}).not.toThrow();
5555
});
5656

57-
test("should throw error when using non-Cartridge RPC for sepolia", async () => {
58-
const invalidChains = [
57+
test("should allow non-Cartridge RPC for sepolia", async () => {
58+
const customChains = [
5959
{ rpcUrl: "https://some-other-provider.com/starknet/sepolia/rpc/v0_9" },
6060
];
6161

6262
expect(() => {
6363
new ControllerProvider({
64-
chains: invalidChains,
64+
chains: customChains,
6565
defaultChainId: constants.StarknetChainId.SN_SEPOLIA,
6666
});
67-
}).toThrow("Only Cartridge RPC providers are allowed for sepolia");
67+
}).not.toThrow();
6868
});
6969

7070
test("should allow non-Cartridge RPC for custom chains", () => {

packages/controller/src/__tests__/parseChainId.test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,25 @@ describe("parseChainId", () => {
4242
});
4343
});
4444

45+
describe("Non-Cartridge hosts", () => {
46+
test("returns placeholder chainId in Node", () => {
47+
expect(parseChainId(new URL("http://dl:123123"))).toBe(
48+
shortString.encodeShortString("LOCALHOST"),
49+
);
50+
});
51+
});
52+
4553
describe("Error cases", () => {
4654
test("throws error for unsupported URL format", () => {
4755
expect(() =>
48-
parseChainId(new URL("https://api.example.com/unsupported")),
49-
).toThrow("Chain https://api.example.com/unsupported not supported");
56+
parseChainId(new URL("https://api.cartridge.gg/unsupported")),
57+
).toThrow("Chain https://api.cartridge.gg/unsupported not supported");
5058
});
5159

5260
test("throws error for URLs without proper chain identifiers", () => {
5361
expect(() =>
54-
parseChainId(new URL("https://api.example.com/v1/starknet")),
55-
).toThrow("Chain https://api.example.com/v1/starknet not supported");
62+
parseChainId(new URL("https://api.cartridge.gg/v1/starknet")),
63+
).toThrow("Chain https://api.cartridge.gg/v1/starknet not supported");
5664
});
5765
});
5866
});

packages/controller/src/controller.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -570,20 +570,6 @@ export default class ControllerProvider extends BaseProvider {
570570
const url = new URL(chain.rpcUrl);
571571
const chainId = parseChainId(url);
572572

573-
// Validate that mainnet and sepolia must use Cartridge RPC
574-
const isMainnet = chainId === constants.StarknetChainId.SN_MAIN;
575-
const isSepolia = chainId === constants.StarknetChainId.SN_SEPOLIA;
576-
const isCartridgeRpc = url.hostname === "api.cartridge.gg";
577-
const isLocalhost =
578-
url.hostname === "localhost" || url.hostname === "127.0.0.1";
579-
580-
if ((isMainnet || isSepolia) && !(isCartridgeRpc || isLocalhost)) {
581-
throw new Error(
582-
`Only Cartridge RPC providers are allowed for ${isMainnet ? "mainnet" : "sepolia"}. ` +
583-
`Please use: https://api.cartridge.gg/x/starknet/${isMainnet ? "mainnet" : "sepolia"}/rpc/v0_9`,
584-
);
585-
}
586-
587573
this.chains.set(chainId, chain);
588574
} catch (error) {
589575
console.error(`Failed to parse chainId for ${chain.rpcUrl}:`, error);

packages/controller/src/utils.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,14 @@ export function humanizeString(str: string): string {
138138

139139
export function parseChainId(url: URL): ChainId {
140140
const parts = url.pathname.split("/");
141+
const isCartridgeHost = url.hostname === "api.cartridge.gg";
141142

142-
// Handle localhost URLs by making a synchronous call to getChainId
143-
if (
144-
url.hostname === "localhost" ||
145-
url.hostname === "127.0.0.1" ||
146-
url.hostname === "0.0.0.0"
147-
) {
143+
// Handle non-Cartridge hosts by making a synchronous call to getChainId
144+
if (!isCartridgeHost) {
148145
// Check if we're in a browser environment
149146
if (typeof XMLHttpRequest === "undefined") {
150147
// In Node.js environment (like tests), we can't make synchronous HTTP calls
151-
// For now, we'll use a placeholder chainId for localhost in tests
148+
// For now, we'll use a placeholder chainId for non-Cartridge hosts in tests
152149
console.warn(
153150
`Cannot make synchronous HTTP call in Node.js environment for ${url.toString()}`,
154151
);

0 commit comments

Comments
 (0)