UNPKG

@blockchain-lab-um/masca-connector

Version:

Library for using Masca on the frontend

350 lines (347 loc) 9.09 kB
import { ProviderStore } from './chunk-S36CRCIQ.js'; import { ViemClient } from './chunk-G7G7UPOD.js'; import { validateAndSetCeramicSession, signVerifiablePresentation, signVerifiableCredential } from './chunk-OMAQRS7F.js'; import { isError, ResultObject } from '@blockchain-lab-um/utils'; async function sendSnapMethod(context, request, snapId) { const provider = context.providerStore.getCurrentProvider()?.provider; if (!provider) throw new Error("No provider found"); return provider.request({ method: "wallet_invokeSnap", params: { snapId, request } }); } async function queryCredentials(params) { await validateAndSetCeramicSession.bind(this)(); return sendSnapMethod( this, { method: "queryCredentials", params: params ?? {} }, this.snapId ); } async function createPresentation(params) { await validateAndSetCeramicSession.bind(this)(); const result = await sendSnapMethod( this, { method: "createPresentation", params }, this.snapId ); if (isError(result)) { return result; } if (result.data.proof) { return result; } const signedResult = ResultObject.success( await signVerifiablePresentation.bind(this)(result.data) ); return signedResult; } async function decodeSdJwtPresentation(params) { return sendSnapMethod( this, { method: "decodeSdJwtPresentation", params: { ...params } }, this.snapId ); } async function saveCredential(vc, options) { await validateAndSetCeramicSession.bind(this)(); return sendSnapMethod( this, { method: "saveCredential", params: { verifiableCredential: vc, options } }, this.snapId ); } async function deleteCredential(id, options) { await validateAndSetCeramicSession.bind(this)(); return sendSnapMethod( this, { method: "deleteCredential", params: { id, options } }, this.snapId ); } async function getDID() { return sendSnapMethod(this, { method: "getDID" }, this.snapId); } async function getSelectedMethod() { return sendSnapMethod(this, { method: "getSelectedMethod" }, this.snapId); } async function getAvailableMethods() { return sendSnapMethod(this, { method: "getAvailableMethods" }, this.snapId); } async function switchDIDMethod(method) { if (this.supportedMethods.includes(method)) { return sendSnapMethod( this, { method: "switchDIDMethod", params: { didMethod: method } }, this.snapId ); } return ResultObject.error(`Method ${method} is not supported on this dapp.`); } async function togglePopups() { return sendSnapMethod(this, { method: "togglePopups" }, this.snapId); } async function addTrustedDapp(origin) { return sendSnapMethod( this, { method: "addTrustedDapp", params: { origin } }, this.snapId ); } async function removeTrustedDapp(origin) { return sendSnapMethod( this, { method: "removeTrustedDapp", params: { origin } }, this.snapId ); } async function addDappSettings(origin) { return sendSnapMethod( this, { method: "addDappSettings", params: { origin } }, this.snapId ); } async function removeDappSettings(origin) { return sendSnapMethod( this, { method: "removeDappSettings", params: { origin } }, this.snapId ); } async function changePermission(origin, method, value) { return sendSnapMethod( this, { method: "changePermission", params: { origin, method, value } }, this.snapId ); } async function getCredentialStore() { return sendSnapMethod(this, { method: "getCredentialStore" }, this.snapId); } async function getAvailableCredentialStores() { return sendSnapMethod( this, { method: "getAvailableCredentialStores" }, this.snapId ); } async function setCredentialStore(store, value) { return sendSnapMethod( this, { method: "setCredentialStore", params: { store, value } }, this.snapId ); } async function getAccountSettings() { return sendSnapMethod(this, { method: "getAccountSettings" }, this.snapId); } async function getSnapSettings() { return sendSnapMethod(this, { method: "getSnapSettings" }, this.snapId); } async function resolveDID(did) { return sendSnapMethod( this, { method: "resolveDID", params: { did } }, this.snapId ); } async function createCredential(params) { await validateAndSetCeramicSession.bind(this)(); const result = await sendSnapMethod( this, { method: "createCredential", params }, this.snapId ); const vcResult = result; if (vcResult.success && (vcResult.data.proof || vcResult.data.proofType === "sd-jwt")) { return vcResult; } if (isError(vcResult)) { return vcResult; } const signedResult = ResultObject.success( await signVerifiableCredential.bind(this)(vcResult.data, params) ); return signedResult; } async function setCurrentAccount(params) { return sendSnapMethod( this, { method: "setCurrentAccount", params }, this.snapId ); } async function verifyData(params) { return sendSnapMethod( this, { method: "verifyData", params }, this.snapId ); } async function handleCredentialOffer(params) { return sendSnapMethod( this, { method: "handleCredentialOffer", params }, this.snapId ); } async function handleAuthorizationRequest(params) { return sendSnapMethod( this, { method: "handleAuthorizationRequest", params }, this.snapId ); } async function setCeramicSession(serializedSession) { return sendSnapMethod( this, { method: "setCeramicSession", params: { serializedSession } }, this.snapId ); } async function validateStoredCeramicSession() { return sendSnapMethod( this, { method: "validateStoredCeramicSession" }, this.snapId ); } async function exportStateBackup() { return sendSnapMethod( this, { method: "exportStateBackup" }, this.snapId ); } async function importStateBackup(params) { return sendSnapMethod( this, { method: "importStateBackup", params }, this.snapId ); } async function getWalletId() { return sendSnapMethod( this, { method: "getWalletId" }, this.snapId ); } async function signData(params) { return sendSnapMethod( this, { method: "signData", params }, this.snapId ); } var wrapper = (fn) => async (...args) => { try { return await fn(...args); } catch (e) { return ResultObject.error(e.toString()); } }; var Masca = class { constructor(snapId, supportedMethods) { this.getMascaApi = () => ({ saveCredential: wrapper(saveCredential.bind(this)), queryCredentials: wrapper(queryCredentials.bind(this)), createPresentation: wrapper(createPresentation.bind(this)), decodeSdJwtPresentation: wrapper(decodeSdJwtPresentation.bind(this)), togglePopups: wrapper(togglePopups.bind(this)), addTrustedDapp: wrapper(addTrustedDapp.bind(this)), removeTrustedDapp: wrapper(removeTrustedDapp.bind(this)), getDID: wrapper(getDID.bind(this)), getSelectedMethod: wrapper(getSelectedMethod.bind(this)), getAvailableMethods: wrapper(getAvailableMethods.bind(this)), switchDIDMethod: wrapper(switchDIDMethod.bind(this)), getCredentialStore: wrapper(getCredentialStore.bind(this)), setCredentialStore: wrapper(setCredentialStore.bind(this)), getAvailableCredentialStores: wrapper( getAvailableCredentialStores.bind(this) ), deleteCredential: wrapper(deleteCredential.bind(this)), getSnapSettings: wrapper(getSnapSettings.bind(this)), getAccountSettings: wrapper(getAccountSettings.bind(this)), resolveDID: wrapper(resolveDID.bind(this)), createCredential: wrapper(createCredential.bind(this)), setCurrentAccount: wrapper(setCurrentAccount.bind(this)), verifyData: wrapper(verifyData.bind(this)), handleCredentialOffer: wrapper(handleCredentialOffer.bind(this)), handleAuthorizationRequest: wrapper(handleAuthorizationRequest.bind(this)), setCeramicSession: wrapper(setCeramicSession.bind(this)), validateStoredCeramicSession: wrapper( validateStoredCeramicSession.bind(this) ), importStateBackup: wrapper(importStateBackup.bind(this)), exportStateBackup: wrapper(exportStateBackup.bind(this)), getWalletId: wrapper(getWalletId.bind(this)), signData: wrapper(signData.bind(this)), changePermission: wrapper(changePermission.bind(this)), addDappSettings: wrapper(addDappSettings.bind(this)), removeDappSettings: wrapper(removeDappSettings.bind(this)) }); this.snapId = snapId; this.supportedMethods = supportedMethods; this.providerStore = new ProviderStore(); this.viemClient = new ViemClient({ provider: this.providerStore.getCurrentProvider()?.provider }); } }; export { Masca };