Skip to content

Commit 9682924

Browse files
committed
Drop the redundant copy in decodePublicKey
Buffer is a subclass of Uint8Array, so Buffer.from(encoded, "base64url") already returns something that satisfies the Uint8Array<ArrayBuffer> return type and the only downstream caller (SimpleWebAuthn's verifyAuthenticationResponse) reads the bytes either way. The previous version allocated a fresh ArrayBuffer and copied the buffer's contents into a plain Uint8Array; nothing here benefits from that round trip. The two unit tests that round-trip a key now compare via Array.from() rather than toEqual() on the typed-array instance, since toEqual checks the prototype as well and a Buffer doesn't deep-equal a Uint8Array under that rule. #487 (comment) Assisted-by: Claude Code:claude-opus-4-7
1 parent dc25d17 commit 9682924

2 files changed

Lines changed: 3 additions & 6 deletions

File tree

src/passkey.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe("encodePublicKey / decodePublicKey", () => {
8787
const bytes = new Uint8Array([0, 1, 2, 250, 251, 252, 253, 254, 255]);
8888
const encoded = encodePublicKey(bytes);
8989
expect(typeof encoded).toBe("string");
90-
expect(decodePublicKey(encoded)).toEqual(bytes);
90+
expect(Array.from(decodePublicKey(encoded))).toEqual(Array.from(bytes));
9191
});
9292

9393
it("encodes to base64url (no +, /, or = padding)", () => {
@@ -99,7 +99,7 @@ describe("encodePublicKey / decodePublicKey", () => {
9999
it("decodes the same value when given a string with padding stripped", () => {
100100
const bytes = new Uint8Array([1, 2, 3]);
101101
const encoded = encodePublicKey(bytes);
102-
expect(decodePublicKey(encoded)).toEqual(bytes);
102+
expect(Array.from(decodePublicKey(encoded))).toEqual(Array.from(bytes));
103103
});
104104
});
105105

src/passkey.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,7 @@ export function encodePublicKey(bytes: Uint8Array): string {
225225

226226
/** Decode a base64url-encoded public-key blob back into bytes. */
227227
export function decodePublicKey(encoded: string): Uint8Array<ArrayBuffer> {
228-
const buf = Buffer.from(encoded, "base64url");
229-
const out = new Uint8Array(new ArrayBuffer(buf.byteLength));
230-
out.set(buf);
231-
return out;
228+
return Buffer.from(encoded, "base64url");
232229
}
233230

234231
const PLATFORM_LABELS: ReadonlyArray<{ pattern: RegExp; label: string }> = [

0 commit comments

Comments
 (0)