Files
inji-wallet/shared/Utils.ts
abhip2565 a917a5d994 [INJIMOB-3177] add presentation during issuance support. (#2189)
* [INJIMOB-3177] add: vci-client library version from local

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: callbacks for PDI flow

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: VCIClientBridge sync fucntions for PDI flow

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] refactor: use requestCredentialByOfferV2 pointing to new api

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: in case of PDI - give request to ovp machine from issuers machine

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: sending back selected credentials for PDI from wallet to vci-client

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: update timeout for PDI flow VP

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: send signed VP token for PDI

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: modal to inform user of PDI flow during download

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: show loader during presenting credentials on PDI

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] fix: VP token not sent during PDI

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] fix: share screen fail on OVP service not found

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] refactor: redirect to web flow - implicit auth

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: PDI flow for iOS

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] refactor: register RCT methods for selected credentials and signed VP return

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] add: basic UI for processing screen

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] add: progress screen to show text as per auth process

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: modify VCI client bridge as per library method names

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] refactor kotlin bridge for vci client

Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

* [INJIMOB-3590] fix: authorization code key name

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3598] refactor: add presentation during issuance for trusted flow - iOS

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] add: redirect to home with timer on success download

Affected flow:
- Only on presentation during issuance this is applied

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3598] refactor: fix failing tests

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3598] test: add unit test for useTimer

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: update processing modal for trusted flow download

Affected flow:
- Only on presentation during issuance this is applied

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: ignore VP share consent for VP auth flow

Affected flow:
- Only on presentation during issuance this is applied

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: show select card header on selecting VCs screen

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] refactor: show loader skeleton while fetching matching credentials in sendVP screen

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: add testIDs to ProcessingModal component

Other changes
- move ProcessingModal styles to Theme file

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] fix: OVP share failure due to accessible null property

Other changes
- add telemetry event for the presentation auth in issuers machine

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] handle negative scenarios for pdi flow

Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

* [INJIMOB-3177] addd: update VC download loader

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: expose abortPresentationFlowFromJS native method to JS layer

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: update share with selfie consent text

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] fix no matching vcs scenario for PDI flow

Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

* [INJIMOB-3177] update locales

Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

* [INJIMOB-3690] refactor: udpate redirectToWeb method as per updated clalback type

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: extract conversion to OVP exception method

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: send OpenID4VPException for abortPresentationFlowFromJS

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: send error to verifier for non auth flows

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] test: add tests for the IssuersModel and IssuersSelectors presentation auth

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] test: add tests for the openIDModel and OpenIDSelectors

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] add: ignore log activity for authorization flow

Applicable for in case of presentation during issuance, activity logs from OVP machine is not added

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] refactor machines

Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

* [INJIMOB-3690] fix: error actions not working

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: modify selector name as per convention select*

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] fix: import issue in IssuersScreen

Co-authored-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: clear interval before starting new interval in useTimer

Co-authored-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove unused react hooks package

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] fix imports

Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

* [INJIMOB-3690] refcator: extract unsigned vp token setter action

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: update presentation during issuance to send signature suite

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove context variable in openID4VPMachine

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: modify syntax

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove logging sensitive info

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: rename folder as per convention

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove by passing verify credential for Presentation during issuance VC download

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove logging sensitive info

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: update doc string

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove unused file

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove debug logs

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] update java bridge vci client and fix verification error screen

Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

* [INJIMOB-3690] refactor: update OVP machine ID for presentation during issuance

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] add: update reject button text in send VP screen for presentation during issuance

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3177] update locales

Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>

* [INJIMOB-3690] refactor: remove unused states and functions

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove unused functions

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove unused locales

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] fix: update failing tests

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: remove state transition on VP_CONSENT_REJECT/SHOW_ERROR

Reason:
- VP_CONSENT_REJECT is sent to vci client library which communicates the error to issuer
- The issuer then responds with an error accordingly or so, this is being listened for any error transition

Co-authored-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>
Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] refactor: ignore multiDexEnabled setting in build.gradle

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] fix: handle no matching VCs when there are no VCs in wallet

Co-authored-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>
Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] chore: update vci-client and openeid4vp lib version - iOS

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

* [INJIMOB-3690] chore: update vci-client and openeid4vp lib version - android

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>

---------

Signed-off-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>
Signed-off-by: abhip2565 <paul.apaul.abhishek.AP@gmail.com>
Co-authored-by: KiruthikaJeyashankar <kiruthikavjshankar@gmail.com>
2026-01-14 18:33:06 +05:30

154 lines
4.2 KiB
TypeScript

