-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathcaip-2.test.ts
More file actions
127 lines (103 loc) · 3.52 KB
/
caip-2.test.ts
File metadata and controls
127 lines (103 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import { describe, expect, it } from "vitest"
import {
caip2ChainIdRegex,
caip2ChainIds,
caip2NamespaceRegex,
caip2Parts,
caip2ReferenceRegex,
} from "./index"
describe("caip2Parts", () => {
it("parses a valid EIP-155 chain ID", () => {
const result = caip2Parts("eip155:1")
expect(result).toEqual({ namespace: "eip155", reference: "1" })
})
it("parses a valid Solana chain ID", () => {
const result = caip2Parts("solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp")
expect(result).toEqual({
namespace: "solana",
reference: "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
})
})
it("throws for an empty string", () => {
expect(() => caip2Parts("" as `${string}:${string}`)).toThrow(
"Invalid CAIP-2 chain ID",
)
})
it("throws for a string without a colon", () => {
expect(() => caip2Parts("eip155" as `${string}:${string}`)).toThrow(
"Invalid CAIP-2 chain ID",
)
})
it("throws when the reference is missing after the colon", () => {
expect(() => caip2Parts("eip155:" as `${string}:${string}`)).toThrow(
"Invalid CAIP-2 chain ID",
)
})
})
describe("caip2ChainIdRegex", () => {
it("matches a valid EIP-155 chain ID", () => {
expect(caip2ChainIdRegex.test("eip155:1")).toBe(true)
})
it("matches a valid Solana chain ID", () => {
expect(
caip2ChainIdRegex.test("solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp"),
).toBe(true)
})
it("matches a chain ID with hyphens and underscores in reference", () => {
expect(caip2ChainIdRegex.test("abc:ref_with-chars")).toBe(true)
})
it("rejects a namespace shorter than 3 characters", () => {
expect(caip2ChainIdRegex.test("ab:1")).toBe(false)
})
it("rejects a namespace longer than 8 characters", () => {
expect(caip2ChainIdRegex.test("abcdefghi:1")).toBe(false)
})
it("rejects uppercase characters in namespace", () => {
expect(caip2ChainIdRegex.test("EIP155:1")).toBe(false)
})
it("rejects an empty reference", () => {
expect(caip2ChainIdRegex.test("eip155:")).toBe(false)
})
it("rejects a reference longer than 32 characters", () => {
const longRef = "a".repeat(33)
expect(caip2ChainIdRegex.test(`eip155:${longRef}`)).toBe(false)
})
it("rejects a chain ID without a colon", () => {
expect(caip2ChainIdRegex.test("eip1551")).toBe(false)
})
})
describe("caip2NamespaceRegex", () => {
it("matches a valid namespace", () => {
expect(caip2NamespaceRegex.test("eip155")).toBe(true)
})
it("rejects a namespace with uppercase letters", () => {
expect(caip2NamespaceRegex.test("EIP155")).toBe(false)
})
})
describe("caip2ReferenceRegex", () => {
it("matches a numeric reference", () => {
expect(caip2ReferenceRegex.test("1")).toBe(true)
})
it("matches an alphanumeric reference with hyphens and underscores", () => {
expect(caip2ReferenceRegex.test("my_ref-123")).toBe(true)
})
it("rejects an empty reference", () => {
expect(caip2ReferenceRegex.test("")).toBe(false)
})
})
describe("caip2ChainIds", () => {
it("returns the correct Ethereum mainnet chain ID", () => {
expect(caip2ChainIds.ethereumMainnet).toBe("eip155:1")
})
it("returns the correct Base mainnet chain ID", () => {
expect(caip2ChainIds.baseMainnet).toBe("eip155:8453")
})
it("returns the correct Solana mainnet chain ID", () => {
expect(caip2ChainIds.solanaMainnet).toBe(
"solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp",
)
})
it("returns the correct Arbitrum Sepolia chain ID", () => {
expect(caip2ChainIds.arbitrumSepolia).toBe("eip155:421614")
})
})