Skip to content

Commit 45c3ddc

Browse files
committed
fix(ens): support CIDv1 by converting to CIDv0 for contenthash encoding
- Add multiformats and convert bafy (CIDv1) → Qm (CIDv0) when possible - Throw a clear error if conversion is impossible (non dag-pb/sha2-256) - Prevent "Non-base58 character" from content-hash with bafy CIDs
1 parent 8ffe401 commit 45c3ddc

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
},
1010
"dependencies": {
1111
"content-hash": "^2.5.2",
12-
"ethers": "^6.12.1"
12+
"ethers": "^6.12.1",
13+
"multiformats": "^12.1.3"
1314
}
1415
}
15-

scripts/update-ens.mjs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import { Contract, JsonRpcProvider, Wallet, keccak256, toUtf8Bytes, getBytes, concat } from 'ethers'
66
import contentHash from 'content-hash'
7+
import { CID } from 'multiformats/cid'
78

89
function requiredEnv(name) {
910
const v = process.env[name]
@@ -51,7 +52,23 @@ async function main() {
5152
const resolver = new Contract(resolverAddr, RESOLVER_ABI, signer)
5253

5354
const current = await resolver.contenthash(node).catch(() => '0x')
54-
const encoded = contentHash.fromIpfs(IPFS_CID)
55+
56+
// content-hash library requires base58 (CIDv0) for ipfs-ns; convert when possible
57+
let cidForEns = IPFS_CID
58+
try {
59+
const parsed = CID.parse(IPFS_CID)
60+
if (parsed.version === 1) {
61+
try {
62+
cidForEns = parsed.toV0().toString()
63+
} catch {
64+
throw new Error('Provided CID is CIDv1 and cannot be converted to CIDv0 (required by content-hash). Ensure the CID is dag-pb with sha2-256, or provide a CIDv0 (Qm...).')
65+
}
66+
}
67+
} catch {
68+
// Not a CID, let content-hash try; will likely fail fast with a clear message
69+
}
70+
71+
const encoded = contentHash.fromIpfs(cidForEns)
5572

5673
if (current && current.toLowerCase() === encoded.toLowerCase()) {
5774
console.log(`No-op: ${ENS_NAME} already points to ${IPFS_CID}`)
@@ -69,4 +86,3 @@ main().catch((err) => {
6986
console.error(err)
7087
process.exit(1)
7188
})
72-

0 commit comments

Comments
 (0)