Skip to content

Commit

Permalink
[openwallet-foundation#26]Provide custom header in issue API (openwal…
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjhan authored Dec 18, 2023
1 parent 688db38 commit 8266bd3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
27 changes: 27 additions & 0 deletions examples/custom_header.ts
Original file line number Diff line number Diff line change
@@ -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<typeof claims> = {
_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);
})();
3 changes: 2 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export class SDJwtInstance {
privateKey: Uint8Array | KeyLike,
disclosureFrame?: DisclosureFrame<Payload>,
options?: {
header?: object;
sign_alg?: string;
hash_alg?: string;
kb?: {
Expand All @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down

0 comments on commit 8266bd3

Please sign in to comment.