Skip to content
Open
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
15 changes: 15 additions & 0 deletions packages/caip/src/caips/caip-10.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,19 @@ describe("createCaip10AccountId", () => {
"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp:FNoGHiv7DKPLXHfuhiEWpJ8qYitawGkuaYwfYkuvFk1P",
)
})

it("throws for invalid chain ID", () => {
expect(() =>
createCaip10AccountId(
"bad" as any,
"0x1234567890123456789012345678901234567890",
),
).toThrow("Invalid CAIP-2 chain ID")
})

it("throws for invalid account address", () => {
expect(() => createCaip10AccountId("eip155:1", "")).toThrow(
"Invalid CAIP-10 account address",
)
})
})
7 changes: 7 additions & 0 deletions packages/caip/src/caips/caip-10.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
caip2ChainIdPattern,
caip2ChainIdRegex,
type Caip2ChainId,
type Caip2ChainIdParts,
} from "./caip-2"
Expand Down Expand Up @@ -37,6 +38,12 @@ export function createCaip10AccountId(
chainId: Caip2ChainId,
address: string,
): Caip10AccountId {
if (!caip2ChainIdRegex.test(chainId)) {
throw new Error(`Invalid CAIP-2 chain ID: ${chainId}`)
}
if (!caip10AccountAddressRegex.test(address)) {
throw new Error(`Invalid CAIP-10 account address: ${address}`)
}
return `${chainId}:${address}`
}

Expand Down