This commit is contained in:
Aniket-Engg
2018-10-01 22:00:42 +05:30
parent fa7809a4ea
commit 10abc85ac1
3 changed files with 1 additions and 94 deletions

View File

@ -26,7 +26,7 @@
// Code style
"camelcase": ["error", {"properties": "always"}],
"comma-dangle": ["warn", "always-multiline"],
"comma-dangle": ["error", "always-multiline"],
"comma-spacing": ["error", {"before": false, "after": true}],
"dot-notation": ["error", {"allowKeywords": true, "allowPattern": ""}],
"eol-last": ["error", "always"],

View File

@ -1,53 +0,0 @@
pragma solidity ^0.4.18;
/**
* @title Eliptic curve signature operations
*
* @dev Based on https://gist.github.com/axic/5b33912c6f61ae6fd96d6c4a47afde6d
*/
library ECRecovery {
/**
* @dev Recover signer address from a message by using his signature
* @param hash bytes32 message, the hash is the signed message. What is recovered is the signer address.
* @param sig bytes signature, the signature is generated using web3.eth.sign()
*/
function recover(bytes32 hash, bytes sig) public pure returns (address) {
bytes32 r;
bytes32 s;
uint8 v;
//Check the signature length
if (sig.length != 65) {
return (address(0));
}
// Divide the signature in r, s and v variables
assembly {
r := mload(add(sig, 32))
s := mload(add(sig, 64))
v := byte(0, mload(add(sig, 96)))
}
// Version of signature should be 27 or 28, but 0 and 1 are also possible versions
if (v < 27) {
v += 27;
}
// If the version is correct return the signer address
if (v != 27 && v != 28) {
return (address(0));
} else {
/*
* https://github.com/ethereum/go-ethereum/issues/3731
*/
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
return ecrecover(keccak256(prefix, hash), v, r, s);
}
}
}

View File

@ -1,40 +0,0 @@
var ECRecoveryMock = artifacts.require('ECRecoveryMock');
var ECRecoveryLib = artifacts.require('ECRecovery');
contract('ECRecovery', function (accounts) {
let ecrecovery;
const TEST_MESSAGE = 'OpenZeppelin';
before(async function () {
const ecRecoveryLib = await ECRecoveryLib.new();
ECRecoveryMock.link('ECRecovery', ecRecoveryLib.address);
ecrecovery = await ECRecoveryMock.new();
});
it('recover using web3.eth.sign()', async function () {
// Create the signature using account[0]
const signature = web3.eth.sign(accounts[0], web3.sha3(TEST_MESSAGE));
// Recover the signer address from the generated message and signature.
await ecrecovery.recover(web3.sha3(TEST_MESSAGE), signature);
assert.equal(accounts[0], await ecrecovery.addrRecovered());
});
it('recover using web3.eth.sign() should return wrong signer', async function () {
// Create the signature using account[0]
const signature = web3.eth.sign(accounts[0], web3.sha3(TEST_MESSAGE));
// Recover the signer address from the generated message and wrong signature.
await ecrecovery.recover(web3.sha3('Test'), signature);
assert.notEqual(accounts[0], await ecrecovery.addrRecovered());
});
it('recover should fail when a wrong hash is sent', async function () {
// Create the signature using account[0]
let signature = web3.eth.sign(accounts[0], web3.sha3(TEST_MESSAGE));
// Recover the signer address from the generated message and wrong signature.
await ecrecovery.recover(web3.sha3(TEST_MESSAGE).substring(2), signature);
assert.equal('0x0000000000000000000000000000000000000000', await ecrecovery.addrRecovered());
});
});