|
| 1 | +import { IPFS_GATEWAY, isContentAddressed, toHttpUrl } from "./ipfs"; |
| 2 | + |
| 3 | +//Mainnet General Court policy |
| 4 | +const CID_V0 = "Qmd1TMEbtic3TSonu5dfqa5k3aSrjxRGY8oJH3ruGgazRB"; |
| 5 | + |
| 6 | +//IPFS docs example |
| 7 | +const CID_V1 = "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi"; |
| 8 | + |
| 9 | +const GATEWAY_URL = `${IPFS_GATEWAY}/ipfs/${CID_V0}`; |
| 10 | + |
| 11 | +describe("toHttpUrl", () => { |
| 12 | + it("resolves all common IPFS URI forms to the gateway", () => { |
| 13 | + expect(toHttpUrl(`/ipfs/${CID_V0}`)).toBe(GATEWAY_URL); |
| 14 | + expect(toHttpUrl(`ipfs/${CID_V0}`)).toBe(GATEWAY_URL); |
| 15 | + expect(toHttpUrl(`ipfs://${CID_V0}`)).toBe(GATEWAY_URL); |
| 16 | + expect(toHttpUrl(`ipfs:${CID_V0}`)).toBe(GATEWAY_URL); |
| 17 | + expect(toHttpUrl(`ipfs://ipfs/${CID_V0}`)).toBe(GATEWAY_URL); |
| 18 | + }); |
| 19 | + |
| 20 | + it("resolves legacy fs: forms to the gateway", () => { |
| 21 | + expect(toHttpUrl(`fs:/ipfs/${CID_V0}`)).toBe(GATEWAY_URL); |
| 22 | + expect(toHttpUrl(`fs://ipfs/${CID_V0}`)).toBe(GATEWAY_URL); |
| 23 | + }); |
| 24 | + |
| 25 | + it("resolves bare hashes to the gateway", () => { |
| 26 | + expect(toHttpUrl(CID_V0)).toBe(GATEWAY_URL); |
| 27 | + expect(toHttpUrl(`${CID_V0}/file.json`)).toBe(`${GATEWAY_URL}/file.json`); |
| 28 | + }); |
| 29 | + |
| 30 | + it("returns absolute http(s) URLs unchanged", () => { |
| 31 | + const curateURL = "https://curate.kleros.io/tcr/1/0x6e31D83B0c696f7D57241d3DffD0f2B628D14C67"; |
| 32 | + expect(toHttpUrl(curateURL)).toBe(curateURL); |
| 33 | + expect(toHttpUrl("http://google.com")).toBe("http://google.com"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns undefined for empty or non-string input", () => { |
| 37 | + expect(toHttpUrl("")).toBeUndefined(); |
| 38 | + expect(toHttpUrl(" ")).toBeUndefined(); |
| 39 | + expect(toHttpUrl(null)).toBeUndefined(); |
| 40 | + expect(toHttpUrl(undefined)).toBeUndefined(); |
| 41 | + expect(toHttpUrl(42)).toBeUndefined(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("ignores surrounding whitespace", () => { |
| 45 | + expect(toHttpUrl(` /ipfs/${CID_V0} `)).toBe(GATEWAY_URL); |
| 46 | + expect(toHttpUrl(" https://curate.kleros.io ")).toBe("https://curate.kleros.io"); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("isContentAddressed", () => { |
| 51 | + it("accepts all common IPFS URI forms", () => { |
| 52 | + expect(isContentAddressed(`/ipfs/${CID_V0}`)).toBe(true); |
| 53 | + expect(isContentAddressed(`ipfs/${CID_V0}`)).toBe(true); |
| 54 | + expect(isContentAddressed(`ipfs://${CID_V1}`)).toBe(true); |
| 55 | + expect(isContentAddressed(`ipfs:${CID_V0}`)).toBe(true); |
| 56 | + expect(isContentAddressed(`fs:/ipfs/${CID_V0}`)).toBe(true); |
| 57 | + }); |
| 58 | + |
| 59 | + it("accepts bare CIDv0 and base32 CIDv1 hashes, with or without a path", () => { |
| 60 | + expect(isContentAddressed(CID_V0)).toBe(true); |
| 61 | + expect(isContentAddressed(CID_V1)).toBe(true); |
| 62 | + expect(isContentAddressed(`${CID_V0}/file.json`)).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it("rejects host-anchored and mutable URIs", () => { |
| 66 | + expect(isContentAddressed(`https://cdn.kleros.link/ipfs/${CID_V1}`)).toBe(false); |
| 67 | + expect(isContentAddressed("http://example.com")).toBe(false); |
| 68 | + expect(isContentAddressed("ipns://k51qzi5uqu5dgutdk6i1ynyzg")).toBe(false); |
| 69 | + expect(isContentAddressed("/ipns/k51qzi5uqu5dgutdk6i1ynyzg")).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it("rejects strings that only resemble hashes", () => { |
| 73 | + expect(isContentAddressed("Qmtooshort")).toBe(false); |
| 74 | + expect(isContentAddressed(`${CID_V0}X`)).toBe(false); |
| 75 | + expect(isContentAddressed("whatever")).toBe(false); |
| 76 | + expect(isContentAddressed("some random text")).toBe(false); |
| 77 | + }); |
| 78 | + |
| 79 | + it("rejects accepted prefixes that are not followed by a CID", () => { |
| 80 | + expect(isContentAddressed("/ipfs/whatever")).toBe(false); |
| 81 | + expect(isContentAddressed("ipfs:whatever")).toBe(false); |
| 82 | + expect(isContentAddressed("ipfs::whatever")).toBe(false); |
| 83 | + expect(isContentAddressed("fs:/ipfs/whatever")).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it("rejects empty or non-string input", () => { |
| 87 | + expect(isContentAddressed("")).toBe(false); |
| 88 | + expect(isContentAddressed(null)).toBe(false); |
| 89 | + expect(isContentAddressed(undefined)).toBe(false); |
| 90 | + }); |
| 91 | +}); |
0 commit comments