-
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.
feat: Add sample tester script (#18)
Signed-off-by: Lukas.J.Han <lukas.j.han@gmail.com>
- Loading branch information
Showing
8 changed files
with
511 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ node_modules | |
.git | ||
.gitignore | ||
*.md | ||
dist | ||
dist | ||
tester |
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,4 +1,4 @@ | ||
export const credential = | ||
'eyJhbGciOiJFUzI1NiJ9.eyJfc2QiOlsiZ05GSGg4WkFFeEQ4UjN2d3BDYU50Z0hMc2NON0RyS3hxMVd0NkgzLUZwcyJdLCJfc2RfYWxnIjoic2hhLTI1NiJ9.ciWtYfxKlHFvYHpUPhIHsycWBlDZO_D5HaakynT9lXIG1Db2b_4WShb1GUqMKwGvd8qLeaz5ar8S2TbbQqKF3g~WyIzNjA5YzMzZDFmMzExMmU0IiwibmFtZSIsIkpvbG4iXQ~'; | ||
'eyJhbGciOiJFUzI1NiJ9.eyJfc2QiOlsiZ05GSGg4WkFFeEQ4UjN2d3BDYU50Z0hMc2NON0RyS3hxMVd0NkgzLUZwcyJdLCJfc2RfYWxnIjoic2hhLTI1NiJ9.ciWtYfxKlHFvYHpUPhIHsycWBlDZO_D5HaakynT9lXIG1Db2b_4WShb1GUqMKwGvd8qLeaz5ar8a2TbbQqKF3g~WyIzNjA5YzMzZDFmMzExMmU0IiwibmFtZSIsIkpvbG4iXQ~'; | ||
|
||
export const result = false; |
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,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 | ||
``` |
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,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); | ||
} | ||
})(); |
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,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" | ||
} | ||
} |
Oops, something went wrong.