forked from ubiquity-os/permit-generation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
961ebeb
commit f0a3ebe
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.