Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility to query Snaps for conditions. #520

Merged
merged 7 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
},

{
files: ['*.js', 'scripts/*.ts'],
files: ['*.js', 'scripts/**/*.ts'],
parserOptions: {
sourceType: 'script',
},
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,5 @@ node_modules/
src/signature.json

secp256k1-key

scripts/query/data.json
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
43 changes: 43 additions & 0 deletions scripts/query/get-manifests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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';

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);
Mrtenz marked this conversation as resolved.
Show resolved Hide resolved
const latestVersion = Object.keys(snap.versions).reduce(
(result, version) => {
if (result === null || semver.gt(version, result)) {
return version;
}
return result;
},
) as SemVerRange;

const location = detectSnapLocation(snap.id, {
versionRange: latestVersion,
});
mario-christopher marked this conversation as resolved.
Show resolved Hide resolved

const fetchedSnap = await fetchSnap(snap.id as SnapId, location);
allManifests.push(fetchedSnap.manifest.result);
}

await fs.promises.writeFile(
MANIFESTS_FILE_LOCATION,
JSON.stringify(allManifests),
);
mario-christopher marked this conversation as resolved.
Show resolved Hide resolved

console.log('Fetching Snap manifests and writing file - Done');
}
41 changes: 41 additions & 0 deletions scripts/query/query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import type { SnapManifest } from '@metamask/snaps-utils';
import fsPromises from 'fs/promises';

import { MANIFESTS_FILE_LOCATION, getManifests } from './get-manifests';

/**
* Entry point.
*/
async function main() {
const forceDownload = process.argv.includes('--force-download');
if (forceDownload) {
await getManifests();
} else {
try {
// Check if local file exists.
await fsPromises.readFile(MANIFESTS_FILE_LOCATION, 'utf8');
} catch (eror) {
await getManifests();
}
}

const manifests: SnapManifest[] = JSON.parse(
await fsPromises.readFile(MANIFESTS_FILE_LOCATION, 'utf8'),
);

/**
* Write your custom code to query the `manifests` array for the data you are interested in.
*/

Mrtenz marked this conversation as resolved.
Show resolved Hide resolved
// 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;
});
Loading