Fix CAIP-2 support in schemaCheck.js (#1674)

Co-authored-by: ligi <ligi@ligi.de>
This commit is contained in:
Frederik Bolding 2022-11-04 01:53:14 +01:00 committed by GitHub
parent c57b92cff3
commit 2f42e78adb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,29 +1,37 @@
const fs = require('fs'); const fs = require("fs")
const Ajv = require("ajv") const Ajv = require("ajv")
const ajv = new Ajv() const ajv = new Ajv()
const schema = require('./schema/chainSchema.json') const schema = require("./schema/chainSchema.json")
const chainFiles = fs.readdirSync('../_data/chains/'); const chainFiles = fs.readdirSync("../_data/chains/")
// https://chainagnostic.org/CAIPs/caip-2
const parseChainId = (chainId) =>
/^(?<namespace>[-a-z0-9]{3,8})-(?<reference>[-a-zA-Z0-9]{1,32})$/u.exec(
chainId
)
const filesWithErrors = [] const filesWithErrors = []
for(const chainFile of chainFiles){ for (const chainFile of chainFiles) {
const fileLocation = `../_data/chains/${chainFile}` const fileLocation = `../_data/chains/${chainFile}`
const fileData = fs.readFileSync(fileLocation,'utf8') const fileData = fs.readFileSync(fileLocation, "utf8")
const fileDataJson = JSON.parse(fileData) const fileDataJson = JSON.parse(fileData)
const chainIdFromFileName = chainFile.match(/eip155-(\d+)\.json/)[1] const fileName = chainFile.split(".")[0]
const parsedChainId = parseInt(chainIdFromFileName, 10); const parsedChainId = parseChainId(fileName)?.groups
if(chainIdFromFileName != parsedChainId.toString()){ const chainIdFromFileName = parsedChainId?.reference
throw new Error(`File name does not match parsed ChainID ${parsedChainId} in ${chainFile}`) if (chainIdFromFileName != fileDataJson.chainId) {
} throw new Error(`File Name does not match with ChainID in ${chainFile}`)
if(parsedChainId !== fileDataJson.chainId){ }
throw new Error(`File Name does not match with ChainID in ${chainFile}`) const valid = ajv.validate(schema, fileDataJson)
} if (!valid) {
const valid = ajv.validate(schema, fileDataJson) console.error(ajv.errors)
if(!valid) { filesWithErrors.push(chainFile)
console.error(ajv.errors) }
filesWithErrors.push(chainFile)
}
} }
if(filesWithErrors.length > 0){ if (filesWithErrors.length > 0) {
throw new Error(`Invalid JSON Schema in ${filesWithErrors.length} files at ${filesWithErrors.join(",")}`) throw new Error(
} `Invalid JSON Schema in ${
filesWithErrors.length
} files at ${filesWithErrors.join(",")}`
)
}