Skip to content

Commit

Permalink
added lowr as default for getEllipticPairFromPrivateKey
Browse files Browse the repository at this point in the history
  • Loading branch information
fuxingloh committed Apr 16, 2021
1 parent 8b96224 commit 7b7d6d2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
18 changes: 14 additions & 4 deletions packages/jellyfish-crypto/__tests__/elliptic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand Down
13 changes: 12 additions & 1 deletion packages/jellyfish-crypto/src/elliptic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,18 @@ class SECP256K1 implements EllipticPair {
}

async sign (hash: Buffer): Promise<Buffer> {
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)
}

Expand Down

0 comments on commit 7b7d6d2

Please sign in to comment.