From d1b2b07ca94d409475d341a5756921f9a0715f80 Mon Sep 17 00:00:00 2001 From: Daniel McNally Date: Thu, 17 Aug 2023 18:39:42 +0000 Subject: [PATCH] Improve error message for invalid privkey length This adds detail to the error message when a Uint8Array is provided as a privkey but it does not have the correct length. Previously, the error message stated only "Uint8Array expected" - which is confusing because a Uint8Array was in fact provided. This should make debugging invalid privkeys easier. A test case is added that checks for the new error message. --- index.js | 2 +- index.ts | 2 +- test/ed25519.test.js | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 121220f..823e69d 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ const err = (m = '') => { throw new Error(m); }; // error helper, messes-up stac const str = (s) => typeof s === 'string'; // is string const au8 = (a, l) => // is Uint8Array (of specific length) !(a instanceof Uint8Array) || (typeof l === 'number' && l > 0 && a.length !== l) ? - err('Uint8Array expected') : a; + err('Uint8Array of valid length expected') : a; const u8n = (data) => new Uint8Array(data); // creates Uint8Array const toU8 = (a, len) => au8(str(a) ? h2b(a) : u8n(a), len); // norm(hex/u8a) to u8a const mod = (a, b = P) => { let r = a % b; return r >= 0n ? r : b + r; }; // mod division diff --git a/index.ts b/index.ts index aaa0214..93dc415 100644 --- a/index.ts +++ b/index.ts @@ -13,7 +13,7 @@ const err = (m = ''): never => { throw new Error(m); }; // error helper, messes- const str = (s: unknown): s is string => typeof s === 'string'; // is string const au8 = (a: unknown, l?: number): Bytes => // is Uint8Array (of specific length) !(a instanceof Uint8Array) || (typeof l === 'number' && l > 0 && a.length !== l) ? - err('Uint8Array expected') : a; + err('Uint8Array of valid length expected') : a; const u8n = (data?: any) => new Uint8Array(data); // creates Uint8Array const toU8 = (a: Hex, len?: number) => au8(str(a) ? h2b(a) : u8n(a), len); // norm(hex/u8a) to u8a const mod = (a: bigint, b = P) => { let r = a % b; return r >= 0n ? r : b + r; }; // mod division diff --git a/test/ed25519.test.js b/test/ed25519.test.js index f034ff8..e83e3fc 100644 --- a/test/ed25519.test.js +++ b/test/ed25519.test.js @@ -29,6 +29,10 @@ describe('ed25519', () => { 100000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800073278156000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000n; throws(() => ed.getPublicKey(invalidPriv)); }); + should('not accept >32byte private keys in Uint8Array format', () => { + const invalidPriv = new Uint8Array(33); + throws(() => ed.getPublicKey(invalidPriv), new Error('Uint8Array of valid length expected')); + }); should('verify recent signature', () => { fc.assert( fc.property(