Skip to content

Commit

Permalink
chore: start #ubiquity-os#71
Browse files Browse the repository at this point in the history
  • Loading branch information
kingsley-einstein committed Nov 3, 2024
1 parent 961ebeb commit f0a3ebe
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/helpers/signature.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
Empty file added src/worker.ts
Empty file.

0 comments on commit f0a3ebe

Please sign in to comment.