import { makeAutoObservable, runInAction } from "mobx"; import api, { W3cCredentialDto } from "@dashboard/engine-api"; import { toast } from "react-toastify"; import displayError from "@dashboard/utils/displayError"; import { FormType, LegalParticipantFormData, PrivatePersonFormData, } from "./interfaces"; class OfferJsonLdCredentialDialogStore { public formType: FormType | null = null; public connectionId: string | undefined; public signing = false; public offerSending = false; constructor(connectionId: string | undefined) { this.connectionId = connectionId; makeAutoObservable(this); } private buildPrivatePersonJsonLd(data: PrivatePersonFormData) { return { "@context": [ "https://www.w3.org/2018/credentials/v1", "https://w3id.org/security/suites/jws-2020/v1", "https://www.vereign.com/.well-known/vereign-schema", ], type: ["VerifiableCredential"], id: "", issuer: "", issuanceDate: "", credentialSubject: { type: "vereign:PrivatePerson", "vereign:name": data.name, "vereign:dateOfBirth": data.dateOfBirth, "vereign:address": { "vereign:street": data.street, "vereign:building": data.building, "vereign:city": data.city, "vereign:country": data.country, }, }, }; } private buildLegalParticipantJsonLd(data: LegalParticipantFormData) { return { "@context": [ "https://www.w3.org/2018/credentials/v1", "https://w3id.org/security/suites/jws-2020/v1", "https://www.vereign.com/.well-known/vereign-schema", ], type: ["VerifiableCredential"], id: "", issuer: "", issuanceDate: "", credentialSubject: { type: "vereign:LegalParticipant", "vereign:companyName": data.companyName, "vereign:taxID": data.taxID, "vereign:gleiCode": data.gleiCode, "vereign:address": { "vereign:street": data.street, "vereign:building": data.building, "vereign:city": data.city, "vereign:country": data.country, }, }, }; } public offerPrivatePersonCreds = async (data: PrivatePersonFormData) => { return this.offerCredential(this.buildPrivatePersonJsonLd(data)); }; public offerLegalParticipantCreds = async ( data: LegalParticipantFormData, ) => { return this.offerCredential(this.buildLegalParticipantJsonLd(data)); }; public offerCustomJsonLdCreds = async (doc: unknown) => { return this.offerCredential(doc); }; private offerCredential = async (doc: unknown) => { runInAction(() => (this.offerSending = true)); try { const cred = await api.offerJsonLdCredential({ connectionId: this.connectionId, doc: doc as W3cCredentialDto, }); toast("Credential offer sent"); runInAction(() => { this.offerSending = false; }); return cred; } catch (e: unknown) { displayError(e); runInAction(() => { this.offerSending = false; }); } return null; }; public signJsonLd = async (doc: unknown) => { runInAction(() => (this.signing = true)); try { const cred = await api.signJsonLdCredential({ doc: doc as W3cCredentialDto, }); toast("Credential signed"); runInAction(() => { this.signing = false; }); return cred; } catch (e: unknown) { displayError(e); runInAction(() => { this.signing = false; }); } return null; }; } export type { OfferJsonLdCredentialDialogStore }; export default OfferJsonLdCredentialDialogStore;