-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
forward client javascript component (#3)
- Loading branch information
Showing
11 changed files
with
299 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MEDIATOR_ENDPOINT = 'https://mediator-endpoint.com'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const MEDIATE_REQUEST = 'mediate_request'; | ||
export const ROUTING = 'forward'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.