Identify Snap connects to your currently connected Metamask account by default. To learn how apps can connect to Identify Snap using a non-metamask(external) account, refer to this documentation.
Then, depending on whether you're trying to connect to a metamask account or a non-metamask account, you can call the snap API in the following way:
constsnapId=`npm:@tuum-tech/identify`constmetamaskAddress='0x2e5fF0267b678A0FAF9A9f5b0FBf7Ac9638B5b57'constparams= { metamaskAddress,/* Uncomment the below line if you want to connect to a non-metamask account */// ...externalAccountParams}consthandleSyncGoogleVCsAPI=async () => {awaitwindow.ethereum.request({ method:'wallet_invokeSnap', params: { snapId, request: { method:'syncGoogleVCs', params: params } } })}
How the API is handled between the app and snap
What the API does
Retrieves the currently connected account and the blockchain network the user has selected on Metamask. If it's the first time, the account info is also saved in snap state.
Retrieves the VCs from google drive
Retrieves the VCs from the local Metamask snap state
Compare the VCs from both stores and figure out what VCs are missing in snap state that are present in google drive and what VCs are missing in google drive that are present in snap state
Import the missing VCs from google drive onto Metamask snap state
Export the missing VCs from Metamask snap state onto google drive
Some example responses:
true
How the API works
Live Demo on CodePen
Some things to keep in mind while interacting with the above demo
If you're getting any errors with the live demo, make sure you go through the FAQs section to learn about what you may be missing. You need to install Metamask in your browser for the live demo to work
Whenever there is a new version of the IdentifySnap, always make sure to first uninstall the old version of the snap from Metamask and only then try the above demo so it can install the latest version
To ease the integration of Identify Snap on an application, we have created a template web application that you can run locally and check out the code in its entirety to learn how you can integrate and interact with various APIs exposed by Identify Snap. Check out the full source code at template application github repository.
You can also check out the API reference to learn how each API works.
import { Account, IdentitySnapParams, IdentitySnapState, SnapDialogParams,} from'../../interfaces';import { verifyToken } from'../../plugins/veramo/google-drive-data-store';import { IDataManagerQueryResult, IDataManagerSaveResult, ISaveVC, QueryOptions, SaveOptions,} from'../../plugins/veramo/verifiable-creds-manager';import { generateVCPanel, snapDialog } from'../../snap/dialog';import { getAccountStateByCoinType } from'../../snap/state';import { Agent, getVeramoAgent } from'../../veramo/agent';/** * Function to sync Google VCs with snap. * * @param identitySnapParams - Identity snap params. */exportasyncfunctionsyncGoogleVCs( identitySnapParams:IdentitySnapParams,):Promise<boolean> {const { origin,state,account } = identitySnapParams;try {// Get Veramo agentconstagent=awaitgetVeramoAgent(snap, state);constaccountState=awaitgetAccountStateByCoinType( state,account.evmAddress, );constgUserEmail=awaitverifyToken(accountState.accountConfig.identity.googleUserInfo.accessToken, );constoptions:QueryOptions= { store:'snap', returnStore:true };// Get VCs from the snap state storageconstsnapVCs= (awaitagent.queryVC({ filter:undefined, options, })) asIDataManagerQueryResult[];// Get VCs from google drive storageoptions.store ='googleDrive';constgoogleVCs= (awaitagent.queryVC({ filter:undefined, options, accessToken:accountState.accountConfig.identity.googleUserInfo.accessToken, })) asIDataManagerQueryResult[];/* googleVCs = googleVCs.filter( (vc) => (vc.data as VerifiableCredential).credentialSubject.id?.split(':')[4] === account.evmAddress, // Note that we're only doing this because this is a did:pkh VC. We need to handle other VCs differently
); */constsnapVCIds=snapVCs.map((vc) =>vc.metadata.id);constgoogleVCIds=googleVCs.map((vc) =>vc.metadata.id);constvcsNotInSnap=googleVCs.filter( (vc) =>!snapVCIds.includes(vc.metadata.id), );console.log('vcsNotInSnap: ',JSON.stringify(vcsNotInSnap,null,4));constvcsNotInGDrive=snapVCs.filter( (vc) =>!googleVCIds.includes(vc.metadata.id), );console.log('vcsNotInGDrive: ',JSON.stringify(vcsNotInGDrive,null,4));constheader='Sync Verifiable Credentials';let vcsNotInSnapSync =true;if (vcsNotInSnap.length>0) { vcsNotInSnapSync =awaithandleSync( state, account, agent, origin,`${header} - Import VCs from Google drive: ${gUserEmail}`,'Would you like to sync VCs in Google drive with Metamask snap?','This action will import the VCs that are in Google drive to the Metamask snap', vcsNotInSnap,'snap', ); }let vcsNotInGDriveSync =true;if (vcsNotInGDrive.length>0) { vcsNotInGDriveSync =awaithandleSync( state, account, agent, origin,`${header} - Export VCs to Google drive: ${gUserEmail}`,'Would you like to sync VCs in Metamask snap with Google drive?','This action will export the VCs that are in Metamask snap to Google drive', vcsNotInGDrive,'googleDrive', ); }if (vcsNotInSnapSync && vcsNotInGDriveSync) {returntrue; } } catch (error) {console.error('Could not sync Verifiable Credentials between the Metamask snap and Google Drive properly. Please try again',JSON.stringify(error,null,4), );throw error; }console.log('Could not sync Verifiable Credentials between the Metamask snap and Google drive properly. Please try again', );thrownewError('Could not sync Verifiable Credentials between the Metamask snap and Google drive properly. Please try again', );}/** * Function to handle the snap dialog and import/export each VC. * * @param state - Identity state. * @param account - Currently connected account. * @param agent - Veramo. * @param origin - The origin of where the call is being made from. * @param header - Header text of the metamask dialog box(eg. 'Retrieve Verifiable Credentials'). * @param prompt - Prompt text of the metamask dialog box(eg. 'Are you sure you want to send VCs to the dApp?'). * @param description - Description text of the metamask dialog box(eg. 'Some dApps are less secure than others and could save data from VCs against your will. Be careful where you send your private VCs! Number of VCs submitted is 2').
* @param vcs - The Verifiable Credentials to show on the metamask dialog box. * @param store - The snap store to use(snap or googleDrive). */asyncfunctionhandleSync( state:IdentitySnapState, account:Account, agent:Agent, origin:string, header:string, prompt:string, description:string, vcs:IDataManagerQueryResult[], store:string,):Promise<boolean> {constdialogParams:SnapDialogParams= { type:'confirmation', content:awaitgenerateVCPanel(origin, header, prompt, description, vcs), };if (awaitsnapDialog(snap, dialogParams)) {constoptions= { store, } asSaveOptions;constaccountState=awaitgetAccountStateByCoinType( state,account.evmAddress, );constdata=vcs.map((x) => {return { vc:x.data, id:x.metadata.id } asISaveVC; }) asISaveVC[];constresult:IDataManagerSaveResult[] =awaitagent.saveVC({ data, options, accessToken:accountState.accountConfig.identity.googleUserInfo.accessToken, });if (!(result.length>0&& result[0].id !=='')) {console.log('Could not sync the vc: ',JSON.stringify(data,null,4));returnfalse; }returntrue; }console.log('User rejected the sync operation');returnfalse;}