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

feat: Snap Home Page #563

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/friendly-comics-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@blockchain-lab-um/masca': patch
---

Added home page, update page and install page. Updated Snaps SDK to latest version.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { headers } from 'next/headers';
import { notFound } from 'next/navigation';
import { createClient } from '@supabase/supabase-js';
import { VerifiablePresentation } from '@veramo/core';
Expand Down
4 changes: 2 additions & 2 deletions packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@
"@iden3/js-merkletree": "1.0.0",
"@metamask/key-tree": "^9.0.0",
"@metamask/providers": "14.0.2",
"@metamask/snaps-sdk": "1.2.0",
"@metamask/utils": "^8.2.1",
"@metamask/snaps-sdk": "1.4.0",
"@metamask/utils": "^8.3.0",
"@types/lodash.clonedeep": "^4.5.7",
"@veramo/core": "5.6.0",
"@veramo/credential-eip712": "5.6.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@
"./files/circuits/credentialAtomicQuerySigV2/circuit_final.zkey",
"./files/circuits/credentialAtomicQuerySigV2/verification_key.json"
],
"shasum": "WcGYh+wxUGmNhtRywe2CtCMeSCk2kSi4GMm+tLoE4No="
"shasum": "MhBfgnW6fXbMbk3uGOsSTkONFX79i7DJVnKeh+0s5D4="
},
"initialPermissions": {
"endowment:ethereum-provider": {},
"endowment:network-access": {},
"endowment:lifecycle-hooks": {},
"endowment:rpc": {
"dapps": true,
"snaps": true
},
"snap_dialog": {},
"snap_getEntropy": {},
"snap_manageState": {},
"endowment:webassembly": {}
"endowment:webassembly": {},
"endowment:page-home": {}
},
"manifestVersion": "0.1"
}
9 changes: 5 additions & 4 deletions packages/snap/src/Snap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ class SnapService {
* @param params.filter.filter - Filter to apply.
* @param params.options.store - VC store to query.
* @param params.options.returnStore - Whether to return the store name.
* @param isHomePage - Whether the request is from the home page.
* @returns array - Array of VCs.
*/
static async queryCredentials(
params: QueryCredentialsRequestParams
params: QueryCredentialsRequestParams,
isHomePage = false
): Promise<QueryCredentialsRequestResult[]> {
const { filter, options } = params ?? {};
const { store, returnStore = true } = options ?? {};

// FIXME: Maybe do this in parallel? Does it make sense?
const veramoCredentials = await VeramoService.queryCredentials({
options: { store, returnStore },
Expand All @@ -79,8 +80,8 @@ class SnapService {
}));

const vcs = [...veramoCredentials, ...polygonCredentials];

if (!vcs.length) return [];
if (vcs.length === 0) return [];
if (isHomePage) return vcs;
if (await UIService.queryAllDialog({ vcs })) {
return vcs;
}
Expand Down
80 changes: 80 additions & 0 deletions packages/snap/src/UI.service.ts

Large diffs are not rendered by default.

58 changes: 56 additions & 2 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import './polyfills/intl';

import { isValidSetCurrentAccountRequest } from '@blockchain-lab-um/masca-types';
import {
CURRENT_STATE_VERSION,
isValidSetCurrentAccountRequest,
QueryCredentialsOptions,
QueryCredentialsRequestParams,
} from '@blockchain-lab-um/masca-types';
import { ResultObject, type Result } from '@blockchain-lab-um/utils';
import type { OnRpcRequestHandler } from '@metamask/snaps-sdk';
import type {
OnHomePageHandler,
OnInstallHandler,
OnRpcRequestHandler,
OnUpdateHandler,
} from '@metamask/snaps-sdk';

import EthereumService from './Ethereum.service';
import GeneralService from './General.service';
Expand Down Expand Up @@ -51,3 +61,47 @@ export const onRpcRequest: OnRpcRequestHandler = async ({
return ResultObject.error((e as Error).toString());
}
};

export const onHomePage: OnHomePageHandler = async () => {
await StorageService.init();
await GeneralService.initAccountState();
await WalletService.init();
await VeramoService.init();
await EthereumService.init();

const did = await SnapService.getDID();

let vcs = [];

try {
const state = StorageService.get();
const session =
state[CURRENT_STATE_VERSION].accountState[
state[CURRENT_STATE_VERSION].currentAccount
].general.ceramicSession;

const queryParams = {} as QueryCredentialsRequestParams;

if (!session) {
queryParams.options = {
store: ['snap'],
} as QueryCredentialsOptions;
}

vcs = await SnapService.queryCredentials(queryParams, true);
} catch (e) {
console.error(e);
}
await UIService.init('');
return UIService.homePage(did, vcs.length);
};

export const onInstall: OnInstallHandler = async () => {
await UIService.init('');
await UIService.install();
};

export const onUpdate: OnUpdateHandler = async () => {
await UIService.init('');
await UIService.update();
};
Loading
Loading