Skip to content

Commit

Permalink
fix: not working with Next.js Edge Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
tksst committed Feb 24, 2024
1 parent 3652767 commit a6f3f9e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fix-not-working-with-nextjs-edge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobilepass": patch
---

fix: not working with Next.js Edge Runtime, see https://github.com/vercel/edge-runtime/issues/813
8 changes: 5 additions & 3 deletions packages/mobilepass/src/lib/utils/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ export async function sha256(data: BufferSource): Promise<ArrayBuffer> {
return await crypto.subtle.digest("SHA-256", data);
}

export async function hmacSHA256(key: BufferSource, data: BufferSource): Promise<ArrayBuffer> {
export async function hmacSHA256(key: ArrayBuffer, data: ArrayBuffer): Promise<ArrayBuffer> {
const importedKey = await crypto.subtle.importKey(
"raw",
key,
// Next.js Edge Runtime has a bug and wraps with Uint8Array to work around it.
// https://github.com/vercel/edge-runtime/issues/813
new Uint8Array(key),
{
name: "HMAC",
hash: { name: "SHA-256" },
Expand All @@ -14,5 +16,5 @@ export async function hmacSHA256(key: BufferSource, data: BufferSource): Promise
["sign"],
);

return await crypto.subtle.sign("HMAC", importedKey, data);
return await crypto.subtle.sign("HMAC", importedKey, new Uint8Array(data));
}
2 changes: 1 addition & 1 deletion packages/mobilepass/src/lib/utils/hotp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function truncate(mac: Uint8Array, digits: number): number {
return value % 10 ** digits;
}

export async function hotpSHA256(key: ArrayBuffer | ArrayBufferView, counter: number | bigint): Promise<string> {
export async function hotpSHA256(key: ArrayBuffer, counter: number | bigint): Promise<string> {
if (counter < 0) {
throw new RangeError(`counter: ${counter} is not a natural number.`);
}
Expand Down

0 comments on commit a6f3f9e

Please sign in to comment.