From 82776006453b6e3d5ab2f9ce15b43da118d397b1 Mon Sep 17 00:00:00 2001 From: giwaov Date: Mon, 6 Jul 2026 11:17:47 +0100 Subject: [PATCH] Validate uncompressed ETH public keys --- src/__tests__/curve-detection.test.ts | 16 ++++++++++++++++ src/curve-detection.ts | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/curve-detection.test.ts diff --git a/src/__tests__/curve-detection.test.ts b/src/__tests__/curve-detection.test.ts new file mode 100644 index 0000000..d449f52 --- /dev/null +++ b/src/__tests__/curve-detection.test.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from "vitest"; +import { detectPublicKeyCurve } from "../curve-detection.js"; +import { CurveType } from "../types.js"; + +describe("detectPublicKeyCurve", () => { + it("accepts raw and uncompressed Ethereum secp256k1 public keys", () => { + expect(detectPublicKeyCurve("11".repeat(64))).toBe(CurveType.ETHSECP256K1); + expect(detectPublicKeyCurve("04" + "11".repeat(64))).toBe(CurveType.ETHSECP256K1); + }); + + it("rejects 65-byte Ethereum secp256k1 public keys without the uncompressed prefix", () => { + expect(() => detectPublicKeyCurve("05" + "11".repeat(64))).toThrow( + "65-byte ETHSECP256K1 keys must start with 0x04", + ); + }); +}); \ No newline at end of file diff --git a/src/curve-detection.ts b/src/curve-detection.ts index c6a6564..f3e3b58 100644 --- a/src/curve-detection.ts +++ b/src/curve-detection.ts @@ -12,8 +12,10 @@ export function detectPublicKeyCurve(publicKeyHex: string): CurveType { case KEY_SIZES.PUBLIC.BLS12381: return CurveType.BLS12381; case KEY_SIZES.PUBLIC.ETHSECP256K1: - case 65: return CurveType.ETHSECP256K1; + case 65: + if (bytes[0] === 0x04) return CurveType.ETHSECP256K1; + throw new Error("Unrecognized public key format: 65-byte ETHSECP256K1 keys must start with 0x04"); default: throw new Error(`Unrecognized public key format: ${bytes.length} bytes`); }