Files
inji-wallet/shared/openId4VCI/Utils.ts
KiruthikaJeyashankar 55c666b121 feat: download credentials from Esignet using openId4VCI (#851)
* feat(INJI-245): dowload and view card via issuers

Co-authored-by: Harsh Vardhan <harsh59v@gmail.com>

* fix(INJI-245): remove vc from wallet

Co-authored-by: Harsh Vardhan <harsh59v@gmail.com>

* feat(INJI-245): pin card downloaded via eSignet

* refactor(INJI-245): remove debug logs

* refactor(INJI-245): rename vcItem related component to ExistingVcItem

* refactor(INJI-245): add lock file modifications

* refactor(INJI-245): add styles in purple theme for issuer related components

* refactor(INJI-245): update VID for wallet binding usecase and issuer logo display in vc

* refactor(INJI-245): remove duplicate loader component

* refactor(INJI-245): remove unused props in vc details container

---------

Co-authored-by: Harsh Vardhan <harsh59v@gmail.com>
Co-authored-by: Vijay <94220135+vijay151096@users.noreply.github.com>
2023-09-22 17:22:59 +05:30

87 lines
2.3 KiB
TypeScript

import {ENABLE_OPENID_FOR_VC} from 'react-native-dotenv';
import {createSignature, encodeB64} from '../cryptoutil/cryptoUtil';
import jwtDecode from 'jwt-decode';
import jose from 'node-jose';
import {VCMetadata} from '../VCMetadata';
export const OpenId4VCIProtocol = 'OpenId4VCI';
export const isVCFromOpenId4VCI = (vcMetadata: VCMetadata) => {
return vcMetadata.isFromOpenId4VCI();
};
export const isOpenId4VCIEnabled = () => {
return ENABLE_OPENID_FOR_VC === 'true';
};
export const getIdentifier = (context, credential) => {
const credId = credential.credential.id.split('/');
return (
context.selectedIssuer.id +
':' +
context.selectedIssuer.protocol +
':' +
credId[credId.length - 1]
);
};
export const getBody = async context => {
const proofJWT = await getJWT(context);
return {
format: 'ldp_vc',
credential_definition: {
'@context': ['https://www.w3.org/2018/credentials/v1'],
type: ['VerifiableCredential', 'MOSIPVerifiableCredential'],
},
proof: {
proof_type: 'jwt',
jwt: proofJWT,
},
};
};
export const getJWK = async publicKey => {
try {
const publicKeyJWKString = await jose.JWK.asKey(publicKey, 'pem');
const publicKeyJWK = publicKeyJWKString.toJSON();
return {
...publicKeyJWK,
alg: 'RS256',
use: 'sig',
};
} catch (e) {
console.log(
'Exception occured while constructing JWK from PEM : ' +
publicKey +
' Exception is ',
e,
);
}
};
export const getJWT = async context => {
try {
const header64 = encodeB64(
JSON.stringify({
alg: 'RS256',
jwk: await getJWK(context.publicKey),
typ: 'openid4vci-proof+jwt',
}),
);
const decodedToken = jwtDecode(context.tokenResponse.accessToken);
const payload64 = encodeB64(
JSON.stringify({
iss: context.selectedIssuer.clientId,
nonce: decodedToken.c_nonce,
aud: 'https://esignet.dev1.mosip.net/v1/esignet',
iat: Math.floor(new Date().getTime() / 1000),
exp: Math.floor(new Date().getTime() / 1000) + 18000,
}),
);
const preHash = header64 + '.' + payload64;
const signature64 = await createSignature(context.privateKey, preHash, '');
return header64 + '.' + payload64 + '.' + signature64;
} catch (e) {
console.log(e);
throw e;
}
};