import type { ManageStateResult, Panel } from '@metamask/snaps-sdk'; import { panel, text, heading, copyable, divider, } from '@metamask/snaps-sdk'; import { SnapVerifiable } from '../snap-classes/SnapVerifiable'; import type { BasicCredential, IdentifyCredential, SnapCredential, } from '../snap-types/SnapTypes'; export class SnapViewModels { public static helloViewModel(origin: string): Panel { return panel([ copyable(`Hello, **${origin}**!`), text('This is an AuthFlow test message!!'), text('AuthFlow test message, keep those passwords safe!!!!!'), ]); } public static failureViewModel(): Panel { return panel([heading(`Failure`), text(`Error: No credentials stored!`)]); } public static clearAllViewModel(): Panel { return panel([ heading('Clear all passwords?'), text('Are you sure you want to clear all passwords?'), ]); } public static clearAllVCViewModel(): Panel { return panel([ heading('Clear all verified credentials?'), text('Are you sure you want to clear all verified credentials?'), ]); } public static clearOneVCViewModel(): Panel { return panel([ heading('Clear all verified credentials?'), text('Are you sure you want to clear all verified credentials?'), ]); } public static retrieveBasicCredsViewModel( description: string, site: string, ): Panel { return panel([ heading(`Send login for ${description}`), copyable( `Are you sure you want to send the login for ${description} to ${site}?`, ), ]); } public static retrieveVerifiableCredsViewModel( description: string, site: string, ): Panel { return panel([ heading(`Send verifiable credential for ${description}`), copyable( `Are you sure you want to send the verifiable credential for ${description} to ${site}?`, ), ]); } public static createVerifiablePresentationViewModel( description: string, site: string, ): Panel { return panel([ heading( `Create verifiable presentation for verifiable credential(s) ${description}`, ), copyable( `Are you sure you want to create a verifiable presentation for ${description} and send to ${site}?`, ), ]); } public static async displayBasicCredentialsViewModel( credentials: ManageStateResult, ): Promise { const returnPanel: any = [heading('Basic Credentials')]; if (credentials === null) { return this.failureViewModel(); } try { Object.values(credentials).forEach((value) => { if (value !== null) { const cred = value as SnapCredential; if (cred.type === 'Basic') { const credData = cred.credentialData as BasicCredential; returnPanel.push(copyable(cred.description)); returnPanel.push(copyable(credData.username)); returnPanel.push(copyable(credData.password)); returnPanel.push(divider()); } } }); return panel(returnPanel); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : "An unknown error occurred"; console.error(`Error in displayBasicCredentialsViewModel: ${errorMessage}`); return this.failureViewModel(); } } public static async displayVerifiableCredentialsViewModel( credentials: ManageStateResult, ): Promise { const returnPanel: any = [heading('Verifiable Credentials')]; if (credentials === null) { return this.failureViewModel(); } let identifyCredentials; try { identifyCredentials = await SnapVerifiable.getVerifiableCredentials([ 'snap', 'googleDrive', ]); } catch (error:unknown) { identifyCredentials = await SnapVerifiable.getVerifiableCredentials([ 'snap', ]); } if(identifyCredentials) { const identifyCredValues: any[] = JSON.parse(identifyCredentials); const identifyCredTable: Record = {}; identifyCredValues.forEach((item) => { identifyCredTable[item.metadata.id] = item.data; }); try { Object.entries(credentials).forEach(([key, value]) => { if (value !== null) { const cred = value as SnapCredential; if (cred.type === 'Identify') { const credId = (cred.credentialData as IdentifyCredential).id; returnPanel.push(copyable(cred.description)); returnPanel.push(copyable(credId)); returnPanel.push( copyable(JSON.stringify(identifyCredTable[credId])), ); returnPanel.push(divider()); } } }); return panel(returnPanel); } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : "An unknown error occurred"; console.error(`Error in displayVerifiedCredentialsViewModel: ${errorMessage}`); return panel(returnPanel); } } return this.failureViewModel(); } public static async displayRenameVerifiableCredentialViewModel(): Promise { const returnPanel: any = [heading('Rename Verifiable Credential')]; returnPanel.push(text('Enter name')); return panel(returnPanel); } public static async displayCreateVerifiablePresentationViewModel(): Promise { const returnPanel: any = [heading('Create Verifiable Presentation')]; returnPanel.push(text('Enter a comma separated list of credential names')); return panel(returnPanel); } public static async displayShowVerifiablePresentationViewModel( json: string, ): Promise { const returnPanel: any = [heading(`Verifiable Presentation`)]; returnPanel.push(text('Generated Verifiable Presentation:')); returnPanel.push(copyable(json)); return panel(returnPanel); } public static async displayEnterBasicDescriptionViewModel(): Promise { const returnPanel: any = [heading('Basic Credential')]; returnPanel.push(text('Enter basic credential description')); return panel(returnPanel); } public static async displayEnterBasicUserViewModel(): Promise { const returnPanel: any = [heading('Basic Credential')]; returnPanel.push(text('Enter basic credential user')); return panel(returnPanel); } public static async displayEnterBasicPasswordViewModel(): Promise { const returnPanel: any = [heading('Basic Credential')]; returnPanel.push(text('Enter basic credential password')); return panel(returnPanel); } public static async displayDeleteBasicPasswordViewModel(): Promise { const returnPanel: any = [heading('Delete A Basic Credential')]; returnPanel.push(text('Enter name of basic credential to delete')); return panel(returnPanel); } public static async displayDeleteVerifiableCredentialViewModel(): Promise { const returnPanel: any = [heading('Delete A Verified Credential')]; returnPanel.push(text('Enter name of verified credential to delete')); return panel(returnPanel); } }