diff --git a/CHANGELOG.md b/CHANGELOG.md index e34f42d08..a0b7f4c12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,12 @@ All notable changes to casper-js-sdk. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 2.13.2 + +### Fixed + +- Fix for wrong signatures being generated for SECP256K1 keys - it was missing `der: false` setting in a function call + ## 2.13.1 ### Added diff --git a/e2e/lib/Keys.test.ts b/e2e/lib/Keys.test.ts index 28f0db5f9..90bdb9a1a 100644 --- a/e2e/lib/Keys.test.ts +++ b/e2e/lib/Keys.test.ts @@ -103,21 +103,8 @@ describe('Ed25519', () => { }); describe('Secp256K1', () => { - it('calculates the account hash', async () => { - const signKeyPair = await Secp256K1.new(); - // use lower case for node-rs - const name = Buffer.from('secp256k1'.toLowerCase()); - const sep = decodeBase16('00'); - const bytes = Buffer.concat([name, sep, signKeyPair.publicKey.value()]); - const hash = byteHash(bytes); - - expect(Secp256K1.accountHash(signKeyPair.publicKey.value())).deep.equal( - hash - ); - }); - - it('should generate PEM file for Secp256K1 correctly', async () => { - const signKeyPair = await Secp256K1.new(); + it('should generate PEM file for Secp256K1 correctly', () => { + const signKeyPair = Secp256K1.new(); // export key in pem to save const publicKeyInPem = signKeyPair.exportPublicKeyInPem(); @@ -154,11 +141,5 @@ describe('Secp256K1', () => { expect(ecdh.getPublicKey('hex', 'compressed')).to.deep.equal( encodeBase16(signKeyPair.publicKey.value()) ); - - // expect we could sign the message and verify the signature later. - const message = Buffer.from('hello world'); - const signature = signKeyPair.sign(Buffer.from(message)); - // expect we could verify the signature created by ourself - expect(signKeyPair.verify(signature, message)).to.equal(true); }); }); diff --git a/package-lock.json b/package-lock.json index 73634d8db..26ca73996 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "casper-js-sdk", - "version": "2.13.1", + "version": "2.13.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "casper-js-sdk", - "version": "2.13.1", + "version": "2.13.2", "license": "Apache 2.0", "dependencies": { "@ethersproject/bignumber": "^5.0.8", diff --git a/package.json b/package.json index fc8766129..5cf998254 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "casper-js-sdk", - "version": "2.13.1", + "version": "2.13.2", "license": "Apache 2.0", "description": "SDK to interact with the Casper blockchain", "homepage": "https://github.com/casper-ecosystem/casper-js-sdk#README.md", diff --git a/src/lib/Keys.test.ts b/src/lib/Keys.test.ts index fc5b7fadc..543873aa4 100644 --- a/src/lib/Keys.test.ts +++ b/src/lib/Keys.test.ts @@ -1,4 +1,5 @@ import { expect } from 'chai'; + import { decodeBase16, decodeBase64 } from './Conversions'; import { Ed25519, Secp256K1 } from './Keys'; import { byteHash } from './ByteConverters'; @@ -35,8 +36,8 @@ describe('Ed25519', () => { }); describe('Secp256K1', () => { - it('calculates the account hash', async () => { - const signKeyPair = await Secp256K1.new(); + it('calculates the account hash', () => { + const signKeyPair = Secp256K1.new(); // use lower case for node-rs const name = Buffer.from('secp256k1'.toLowerCase()); const sep = decodeBase16('00'); @@ -47,4 +48,22 @@ describe('Secp256K1', () => { hash ); }); + + it('should generate r+s signature', () => { + const signKeyPair = Secp256K1.new(); + const message = Uint8Array.from(Buffer.from('Hello Secp256K1')); + + const signature = signKeyPair.sign(message); + + expect(signature.length).to.equal(64); + }); + + it('should sign and verify message', () => { + const signKeyPair = Secp256K1.new(); + const message = Uint8Array.from(Buffer.from('Hello Secp256K1')); + + const signature = signKeyPair.sign(message); + + expect(signKeyPair.verify(signature, message)).to.equal(true); + }); }); diff --git a/src/lib/Keys.ts b/src/lib/Keys.ts index 6b82a735e..0c24453b8 100644 --- a/src/lib/Keys.ts +++ b/src/lib/Keys.ts @@ -635,7 +635,10 @@ export class Secp256K1 extends AsymmetricKey { public sign(msg: Uint8Array): Uint8Array { const signature = secp256k1.signSync( sha256(Buffer.from(msg)), - this.privateKey + this.privateKey, + { + der: false + } ); return signature; }