diff options
| -rw-r--r-- | package.json | 4 | ||||
| -rw-r--r-- | src/digiid.ts | 27 |
2 files changed, 17 insertions, 14 deletions
diff --git a/package.json b/package.json index e326047..1edba61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "digiid-ts", - "version": "2.0.0", + "version": "2.0.1-beta.0", "description": "A modern TypeScript implementation of the DigiID authentication protocol.", "main": "dist/digiid-ts.umd.js", "module": "dist/digiid-ts.es.js", @@ -78,4 +78,4 @@ "glob": "^10.5.0", "brace-expansion": "^2.0.2" } -} +}
\ No newline at end of file diff --git a/src/digiid.ts b/src/digiid.ts index 11bc309..168e097 100644 --- a/src/digiid.ts +++ b/src/digiid.ts @@ -178,7 +178,7 @@ function recoverPublicKey(messageHash: Uint8Array, signature: Uint8Array): Uint8 const compressed = recoveryId >= 4; const actualRecoveryId = recoveryId % 4; - if (actualRecoveryId > 3) { + if (actualRecoveryId < 0 || actualRecoveryId > 3) { throw new Error('Invalid recovery ID'); } @@ -189,20 +189,23 @@ function recoverPublicKey(messageHash: Uint8Array, signature: Uint8Array): Uint8 const sig = new secp256k1.Signature( BigInt('0x' + Array.from(r).map(b => b.toString(16).padStart(2, '0')).join('')), BigInt('0x' + Array.from(s).map(b => b.toString(16).padStart(2, '0')).join('')) - ); + ).addRecoveryBit(actualRecoveryId); try { const point = sig.recoverPublicKey(messageHash); - return [point.toHex(compressed)].map(hex => { - // Convert hex string to Uint8Array - const bytes = new Uint8Array(hex.length / 2); - for (let i = 0; i < hex.length; i += 2) { - bytes[i / 2] = parseInt(hex.slice(i, i + 2), 16); - } - return bytes; - }); - } catch { - throw new Error('Failed to recover public key'); + // Return both compressed and uncompressed versions to try both + const compressedBytes = point.toBytes(true); + const uncompressedBytes = point.toBytes(false); + + // Based on the recoveryId flag, return the appropriate format(s) + if (compressed) { + return [compressedBytes]; + } else { + // For uncompressed signatures, try both formats as different wallets may encode differently + return [uncompressedBytes, compressedBytes]; + } + } catch (e) { + throw new Error('Failed to recover public key: ' + (e instanceof Error ? e.message : String(e))); } } |
