Skip to content

Commit

Permalink
add batch stamping logic
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Dec 8, 2024
1 parent 633fbca commit be74338
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions auth/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,16 @@ <h2>Message log</h2>
TKHQ.sendMessageUp("ERROR", e.toString());
}
}
if (event.data && event.data["type"] == "BATCH_STAMP_REQUEST") {
TKHQ.logMessage(
`⬇️ Received message ${event.data["type"]}: ${event.data["value"]}`
);
try {
await onBatchStampRequest(event.data["value"]);
} catch (e) {
TKHQ.sendMessageUp("ERROR", e.toString());
}
}
if (event.data && event.data["type"] == "RESET_EMBEDDED_KEY") {
TKHQ.logMessage(`⬇️ Received message ${event.data["type"]}`);
try {
Expand Down Expand Up @@ -1198,6 +1208,57 @@ <h2>Message log</h2>
);
TKHQ.sendMessageUp("STAMP", stampHeaderValue);
};
/**
* Function triggered when BATCH_STAMP_REQUEST event is received.
* @param {string} payloads to sign
*/
var onBatchStampRequest = async function (payloads) {
if (CREDENTIAL_BYTES === null) {
throw new Error(
"cannot sign payload without credential. Credential bytes are null"
);
}
var key = await TKHQ.importCredential(CREDENTIAL_BYTES);

// This is a bit of a pain, but we need to go through this:
// - Key needs to be exported to JWK first
// - Then imported without the private "d" component, and exported to get the public key
// ^^ (that's what `p256JWKPrivateToPublic` does)
// - Finally, compress the public key.
var jwkKey = await crypto.subtle.exportKey("jwk", key);
var publicKey = await TKHQ.p256JWKPrivateToPublic(jwkKey);
var compressedPublicKey = TKHQ.compressRawPublicKey(publicKey);

var stamps = payloads.map(async (p) => {
var signatureIeee1363 = await window.crypto.subtle.sign(
{
name: "ECDSA",
hash: { name: "SHA-256" },
},
key,
new TextEncoder().encode(p)
);

var derSignature = TKHQ.convertEcdsaIeee1363ToDer(
new Uint8Array(signatureIeee1363)
);
var derSignatureHexString = TKHQ.uint8arrayToHexString(derSignature);

var stamp = {
publicKey: TKHQ.uint8arrayToHexString(compressedPublicKey),
scheme: "SIGNATURE_SCHEME_TK_API_P256",
signature: derSignatureHexString,
};

var stampHeaderValue = TKHQ.stringToBase64urlString(
JSON.stringify(stamp)
);

return stampHeaderValue;
});

TKHQ.sendMessageUp("BATCH_STAMP", stamps);
};

/**
* Decrypt the ciphertext (ArrayBuffer) given an encapsulation key (ArrayBuffer)
Expand Down

0 comments on commit be74338

Please sign in to comment.