Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Jul 5, 2024
1 parent 4cff298 commit 8e82bf1
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions packages/shared/src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const encoder = new TextEncoder();
const sha256Hex = (data: string) =>
Micro.map(
Micro.promise(() => crypto.subtle.digest("SHA-256", encoder.encode(data))),
(buf) => Encoding.encodeHex(new Uint8Array(buf)),
(arrayBuffer) => Encoding.encodeHex(new Uint8Array(arrayBuffer)),
);

export const signPayload = (payload: string, secret: string) =>
Expand All @@ -34,15 +34,14 @@ export const signPayload = (payload: string, secret: string) =>
}),
});

const signature = yield* Micro.tryPromise({
try: () =>
crypto.subtle
.sign(algorithm, signingKey, encoder.encode(payload))
.then((arrayBuffer) =>
Encoding.encodeHex(new Uint8Array(arrayBuffer)),
),
catch: (e) => new UploadThingError({ code: "BAD_REQUEST", cause: e }),
});
const signature = yield* Micro.map(
Micro.tryPromise({
try: () =>
crypto.subtle.sign(algorithm, signingKey, encoder.encode(payload)),
catch: (e) => new UploadThingError({ code: "BAD_REQUEST", cause: e }),
}),
(arrayBuffer) => Encoding.encodeHex(new Uint8Array(arrayBuffer)),
);

return `${signaturePrefix}${signature}`;
});
Expand All @@ -63,7 +62,7 @@ export const verifySignature = (

const sigBytes = yield* Micro.fromEither(Encoding.decodeHex(sig));
const payloadBytes = encoder.encode(payload);
return yield* Micro.promise(async () =>
return yield* Micro.promise(() =>
crypto.subtle.verify(algorithm, signingKey, sigBytes, payloadBytes),
);
}).pipe(Micro.orElseSucceed(() => false));
Expand Down Expand Up @@ -108,7 +107,7 @@ export const verifyKey = (key: string, apiKey: string) =>

// q: Does it need to be timing safe?
return expected === given;
});
}).pipe(Micro.orElseSucceed(() => false));

export const generateSignedURL = (
url: string | URL,
Expand Down

0 comments on commit 8e82bf1

Please sign in to comment.