ADD more tests

pull/1/head
pubkey 7 years ago
parent 958d60c002
commit e6c28e38cf
  1. 1
      package.json
  2. 44
      src/index.js
  3. 158
      test/index.test.js

@ -55,6 +55,7 @@
"dependencies": {
"babel-runtime": "6.26.0",
"ethereumjs-util": "^5.1.3",
"js-sha3": "^0.7.0",
"randombytes": "2.0.6",
"secp256k1": "3.4.0"
}

@ -1,6 +1,9 @@
import * as ethUtil from 'ethereumjs-util';
import randombytes from 'randombytes';
import * as secp256k1 from 'secp256k1';
import {
sha3_256
} from 'js-sha3';
import {
ensureBuffer,
@ -40,15 +43,38 @@ export function createPrivateKey() {
return key;
}
/**
* create the publicKey from the privateKey
* @param {string} privateKey as hex
* @return {string} publicKey as hex
*/
export function publicKeyFromPrivateKey(privateKey) {
return secp256k1
.publicKeyCreate(
ensureBuffer(privateKey)
)
.toString('hex');
}
/**
* creates a sha3_256 of the message
* @param {string} message
* @return {string} the hash
*/
export function hash(message) {
return sha3_256(message);
}
/**
* signs the message with the privateKey
* signs the sha3_256-hash with the privateKey
* @param {string} privateKey
* @param {string} message
* @param {string} hash
* @return {string} signature as hex
*/
export function sign(privateKey, message) {
export function signHash(privateKey, hash) {
const sigObj = secp256k1.sign(
ensureBuffer(message),
ensureBuffer(hash),
ensureBuffer(privateKey)
);
return sigObj.signature.toString('hex');
@ -57,14 +83,14 @@ export function sign(privateKey, message) {
/**
* check if signature of message is signed by the privateKey of the publicKey
* @param {string} publicKey
* @param {string} message
* @param {string} hash sha3_256-hash
* @param {string} signature
* @return {boolean} true if valid, false if not
*/
export function verify(publicKey, message, signature) {
export function verifyHashSignature(publicKey, hash, signature) {
return secp256k1.verify(
_helper.ensureBuffer(message),
_helper.ensureBuffer(signature),
_helper.ensureBuffer(publicKey)
ensureBuffer(hash),
ensureBuffer(signature),
ensureBuffer(publicKey)
);
}

@ -31,6 +31,162 @@ describe('index.test.js', () => {
assert.ok(key.length > 55);
assert.ok(key.length < 90);
});
it('should have different keys because of randomBytes', () => {
const key1 = EthereumEncryption.createPrivateKey();
const key2 = EthereumEncryption.createPrivateKey();
assert.notEqual(key1, key2);
});
});
describe('.publicKeyFromPrivateKey()', () => {
describe('positive', () => {
it('should create the correct publicKey', () => {
const publicKey = EthereumEncryption.publicKeyFromPrivateKey(
testData.privateKey
);
assert.equal(publicKey, testData.publicKey);
});
it('should create a publicKey from generated privateKey', () => {
const privateKey = EthereumEncryption.createPrivateKey();
const publicKey = EthereumEncryption.publicKeyFromPrivateKey(
privateKey
);
assert.equal(typeof publicKey, 'string');
assert.ok(publicKey.length > 55);
assert.ok(publicKey.length < 90);
});
});
describe('negative', () => {
it('should throw when non-privateKey given', () => {
assert.throws(
() => EthereumEncryption.publicKeyFromPrivateKey(
'foobar'
)
);
});
});
});
describe('.hash()', () => {
describe('positive', () => {
it('should create a hash', () => {
const hash = EthereumEncryption.hash('foobar');
assert.equal('09234807e4af85f17c66b48ee3bca89dffd1f1233659f9f940a2b17b0b8c6bc5', hash);
});
});
describe('negative', () => {
it('should throw when no string given', () => {
assert.throws(
() => EthereumEncryption.hash({
foo: 'bar'
})
);
});
});
});
describe('.signHash()', () => {
describe('positive', () => {
it('should sign the hash', () => {
const privateKey = EthereumEncryption.createPrivateKey();
const hash = EthereumEncryption.hash('foobar');
const signature = EthereumEncryption.signHash(
privateKey,
hash
);
assert.equal(typeof signature, 'string');
assert.ok(signature.length > 100);
assert.ok(signature.length < 160);
});
it('should always create the same signature', () => {
const privateKey = EthereumEncryption.createPrivateKey();
const hash = EthereumEncryption.hash('foobar');
const signature1 = EthereumEncryption.signHash(
privateKey,
hash
);
const signature2 = EthereumEncryption.signHash(
privateKey,
hash
);
assert.equal(signature1, signature2);
});
});
describe('negative', () => {
it('should throw when non-hash is given', () => {
const privateKey = EthereumEncryption.createPrivateKey();
assert.throws(
() => EthereumEncryption.signHash(
privateKey,
'foobar'
)
);
});
it('should throw when non-private-key is given', () => {
const hash = EthereumEncryption.hash('foobar');
assert.throws(
() => EthereumEncryption.signHash(
'foobar',
hash
)
);
});
});
});
describe('.verifyHashSignature()', () => {
describe('positive', () => {
it('should verify the signature', () => {
const privateKey = EthereumEncryption.createPrivateKey();
const hash = EthereumEncryption.hash('foobar');
const signature = EthereumEncryption.signHash(
privateKey,
hash
);
const publicKey = EthereumEncryption.publicKeyFromPrivateKey(
privateKey
);
const valid = EthereumEncryption.verifyHashSignature(
publicKey,
hash,
signature
);
assert.ok(valid);
});
it('should not verify wrong signature', () => {
const privateKey = EthereumEncryption.createPrivateKey();
const wrongPrivateKey = EthereumEncryption.createPrivateKey();
const hash = EthereumEncryption.hash('foobar');
const signature = EthereumEncryption.signHash(
wrongPrivateKey,
hash
);
const publicKey = EthereumEncryption.publicKeyFromPrivateKey(
privateKey
);
const valid = EthereumEncryption.verifyHashSignature(
publicKey,
hash,
signature
);
assert.equal(false, valid);
});
});
describe('negative', () => {
it('should throw when non publicKey given', () => {
const privateKey = EthereumEncryption.createPrivateKey();
const hash = EthereumEncryption.hash('foobar');
const signature = EthereumEncryption.signHash(
privateKey,
hash
);
const publicKey = EthereumEncryption.publicKeyFromPrivateKey(
privateKey
);
assert.throws(
() => EthereumEncryption.verifyHashSignature(
'foobar',
hash,
signature
)
);
});
});
});
});

Loading…
Cancel
Save