Skip to content

Commit

Permalink
feat: Add sample tester script (#18)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
  • Loading branch information
lukasjhan authored Mar 14, 2024
1 parent 34395d7 commit 6b99ad3
Show file tree
Hide file tree
Showing 8 changed files with 511 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
.git
.gitignore
*.md
dist
dist
tester
2 changes: 1 addition & 1 deletion src/cases/verifies/sig_fail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const credential =
'eyJhbGciOiJFUzI1NiJ9.eyJfc2QiOlsiZ05GSGg4WkFFeEQ4UjN2d3BDYU50Z0hMc2NON0RyS3hxMVd0NkgzLUZwcyJdLCJfc2RfYWxnIjoic2hhLTI1NiJ9.ciWtYfxKlHFvYHpUPhIHsycWBlDZO_D5HaakynT9lXIG1Db2b_4WShb1GUqMKwGvd8qLeaz5ar8S2TbbQqKF3g~WyIzNjA5YzMzZDFmMzExMmU0IiwibmFtZSIsIkpvbG4iXQ~';
'eyJhbGciOiJFUzI1NiJ9.eyJfc2QiOlsiZ05GSGg4WkFFeEQ4UjN2d3BDYU50Z0hMc2NON0RyS3hxMVd0NkgzLUZwcyJdLCJfc2RfYWxnIjoic2hhLTI1NiJ9.ciWtYfxKlHFvYHpUPhIHsycWBlDZO_D5HaakynT9lXIG1Db2b_4WShb1GUqMKwGvd8qLeaz5ar8a2TbbQqKF3g~WyIzNjA5YzMzZDFmMzExMmU0IiwibmFtZSIsIkpvbG4iXQ~';

export const result = false;
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ app.post(`/tests/verify/:name`, async (c) => {
throw new HTTPException(404, { message: 'test not found' });
}

const sdjwt = await getSDJwt();
const { result } = verifyTestCases[name];

try {
Expand All @@ -181,7 +180,7 @@ app.get('/tests/issue/:name', (c) => {
const description = 'I will give you claim, you have to send me a token!';
const name = c.req.param('name') as keyof typeof issueTestCases;

if (!verifyedNames.includes(name)) {
if (!issuedNames.includes(name)) {
throw new HTTPException(404, { message: 'test not found' });
}

Expand Down
12 changes: 12 additions & 0 deletions tester/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Sample tester for SD-JWT Test API

This is a sample tester for the SD-JWT Test API. It is written in Typescript and uses the axios library to make HTTP requests to the API.
It uses Typescript SD-JWT library implementation to test. see (https://github.com/openwallet-foundation-labs/sd-jwt-js)

## How to use test

```bash
pnpm install

pnpm test
```
106 changes: 106 additions & 0 deletions tester/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import axios from 'axios';
import { SDJwtInstance, SdJwtPayload } from '@sd-jwt/core';
import { ES256, digest, generateSalt } from '@sd-jwt/crypto-nodejs';

async function getTestLists(): Promise<Array<{ method: string; path: string }>> {
const response = await axios.get('http://localhost:5600/test-lists');
return response.data.results.apiList;
}

async function getKeys() {
const response = await axios.get('http://localhost:5600/keys');
return response.data.results;
}

async function getTest(path: string) {
const response = await axios.get(`http://localhost:5600${path}`);
return response.data;
}

async function postTest(path: string, answer: any) {
const response = await axios.post(`http://localhost:5600${path}`, { answer });
return response.data.results;
}

async function run(sdjwt: SDJwtInstance<SdJwtPayload>, type: string, path: string) {
switch (type) {
case 'issue': {
const problem = await getTest(path);
const { claims, disclosureFrame } = problem.results;
const credential = await sdjwt.issue(claims, disclosureFrame);
const result = await postTest(path, credential);
console.log(`Test ${type}: [${path}] [${result}]`);
break;
}
case 'present': {
const problem = await getTest(path);
const { credential, presentationFrame } = problem.results;
const present = await sdjwt.present(credential, presentationFrame);
const result = await postTest(path, present);
console.log(`Test ${type}: [${path}] [${result}]`);
break;
}
case 'verify': {
const problem = await getTest(path);
const { credential } = problem.results;
const kb = !credential.endsWith('~');
try {
await sdjwt.verify(credential, undefined, kb);
const result = await postTest(path, true);
console.log(`Test ${type}: [${path}] [${result}]`);
} catch (e) {
const result = await postTest(path, false);
console.log(`Test ${type}: [${path}] [${result}]`);
}
break;
}
case 'decode': {
const problem = await getTest(path);
const { credential } = problem.results;
const claims = await sdjwt.getClaims(credential);
const result = await postTest(path, claims);
console.log(`Test ${type}: [${path}] [${result}]`);
break;
}
}
}

(async () => {
console.log('Testing Start');

const testList = await getTestLists();
const { publicKey, privateKey } = await getKeys();

const sdjwt = new SDJwtInstance({
hasher: digest,
hashAlg: 'sha-256',
saltGenerator: generateSalt,
signAlg: ES256.alg,
signer: await ES256.getSigner(privateKey),
verifier: await ES256.getVerifier(publicKey),
kbSignAlg: ES256.alg,
kbSigner: await ES256.getSigner(privateKey),
kbVerifier: await ES256.getVerifier(publicKey),
});

const tests = testList
.filter((test) => test.method === 'GET')
.map((test) => {
if (test.path.includes('issue')) {
return { path: test.path, type: 'issue' };
}
if (test.path.includes('verify')) {
return { path: test.path, type: 'verify' };
}
if (test.path.includes('decode')) {
return { path: test.path, type: 'decode' };
}
if (test.path.includes('present')) {
return { path: test.path, type: 'present' };
}
return { path: test.path, type: 'unknown' };
});
for (const test of tests) {
await run(sdjwt, test.type, test.path);
}
})();
20 changes: 20 additions & 0 deletions tester/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "test-project",
"version": "1.0.0",
"description": "This is the test project of sd-jwt test API",
"main": "index.js",
"scripts": {
"test": "ts-node index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@sd-jwt/core": "^0.6.0",
"@sd-jwt/crypto-nodejs": "^0.6.0",
"@sd-jwt/types": "^0.6.0",
"axios": "^1.6.7",
"ts-node": "^10.9.2",
"typescript": "^5.4.2"
}
}
Loading

0 comments on commit 6b99ad3

Please sign in to comment.