import {sha256} from '@noble/hashes/sha256';
import {VCMetadata} from './VCMetadata';
import {CACHE_TTL, NETWORK_REQUEST_FAILED} from './constants';
import {groupBy} from './javascript';
import {Issuers} from './openId4VCI/Utils';
import {v4 as uuid} from 'uuid';
import {utf8ToBytes} from '@noble/hashes/utils';
import {Buffer} from 'buffer';
import base64url from 'base64url';
import jsonld from 'jsonld';
export const getVCsOrderedByPinStatus = (vcMetadatas: VCMetadata[]) => {
const [pinned, unpinned] = groupBy(
vcMetadatas,
(vcMetadata: VCMetadata) => vcMetadata.isPinned,
);
return pinned.concat(unpinned);
};
export enum VCShareFlowType {
SIMPLE_SHARE = 'simple share',
MINI_VIEW_SHARE = 'mini view share',
MINI_VIEW_SHARE_WITH_SELFIE = 'mini view share with selfie',
MINI_VIEW_QR_LOGIN = 'mini view qr login',
OPENID4VP = 'OpenID4VP',
OPENID4VP_AUTHORIZATION = 'OpenID4VP authorization',
MINI_VIEW_SHARE_OPENID4VP = 'OpenID4VP share from mini view',
MINI_VIEW_SHARE_WITH_SELFIE_OPENID4VP = 'OpenID4VP share with selfie from mini view',
}
export enum VCItemContainerFlowType {
QR_LOGIN = 'qr login',
VC_SHARE = 'vc share',
VP_SHARE = 'vp share',
}
export enum CameraPosition {
FRONT = 'front',
BACK = 'back',
}
export interface CommunicationDetails {
phoneNumber: string;
emailId: string;
}
export const isMosipVC = (issuer: string) => {
return issuer === Issuers.Mosip || issuer === Issuers.MosipOtp;
};
export const parseJSON = (input: any) => {
let result = null;
try {
result = JSON.parse(input);
} catch (e) {
console.warn('Error occurred while parsing JSON ', e);
result = JSON.parse(JSON.stringify(input));
}
return result;
};
export const isNetworkError = (error: string) => {
return error.includes(NETWORK_REQUEST_FAILED);
};
export class UUID {
public static generate(): string {
return uuid();
}
}
export const formatTextWithGivenLimit = (value: string, limit = 15) => {
if (value.length > limit) {
return value.substring(0, limit) + '...';
}
return value;
};
export enum DEEPLINK_FLOWS {
QR_LOGIN = 'qrLoginFlow',
OVP = 'ovpFlow',
}
export function base64ToByteArray(base64String) {
try {
let cleanBase64 = base64String.trim();
cleanBase64 = cleanBase64.replace(/-/g, '+').replace(/_/g, '/');
while (cleanBase64.length % 4) {
cleanBase64 += '=';
}
const binaryString = atob(cleanBase64);
const byteArray = new Uint8Array(binaryString.length);
for (let i = 0; i < binaryString.length; i++) {
byteArray[i] = binaryString.charCodeAt(i);
}
return byteArray;
} catch (error) {
throw new Error('Invalid Base64 string: ' + error.message);
}
}
export async function canonicalize(unsignedVp: any) {
try {
const jsonldProof = {...unsignedVp['proof']};
jsonldProof['@context'] = unsignedVp['@context'];
const jsonldObjectClone = {...unsignedVp};
if ('proof' in jsonldObjectClone) {
delete jsonldObjectClone.proof;
}
const expandedJsonldObject = await jsonld.expand(jsonldObjectClone);
const normalizedJsonldObject = await jsonld.canonize(expandedJsonldObject, {
algorithm: 'URDNA2015',
});
const expandedJsonldProof = await jsonld.expand(jsonldProof);
const normalizedJsonldProof = await jsonld.canonize(expandedJsonldProof, {
algorithm: 'URDNA2015',
});
const canonicalizationResult = Buffer.alloc(64);
Buffer.concat([
//noble sha256 deprecated need to use alternative library
sha256(utf8ToBytes(normalizedJsonldProof)),
sha256(utf8ToBytes(normalizedJsonldObject)),
]).copy(canonicalizationResult, 0);
return base64url(canonicalizationResult);
} catch (err) {
console.error('Canonization failed:', err);
}
}
export const createCacheObject = (response: any) => {
const currentTime = Date.now();
return {
response,
cachedTime: currentTime,
};
};
export const isCacheExpired = (timestamp: number) => {
return Date.now() - timestamp >= CACHE_TTL;
};
export function getVerifierKey(verifier: string): string {
return `trusted_verifier_${verifier}`;
}
export const enum VerificationStatus {
VALID = 'VALID',
REVOKED = 'REVOKED',
PENDING = 'PENDING',
EXPIRED = 'EXPIRED',
}