forked from openwallet-foundation/sd-jwt-js
-
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.
Add initial version of sd-jwt-js (openwallet-foundation#8)
Signed-off-by: Lukas.J.Han <[email protected]> Signed-off-by: Lukas <[email protected]>
- Loading branch information
Showing
55 changed files
with
5,623 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
dist | ||
|
||
.vscode |
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,5 @@ | ||
{ | ||
"trailingComma": "all", | ||
"tabWidth": 2, | ||
"singleQuote": true | ||
} |
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,5 @@ | ||
decoy: 1~5 random by default -> number of decoy is increased, size of sd-jwt is increased | ||
random number of hashing salt is better. decoy value: hash(hash(hash(salt))) | ||
salt default length is 128 | ||
|
||
kb-jwt and all needed disclosure checking logic in verifier is required |
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,32 @@ | ||
# SD JWT Examples | ||
|
||
This directory contains examples of how to use the SD JWT(sd-jwt-js) library. | ||
|
||
## How to run the examples | ||
|
||
```bash | ||
pnpm install | ||
``` | ||
|
||
## Run the example | ||
|
||
```bash | ||
pnpm run {example_file_name} | ||
|
||
# example | ||
pnpm run all | ||
``` | ||
|
||
### Example lists | ||
|
||
- all : Example of issue, present and verify the comprehensive data. | ||
- issue: Example of issue SD JWT | ||
- present: Example of present SD JWT | ||
- verify: Example of verify SD JWT | ||
- validate: Example of validate SD JWT | ||
- custom: Example of using custom hasher and salt generator for SD JWT | ||
- decoy: Example of adding decoy digest in SD JWT | ||
|
||
## More examples from tests | ||
|
||
You can find more examples from [tests](../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,74 @@ | ||
import sdjwt, { DisclosureFrame } from '@hopae/sd-jwt'; | ||
import Crypto from 'node:crypto'; | ||
|
||
export const createKeyPair = () => { | ||
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519'); | ||
return { privateKey, publicKey }; | ||
}; | ||
|
||
(async () => { | ||
const { privateKey, publicKey } = createKeyPair(); | ||
const claims = { | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
ssn: '123-45-6789', | ||
id: '1234', | ||
data: { | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
ssn: '123-45-6789', | ||
list: [{ r: '1' }, 'b', 'c'], | ||
}, | ||
data2: { | ||
hi: 'bye', | ||
}, | ||
}; | ||
const disclosureFrame: DisclosureFrame<typeof claims> = { | ||
_sd: ['firstname', 'id', 'data2'], | ||
data: { | ||
_sd: ['list'], | ||
_sd_decoy: 2, | ||
list: { | ||
_sd: [0, 2], | ||
_sd_decoy: 1, | ||
0: { | ||
_sd: ['r'], | ||
}, | ||
}, | ||
}, | ||
data2: { | ||
_sd: ['hi'], | ||
}, | ||
}; | ||
const encodedSdjwt = await sdjwt.issue(claims, privateKey, disclosureFrame); | ||
console.log('encodedJwt:', encodedSdjwt); | ||
const validated = await sdjwt.validate(encodedSdjwt, publicKey); | ||
console.log('validated:', validated); | ||
|
||
const decoded = sdjwt.decode(encodedSdjwt); | ||
console.log({ keys: await decoded.keys() }); | ||
const payloads = await decoded.getClaims(); | ||
const keys = await decoded.presentableKeys(); | ||
console.log({ | ||
payloads: JSON.stringify(payloads, null, 2), | ||
disclosures: JSON.stringify(decoded.disclosures, null, 2), | ||
claim: JSON.stringify(decoded.jwt?.payload, null, 2), | ||
keys, | ||
}); | ||
|
||
console.log( | ||
'================================================================', | ||
); | ||
|
||
const presentationFrame = ['firstname', 'id']; | ||
const presentedSDJwt = await sdjwt.present(encodedSdjwt, presentationFrame); | ||
console.log('presentedSDJwt:', presentedSDJwt); | ||
|
||
const requiredClaimKeys = ['firstname', 'id', 'data.ssn']; | ||
const verified = await sdjwt.verify( | ||
encodedSdjwt, | ||
publicKey, | ||
requiredClaimKeys, | ||
); | ||
console.log('verified:', verified); | ||
})(); |
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,75 @@ | ||
import sdjwt, { DisclosureFrame } from '@hopae/sd-jwt'; | ||
import Crypto from 'node:crypto'; | ||
|
||
export const salt = (length: number): string => { | ||
const saltBytes = Crypto.randomBytes(length); | ||
const salt = saltBytes.toString('hex'); | ||
return salt; | ||
}; | ||
|
||
export const digest = async ( | ||
data: string, | ||
algorithm: string = 'SHA-256', | ||
): Promise<string> => { | ||
const hash = Crypto.createHash(algorithm); | ||
hash.update(data); | ||
return hash.digest('hex'); | ||
}; | ||
|
||
export const createKeyPair = () => { | ||
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519'); | ||
return { privateKey, publicKey }; | ||
}; | ||
|
||
(async () => { | ||
const SDJwtInstance = sdjwt.create({ hasher: digest, saltGenerator: salt }); | ||
|
||
const { privateKey, publicKey } = createKeyPair(); | ||
const claims = { | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
ssn: '123-45-6789', | ||
id: '1234', | ||
}; | ||
const disclosureFrame: DisclosureFrame<typeof claims> = { | ||
_sd: ['firstname', 'id'], | ||
}; | ||
const encodedSdjwt = await SDJwtInstance.issue( | ||
claims, | ||
privateKey, | ||
disclosureFrame, | ||
); | ||
console.log('encodedJwt:', encodedSdjwt); | ||
const validated = await SDJwtInstance.validate(encodedSdjwt, publicKey); | ||
console.log('validated:', validated); | ||
|
||
const decoded = SDJwtInstance.decode(encodedSdjwt); | ||
console.log({ keys: await decoded.keys() }); | ||
const payloads = await decoded.getClaims(); | ||
const keys = await decoded.presentableKeys(); | ||
console.log({ | ||
payloads: JSON.stringify(payloads, null, 2), | ||
disclosures: JSON.stringify(decoded.disclosures, null, 2), | ||
claim: JSON.stringify(decoded.jwt?.payload, null, 2), | ||
keys, | ||
}); | ||
|
||
console.log( | ||
'================================================================', | ||
); | ||
|
||
const presentationFrame = ['firstname', 'id']; | ||
const presentedSDJwt = await SDJwtInstance.present( | ||
encodedSdjwt, | ||
presentationFrame, | ||
); | ||
console.log('presentedSDJwt:', presentedSDJwt); | ||
|
||
const requiredClaimKeys = ['firstname', 'id']; | ||
const verified = await SDJwtInstance.verify( | ||
encodedSdjwt, | ||
publicKey, | ||
requiredClaimKeys, | ||
); | ||
console.log('verified:', verified); | ||
})(); |
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,22 @@ | ||
import sdjwt, { DisclosureFrame } from '@hopae/sd-jwt'; | ||
import Crypto from 'node:crypto'; | ||
|
||
export const createKeyPair = () => { | ||
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519'); | ||
return { privateKey, publicKey }; | ||
}; | ||
|
||
(async () => { | ||
const { privateKey, publicKey } = createKeyPair(); | ||
const claims = { | ||
lastname: 'Doe', | ||
ssn: '123-45-6789', | ||
id: '1234', | ||
}; | ||
const disclosureFrame: DisclosureFrame<typeof claims> = { | ||
_sd: ['id'], | ||
_sd_decoy: 1, | ||
}; | ||
const encodedSdjwt = await sdjwt.issue(claims, privateKey, disclosureFrame); | ||
console.log('encodedSdjwt:', encodedSdjwt); | ||
})(); |
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,22 @@ | ||
import sdjwt, { DisclosureFrame } from '@hopae/sd-jwt'; | ||
import Crypto from 'node:crypto'; | ||
|
||
export const createKeyPair = () => { | ||
const { privateKey, publicKey } = Crypto.generateKeyPairSync('ed25519'); | ||
return { privateKey, publicKey }; | ||
}; | ||
|
||
(async () => { | ||
const { privateKey, publicKey } = createKeyPair(); | ||
const claims = { | ||
firstname: 'John', | ||
lastname: 'Doe', | ||
ssn: '123-45-6789', | ||
id: '1234', | ||
}; | ||
const disclosureFrame: DisclosureFrame<typeof claims> = { | ||
_sd: ['firstname', 'id'], | ||
}; | ||
const encodedSdjwt = await sdjwt.issue(claims, privateKey, disclosureFrame); | ||
console.log('encodedSdjwt:', encodedSdjwt); | ||
})(); |
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,27 @@ | ||
{ | ||
"name": "sdjwt-examples", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"prepare": "cd ../ && pnpm install && pnpm build", | ||
"all": "ts-node all.ts", | ||
"issue": "ts-node issue.ts", | ||
"present": "ts-node present.ts", | ||
"validate": "ts-node validate.ts", | ||
"verify": "ts-node verify.ts", | ||
"custom": "ts-node custom.ts", | ||
"decoy": "ts-node decoy.ts" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@types/node": "^20.10.4", | ||
"ts-node": "^10.9.2", | ||
"typescript": "^5.3.3" | ||
}, | ||
"dependencies": { | ||
"@hopae/sd-jwt": "link:.." | ||
} | ||
} |
Oops, something went wrong.