import { useContext, useState, useEffect } from "react"; import { Box, Button, Flex, Form, Flash, Loader } from "rimble-ui"; import { Send } from "@rimble/icons"; import { VC } from "vc-schema-tools"; import { DidSearchWithMessagingInfo } from "../../../elements/DidSearchWithMessagingInfo"; import { SertoUiContext, SertoUiContextInterface } from "../../../../context/SertoUiContext"; export interface ShareViaDidCommProps { vc: VC; } export const ShareViaDidComm: React.FunctionComponent = (props) => { const { vc } = props; const context = useContext(SertoUiContext); const [loading, setLoading] = useState(false); const [success, setSuccess] = useState(false); const [error, setError] = useState(""); const [recipientDid, setRecipientDid] = useState(""); const [recipientSupportsMessaging, setRecipientSupportsMessaging] = useState(false); useEffect(() => { setError(""); }, [recipientDid, recipientSupportsMessaging]); async function sendVc(e: Event) { e.preventDefault(); if (!recipientDid || !recipientSupportsMessaging) { return; } if (!context.sendVc) { console.error("serto-ui context missing sendVc", context); setError("This Serto instance is missing the ability to send VCs via DIDComm."); return; } setError(""); setLoading(true); setSuccess(false); try { let senderDid = typeof vc.issuer === "string" ? vc.issuer : vc.issuer.id; const senderDidBelongsToUser = context.userDids?.some((did) => did.did === senderDid); if (!senderDidBelongsToUser) { if (!context.userDids?.[0]) { throw Error( `VC issuer DID ${senderDid} does not belong to user, and could not get any user DIDs to use as DIDComm sender`, ); } // @TODO/tobek Is this the right behavior in this case? console.warn( `VC issuer ${senderDid} does not belong to user, sending VC from user's first DID instead`, context.userDids[0], ); senderDid = context.userDids[0].did; } await context.sendVc(senderDid, recipientDid, vc); } catch (err) { console.error("failed to send VC:", err); setError("Failed to send credential to subject: " + err.message); setLoading(false); return; } setLoading(false); setSuccess(true); } return (
setRecipientDid(val.did)} supportsMessaging={recipientSupportsMessaging} setSupportsMessaging={setRecipientSupportsMessaging} messagingUnsupportedText="This DID does not support receiving credentials via DIDComm." /> {recipientDid && error && ( {error} )} {recipientDid && success && ( Credential sent successfully. )}
); };