diff --git a/packages/jellyfish-crypto/__tests__/elliptic.test.ts b/packages/jellyfish-crypto/__tests__/elliptic.test.ts index 0ad025d78a..a81c4e85fb 100644 --- a/packages/jellyfish-crypto/__tests__/elliptic.test.ts +++ b/packages/jellyfish-crypto/__tests__/elliptic.test.ts @@ -109,8 +109,13 @@ describe('DER Signature: sign and verify', () => { return await shouldReturnPubKeyFromPubKey(privateKeyHex, publicKeyHex) }) - it('should sign hash Buffer and get signature', async () => { - return await shouldSignHashBufferGetSignature(privateKeyHex, hashHex, signatureHex) + it('should sign hash Buffer and verify signature', async () => { + const privateKey = Buffer.from(privateKeyHex, 'hex') + const curvePair = elliptic.getEllipticPairFromPrivateKey(privateKey) + + const hash = Buffer.from(hashHex, 'hex') + const signature = await curvePair.sign(hash) + expect(curvePair.verify(hash, signature)).toBeTruthy() }) it('should verify hash with signature', async () => { @@ -128,8 +133,13 @@ describe('DER Signature: sign and verify', () => { return await shouldReturnPubKeyFromPubKey(privateKeyHex, publicKeyHex) }) - it('should sign hash Buffer and get signature', async () => { - return await shouldSignHashBufferGetSignature(privateKeyHex, hashHex, signatureHex) + it('should sign hash Buffer and verify signature', async () => { + const privateKey = Buffer.from(privateKeyHex, 'hex') + const curvePair = elliptic.getEllipticPairFromPrivateKey(privateKey) + + const hash = Buffer.from(hashHex, 'hex') + const signature = await curvePair.sign(hash) + expect(curvePair.verify(hash, signature)).toBeTruthy() }) it('should verify hash with signature', async () => { diff --git a/packages/jellyfish-crypto/src/elliptic.ts b/packages/jellyfish-crypto/src/elliptic.ts index a951af071a..8932baefe7 100644 --- a/packages/jellyfish-crypto/src/elliptic.ts +++ b/packages/jellyfish-crypto/src/elliptic.ts @@ -62,7 +62,18 @@ class SECP256K1 implements EllipticPair { } async sign (hash: Buffer): Promise { - const signature = ecc.sign(hash, this.privKey) + let signature = ecc.sign(hash, this.privKey) + + const extraData = Buffer.alloc(32, 0) + let counter = 0 + + // if first try is lowR, skip the loop, for second try and on, add extra entropy counting up + while (signature[0] > 0x7f) { + counter++ + extraData.writeUIntLE(counter, 0, 6) + // @ts-expect-error + signature = ecc.signWithEntropy(hash, this.privKey, extraData) + } return DERSignature.encode(signature) }