From 85bb6fa6e1c04021e8308dd26e3dd1e95fc4a3b6 Mon Sep 17 00:00:00 2001 From: Lukas Date: Mon, 18 Dec 2023 15:15:14 +0900 Subject: [PATCH 1/2] [#26]Provide custom header in issue API Signed-off-by: Lukas --- src/index.ts | 7 ++++++- src/type.ts | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8abf0e82..558c6100 100644 --- a/src/index.ts +++ b/src/index.ts @@ -64,6 +64,7 @@ export class SDJwtInstance { privateKey: Uint8Array | KeyLike, disclosureFrame?: DisclosureFrame, options?: { + header?: object; sign_alg?: string; hash_alg?: string; kb?: { @@ -85,7 +86,11 @@ export class SDJwtInstance { this.userConfig.saltGenerator ?? defaultConfig.saltGenerator, ); const alg = options?.sign_alg ?? SDJwtInstance.DEFAULT_ALG; - const header = this.userConfig.omitTyp ? { alg } : { alg, typ: SD_JWT_TYP }; + const OptionHeader = options?.header ?? {}; + const CustomHeader = this.userConfig.omitTyp + ? OptionHeader + : { typ: SD_JWT_TYP, ...OptionHeader }; + const header = { ...CustomHeader, alg }; const jwt = new Jwt({ header, payload: { diff --git a/src/type.ts b/src/type.ts index 6f4af087..02f8cead 100644 --- a/src/type.ts +++ b/src/type.ts @@ -3,7 +3,7 @@ import { Jwt } from './jwt'; export const SD_SEPARATOR = '~'; export const SD_LIST_KEY = '...'; export const SD_DIGEST = '_sd'; -export const SD_JWT_TYP = 'sd+jwt'; +export const SD_JWT_TYP = 'sd-jwt'; export const SD_DECOY = '_sd_decoy'; export const KB_JWT_TYP = 'kb+jwt'; From 7661e228393848245a104d0c4cc9bf8d3a63f390 Mon Sep 17 00:00:00 2001 From: Lukas Date: Mon, 18 Dec 2023 15:32:04 +0900 Subject: [PATCH 2/2] Add custom header example Signed-off-by: Lukas --- examples/custom_header.ts | 27 +++++++++++++++++++++++++++ examples/package.json | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 examples/custom_header.ts diff --git a/examples/custom_header.ts b/examples/custom_header.ts new file mode 100644 index 00000000..753588d8 --- /dev/null +++ b/examples/custom_header.ts @@ -0,0 +1,27 @@ +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 = { + _sd: ['firstname', 'id'], + }; + + const encodedSdjwt = await sdjwt.issue(claims, privateKey, disclosureFrame, { + header: { typ: 'vc+sd-jwt', custom: 'data' }, + }); + console.log('encodedSdjwt:', encodedSdjwt); + const sdjwttoken = sdjwt.decode(encodedSdjwt); + console.log(sdjwttoken); +})(); diff --git a/examples/package.json b/examples/package.json index 6b0ba61b..2feef1f9 100644 --- a/examples/package.json +++ b/examples/package.json @@ -11,7 +11,8 @@ "validate": "ts-node validate.ts", "verify": "ts-node verify.ts", "custom": "ts-node custom.ts", - "decoy": "ts-node decoy.ts" + "decoy": "ts-node decoy.ts", + "custom_header": "ts-node custom_header.ts" }, "keywords": [], "author": "",