Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update DIDComm json message types #375

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions src/domain/models/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ export class Message implements Pluto.Storable {
}

static isJsonAttachment(data: any): data is AttachmentJsonData {
return data.data !== undefined || data.json !== undefined;
// data.data handled for backwards compatibility with stored messages
return data.json !== undefined || data.data !== undefined;
}
}

Expand Down Expand Up @@ -173,12 +174,9 @@ export namespace Message {
}

if (isJson(attachment.data)) {
let decoded: any;
if ("data" in attachment.data) {
decoded = attachment.data.data;
} else if ("json" in attachment.data) {
decoded = attachment.data.json;
}
// data.data handled for backwards compatibility
const decoded = attachment.data.json ?? (attachment.data as any).data;

return typeof decoded === "object"
? decoded
: JSON.parse(decoded);
Expand All @@ -193,8 +191,8 @@ export namespace Message {
};

const isJson = (data: AttachmentData): data is AttachmentJsonData => {
// ?? why do we mutate json -> data in didcomm Wrapper
return "data" in data || "json" in data;
// data.data handled for backwards compatibility
return "json" in data || "data" in data;
};
}
}
2 changes: 0 additions & 2 deletions src/domain/models/MessageAttachment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export interface AttachmentLinkData {

export type AttachmentJsonData = {
json: any;
} | {
data: any;
};

export type AttachmentData =
Expand Down
14 changes: 6 additions & 8 deletions src/edge-agent/protocols/pickup/PickupRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { ProtocolType } from "../ProtocolTypes";
import { PickupAttachment } from "../types";

type PickupResponse =
| { type: "status"; message: Message }
| { type: "delivery"; message: Message }
| { type: 'report', message: Message };
| { type: "status"; message: Message; }
| { type: "delivery"; message: Message; }
| { type: 'report', message: Message; };

export class PickupRunner {
private message: PickupResponse;
Expand Down Expand Up @@ -41,9 +41,7 @@ export class PickupRunner {
} else if (Message.isJsonAttachment(attachment.data)) {
return {
attachmentId: attachment.id,
data: "data" in attachment.data ?
JSON.stringify(attachment.data.data) :
JSON.stringify(attachment.data.json),
data: JSON.stringify(attachment.data.json),
};
}

Expand All @@ -56,7 +54,7 @@ export class PickupRunner {
return attachment !== null;
}

async run(): Promise<Array<{ attachmentId: string; message: Message }>> {
async run(): Promise<Array<{ attachmentId: string; message: Message; }>> {
if (this.message.type === "delivery") {
return Promise.all(
this.message.message.attachments
Expand All @@ -73,7 +71,7 @@ export class PickupRunner {
attachmentId: this.message.message.id,
message: this.message.message
}
]
];
}

return [];
Expand Down
36 changes: 5 additions & 31 deletions src/mercury/didcomm/Wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,29 +174,11 @@ export class DIDCommWrapper implements DIDCommProtocol {
private parseAttachmentDataToDomain(
data: AttachmentData
): Domain.AttachmentData {
if ("base64" in data) {
const parsed: Domain.AttachmentBase64 = {
base64: data.base64,
};

return parsed;
}

if ("json" in data) {
const parsed: Domain.AttachmentJsonData = {
data: data.json,
};

return parsed;
}

if ("links" in data) {
const parsed: Domain.AttachmentLinkData = {
hash: data.hash,
links: data.links,
};

return parsed;
if ("base64" in data
|| "json" in data
|| "links" in data
) {
return data;
}

throw new MercuryError.UnknownAttachmentDataError();
Expand Down Expand Up @@ -251,14 +233,6 @@ export class DIDCommWrapper implements DIDCommProtocol {
return parsed;
}

if ("data" in data) {
const parsed: JsonAttachmentData = {
json: JSON.parse(data.data),
};

return parsed;
}

if ("links" in data) {
const parsed: LinksAttachmentData = {
hash: data.hash,
Expand Down
2 changes: 1 addition & 1 deletion src/mercury/forward/ForwardMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class ForwardMessage {
makeMessage(): Message {
const body = JSON.stringify(this.body);
const attachment = new AttachmentDescriptor(
{ data: this.encryptedMessage },
{ json: this.encryptedMessage },
"application/json"
);
return new Message(body, this.id, ForwardMessage.type, this.from, this.to, [
Expand Down
47 changes: 47 additions & 0 deletions tests/domain/message.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, it, expect, test, beforeEach, afterEach } from 'vitest';
import { Message } from "../../src/domain";
import { UnsupportedAttachmentType } from '../../src/domain/models/errors/Agent';

describe("Message", () => {
describe("fromJson", () => {
Expand All @@ -10,5 +11,51 @@ describe("Message", () => {
expect(result).to.be.an.instanceOf(Message);
expect(result.extraHeaders).to.be.an("object");
});

test("attachments - doesnt match - not added", () => {
const dataJson = {
body: { prop: 1 },
piuri: "https://didcomm.org/issue-credential/3.0/offer-credential",
attachments: [{
data: {
notmatch: { test: 123 }
},
}],
};
const sut = () => Message.fromJson(dataJson);

expect(sut).toThrow(UnsupportedAttachmentType);
});

test("attachments - matches .json payload", () => {
const dataJson = {
body: { prop: 1 },
piuri: "https://didcomm.org/issue-credential/3.0/offer-credential",
attachments: [{
data: {
json: { test: 123 }
},
}],
};
const result = Message.fromJson(dataJson);

expect(result.attachments).toHaveLength(1);
});

// for backwards compatibility with Pluto stored messages
test("attachments - matches .data payload", () => {
const dataJson = {
body: { prop: 1 },
piuri: "https://didcomm.org/issue-credential/3.0/offer-credential",
attachments: [{
data: {
data: { test: 123 }
},
}],
};
const result = Message.fromJson(dataJson);

expect(result.attachments).toHaveLength(1);
});
});
});
Loading