Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions packages/controller/src/__tests__/controllerDefaults.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,30 @@ describe("ControllerProvider defaults", () => {
);
});

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

expect(() => {
new ControllerProvider({
chains: invalidChains,
chains: customChains,
defaultChainId: constants.StarknetChainId.SN_MAIN,
});
}).toThrow("Only Cartridge RPC providers are allowed for mainnet");
}).not.toThrow();
});

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

expect(() => {
new ControllerProvider({
chains: invalidChains,
chains: customChains,
defaultChainId: constants.StarknetChainId.SN_SEPOLIA,
});
}).toThrow("Only Cartridge RPC providers are allowed for sepolia");
}).not.toThrow();
});

test("should allow non-Cartridge RPC for custom chains", () => {
Expand Down
16 changes: 12 additions & 4 deletions packages/controller/src/__tests__/parseChainId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ describe("parseChainId", () => {
});
});

describe("Non-Cartridge hosts", () => {
test("returns placeholder chainId in Node", () => {
expect(parseChainId(new URL("http://dl:123123"))).toBe(
shortString.encodeShortString("LOCALHOST"),
);
});
});

describe("Error cases", () => {
test("throws error for unsupported URL format", () => {
expect(() =>
parseChainId(new URL("https://api.example.com/unsupported")),
).toThrow("Chain https://api.example.com/unsupported not supported");
parseChainId(new URL("https://api.cartridge.gg/unsupported")),
).toThrow("Chain https://api.cartridge.gg/unsupported not supported");
});

test("throws error for URLs without proper chain identifiers", () => {
expect(() =>
parseChainId(new URL("https://api.example.com/v1/starknet")),
).toThrow("Chain https://api.example.com/v1/starknet not supported");
parseChainId(new URL("https://api.cartridge.gg/v1/starknet")),
).toThrow("Chain https://api.cartridge.gg/v1/starknet not supported");
});
});
});
14 changes: 0 additions & 14 deletions packages/controller/src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -570,20 +570,6 @@ export default class ControllerProvider extends BaseProvider {
const url = new URL(chain.rpcUrl);
const chainId = parseChainId(url);

// Validate that mainnet and sepolia must use Cartridge RPC
const isMainnet = chainId === constants.StarknetChainId.SN_MAIN;
const isSepolia = chainId === constants.StarknetChainId.SN_SEPOLIA;
const isCartridgeRpc = url.hostname === "api.cartridge.gg";
const isLocalhost =
url.hostname === "localhost" || url.hostname === "127.0.0.1";

if ((isMainnet || isSepolia) && !(isCartridgeRpc || isLocalhost)) {
throw new Error(
`Only Cartridge RPC providers are allowed for ${isMainnet ? "mainnet" : "sepolia"}. ` +
`Please use: https://api.cartridge.gg/x/starknet/${isMainnet ? "mainnet" : "sepolia"}/rpc/v0_9`,
);
}

this.chains.set(chainId, chain);
} catch (error) {
console.error(`Failed to parse chainId for ${chain.rpcUrl}:`, error);
Expand Down
11 changes: 4 additions & 7 deletions packages/controller/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,14 @@ export function humanizeString(str: string): string {

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

// Handle localhost URLs by making a synchronous call to getChainId
if (
url.hostname === "localhost" ||
url.hostname === "127.0.0.1" ||
url.hostname === "0.0.0.0"
) {
// Handle non-Cartridge hosts by making a synchronous call to getChainId
if (!isCartridgeHost) {
// Check if we're in a browser environment
if (typeof XMLHttpRequest === "undefined") {
// In Node.js environment (like tests), we can't make synchronous HTTP calls
// For now, we'll use a placeholder chainId for localhost in tests
// For now, we'll use a placeholder chainId for non-Cartridge hosts in tests
console.warn(
`Cannot make synchronous HTTP call in Node.js environment for ${url.toString()}`,
);
Expand Down
Loading