Skip to content

Commit

Permalink
generate json proof script and data changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijay Shiyani committed Jun 6, 2024
1 parent 578991d commit 132d4ba
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 12 deletions.
6 changes: 4 additions & 2 deletions .data/haip.claims.example.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"given_name": "John",
"last_name": "Doe"
"credentialSubject": {
"given_name": "John",
"family_name": "Doe"
}
}
8 changes: 7 additions & 1 deletion .data/studentid.claims.example.json
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
{}
{
"id": "1234567890",
"studentID": "1234567890",
"givenName": "John",
"familyName": "Doe",
"dateOfBirth": "1963-07-07"
}
21 changes: 21 additions & 0 deletions scripts/generate-proof.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { getJwtVcJsonProof, getSdJwtVcJsonProof } from '../src/utils/openid/vci.proof-jwt.js';

async function main() {
const issuer = 'https://dvfmz2oh-dev.meeco.cloud';
const c_nonce = '98b27583-2609-49ed-b9bc-3cd9cdd29811';

const result = await getJwtVcJsonProof(issuer, c_nonce);
console.log(result);
}

async function main1() {
const issuer = 'https://dvfmz2oh-dev.meeco.cloud';
const c_nonce = '79493288-9221-4fc6-9c01-527995a3bb0d';

const result = await getSdJwtVcJsonProof(issuer, c_nonce);
console.log(result);
}

await main();

await main1();
15 changes: 7 additions & 8 deletions src/utils/openid/vci.proof-jwt.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { SignJWT, importJWK } from 'jose';
import { readFile } from 'node:fs/promises';
import { TokenSet } from 'openid-client';

import { SIGNING_ALG } from '../../types/openid.types.js';
import { createDidKey, signJWT } from '../signature.js';

export async function getJwtVcJsonProof(issuer: string, token: TokenSet) {
export async function getJwtVcJsonProof(issuer: string, c_nonce: unknown) {
const didKey = createDidKey();

const jwt = await signJWT({
Expand All @@ -16,31 +15,31 @@ export async function getJwtVcJsonProof(issuer: string, token: TokenSet) {
aud: issuer,
exp: Math.floor(Date.now() / 1000) + 60 * 2,
iss: didKey.identifier,
nonce: token.c_nonce,
nonce: c_nonce,
},
secretKey: didKey.secretKey,
});

return jwt;
}

export async function getSdJwtVcJsonProof(issuer: string, token: TokenSet) {
export async function getSdJwtVcJsonProof(issuer: string, c_nonce: unknown) {
const holder = await readFile('./config/holder.json').then((data) => JSON.parse(data.toString()));

const privateKey = await importJWK(holder.jwk);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { d: _d, ...publicKey } = holder.jwk;
const { d: _d, ...publicKey } = holder.jwk;

const jwt: string = await new SignJWT({
aud: issuer,
exp: Math.floor(Date.now() / 1000) + 60 * 2,
iss: holder.uri,
nonce: token.c_nonce,
nonce: c_nonce,
})
.setProtectedHeader({ alg: SIGNING_ALG.ES256, jwk: publicKey, typ: 'openid4vci-proof+jwt' })
.setIssuedAt()
.sign(privateKey);

return jwt;
}
}
8 changes: 7 additions & 1 deletion src/utils/openid/vci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ export async function claimCredentialOffer(credentialOfferURL: string) {
openidConfig,
});

console.log('token:', token);

ux.action.stop();
} else {
throw new Error('could not find a supported grant type');
Expand Down Expand Up @@ -180,7 +182,9 @@ export async function issueVC(issuer: string, endpoint: string, token: TokenSet,
* Get VC
*/

const jwt = isVcSdJwt(metadata) ? await getSdJwtVcJsonProof(issuer, token) : await getJwtVcJsonProof(issuer, token);
const jwt = isVcSdJwt(metadata)
? await getSdJwtVcJsonProof(issuer, token.c_nonce)
: await getJwtVcJsonProof(issuer, token.c_nonce);

const vcPayload = {
...metadata,
Expand All @@ -190,6 +194,8 @@ export async function issueVC(issuer: string, endpoint: string, token: TokenSet,
},
};

console.log('vc payload', vcPayload);

const result = await fetch(endpoint, {
body: JSON.stringify(vcPayload),
headers: {
Expand Down

0 comments on commit 132d4ba

Please sign in to comment.