Created
February 2, 2023 11:22
-
-
Save bartubozkurt/27a0ea5cf0d5207993d01f874f002991 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Bad */ | |
function unlock( | |
address _to, | |
uint256 _amount, | |
uint8[] _v, | |
bytes32[] _r, | |
bytes32[] _s | |
) | |
external | |
{ | |
require(_v.length >= 5); | |
bytes32 hashData = keccak256(_to, _amount); | |
for (uint i = 0; i < _v.length; i++) { | |
address recAddr = ecrecover(hashData, _v[i], _r[i], _s[i]); | |
require(_isValidator(recAddr)); | |
} | |
to.transfer(_amount); | |
} | |
/* Better */ | |
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol"; | |
function unlock( | |
address _to, | |
uint256 _amount, | |
uint256 _nonce, | |
uint8[] _v, | |
bytes32[] _r, | |
bytes32[] _s | |
) | |
external | |
{ | |
require(_v.length >= 5); | |
require(_nonce == nonce++); | |
bytes32 hashData = keccak256(_to, _amount, _nonce); | |
for (uint i = 0; i < _v.length; i++) { | |
address recAddr = ecrecover(hashData, _v[i], _r[i], _s[i]); | |
address recAddr = ECDSA.recover(hashData, _v[i], _r[i], _s[i]); | |
require(_isValidator(recAddr)); | |
} | |
to.transfer(_amount); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment