From f0a3ebe09ad2496567fdf7bcf0073785f75bc535 Mon Sep 17 00:00:00 2001 From: Kingsley Victor Date: Sun, 3 Nov 2024 23:42:25 +0100 Subject: [PATCH] chore: start #https://github.com/ubiquity-os/permit-generation/issues/71 --- src/helpers/signature.ts | 43 ++++++++++++++++++++++++++++++++++++++++ src/worker.ts | 0 2 files changed, 43 insertions(+) create mode 100644 src/helpers/signature.ts create mode 100644 src/worker.ts diff --git a/src/helpers/signature.ts b/src/helpers/signature.ts new file mode 100644 index 0000000..c4022fc --- /dev/null +++ b/src/helpers/signature.ts @@ -0,0 +1,43 @@ +interface Inputs { + stateId: string; + eventName: string; + eventPayload: unknown; + authToken: string; + settings: unknown; + ref: unknown; +} + +export async function verifySignature(publicKeyPem: string, inputs: Inputs, signature: string) { + try { + const inputsOrdered = { + stateId: inputs.stateId, + eventName: inputs.eventName, + eventPayload: inputs.eventPayload, + settings: inputs.settings, + authToken: inputs.authToken, + ref: inputs.ref, + }; + + const pemContents = publicKeyPem.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "").trim(); + const binaryDer = Uint8Array.from(atob(pemContents), (c) => c.charCodeAt(0)); + + const publicKey = await crypto.subtle.importKey( + "spki", + binaryDer, + { + name: "RSASSA-PKCS1-v1_5", + hash: "SHA-256", + }, + true, + ["verify"] + ); + + const signatureArray = Uint8Array.from(atob(signature), (c) => c.charCodeAt(0)); + const dataArray = new TextEncoder().encode(JSON.stringify(inputsOrdered)); + + return await crypto.subtle.verify("RSASSA-PKCS1-v1_5", publicKey, signatureArray, dataArray); + } catch (error) { + console.error(error); + return false; + } +} diff --git a/src/worker.ts b/src/worker.ts new file mode 100644 index 0000000..e69de29