Commit 4f444279 by AugustoL Committed by Francisco Giordano

Added function helper, few changes on ECRecover lib

parent f8c0fab5
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -20,8 +20,9 @@ library ECRecovery {
uint8 v;
//Check the signature length
if (sig.length != 65)
if (sig.length != 65) {
return (address(0));
}
// Divide the signature in r, s and v variables
assembly {
......@@ -31,14 +32,16 @@ library ECRecovery {
}
// Version of signature should be 27 or 28, but 0 and 1 are also possible versions
if (v < 27)
v += 27;
if (v < 27) {
v += 27;
}
// If the version is correct return the signer address
if (v != 27 && v != 28)
if (v != 27 && v != 28) {
return (address(0));
else
} else {
return ecrecover(hash, v, r, s);
}
}
}
var ECRecovery = artifacts.require("../contracts/ECRecovery.sol");
var utils = require('ethereumjs-util');
var hashMessage = require('./helpers/hashMessage.js');
contract('ECRecovery', function(accounts) {
......@@ -29,42 +30,26 @@ contract('ECRecovery', function(accounts) {
// Create the signature using account[0]
const signature = web3.eth.sign(web3.eth.accounts[0], web3.sha3('OpenZeppelin'));
// Testrpc add a prefix to the signed message, we generate the hash of
// 'OpenZeppelin' string repeating testrpc steps
const message = new Buffer(web3.sha3('OpenZeppelin').substring(2), 'hex');
const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + message.length.toString());
const prefixedHash = utils.bufferToHex( utils.sha3(Buffer.concat([prefix, message])) );
// Recover the signer address form the generated message and signature.
assert.equal(web3.eth.accounts[0], await ecrecovery.recover(prefixedHash, signature));
assert.equal(web3.eth.accounts[0], await ecrecovery.recover(hashMessage('OpenZeppelin'), signature));
});
it("recover using web3.eth.sign() should return wrong signer", async function() {
// Create the signature using account[0]
const signature = web3.eth.sign(web3.eth.accounts[0], web3.sha3('OpenZeppelin'));
// Testrpc add a prefix to the signed message, we generate the hash of
// 'Test' string repeating testrpc steps
const message = new Buffer(web3.sha3('Test').substring(2), 'hex');
const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + message.length.toString());
const prefixedHash = utils.bufferToHex( utils.sha3(Buffer.concat([prefix, message])) );
// Recover the signer address form the generated message and wrong signature.
assert.notEqual(web3.eth.accounts[0], await ecrecovery.recover(prefixedHash, signature));
assert.notEqual(web3.eth.accounts[0], await ecrecovery.recover(hashMessage('Test'), signature));
});
it("recover should fail when a wrong hash is sent", async function() {
// Create the signature using account[0]
let signature = web3.eth.sign(web3.eth.accounts[0], web3.sha3('OpenZeppelin'));
// Testrpc add a prefix to the signed message, we generate the hash of
// 'OpenZeppelin' string repeating testrpc steps
let message = new Buffer(web3.sha3('OpenZeppelin'), 'hex');
let prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + message.length.toString());
let prefixedHash = utils.sha3(Buffer.concat([prefix, message]));
// Recover the signer address form the generated message and wrong signature.
assert.equal('0x0000000000000000000000000000000000000000', await ecrecovery.recover(prefixedHash, signature));
assert.equal('0x0000000000000000000000000000000000000000',
await ecrecovery.recover(hashMessage('OpenZeppelin').substring(2), signature)
);
});
});
import utils from 'ethereumjs-util';
// Hash and add same prefix to the hash that testrpc use.
module.exports = function(message) {
const messageHex = new Buffer(utils.sha3(message).toString('hex'), 'hex');
const prefix = utils.toBuffer('\u0019Ethereum Signed Message:\n' + messageHex.length.toString());
return utils.bufferToHex( utils.sha3(Buffer.concat([prefix, messageHex])) );
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment