Skip to content

Commit

Permalink
fix: return compressed key consistent with iOS (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: Berend Sliedrecht <[email protected]>
  • Loading branch information
berendsliedrecht authored Aug 19, 2024
1 parent 7725fe1 commit 4e6171a
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,24 @@ export function getPublicBytesForKeyId(keyId: string): Uint8Array {

if (Platform.OS === "android") {
let spki = AsnParser.parse(publicBytes, SubjectPublicKeyInfo);
return new Uint8Array(spki.subjectPublicKey);
const uncompressedKey = new Uint8Array(spki.subjectPublicKey);
if (uncompressedKey.length !== 65 || uncompressedKey[0] !== 0x04) {
throw new Error("Invalid uncompressed key format");
}

// Extract the X and Y coordinates
const x = uncompressedKey.slice(1, 33); // bytes 1 to 32 (X coordinate)
const y = uncompressedKey.slice(33, 65); // bytes 33 to 64 (Y coordinate)

// Determine the parity of the Y coordinate
const prefix = y[y.length - 1] % 2 === 0 ? 0x02 : 0x03;

// Return the compressed key (prefix + X coordinate)
const compressedKey = new Uint8Array(33);
compressedKey[0] = prefix;
compressedKey.set(x, 1);

return compressedKey;
}

return publicBytes;
Expand Down

0 comments on commit 4e6171a

Please sign in to comment.