Skip to content

Commit

Permalink
forward client javascript component (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
chendiblessing authored Nov 21, 2024
2 parents 42fd841 + 477ec62 commit c8b5ba5
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 106 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.vscode
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)
Expand Down
9 changes: 6 additions & 3 deletions libs/message-handler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
"dev": "vite",
"test": "vitest --run",
"build": "tsc -b",
"format": "prettier --write .",
"format:check": "prettier --check .",
"preview": "vite preview"
},
"dependencies": {},
"devDependencies": {
"@babel/preset-typescript": "^7.25.9",
"@eslint/js": "^9.11.1",
"@jest/globals": "^29.7.0",
"@types/axios": "^0.14.4",
"@types/node": "^22.7.6",
"@types/react": "^18.3.10",
"@types/react-dom": "^18.3.0",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
"@types/uuid": "^10.0.0",
"@vitejs/plugin-react": "^4.3.2",
"@vitejs/plugin-react": "^4.3.3",
"eslint": "^9.11.1",
"eslint-plugin-react-hooks": "^5.1.0-rc.0",
"eslint-plugin-react-refresh": "^0.4.12",
Expand Down
101 changes: 101 additions & 0 deletions libs/message-handler/src/protocols/forward-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {
DIDDoc,
DIDResolver,
IMessage,
Message,
SecretsResolver,
} from 'didcomm';
import { v4 as uuidv4 } from 'uuid';
import axios from 'axios';
import {
PeerDIDResolver,
ExampleDIDResolver,
ExampleSecretsResolver,
} from 'did-resolver-lib';
//import { FROM } from '../did_doc/client';
import { ROUTING } from '../shared_data/message_types';
import { CLIENT_SECRETS } from '../secrets/client';
import { MEDIATOR_ENDPOINT } from '../shared_data/endpoints';
import { CONTENT_TYPE, FROM } from '../shared_data/constants';

export function buildMessage(to: string[], message: string): Message {
const imsg: IMessage = {
id: uuidv4(),
typ: 'application/didcomm-plain+json',
type: ROUTING,
body: {},
from: FROM,
to: to,
attachments: [
{
data: {
json: { data: message },
},
id: uuidv4(),
description: 'example',
media_type: 'application/didcomm-encrypted+json',
},
],
};
const msg = new Message(imsg);
return msg;
}
export async function forward_msg(to: string[], message: string) {
const msg = buildMessage(to, message);

const packed_msg = await pack_encrypt(msg, to);
await sendRequest(packed_msg as string);
}

export async function pack_encrypt(
msg: Message,
to: string[],
): Promise<string | null> {
const mediator_didoc = await new PeerDIDResolver().resolve(to[0]);

const did_resolver: DIDResolver = new ExampleDIDResolver([
mediator_didoc as DIDDoc,
]);
const secret_resolver: SecretsResolver = new ExampleSecretsResolver(
CLIENT_SECRETS,
);

try {
const result = await msg.pack_encrypted(
to[0],
FROM,
null,
did_resolver,
secret_resolver,
{
forward: true,
},
);
return result[0];
} catch (error) {
throw new Error(error as string);
}
}
export async function sendRequest(packed_msg: string) {
try {
const response = await axios.post(MEDIATOR_ENDPOINT, packed_msg, {
headers: {
'Content-type': CONTENT_TYPE,
},
});

if (response.status >= 200 && response.status < 300) {
if (response.data) {
return response.data;
} else {
return { message: 'Request was successful, no further data returned.' };
}
} else {
throw new Error(
`Unexpected response status: ${response.status}, Response data: ${JSON.stringify(response.data)}`,
);
}
} catch (error) {
throw new Error(error as string);
}
}
1 change: 1 addition & 0 deletions libs/message-handler/src/shared_data/endpoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const MEDIATOR_ENDPOINT = 'https://mediator-endpoint.com';
2 changes: 2 additions & 0 deletions libs/message-handler/src/shared_data/message_types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MEDIATE_REQUEST = 'mediate_request';
export const ROUTING = 'forward';
24 changes: 24 additions & 0 deletions libs/message-handler/src/tests/forward.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { describe, expect, test } from 'vitest';
import { buildMessage, pack_encrypt } from '../protocols/forward-client';

describe('Forward Client Tests', () => {
test('should forward a message successfully', async () => {
const to = [
'did:peer:2.Vz6Mkf6r1uMJwoRAbzkuyj2RwPusdZhWSPeEknnTcKv2C2EN7.Ez6LSgbP4b3y8HVWG6C73WF2zLbzjDAPXjc33P2VfnVVHE347.SeyJpZCI6IiNkaWRjb21tIiwicyI6eyJhIjpbImRpZGNvbW0vdjIiXSwiciI6W10sInVyaSI6Imh0dHA6Ly9hbGljZS1tZWRpYXRvci5jb20ifSwidCI6ImRtIn0',
];
const message = 'Hello is a test message to forward';
const msg = buildMessage(to, message);
const packmsg = pack_encrypt(msg, to);
expect(packmsg).not.toBeNull();
});
test('should handle wrong DID and throw an error', async () => {
const to = [
'did:peer:3.InvalidDIDExample.SeyJpZCI6IiNkaWRjb21tIiwicyI6eyJhIjpbImRpZGNvbW0vdjIiXSwiciI6W10sInVyaSI6Imh0dHA6Ly9hbGljZS1tZWRpYXRvci5jb20ifSwidCI6ImRtIn0',
];
const message = 'This should fail due to an invalid DID';

const msg = buildMessage(to, message);

await expect(pack_encrypt(msg, to)).rejects.toThrowError();
});
});
1 change: 1 addition & 0 deletions libs/message-handler/tsconfig.app.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"types": ["node"],
"module": "ESNext",
"skipLibCheck": true,

Expand Down
17 changes: 12 additions & 5 deletions libs/message-handler/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
{
"files": [],
"references": [
{ "path": "./tsconfig.app.json" },
{ "path": "./tsconfig.node.json" }
]
"compilerOptions": {
"typeRoots": ["./node_modules/@types"],
"target": "ES2020",
"module": "CommonJS",
"outDir": "./dist",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "Node",
"skipLibCheck": true
},
"include": ["src/**/*.ts", "src/tests/**/*.ts"],
"exclude": ["node_modules", "dist"]
}
1 change: 1 addition & 0 deletions libs/message-handler/tsconfig.node.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"compilerOptions": {
"target": "ES2022",
"lib": ["ES2023"],
"types": ["node"],
"module": "ESNext",
"skipLibCheck": true,

Expand Down
Loading

0 comments on commit c8b5ba5

Please sign in to comment.