Skip to content

Commit

Permalink
feat: send and process credentials as attachments when sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
nickreynolds committed Apr 3, 2024
1 parent c2a9c35 commit 94834c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/agent-explore/src/plugins/chats/ShareForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import Editor from '@monaco-editor/react';
import { ResponsiveContainer } from '../../components/ResponsiveContainer'
import { v4 } from 'uuid'
import { useNavigate } from 'react-router'
import { computeEntryHash } from '@veramo/utils'


export const ShareForm: React.FC = () => {
const token = theme.useToken()

console.log('here')
const [message, setMessage] = useState<string>(window.localStorage.getItem('bs-post') || '')
const attachment = window.localStorage.getItem('attachment')
const { notification } = App.useApp()
const navigate = useNavigate()

Expand All @@ -26,19 +28,31 @@ console.log('here')
window.localStorage.setItem('bs-post', message)
}, [message])

console.log("attachment: ", attachment)

const handleSend = async (did: string, issuerAgent: TAgent<ICredentialIssuer & IDataStore>) => {
setIsSending(true)
try {
const threadId = v4()
const messageId = v4()

const parsedAttachment = JSON.parse(attachment!)!
const canonicalCredential = parsedAttachment?.proof?.type === 'JwtProof2020' &&
typeof parsedAttachment?.proof?.jwt === 'string'
? parsedAttachment?.proof?.jwt
: parsedAttachment
const threadId = computeEntryHash(canonicalCredential)
const shareMessage = {
type: 'https://didcomm.org/basicmessage/2.0/message',
from: did,
created_time: new Date().getTime(),
to: recepient,
id: threadId,
id: messageId,
thid: threadId,
body: { content: message }
body: { content: message },
attachments: [{
media_type: 'credential+ld+json',
data: { json: JSON.parse(attachment!) }
}]
}
const packedMessage = await issuerAgent.packDIDCommMessage({
message: shareMessage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ export class SaveMessageHandler extends AbstractMessageHandler {
if (!localMessage) {
console.log('Saving message', message)
await context.agent.dataStoreSaveMessage({ message })
if (message.attachments && message.attachments.length > 0) {
console.log("attachments found")
for (const attachment of message.attachments) {
if (attachment.media_type === 'credential+ld+json') {
const credential = await context.agent.dataStoreSaveVerifiableCredential({ verifiableCredential: attachment.data.json})
console.log("saved attached credential: ", credential)
}
}
}
}
}

Expand Down

0 comments on commit 94834c9

Please sign in to comment.