-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathEIP712Lib.sol
107 lines (98 loc) · 3.28 KB
/
EIP712Lib.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../../domain/BosonConstants.sol";
import { ProtocolLib } from "../libs/ProtocolLib.sol";
/**
* @title EIP712Lib
*
* @dev Provides the domain seperator and chain id.
*/
library EIP712Lib {
/**
* @notice Get the domain separator
*
* @param _name - the name of the protocol.
* @param _version - The version of the protocol.
*/
function domainSeparator(string memory _name, string memory _version) internal view returns (bytes32) {
return
keccak256(
abi.encode(
EIP712_DOMAIN_TYPEHASH,
keccak256(bytes(_name)),
keccak256(bytes(_version)),
address(this),
getChainID()
)
);
}
/**
* @notice Get the chain id
*
* @return id - the chain id, 1 for Ethereum mainnet, > 1 for public testnets.
*/
function getChainID() internal view returns (uint256 id) {
assembly {
id := chainid()
}
}
/**
* @notice Recovers the Signer from the Signature components.
*
* Reverts if:
* - signer is a zero address
*
* @param _user - the sender of the transaction.
* @param _hashedMetaTx - hashed meta transaction.
* @param _sigR - r part of the signer's signature.
* @param _sigS - s part of the signer's signature.
* @param _sigV - v part of the signer's signature.
*/
function verify(
address _user,
bytes32 _hashedMetaTx,
bytes32 _sigR,
bytes32 _sigS,
uint8 _sigV
) internal view returns (bool) {
address signer = ecrecover(toTypedMessageHash(_hashedMetaTx), _sigV, _sigR, _sigS);
require(signer != address(0), INVALID_SIGNATURE);
return signer == _user;
}
/**
* @notice Get the domain separator.
*/
function getDomainSeparator() private view returns (bytes32) {
return ProtocolLib.protocolMetaTxInfo().domainSeparator;
}
/**
* @dev Accept message hash and returns hash message in EIP712 compatible form
* So that it can be used to recover signer from signature signed using EIP712 formatted data
* https://eips.ethereum.org/EIPS/eip-712
* "\\x19" makes the encoding deterministic
* "\\x01" is the version byte to make it compatible to EIP-191
*
* @param _messageHash - the message hash.
*/
function toTypedMessageHash(bytes32 _messageHash) internal view returns (bytes32) {
return keccak256(abi.encodePacked("\x19\x01", getDomainSeparator(), _messageHash));
}
/**
* @notice Get the current sender address from storage.
*/
function getCurrentSenderAddress() internal view returns (address) {
return ProtocolLib.protocolMetaTxInfo().currentSenderAddress;
}
/**
* @notice Returns the current sender address.
*/
function msgSender() internal view returns (address) {
bool isItAMetaTransaction = ProtocolLib.protocolMetaTxInfo().isMetaTransaction;
// Get sender from the storage if this is a meta transaction
if (isItAMetaTransaction) {
return getCurrentSenderAddress();
} else {
return msg.sender;
}
}
}