Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/CWALL-174_impl-draft13_w…
Browse files Browse the repository at this point in the history
…ith-199' into feature/CWALL-174_impl-draft13_with-199
  • Loading branch information
sksadjad committed May 27, 2024
2 parents 690b02b + db05e34 commit 1d87208
Show file tree
Hide file tree
Showing 2 changed files with 7,674 additions and 10,475 deletions.
34 changes: 8 additions & 26 deletions packages/common/lib/functions/Encoding.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { BAD_PARAMS, CredentialOfferV1_0_11, DecodeURIAsJsonOpts, EncodeJsonAsURIOpts, JsonURIMode, OpenId4VCIVersion, SearchValue } from '../types';
import {
BAD_PARAMS,
DecodeURIAsJsonOpts,
EncodeJsonAsURIOpts,
JsonURIMode,
OpenId4VCIVersion,
SearchValue
} from '../types'

/**
* @type {(json: {[s:string]: never} | ArrayLike<never> | string | object, opts?: EncodeJsonAsURIOpts)} encodes a Json object into a URI
Expand Down Expand Up @@ -93,35 +100,10 @@ export function convertURIToJsonObject(uri: string, opts?: DecodeURIAsJsonOpts):
throw new Error(BAD_PARAMS);
}

if (opts?.arrayTypeProperties && opts.arrayTypeProperties.includes('credential_offer_uri')) {
return encodedCredentialOfferUri2Json({ uri });
}

if (opts?.arrayTypeProperties && opts.arrayTypeProperties.includes('credential_offer')) {
return encodedCredentialOffer2Json({ uri });
}

const uriComponents = getURIComponentsAsArray(uri, opts?.arrayTypeProperties);
return decodeJsonProperties(uriComponents);
}

function encodedCredentialOfferUri2Json(args: { uri: string }): Pick<CredentialOfferV1_0_11, 'credential_offer_uri'> {
const { uri } = args;
const value = uri.includes('?') ? uri.split('?')[1].split('=') : uri;
return {
credential_offer_uri: value[1],
};
}

function encodedCredentialOffer2Json(args: { uri: string }): Pick<CredentialOfferV1_0_11, 'credential_offer'> {
const { uri } = args;
const decodedUri = decodeURIComponent(uri);
const value = decodedUri.includes('?') ? decodedUri.split('?')[1].split('=') : decodedUri;
return {
credential_offer: JSON.parse(value[1]),
};
}

function decodeJsonProperties(parts: string[] | string[][]): unknown {
const json: { [s: string]: unknown } | ArrayLike<unknown> = {};
for (const key in parts) {
Expand Down
Loading

0 comments on commit 1d87208

Please sign in to comment.