Skip to content

Commit

Permalink
Merge pull request #318 from casper-ecosystem/fix/secp256k1-sign-from…
Browse files Browse the repository at this point in the history
…-2.13.2

Secp256k1 sign fix
  • Loading branch information
hoffmannjan authored Jun 22, 2023
2 parents 958a065 + 7da240d commit 53d7d9a
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 2 additions & 21 deletions e2e/lib/Keys.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
});
});
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
23 changes: 21 additions & 2 deletions src/lib/Keys.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect } from 'chai';

import { decodeBase16, decodeBase64 } from './Conversions';
import { Ed25519, Secp256K1 } from './Keys';
import { byteHash } from './ByteConverters';
Expand Down Expand Up @@ -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');
Expand All @@ -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);
});
});
5 changes: 4 additions & 1 deletion src/lib/Keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 53d7d9a

Please sign in to comment.