Skip to content

Commit 6a069ad

Browse files
authored
fix: use multiformats library and allow paths in cid (#79)
# 🤖 Linear Closes GIT-269 ## Description Fixes IPFS CID validation, by using a more reliable library [multiformats](https://www.npmjs.com/package/multiformats) Also, allow for having CID with paths and params like: {cid}/pathtofile.json?param=val ## Checklist before requesting a review - [ ] I have conducted a self-review of my code. - [ ] I have conducted a QA. - [ ] If it is a core feature, I have included comprehensive tests.
1 parent c44adf7 commit 6a069ad

File tree

4 files changed

+31
-3
lines changed

4 files changed

+31
-3
lines changed

packages/metadata/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"@grants-stack-indexer/repository": "workspace:*",
3232
"@grants-stack-indexer/shared": "workspace:*",
3333
"axios": "1.7.7",
34+
"multiformats": "13.3.2",
3435
"pinata": "1.10.1",
3536
"ts-retry": "5.0.1",
3637
"zod": "3.23.8"
Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
export const isValidCid = (cid: string): boolean => {
2-
const cidRegex = /^(Qm[1-9A-HJ-NP-Za-km-z]{44}|baf[0-9A-Za-z]{50,})$/;
3-
return cidRegex.test(cid);
1+
import { CID } from "multiformats/cid";
2+
3+
export const isValidCid = (input: string): boolean => {
4+
try {
5+
// Extract CID part from optional paths/params
6+
const match = input.match(/^([^/?#]+)(?:[/?#].*)?$/);
7+
if (!match) return false;
8+
9+
const cid = match[1];
10+
11+
if (!cid) return false;
12+
13+
// Quick regex check for basic format
14+
const cidRegex = /^(Qm[1-9A-HJ-NP-Za-km-z]{44}|baf[0-9A-Za-z]+)$/;
15+
if (!cidRegex.test(cid)) return false;
16+
17+
// Full validation using multiformats/cid
18+
CID.parse(cid);
19+
return true;
20+
} catch {
21+
return false;
22+
}
423
};

packages/metadata/test/utils/index.test.ts renamed to packages/metadata/test/utils/index.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,9 @@ describe("isValidCid", () => {
3636
expect(isValidCid(input as unknown as string)).toBe(false);
3737
});
3838
});
39+
40+
it("returns true when CID has path or query parameters", () => {
41+
const cidWithParams = "QmW2WQi7j6c7UgJTarActp7tDNikE4B2qXtFCfLPdsgaTQ/path?param=value";
42+
expect(isValidCid(cidWithParams)).toBe(true);
43+
});
3944
});

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)