Skip to content

Commit

Permalink
feat: Sending encrypted DIDComm messages
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Feb 10, 2020
1 parent 02fefa9 commit 2f12513
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 58 deletions.
32 changes: 11 additions & 21 deletions packages/daf-did-comm/src/action-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,22 @@ export class ActionHandler extends AbstractActionHandler {

debug('Resolving didDoc')
const didDoc = await core.didResolver.resolve(data.to)

const service = didDoc && didDoc.service && didDoc.service.find(item => item.type == 'MessagingService')
const publicKey =
didDoc &&
didDoc.publicKey &&
didDoc.publicKey.find(item => item.type == 'Curve25519EncryptionPublicKey')
const service = didDoc && didDoc.service && didDoc.service.find(item => item.type == 'Messaging')

if (service) {
try {
let body = data.jwt
if (publicKey && publicKey.publicKeyHex && core.encryptionKeyManager) {
await this.didcomm.ready
const senderKeyPair = await core.encryptionKeyManager.getKeyPairForDid(data.from)
if (senderKeyPair) {
const dm = JSON.stringify({
'@type': 'JWT',
id: uuid.v4(),
data: data.jwt,
})

body = await this.didcomm.pack_auth_msg_for_recipients(
dm,
[Uint8Array.from(Buffer.from(publicKey.publicKeyHex, 'hex'))],
senderKeyPair,
)
}
try {
const identity = await core.identityManager.getIdentity(data.from)
const dm = JSON.stringify({
'@type': 'JWT',
id: uuid.v4(),
data: data.jwt,
})
body = await identity.encrypt(data.to, dm)
} catch (e) {
console.log(e)
}

debug('Sending to %s', service.serviceEndpoint)
Expand Down
73 changes: 36 additions & 37 deletions packages/daf-did-comm/src/message-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,50 @@ export class MessageValidator extends AbstractMessageValidator {
}

async validate(message: Message, core: Core): Promise<Message> {
if (core.encryptionKeyManager) {
try {
const parsed = JSON.parse(message.raw)
if (parsed.ciphertext && parsed.protected) {
const keyPairs = await core.encryptionKeyManager.listKeyPairs()
for (const keyPair of keyPairs) {
const unpacked = await this.didcomm.unpackMessage(message.raw, keyPair)
if (unpacked.message) {
debug('Unpacked for publicKey %s', keyPair.publicKeyHex)
debug(unpacked.message)
try {
const parsed = JSON.parse(message.raw)
if (parsed.ciphertext && parsed.protected) {
const identities = await core.identityManager.getIdentities()
for (const identity of identities) {
const decrypted = await identity.decrypt(message.raw)

This comment has been minimized.

Copy link
@mirceanis

mirceanis Feb 10, 2020

Member

@simonas-notcat I see a potential bug lurking here

If an identity can't decrypt, it may thrown an error causing the for loop to terminate prematurely.
It may be worth wrapping the decryption call in its own try/catch

This comment has been minimized.

Copy link
@simonas-notcat

simonas-notcat Feb 11, 2020

Author Contributor

good catch!

if (decrypted) {
debug('Decrypted for %s', identity.did)
debug(decrypted)

try {
const json = JSON.parse(unpacked.message)
if (json['@type'] === 'JWT') {
message.transform({
raw: json.data,
meta: { type: 'DIDComm' },
})
} else {
if (json['@id']) message.id = json['@id']
if (json['@type']) message.type = json['@type']
message.transform({
raw: unpacked.message,
data: json,
meta: { type: 'DIDComm' },
})
}
return super.validate(message, core)
} catch (e) {
debug(e)
try {
const json = JSON.parse(decrypted)
if (json['@type'] === 'JWT') {
message.transform({
raw: json.data,
meta: { type: 'DIDComm' },
})
} else {
if (json['@id']) message.id = json['@id']
if (json['@type']) message.type = json['@type']
message.transform({
raw: decrypted,
data: json,
meta: { type: 'DIDComm' },
})
}

message.transform({
raw: unpacked.message,
meta: { type: 'DIDComm' },
})

return super.validate(message, core)
} catch (e) {
debug(e.message)
}

message.transform({
raw: decrypted,
meta: { type: 'DIDComm' },
})

return super.validate(message, core)
}
}
} catch (e) {
// not a JSON string
}
} catch (e) {
// not a JSON string
}

return super.validate(message, core)
}
}

0 comments on commit 2f12513

Please sign in to comment.