ADD test for #3

pull/15/head
pubkey 7 years ago
parent 0dcdb53c79
commit 96fdae9918
  1. 3
      src/recover-public-key.js
  2. 1
      test/index.test.js
  3. 35
      test/issues.test.js

@ -5,7 +5,7 @@ import * as util from './util';
/**
* returns the publicKey for the privateKEy with which the messageHash was signed
* returns the publicKey for the privateKey with which the messageHash was signed
* @param {string} signature
* @param {string} hash
* @return {string} publicKey
@ -13,6 +13,7 @@ import * as util from './util';
export default function recoverPublicKey(signature, hash) {
const vals = vrs.fromString(signature);
let sigOnly = signature.substring(0, signature.length - 1);
sigOnly = util.removeTrailing0x(sigOnly);

@ -1,5 +1,6 @@
require('./unit.test');
require('./integration.test');
require('./issues.test');
// tutorials
require('./tutorials/signed-data.test');

@ -0,0 +1,35 @@
// const AsyncTestUtil = require('async-test-util');
const assert = require('assert');
const EthCrypto = require('../dist/lib/index');
describe('issues.test.js', () => {
it('#3 Error in recover', async () => {
const payload = {
data: 'something',
val: 5,
other: 'something else'
};
const msgHash = EthCrypto.hash.keccak256(JSON.stringify(payload));
const ident = EthCrypto.createIdentity();
const sig = EthCrypto.sign(
ident.privateKey, // privateKey
msgHash // hash of message
);
assert.ok(sig);
assert.ok(sig.startsWith('0x'));
const recAddress = EthCrypto.recover(
sig,
EthCrypto.hash.keccak256(JSON.stringify(payload)) // signed message hash
);
assert.equal(recAddress, ident.address);
const recKey = EthCrypto.recoverPublicKey(
sig,
EthCrypto.hash.keccak256(JSON.stringify(payload)) // signed message hash
);
assert.equal(recKey, ident.publicKey);
});
});
Loading…
Cancel
Save