Skip to content

Commit c56de82

Browse files
committed
disputes,token: avoid reimplementing ECDSA signature recovery
Use OpenZeppelin’s ECDSA library for all ecrecovery operations
1 parent d7be96c commit c56de82

File tree

2 files changed

+10
-41
lines changed

2 files changed

+10
-41
lines changed

contracts/disputes/DisputeManager.sol

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ pragma solidity ^0.6.12;
22
pragma experimental ABIEncoderV2;
33

44
import "@openzeppelin/contracts/math/SafeMath.sol";
5+
import "@openzeppelin/contracts/cryptography/ECDSA.sol";
6+
57
import "../governance/Managed.sol";
68

79
/*
@@ -717,7 +719,11 @@ contract DisputeManager is Managed {
717719

718720
// Obtain the signer of the fully-encoded EIP-712 message hash
719721
// NOTE: The signer of the attestation is the indexer that served the request
720-
return _recover(messageHash, _attestation.v, _attestation.r, _attestation.s);
722+
return
723+
ECDSA.recover(
724+
messageHash,
725+
abi.encodePacked(_attestation.r, _attestation.s, _attestation.v)
726+
);
721727
}
722728

723729
/**
@@ -755,41 +761,6 @@ contract DisputeManager is Managed {
755761
return Attestation(requestCID, responseCID, subgraphDeploymentID, v, r, s);
756762
}
757763

758-
/**
759-
* @dev Returns the address that signed a hashed message (`hash`) with
760-
* signature `v`, `r', `s`. This address can then be used for verification purposes.
761-
* @return The address recovered from the hash and signature.
762-
*/
763-
function _recover(
764-
bytes32 _hash,
765-
uint8 _v,
766-
bytes32 _r,
767-
bytes32 _s
768-
) internal pure returns (address) {
769-
// EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
770-
// unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
771-
// the valid range for s in (281): 0 < s < secp256k1n ÷ 2 + 1, and for v in (282): v ∈ {27, 28}. Most
772-
// signatures from current libraries generate a unique signature with an s-value in the lower half order.
773-
//
774-
// If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
775-
// with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
776-
// vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
777-
// these malleable signatures as well.
778-
if (uint256(_s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
779-
revert("ECDSA: invalid signature 's' value");
780-
}
781-
782-
if (_v != 27 && _v != 28) {
783-
revert("ECDSA: invalid signature 'v' value");
784-
}
785-
786-
// If the signature is valid (and not malleable), return the signer address
787-
address signer = ecrecover(_hash, _v, _r, _s);
788-
require(signer != address(0), "ECDSA: invalid signature");
789-
790-
return signer;
791-
}
792-
793764
/**
794765
* @dev Parse a uint8 from `_bytes` starting at offset `_start`.
795766
* @return uint8 value

contracts/token/GraphToken.sol

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pragma solidity ^0.6.12;
22

33
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
44
import "@openzeppelin/contracts/token/ERC20/ERC20Burnable.sol";
5+
import "@openzeppelin/contracts/cryptography/ECDSA.sol";
56

67
import "../governance/Governed.sol";
78

@@ -112,11 +113,8 @@ contract GraphToken is Governed, ERC20, ERC20Burnable {
112113
)
113114
);
114115

115-
address recoveredAddress = ecrecover(digest, _v, _r, _s);
116-
require(
117-
recoveredAddress != address(0) && _owner == recoveredAddress,
118-
"GRT: invalid permit"
119-
);
116+
address recoveredAddress = ECDSA.recover(digest, abi.encodePacked(_r, _s, _v));
117+
require(_owner == recoveredAddress, "GRT: invalid permit");
120118
require(_deadline == 0 || block.timestamp <= _deadline, "GRT: expired permit");
121119

122120
_approve(_owner, _spender, _value);

0 commit comments

Comments
 (0)