From 4ec5615201e397c0a4caae35d44c500159182d1e Mon Sep 17 00:00:00 2001 From: Mario Christopher Date: Mon, 25 Mar 2024 18:50:36 -0600 Subject: [PATCH 1/6] Utility to query Snaps for conditions. --- .eslintrc.js | 1 + .gitignore | 2 ++ package.json | 1 + scripts/query/get-manifests.ts | 36 ++++++++++++++++++++++++++++++++++ scripts/query/query.ts | 35 +++++++++++++++++++++++++++++++++ 5 files changed, 75 insertions(+) create mode 100644 scripts/query/get-manifests.ts create mode 100644 scripts/query/query.ts diff --git a/.eslintrc.js b/.eslintrc.js index e75d7618..5d60b536 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -32,5 +32,6 @@ module.exports = { 'dist/', 'docs/', '.yarn/', + 'scripts/query/', ], }; diff --git a/.gitignore b/.gitignore index 3bd8e071..ab2165b5 100644 --- a/.gitignore +++ b/.gitignore @@ -81,3 +81,5 @@ node_modules/ src/signature.json secp256k1-key + +scripts/query/data.json \ No newline at end of file diff --git a/package.json b/package.json index bb261ca5..260ffeda 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write", "lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' --ignore-path .gitignore --no-error-on-unmatched-pattern", "prepack": "./scripts/prepack.sh", + "query-snaps": "ts-node scripts/query/query.ts", "sign": "ts-node scripts/sign-registry.ts", "test": "jest && jest-it-up", "test:watch": "jest --watch", diff --git a/scripts/query/get-manifests.ts b/scripts/query/get-manifests.ts new file mode 100644 index 00000000..4dd8b52a --- /dev/null +++ b/scripts/query/get-manifests.ts @@ -0,0 +1,36 @@ +import { detectSnapLocation, fetchSnap } from '@metamask/snaps-controllers'; +import type { SnapId } from '@metamask/snaps-sdk'; +import fs from 'fs'; +import path from 'path'; +import semver from 'semver/preload'; + +import registry from '../../src/registry.json'; + +export const MANIFESTS_FILE_LOCATION = path.join(__dirname, 'data.json'); + +/** + * Function to fetch Snaps manifests and write it to a local JSON file. + */ +export async function getManifests() { + console.log('Fetching Snaps. Please wait ...'); + const allManifests = []; + for (const snap of Object.values(registry.verifiedSnaps)) { + console.log(snap.metadata.name); + const latestVersion = Object.keys(snap.versions).reduce( + (result, version) => { + if (result === null || semver.gt(version, result)) { + return version; + } + return result; + }, + ); + + const location = detectSnapLocation(snap.id, { + versionRange: latestVersion as any, + }); + const fetchedSnap = await fetchSnap(snap.id as SnapId, location); + allManifests.push(fetchedSnap.manifest.result); + } + fs.writeFileSync(MANIFESTS_FILE_LOCATION, JSON.stringify(allManifests)); + console.log('Fetching Snap manifests and writing file - Done'); +} diff --git a/scripts/query/query.ts b/scripts/query/query.ts new file mode 100644 index 00000000..3a7fc198 --- /dev/null +++ b/scripts/query/query.ts @@ -0,0 +1,35 @@ +import type { SnapManifest } from '@metamask/snaps-utils'; +import fs from 'fs'; + +import { MANIFESTS_FILE_LOCATION, getManifests } from './get-manifests'; + +/** + * Main function. + */ +async function main() { + const fileExists = fs.existsSync(MANIFESTS_FILE_LOCATION); + const forceDownload = process.argv.includes('--force-download'); + + if (!fileExists || forceDownload) { + await getManifests(); + } + const manifests: SnapManifest[] = JSON.parse( + fs.readFileSync(MANIFESTS_FILE_LOCATION, 'utf8'), + ); + + /** + * Write your custom code to query the `manifests` array for the data you are interested in. + */ + + // Eg.: Find all Snaps that use `endowment:transaction-insight` + const filteredSnaps = manifests.filter((manifest) => + manifest.initialPermissions['endowment:transaction-insight'], + ); + console.log('\nQuery results'); + console.log(filteredSnaps.map((snap) => snap.proposedName)); +} + +main().catch((error) => { + console.error(error); + process.exitCode = 1; +}); From 1ffe29966bb8338858ae5c607998e827a9a67bb8 Mon Sep 17 00:00:00 2001 From: Mario Christopher Date: Tue, 26 Mar 2024 07:57:25 -0600 Subject: [PATCH 2/6] Update .gitignore Co-authored-by: Maarten Zuidhoorn --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ab2165b5..b6e4ce06 100644 --- a/.gitignore +++ b/.gitignore @@ -82,4 +82,4 @@ src/signature.json secp256k1-key -scripts/query/data.json \ No newline at end of file +scripts/query/data.json From 1f65e698540f77df35465b78861e32cb3fa5dd25 Mon Sep 17 00:00:00 2001 From: Mario Christopher Date: Wed, 27 Mar 2024 09:57:14 -0600 Subject: [PATCH 3/6] PR Comment fixes. --- .eslintrc.js | 3 +-- scripts/query/get-manifests.ts | 5 ++++- scripts/query/query.ts | 20 +++++++++++++------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 5d60b536..4654e4a5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -10,7 +10,7 @@ module.exports = { }, { - files: ['*.js', 'scripts/*.ts'], + files: ['*.js', 'scripts/**/*.ts'], parserOptions: { sourceType: 'script', }, @@ -32,6 +32,5 @@ module.exports = { 'dist/', 'docs/', '.yarn/', - 'scripts/query/', ], }; diff --git a/scripts/query/get-manifests.ts b/scripts/query/get-manifests.ts index 4dd8b52a..8ab21943 100644 --- a/scripts/query/get-manifests.ts +++ b/scripts/query/get-manifests.ts @@ -31,6 +31,9 @@ export async function getManifests() { const fetchedSnap = await fetchSnap(snap.id as SnapId, location); allManifests.push(fetchedSnap.manifest.result); } - fs.writeFileSync(MANIFESTS_FILE_LOCATION, JSON.stringify(allManifests)); + await fs.promises.writeFile( + MANIFESTS_FILE_LOCATION, + JSON.stringify(allManifests), + ); console.log('Fetching Snap manifests and writing file - Done'); } diff --git a/scripts/query/query.ts b/scripts/query/query.ts index 3a7fc198..c9b347c3 100644 --- a/scripts/query/query.ts +++ b/scripts/query/query.ts @@ -4,17 +4,23 @@ import fs from 'fs'; import { MANIFESTS_FILE_LOCATION, getManifests } from './get-manifests'; /** - * Main function. + * Entry point. */ async function main() { - const fileExists = fs.existsSync(MANIFESTS_FILE_LOCATION); const forceDownload = process.argv.includes('--force-download'); - - if (!fileExists || forceDownload) { + if (forceDownload) { await getManifests(); + } else { + try { + // Check if local file exists. + await fs.promises.readFile(MANIFESTS_FILE_LOCATION, 'utf8'); + } catch (eror) { + await getManifests(); + } } + const manifests: SnapManifest[] = JSON.parse( - fs.readFileSync(MANIFESTS_FILE_LOCATION, 'utf8'), + await fs.promises.readFile(MANIFESTS_FILE_LOCATION, 'utf8'), ); /** @@ -22,8 +28,8 @@ async function main() { */ // Eg.: Find all Snaps that use `endowment:transaction-insight` - const filteredSnaps = manifests.filter((manifest) => - manifest.initialPermissions['endowment:transaction-insight'], + const filteredSnaps = manifests.filter( + (manifest) => manifest.initialPermissions['endowment:transaction-insight'], ); console.log('\nQuery results'); console.log(filteredSnaps.map((snap) => snap.proposedName)); From 30cbf498590b25f2f42f005e1ee078c664e2c89f Mon Sep 17 00:00:00 2001 From: Mario Christopher Date: Wed, 27 Mar 2024 13:21:07 -0600 Subject: [PATCH 4/6] Update scripts/query/get-manifests.ts Co-authored-by: Maarten Zuidhoorn --- scripts/query/get-manifests.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/query/get-manifests.ts b/scripts/query/get-manifests.ts index 8ab21943..a33e6b95 100644 --- a/scripts/query/get-manifests.ts +++ b/scripts/query/get-manifests.ts @@ -28,6 +28,7 @@ export async function getManifests() { const location = detectSnapLocation(snap.id, { versionRange: latestVersion as any, }); + const fetchedSnap = await fetchSnap(snap.id as SnapId, location); allManifests.push(fetchedSnap.manifest.result); } From cb9d7dc609b9f18c86bde4676a8265e3941c4ee2 Mon Sep 17 00:00:00 2001 From: Mario Christopher Date: Wed, 27 Mar 2024 13:21:34 -0600 Subject: [PATCH 5/6] Update scripts/query/get-manifests.ts Co-authored-by: Maarten Zuidhoorn --- scripts/query/get-manifests.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/query/get-manifests.ts b/scripts/query/get-manifests.ts index a33e6b95..5e982ece 100644 --- a/scripts/query/get-manifests.ts +++ b/scripts/query/get-manifests.ts @@ -32,9 +32,11 @@ export async function getManifests() { const fetchedSnap = await fetchSnap(snap.id as SnapId, location); allManifests.push(fetchedSnap.manifest.result); } + await fs.promises.writeFile( MANIFESTS_FILE_LOCATION, JSON.stringify(allManifests), ); + console.log('Fetching Snap manifests and writing file - Done'); } From c2f74e7c783511acdd8208cbe822726eeda7bba2 Mon Sep 17 00:00:00 2001 From: Mario Christopher Date: Wed, 27 Mar 2024 13:27:11 -0600 Subject: [PATCH 6/6] PR Comment fixes --- scripts/query/get-manifests.ts | 5 +++-- scripts/query/query.ts | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/scripts/query/get-manifests.ts b/scripts/query/get-manifests.ts index 5e982ece..a34cafac 100644 --- a/scripts/query/get-manifests.ts +++ b/scripts/query/get-manifests.ts @@ -1,5 +1,6 @@ import { detectSnapLocation, fetchSnap } from '@metamask/snaps-controllers'; import type { SnapId } from '@metamask/snaps-sdk'; +import type { SemVerRange } from '@metamask/utils'; import fs from 'fs'; import path from 'path'; import semver from 'semver/preload'; @@ -23,10 +24,10 @@ export async function getManifests() { } return result; }, - ); + ) as SemVerRange; const location = detectSnapLocation(snap.id, { - versionRange: latestVersion as any, + versionRange: latestVersion, }); const fetchedSnap = await fetchSnap(snap.id as SnapId, location); diff --git a/scripts/query/query.ts b/scripts/query/query.ts index c9b347c3..47a32092 100644 --- a/scripts/query/query.ts +++ b/scripts/query/query.ts @@ -1,5 +1,5 @@ import type { SnapManifest } from '@metamask/snaps-utils'; -import fs from 'fs'; +import fsPromises from 'fs/promises'; import { MANIFESTS_FILE_LOCATION, getManifests } from './get-manifests'; @@ -13,14 +13,14 @@ async function main() { } else { try { // Check if local file exists. - await fs.promises.readFile(MANIFESTS_FILE_LOCATION, 'utf8'); + await fsPromises.readFile(MANIFESTS_FILE_LOCATION, 'utf8'); } catch (eror) { await getManifests(); } } const manifests: SnapManifest[] = JSON.parse( - await fs.promises.readFile(MANIFESTS_FILE_LOCATION, 'utf8'), + await fsPromises.readFile(MANIFESTS_FILE_LOCATION, 'utf8'), ); /**