From 83030264e6154c09fc0d951f2302dca82f974012 Mon Sep 17 00:00:00 2001 From: phoebus-84 <83974413+phoebus-84@users.noreply.github.com> Date: Fri, 26 Jul 2024 08:30:07 +0200 Subject: [PATCH] feat: exec custom code on verification (#66) * feat: exec custom code first Implementation * fix: parse verification error --- src/lib/fakeCredentials.js | 19 ------------- src/lib/preferences/sidRu.ts | 6 ++-- src/lib/slangroom/verificationFlows.ts | 3 +- .../(protected)/[id]/verify/+page.svelte | 7 ++++- .../(protected)/[id]/verify/_lib/tools.ts | 28 +++++++++++++++---- 5 files changed, 35 insertions(+), 28 deletions(-) delete mode 100644 src/lib/fakeCredentials.js diff --git a/src/lib/fakeCredentials.js b/src/lib/fakeCredentials.js deleted file mode 100644 index 5ca6b97..0000000 --- a/src/lib/fakeCredentials.js +++ /dev/null @@ -1,19 +0,0 @@ -const fakeCredentials = [ - { - "title": "Over 18", - "issuedBy": "ItGov", - "link": "0" - }, - { - "title": "Degree in Biology", - "issuedBy": "Venice University", - "link": "1" - }, - { - "title": "Driving license b", - "issuedBy": "motorization", - "link": "2" - } - ] - -export default fakeCredentials \ No newline at end of file diff --git a/src/lib/preferences/sidRu.ts b/src/lib/preferences/sidRu.ts index 5353a55..d093cf4 100644 --- a/src/lib/preferences/sidRu.ts +++ b/src/lib/preferences/sidRu.ts @@ -5,15 +5,17 @@ export type RuAndSid = { sid: string; ru: string; at: number; + code: string; + data:string; }; export const RU_AND_SID_KEY = 'runAndSid'; -export const saveRuAndSid = async (sid: string, ru: string) => { +export const saveRuAndSid = async (sid: string, ru: string, code:string, data:string) => { const at = dayjs().unix(); const ruandSid = await getRuAndSids(); - const r = { sid, ru, at }; + const r = { sid, ru, at, code, data }; if (ruandSid) { const newRuAndSids = [...ruandSid, r] await setStructuredPreferences(RU_AND_SID_KEY, newRuAndSids); diff --git a/src/lib/slangroom/verificationFlows.ts b/src/lib/slangroom/verificationFlows.ts index 824809e..127aeca 100644 --- a/src/lib/slangroom/verificationFlows.ts +++ b/src/lib/slangroom/verificationFlows.ts @@ -87,11 +87,12 @@ export const getVerificationFlow = async (id: string): Promise pb_address: backendUri, show_parameters: { collection: 'verification_flows', - expand: 'relying_party', + expand: 'relying_party, template', id } }; const res = await slangroom.execute(getPbRecord, { data }); + console.log(res.result?.output); //@ts-expect-error output needs to be typed return res.result?.output; } catch (e: unknown) { diff --git a/src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte b/src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte index 547e3d6..e28b9a0 100644 --- a/src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte +++ b/src/routes/[[lang]]/(protected)/[id]/verify/+page.svelte @@ -64,7 +64,12 @@ const result = res.result; qr = result.qrcode; id = result.sid as string; - await saveRuAndSid(result.sid as string, result.ru as string); + await saveRuAndSid( + result.sid as string, + result.ru as string, + verificationFlow.expand.template.zencode_script, + verificationFlow.expand.template.zencode_data + ); generationDate = dayjs(); return qr; }; diff --git a/src/routes/[[lang]]/(protected)/[id]/verify/_lib/tools.ts b/src/routes/[[lang]]/(protected)/[id]/verify/_lib/tools.ts index 5d47db8..52f9d65 100644 --- a/src/routes/[[lang]]/(protected)/[id]/verify/_lib/tools.ts +++ b/src/routes/[[lang]]/(protected)/[id]/verify/_lib/tools.ts @@ -20,6 +20,20 @@ export type JwsToIdResponse = { message?: string; }; +const parseVerificationError = (e: Error):string => { + try { + const message = JSON.parse(e.message); + const trace = message.filter((s)=>s.startsWith("J64 TRACE:"))[0]; + const errorBase64 = trace.split("J64 TRACE: ")[1]; + const errorArray = JSON.parse(atob(errorBase64)); + const errors = errorArray.filter((s)=>s.startsWith("[!]")); + return errors.join("\n"); + } catch { + return e.message + } + +} + export const jwsToId = async (jws: string): Promise => { let id = ''; try { @@ -32,18 +46,22 @@ export const jwsToId = async (jws: string): Promise => { const ruAndSid = await getRuAndSid(id); if (!ruAndSid) throw new Error(`Could not find ru for id ${id}`); - const { ru } = ruAndSid; + const { ru, code, data:keys } = ruAndSid; const dataVerify = { ...data, claims_url: ru }; const res = await slangroom.execute(verify, { data: dataVerify, keys: JSON.parse(verifyKeys) }); - log(JSON.stringify(res)); const result = res.result.result as jwsToIdResult; + const inputToCustomCode = res.result.input_to_custom_code; + // Execute custom code + const customCodeResult = await slangroom.execute(code, { data: inputToCustomCode, keys: JSON.parse(keys) }); + console.log(customCodeResult); return { result, id }; - } catch (e) { - log(JSON.stringify(e)); - return { result: jwsToIdFailure, id, message: JSON.stringify(e) }; + } catch (e:unknown) { + const message = parseVerificationError(e as Error); + log(message); + return { result: jwsToIdFailure, id, message }; } };