"use strict"; async function postJSON(url, data) { const response = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }); if (!response.ok) { if (response.statusText) { throw new Error(response.statusText); } else { throw new Error("Something went wrong"); } } return response.json(); } async function getJSON(url) { const response = await fetch(url); if (!response.ok) { if (response.statusText) { throw new Error(response.statusText); } else { throw new Error("Something went wrong"); } } return response.json(); } function verifySig(pubkey, signature, data) { return window.crypto.subtle.verify( { name: "ECDSA", hash: { name: "SHA-256" }, }, pubkey, signature, data, ); } function signData(privkey, data) { return window.crypto.subtle.sign( { name: "ECDSA", hash: { name: "SHA-256" }, }, privkey, data, ); } function ab2str(buf) { let binary = ''; const bytes = new Uint8Array(buf); for (let i = 0, len = bytes.byteLength; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return binary; } // PEM key import code from: // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey#pkcs_8_import function str2ab(str) { const buf = new ArrayBuffer(str.length); const bufView = new Uint8Array(buf); for (let i = 0, len = str.length; i < len; i++) { bufView[i] = str.charCodeAt(i); } return buf; } function importPrivateKey(pem) { // fetch the part of the PEM string between header and footer const pemHeader = "-----BEGIN PRIVATE KEY-----"; const pemFooter = "-----END PRIVATE KEY-----"; if (pem.length < pemHeader.length + pemFooter.length + 90 || pem.indexOf(pemHeader) < 0 || pem.indexOf(pemFooter) < 0) { throw new Error("Invalid PEM key format"); } const pemContents = pem.substring( pem.indexOf(pemHeader) + pemHeader.length, pem.indexOf(pemFooter), ); // base64 decode the string to get the binary data const binaryDerString = window.atob(pemContents); // convert from a binary string to an ArrayBuffer const binaryDer = str2ab(binaryDerString); return window.crypto.subtle.importKey( "pkcs8", binaryDer, { name: "ECDSA", namedCurve: "P-256", }, false, ["sign"], ); } function adminPublicKey() { const pemContents = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEG4nMe1/gySwCCGQxHL4nlwmpcOYllW1PDH0nQhoNYhGHK/UBtfgUKG9u/XjcWfEYFY2cvZWGrPyHzhzxVnV8bA=="; // base64 decode the string to get the binary data const binaryDerString = window.atob(pemContents); // convert from a binary string to an ArrayBuffer const binaryDer = str2ab(binaryDerString); return window.crypto.subtle.importKey( "spki", binaryDer, { name: "ECDSA", namedCurve: "P-256", }, false, ["verify"], ); }