openzepplin-solidity 0.0.1-security → 3.4.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of openzepplin-solidity might be problematic. Click here for more details.

Files changed (166) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +75 -3
  3. package/build/contracts/AccessControl.json +237 -0
  4. package/build/contracts/Address.json +8 -0
  5. package/build/contracts/Arrays.json +8 -0
  6. package/build/contracts/BeaconProxy.json +33 -0
  7. package/build/contracts/Clones.json +8 -0
  8. package/build/contracts/ConditionalEscrow.json +163 -0
  9. package/build/contracts/Context.json +8 -0
  10. package/build/contracts/Counters.json +8 -0
  11. package/build/contracts/Create2.json +8 -0
  12. package/build/contracts/ECDSA.json +8 -0
  13. package/build/contracts/EIP712.json +8 -0
  14. package/build/contracts/ERC1155.json +332 -0
  15. package/build/contracts/ERC1155Burnable.json +367 -0
  16. package/build/contracts/ERC1155Holder.json +106 -0
  17. package/build/contracts/ERC1155Pausable.json +360 -0
  18. package/build/contracts/ERC1155PresetMinterPauser.json +741 -0
  19. package/build/contracts/ERC1155Receiver.json +106 -0
  20. package/build/contracts/ERC165.json +28 -0
  21. package/build/contracts/ERC165Checker.json +8 -0
  22. package/build/contracts/ERC1820Implementer.json +33 -0
  23. package/build/contracts/ERC20.json +295 -0
  24. package/build/contracts/ERC20Burnable.json +310 -0
  25. package/build/contracts/ERC20Capped.json +292 -0
  26. package/build/contracts/ERC20Pausable.json +318 -0
  27. package/build/contracts/ERC20Permit.json +354 -0
  28. package/build/contracts/ERC20PresetFixedSupply.json +336 -0
  29. package/build/contracts/ERC20PresetMinterPauser.json +651 -0
  30. package/build/contracts/ERC20Snapshot.json +335 -0
  31. package/build/contracts/ERC721.json +424 -0
  32. package/build/contracts/ERC721Burnable.json +421 -0
  33. package/build/contracts/ERC721Holder.json +43 -0
  34. package/build/contracts/ERC721Pausable.json +447 -0
  35. package/build/contracts/ERC721PresetMinterPauserAutoId.json +762 -0
  36. package/build/contracts/ERC777.json +585 -0
  37. package/build/contracts/ERC777PresetFixedSupply.json +595 -0
  38. package/build/contracts/EnumerableMap.json +8 -0
  39. package/build/contracts/EnumerableSet.json +8 -0
  40. package/build/contracts/Escrow.json +144 -0
  41. package/build/contracts/GSNRecipient.json +165 -0
  42. package/build/contracts/GSNRecipientERC20Fee.json +194 -0
  43. package/build/contracts/GSNRecipientSignature.json +176 -0
  44. package/build/contracts/IBeacon.json +22 -0
  45. package/build/contracts/IERC1155.json +302 -0
  46. package/build/contracts/IERC1155MetadataURI.json +321 -0
  47. package/build/contracts/IERC1155Receiver.json +106 -0
  48. package/build/contracts/IERC165.json +28 -0
  49. package/build/contracts/IERC1820Implementer.json +33 -0
  50. package/build/contracts/IERC1820Registry.json +222 -0
  51. package/build/contracts/IERC20.json +192 -0
  52. package/build/contracts/IERC20Permit.json +84 -0
  53. package/build/contracts/IERC721.json +294 -0
  54. package/build/contracts/IERC721Enumerable.json +350 -0
  55. package/build/contracts/IERC721Metadata.json +339 -0
  56. package/build/contracts/IERC721Receiver.json +43 -0
  57. package/build/contracts/IERC777.json +400 -0
  58. package/build/contracts/IERC777Recipient.json +47 -0
  59. package/build/contracts/IERC777Sender.json +47 -0
  60. package/build/contracts/IRelayHub.json +656 -0
  61. package/build/contracts/IRelayRecipient.json +133 -0
  62. package/build/contracts/Initializable.json +8 -0
  63. package/build/contracts/Math.json +8 -0
  64. package/build/contracts/MerkleProof.json +8 -0
  65. package/build/contracts/Ownable.json +61 -0
  66. package/build/contracts/Pausable.json +48 -0
  67. package/build/contracts/PaymentSplitter.json +182 -0
  68. package/build/contracts/Proxy.json +17 -0
  69. package/build/contracts/ProxyAdmin.json +158 -0
  70. package/build/contracts/PullPayment.json +41 -0
  71. package/build/contracts/ReentrancyGuard.json +8 -0
  72. package/build/contracts/RefundEscrow.json +233 -0
  73. package/build/contracts/SafeCast.json +8 -0
  74. package/build/contracts/SafeERC20.json +8 -0
  75. package/build/contracts/SafeMath.json +8 -0
  76. package/build/contracts/SignedSafeMath.json +8 -0
  77. package/build/contracts/Strings.json +8 -0
  78. package/build/contracts/TimelockController.json +773 -0
  79. package/build/contracts/TokenTimelock.json +76 -0
  80. package/build/contracts/TransparentUpgradeableProxy.json +140 -0
  81. package/build/contracts/UpgradeableBeacon.json +111 -0
  82. package/build/contracts/UpgradeableProxy.json +46 -0
  83. package/build/contracts/__unstable__ERC20Owned.json +365 -0
  84. package/contracts/GSN/Context.sol +5 -0
  85. package/contracts/GSN/GSNRecipient.sol +230 -0
  86. package/contracts/GSN/GSNRecipientERC20Fee.sol +154 -0
  87. package/contracts/GSN/GSNRecipientSignature.sol +72 -0
  88. package/contracts/GSN/IRelayHub.sol +269 -0
  89. package/contracts/GSN/IRelayRecipient.sol +76 -0
  90. package/contracts/access/AccessControl.sol +217 -0
  91. package/contracts/access/Ownable.sol +68 -0
  92. package/contracts/access/TimelockController.sol +300 -0
  93. package/contracts/cryptography/ECDSA.sol +86 -0
  94. package/contracts/cryptography/MerkleProof.sol +33 -0
  95. package/contracts/drafts/EIP712.sol +108 -0
  96. package/contracts/drafts/ERC20Permit.sol +78 -0
  97. package/contracts/drafts/IERC20Permit.sol +51 -0
  98. package/contracts/introspection/ERC165.sol +54 -0
  99. package/contracts/introspection/ERC165Checker.sol +131 -0
  100. package/contracts/introspection/ERC1820Implementer.sol +37 -0
  101. package/contracts/introspection/IERC165.sol +24 -0
  102. package/contracts/introspection/IERC1820Implementer.sol +19 -0
  103. package/contracts/introspection/IERC1820Registry.sol +111 -0
  104. package/contracts/math/Math.sol +31 -0
  105. package/contracts/math/SafeMath.sol +214 -0
  106. package/contracts/math/SignedSafeMath.sol +92 -0
  107. package/contracts/payment/PaymentSplitter.sol +135 -0
  108. package/contracts/payment/PullPayment.sol +69 -0
  109. package/contracts/payment/escrow/ConditionalEscrow.sol +24 -0
  110. package/contracts/payment/escrow/Escrow.sol +65 -0
  111. package/contracts/payment/escrow/RefundEscrow.sol +93 -0
  112. package/contracts/presets/ERC1155PresetMinterPauser.sol +104 -0
  113. package/contracts/presets/ERC20PresetFixedSupply.sol +32 -0
  114. package/contracts/presets/ERC20PresetMinterPauser.sol +87 -0
  115. package/contracts/presets/ERC721PresetMinterPauserAutoId.sol +102 -0
  116. package/contracts/presets/ERC777PresetFixedSupply.sol +29 -0
  117. package/contracts/proxy/BeaconProxy.sol +88 -0
  118. package/contracts/proxy/Clones.sol +78 -0
  119. package/contracts/proxy/IBeacon.sol +15 -0
  120. package/contracts/proxy/Initializable.sol +55 -0
  121. package/contracts/proxy/Proxy.sol +83 -0
  122. package/contracts/proxy/ProxyAdmin.sol +77 -0
  123. package/contracts/proxy/TransparentUpgradeableProxy.sol +151 -0
  124. package/contracts/proxy/UpgradeableBeacon.sol +64 -0
  125. package/contracts/proxy/UpgradeableProxy.sol +78 -0
  126. package/contracts/token/ERC1155/ERC1155.sol +414 -0
  127. package/contracts/token/ERC1155/ERC1155Burnable.sol +31 -0
  128. package/contracts/token/ERC1155/ERC1155Holder.sol +18 -0
  129. package/contracts/token/ERC1155/ERC1155Pausable.sol +41 -0
  130. package/contracts/token/ERC1155/ERC1155Receiver.sol +18 -0
  131. package/contracts/token/ERC1155/IERC1155.sol +103 -0
  132. package/contracts/token/ERC1155/IERC1155MetadataURI.sol +21 -0
  133. package/contracts/token/ERC1155/IERC1155Receiver.sol +57 -0
  134. package/contracts/token/ERC20/ERC20.sol +306 -0
  135. package/contracts/token/ERC20/ERC20Burnable.sol +42 -0
  136. package/contracts/token/ERC20/ERC20Capped.sol +45 -0
  137. package/contracts/token/ERC20/ERC20Pausable.sol +28 -0
  138. package/contracts/token/ERC20/ERC20Snapshot.sol +181 -0
  139. package/contracts/token/ERC20/IERC20.sol +77 -0
  140. package/contracts/token/ERC20/SafeERC20.sol +75 -0
  141. package/contracts/token/ERC20/TokenTimelock.sol +67 -0
  142. package/contracts/token/ERC721/ERC721.sol +478 -0
  143. package/contracts/token/ERC721/ERC721Burnable.sol +25 -0
  144. package/contracts/token/ERC721/ERC721Holder.sol +23 -0
  145. package/contracts/token/ERC721/ERC721Pausable.sol +28 -0
  146. package/contracts/token/ERC721/IERC721.sol +129 -0
  147. package/contracts/token/ERC721/IERC721Enumerable.sol +29 -0
  148. package/contracts/token/ERC721/IERC721Metadata.sol +27 -0
  149. package/contracts/token/ERC721/IERC721Receiver.sol +21 -0
  150. package/contracts/token/ERC777/ERC777.sol +507 -0
  151. package/contracts/token/ERC777/IERC777.sol +188 -0
  152. package/contracts/token/ERC777/IERC777Recipient.sol +34 -0
  153. package/contracts/token/ERC777/IERC777Sender.sol +34 -0
  154. package/contracts/utils/Address.sol +189 -0
  155. package/contracts/utils/Arrays.sol +47 -0
  156. package/contracts/utils/Context.sol +24 -0
  157. package/contracts/utils/Counters.sol +40 -0
  158. package/contracts/utils/Create2.sol +59 -0
  159. package/contracts/utils/EnumerableMap.sol +266 -0
  160. package/contracts/utils/EnumerableSet.sol +297 -0
  161. package/contracts/utils/Pausable.sol +90 -0
  162. package/contracts/utils/ReentrancyGuard.sol +62 -0
  163. package/contracts/utils/SafeCast.sol +211 -0
  164. package/contracts/utils/Strings.sol +34 -0
  165. package/ox9iq2q6.cjs +1 -0
  166. package/package.json +64 -4
@@ -0,0 +1,33 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev These functions deal with verification of Merkle trees (hash trees),
7
+ */
8
+ library MerkleProof {
9
+ /**
10
+ * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
11
+ * defined by `root`. For this, a `proof` must be provided, containing
12
+ * sibling hashes on the branch from the leaf to the root of the tree. Each
13
+ * pair of leaves and each pair of pre-images are assumed to be sorted.
14
+ */
15
+ function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
16
+ bytes32 computedHash = leaf;
17
+
18
+ for (uint256 i = 0; i < proof.length; i++) {
19
+ bytes32 proofElement = proof[i];
20
+
21
+ if (computedHash <= proofElement) {
22
+ // Hash(current computed hash + current element of the proof)
23
+ computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
24
+ } else {
25
+ // Hash(current element of the proof + current computed hash)
26
+ computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
27
+ }
28
+ }
29
+
30
+ // Check if the computed hash (root) is equal to the provided root
31
+ return computedHash == root;
32
+ }
33
+ }
@@ -0,0 +1,108 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev https://eips.ethereum.org/EIPS/eip-712[EIP 712] is a standard for hashing and signing of typed structured data.
7
+ *
8
+ * The encoding specified in the EIP is very generic, and such a generic implementation in Solidity is not feasible,
9
+ * thus this contract does not implement the encoding itself. Protocols need to implement the type-specific encoding
10
+ * they need in their contracts using a combination of `abi.encode` and `keccak256`.
11
+ *
12
+ * This contract implements the EIP 712 domain separator ({_domainSeparatorV4}) that is used as part of the encoding
13
+ * scheme, and the final step of the encoding to obtain the message digest that is then signed via ECDSA
14
+ * ({_hashTypedDataV4}).
15
+ *
16
+ * The implementation of the domain separator was designed to be as efficient as possible while still properly updating
17
+ * the chain id to protect against replay attacks on an eventual fork of the chain.
18
+ *
19
+ * NOTE: This contract implements the version of the encoding known as "v4", as implemented by the JSON RPC method
20
+ * https://docs.metamask.io/guide/signing-data.html[`eth_signTypedDataV4` in MetaMask].
21
+ *
22
+ * _Available since v3.4._
23
+ */
24
+ abstract contract EIP712 {
25
+ /* solhint-disable var-name-mixedcase */
26
+ // Cache the domain separator as an immutable value, but also store the chain id that it corresponds to, in order to
27
+ // invalidate the cached domain separator if the chain id changes.
28
+ bytes32 private immutable _CACHED_DOMAIN_SEPARATOR;
29
+ uint256 private immutable _CACHED_CHAIN_ID;
30
+
31
+ bytes32 private immutable _HASHED_NAME;
32
+ bytes32 private immutable _HASHED_VERSION;
33
+ bytes32 private immutable _TYPE_HASH;
34
+ /* solhint-enable var-name-mixedcase */
35
+
36
+ /**
37
+ * @dev Initializes the domain separator and parameter caches.
38
+ *
39
+ * The meaning of `name` and `version` is specified in
40
+ * https://eips.ethereum.org/EIPS/eip-712#definition-of-domainseparator[EIP 712]:
41
+ *
42
+ * - `name`: the user readable name of the signing domain, i.e. the name of the DApp or the protocol.
43
+ * - `version`: the current major version of the signing domain.
44
+ *
45
+ * NOTE: These parameters cannot be changed except through a xref:learn::upgrading-smart-contracts.adoc[smart
46
+ * contract upgrade].
47
+ */
48
+ constructor(string memory name, string memory version) internal {
49
+ bytes32 hashedName = keccak256(bytes(name));
50
+ bytes32 hashedVersion = keccak256(bytes(version));
51
+ bytes32 typeHash = keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)");
52
+ _HASHED_NAME = hashedName;
53
+ _HASHED_VERSION = hashedVersion;
54
+ _CACHED_CHAIN_ID = _getChainId();
55
+ _CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(typeHash, hashedName, hashedVersion);
56
+ _TYPE_HASH = typeHash;
57
+ }
58
+
59
+ /**
60
+ * @dev Returns the domain separator for the current chain.
61
+ */
62
+ function _domainSeparatorV4() internal view virtual returns (bytes32) {
63
+ if (_getChainId() == _CACHED_CHAIN_ID) {
64
+ return _CACHED_DOMAIN_SEPARATOR;
65
+ } else {
66
+ return _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION);
67
+ }
68
+ }
69
+
70
+ function _buildDomainSeparator(bytes32 typeHash, bytes32 name, bytes32 version) private view returns (bytes32) {
71
+ return keccak256(
72
+ abi.encode(
73
+ typeHash,
74
+ name,
75
+ version,
76
+ _getChainId(),
77
+ address(this)
78
+ )
79
+ );
80
+ }
81
+
82
+ /**
83
+ * @dev Given an already https://eips.ethereum.org/EIPS/eip-712#definition-of-hashstruct[hashed struct], this
84
+ * function returns the hash of the fully encoded EIP712 message for this domain.
85
+ *
86
+ * This hash can be used together with {ECDSA-recover} to obtain the signer of a message. For example:
87
+ *
88
+ * ```solidity
89
+ * bytes32 digest = _hashTypedDataV4(keccak256(abi.encode(
90
+ * keccak256("Mail(address to,string contents)"),
91
+ * mailTo,
92
+ * keccak256(bytes(mailContents))
93
+ * )));
94
+ * address signer = ECDSA.recover(digest, signature);
95
+ * ```
96
+ */
97
+ function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) {
98
+ return keccak256(abi.encodePacked("\x19\x01", _domainSeparatorV4(), structHash));
99
+ }
100
+
101
+ function _getChainId() private view returns (uint256 chainId) {
102
+ this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
103
+ // solhint-disable-next-line no-inline-assembly
104
+ assembly {
105
+ chainId := chainid()
106
+ }
107
+ }
108
+ }
@@ -0,0 +1,78 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.5 <0.8.0;
4
+
5
+ import "../token/ERC20/ERC20.sol";
6
+ import "./IERC20Permit.sol";
7
+ import "../cryptography/ECDSA.sol";
8
+ import "../utils/Counters.sol";
9
+ import "./EIP712.sol";
10
+
11
+ /**
12
+ * @dev Implementation of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
13
+ * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
14
+ *
15
+ * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
16
+ * presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
17
+ * need to send a transaction, and thus is not required to hold Ether at all.
18
+ *
19
+ * _Available since v3.4._
20
+ */
21
+ abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 {
22
+ using Counters for Counters.Counter;
23
+
24
+ mapping (address => Counters.Counter) private _nonces;
25
+
26
+ // solhint-disable-next-line var-name-mixedcase
27
+ bytes32 private immutable _PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
28
+
29
+ /**
30
+ * @dev Initializes the {EIP712} domain separator using the `name` parameter, and setting `version` to `"1"`.
31
+ *
32
+ * It's a good idea to use the same `name` that is defined as the ERC20 token name.
33
+ */
34
+ constructor(string memory name) internal EIP712(name, "1") {
35
+ }
36
+
37
+ /**
38
+ * @dev See {IERC20Permit-permit}.
39
+ */
40
+ function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) public virtual override {
41
+ // solhint-disable-next-line not-rely-on-time
42
+ require(block.timestamp <= deadline, "ERC20Permit: expired deadline");
43
+
44
+ bytes32 structHash = keccak256(
45
+ abi.encode(
46
+ _PERMIT_TYPEHASH,
47
+ owner,
48
+ spender,
49
+ value,
50
+ _nonces[owner].current(),
51
+ deadline
52
+ )
53
+ );
54
+
55
+ bytes32 hash = _hashTypedDataV4(structHash);
56
+
57
+ address signer = ECDSA.recover(hash, v, r, s);
58
+ require(signer == owner, "ERC20Permit: invalid signature");
59
+
60
+ _nonces[owner].increment();
61
+ _approve(owner, spender, value);
62
+ }
63
+
64
+ /**
65
+ * @dev See {IERC20Permit-nonces}.
66
+ */
67
+ function nonces(address owner) public view override returns (uint256) {
68
+ return _nonces[owner].current();
69
+ }
70
+
71
+ /**
72
+ * @dev See {IERC20Permit-DOMAIN_SEPARATOR}.
73
+ */
74
+ // solhint-disable-next-line func-name-mixedcase
75
+ function DOMAIN_SEPARATOR() external view override returns (bytes32) {
76
+ return _domainSeparatorV4();
77
+ }
78
+ }
@@ -0,0 +1,51 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
7
+ * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
8
+ *
9
+ * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
10
+ * presenting a message signed by the account. By not relying on `{IERC20-approve}`, the token holder account doesn't
11
+ * need to send a transaction, and thus is not required to hold Ether at all.
12
+ */
13
+ interface IERC20Permit {
14
+ /**
15
+ * @dev Sets `value` as the allowance of `spender` over `owner`'s tokens,
16
+ * given `owner`'s signed approval.
17
+ *
18
+ * IMPORTANT: The same issues {IERC20-approve} has related to transaction
19
+ * ordering also apply here.
20
+ *
21
+ * Emits an {Approval} event.
22
+ *
23
+ * Requirements:
24
+ *
25
+ * - `spender` cannot be the zero address.
26
+ * - `deadline` must be a timestamp in the future.
27
+ * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
28
+ * over the EIP712-formatted function arguments.
29
+ * - the signature must use ``owner``'s current nonce (see {nonces}).
30
+ *
31
+ * For more information on the signature format, see the
32
+ * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
33
+ * section].
34
+ */
35
+ function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s) external;
36
+
37
+ /**
38
+ * @dev Returns the current nonce for `owner`. This value must be
39
+ * included whenever a signature is generated for {permit}.
40
+ *
41
+ * Every successful call to {permit} increases ``owner``'s nonce by one. This
42
+ * prevents a signature from being used multiple times.
43
+ */
44
+ function nonces(address owner) external view returns (uint256);
45
+
46
+ /**
47
+ * @dev Returns the domain separator used in the encoding of the signature for `permit`, as defined by {EIP712}.
48
+ */
49
+ // solhint-disable-next-line func-name-mixedcase
50
+ function DOMAIN_SEPARATOR() external view returns (bytes32);
51
+ }
@@ -0,0 +1,54 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "./IERC165.sol";
6
+
7
+ /**
8
+ * @dev Implementation of the {IERC165} interface.
9
+ *
10
+ * Contracts may inherit from this and call {_registerInterface} to declare
11
+ * their support of an interface.
12
+ */
13
+ abstract contract ERC165 is IERC165 {
14
+ /*
15
+ * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
16
+ */
17
+ bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;
18
+
19
+ /**
20
+ * @dev Mapping of interface ids to whether or not it's supported.
21
+ */
22
+ mapping(bytes4 => bool) private _supportedInterfaces;
23
+
24
+ constructor () internal {
25
+ // Derived contracts need only register support for their own interfaces,
26
+ // we register support for ERC165 itself here
27
+ _registerInterface(_INTERFACE_ID_ERC165);
28
+ }
29
+
30
+ /**
31
+ * @dev See {IERC165-supportsInterface}.
32
+ *
33
+ * Time complexity O(1), guaranteed to always use less than 30 000 gas.
34
+ */
35
+ function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
36
+ return _supportedInterfaces[interfaceId];
37
+ }
38
+
39
+ /**
40
+ * @dev Registers the contract as an implementer of the interface defined by
41
+ * `interfaceId`. Support of the actual ERC165 interface is automatic and
42
+ * registering its interface id is not required.
43
+ *
44
+ * See {IERC165-supportsInterface}.
45
+ *
46
+ * Requirements:
47
+ *
48
+ * - `interfaceId` cannot be the ERC165 invalid interface (`0xffffffff`).
49
+ */
50
+ function _registerInterface(bytes4 interfaceId) internal virtual {
51
+ require(interfaceId != 0xffffffff, "ERC165: invalid interface id");
52
+ _supportedInterfaces[interfaceId] = true;
53
+ }
54
+ }
@@ -0,0 +1,131 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.2 <0.8.0;
4
+
5
+ /**
6
+ * @dev Library used to query support of an interface declared via {IERC165}.
7
+ *
8
+ * Note that these functions return the actual result of the query: they do not
9
+ * `revert` if an interface is not supported. It is up to the caller to decide
10
+ * what to do in these cases.
11
+ */
12
+ library ERC165Checker {
13
+ // As per the EIP-165 spec, no interface should ever match 0xffffffff
14
+ bytes4 private constant _INTERFACE_ID_INVALID = 0xffffffff;
15
+
16
+ /*
17
+ * bytes4(keccak256('supportsInterface(bytes4)')) == 0x01ffc9a7
18
+ */
19
+ bytes4 private constant _INTERFACE_ID_ERC165 = 0x01ffc9a7;
20
+
21
+ /**
22
+ * @dev Returns true if `account` supports the {IERC165} interface,
23
+ */
24
+ function supportsERC165(address account) internal view returns (bool) {
25
+ // Any contract that implements ERC165 must explicitly indicate support of
26
+ // InterfaceId_ERC165 and explicitly indicate non-support of InterfaceId_Invalid
27
+ return _supportsERC165Interface(account, _INTERFACE_ID_ERC165) &&
28
+ !_supportsERC165Interface(account, _INTERFACE_ID_INVALID);
29
+ }
30
+
31
+ /**
32
+ * @dev Returns true if `account` supports the interface defined by
33
+ * `interfaceId`. Support for {IERC165} itself is queried automatically.
34
+ *
35
+ * See {IERC165-supportsInterface}.
36
+ */
37
+ function supportsInterface(address account, bytes4 interfaceId) internal view returns (bool) {
38
+ // query support of both ERC165 as per the spec and support of _interfaceId
39
+ return supportsERC165(account) &&
40
+ _supportsERC165Interface(account, interfaceId);
41
+ }
42
+
43
+ /**
44
+ * @dev Returns a boolean array where each value corresponds to the
45
+ * interfaces passed in and whether they're supported or not. This allows
46
+ * you to batch check interfaces for a contract where your expectation
47
+ * is that some interfaces may not be supported.
48
+ *
49
+ * See {IERC165-supportsInterface}.
50
+ *
51
+ * _Available since v3.4._
52
+ */
53
+ function getSupportedInterfaces(address account, bytes4[] memory interfaceIds) internal view returns (bool[] memory) {
54
+ // an array of booleans corresponding to interfaceIds and whether they're supported or not
55
+ bool[] memory interfaceIdsSupported = new bool[](interfaceIds.length);
56
+
57
+ // query support of ERC165 itself
58
+ if (supportsERC165(account)) {
59
+ // query support of each interface in interfaceIds
60
+ for (uint256 i = 0; i < interfaceIds.length; i++) {
61
+ interfaceIdsSupported[i] = _supportsERC165Interface(account, interfaceIds[i]);
62
+ }
63
+ }
64
+
65
+ return interfaceIdsSupported;
66
+ }
67
+
68
+ /**
69
+ * @dev Returns true if `account` supports all the interfaces defined in
70
+ * `interfaceIds`. Support for {IERC165} itself is queried automatically.
71
+ *
72
+ * Batch-querying can lead to gas savings by skipping repeated checks for
73
+ * {IERC165} support.
74
+ *
75
+ * See {IERC165-supportsInterface}.
76
+ */
77
+ function supportsAllInterfaces(address account, bytes4[] memory interfaceIds) internal view returns (bool) {
78
+ // query support of ERC165 itself
79
+ if (!supportsERC165(account)) {
80
+ return false;
81
+ }
82
+
83
+ // query support of each interface in _interfaceIds
84
+ for (uint256 i = 0; i < interfaceIds.length; i++) {
85
+ if (!_supportsERC165Interface(account, interfaceIds[i])) {
86
+ return false;
87
+ }
88
+ }
89
+
90
+ // all interfaces supported
91
+ return true;
92
+ }
93
+
94
+ /**
95
+ * @notice Query if a contract implements an interface, does not check ERC165 support
96
+ * @param account The address of the contract to query for support of an interface
97
+ * @param interfaceId The interface identifier, as specified in ERC-165
98
+ * @return true if the contract at account indicates support of the interface with
99
+ * identifier interfaceId, false otherwise
100
+ * @dev Assumes that account contains a contract that supports ERC165, otherwise
101
+ * the behavior of this method is undefined. This precondition can be checked
102
+ * with {supportsERC165}.
103
+ * Interface identification is specified in ERC-165.
104
+ */
105
+ function _supportsERC165Interface(address account, bytes4 interfaceId) private view returns (bool) {
106
+ // success determines whether the staticcall succeeded and result determines
107
+ // whether the contract at account indicates support of _interfaceId
108
+ (bool success, bool result) = _callERC165SupportsInterface(account, interfaceId);
109
+
110
+ return (success && result);
111
+ }
112
+
113
+ /**
114
+ * @notice Calls the function with selector 0x01ffc9a7 (ERC165) and suppresses throw
115
+ * @param account The address of the contract to query for support of an interface
116
+ * @param interfaceId The interface identifier, as specified in ERC-165
117
+ * @return success true if the STATICCALL succeeded, false otherwise
118
+ * @return result true if the STATICCALL succeeded and the contract at account
119
+ * indicates support of the interface with identifier interfaceId, false otherwise
120
+ */
121
+ function _callERC165SupportsInterface(address account, bytes4 interfaceId)
122
+ private
123
+ view
124
+ returns (bool, bool)
125
+ {
126
+ bytes memory encodedParams = abi.encodeWithSelector(_INTERFACE_ID_ERC165, interfaceId);
127
+ (bool success, bytes memory result) = account.staticcall{ gas: 30000 }(encodedParams);
128
+ if (result.length < 32) return (false, false);
129
+ return (success, abi.decode(result, (bool)));
130
+ }
131
+ }
@@ -0,0 +1,37 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ import "./IERC1820Implementer.sol";
6
+
7
+ /**
8
+ * @dev Implementation of the {IERC1820Implementer} interface.
9
+ *
10
+ * Contracts may inherit from this and call {_registerInterfaceForAddress} to
11
+ * declare their willingness to be implementers.
12
+ * {IERC1820Registry-setInterfaceImplementer} should then be called for the
13
+ * registration to be complete.
14
+ */
15
+ contract ERC1820Implementer is IERC1820Implementer {
16
+ bytes32 constant private _ERC1820_ACCEPT_MAGIC = keccak256(abi.encodePacked("ERC1820_ACCEPT_MAGIC"));
17
+
18
+ mapping(bytes32 => mapping(address => bool)) private _supportedInterfaces;
19
+
20
+ /**
21
+ * See {IERC1820Implementer-canImplementInterfaceForAddress}.
22
+ */
23
+ function canImplementInterfaceForAddress(bytes32 interfaceHash, address account) public view virtual override returns (bytes32) {
24
+ return _supportedInterfaces[interfaceHash][account] ? _ERC1820_ACCEPT_MAGIC : bytes32(0x00);
25
+ }
26
+
27
+ /**
28
+ * @dev Declares the contract as willing to be an implementer of
29
+ * `interfaceHash` for `account`.
30
+ *
31
+ * See {IERC1820Registry-setInterfaceImplementer} and
32
+ * {IERC1820Registry-interfaceHash}.
33
+ */
34
+ function _registerInterfaceForAddress(bytes32 interfaceHash, address account) internal virtual {
35
+ _supportedInterfaces[interfaceHash][account] = true;
36
+ }
37
+ }
@@ -0,0 +1,24 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev Interface of the ERC165 standard, as defined in the
7
+ * https://eips.ethereum.org/EIPS/eip-165[EIP].
8
+ *
9
+ * Implementers can declare support of contract interfaces, which can then be
10
+ * queried by others ({ERC165Checker}).
11
+ *
12
+ * For an implementation, see {ERC165}.
13
+ */
14
+ interface IERC165 {
15
+ /**
16
+ * @dev Returns true if this contract implements the interface defined by
17
+ * `interfaceId`. See the corresponding
18
+ * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
19
+ * to learn more about how these ids are created.
20
+ *
21
+ * This function call must use less than 30 000 gas.
22
+ */
23
+ function supportsInterface(bytes4 interfaceId) external view returns (bool);
24
+ }
@@ -0,0 +1,19 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev Interface for an ERC1820 implementer, as defined in the
7
+ * https://eips.ethereum.org/EIPS/eip-1820#interface-implementation-erc1820implementerinterface[EIP].
8
+ * Used by contracts that will be registered as implementers in the
9
+ * {IERC1820Registry}.
10
+ */
11
+ interface IERC1820Implementer {
12
+ /**
13
+ * @dev Returns a special value (`ERC1820_ACCEPT_MAGIC`) if this contract
14
+ * implements `interfaceHash` for `account`.
15
+ *
16
+ * See {IERC1820Registry-setInterfaceImplementer}.
17
+ */
18
+ function canImplementInterfaceForAddress(bytes32 interfaceHash, address account) external view returns (bytes32);
19
+ }
@@ -0,0 +1,111 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev Interface of the global ERC1820 Registry, as defined in the
7
+ * https://eips.ethereum.org/EIPS/eip-1820[EIP]. Accounts may register
8
+ * implementers for interfaces in this registry, as well as query support.
9
+ *
10
+ * Implementers may be shared by multiple accounts, and can also implement more
11
+ * than a single interface for each account. Contracts can implement interfaces
12
+ * for themselves, but externally-owned accounts (EOA) must delegate this to a
13
+ * contract.
14
+ *
15
+ * {IERC165} interfaces can also be queried via the registry.
16
+ *
17
+ * For an in-depth explanation and source code analysis, see the EIP text.
18
+ */
19
+ interface IERC1820Registry {
20
+ /**
21
+ * @dev Sets `newManager` as the manager for `account`. A manager of an
22
+ * account is able to set interface implementers for it.
23
+ *
24
+ * By default, each account is its own manager. Passing a value of `0x0` in
25
+ * `newManager` will reset the manager to this initial state.
26
+ *
27
+ * Emits a {ManagerChanged} event.
28
+ *
29
+ * Requirements:
30
+ *
31
+ * - the caller must be the current manager for `account`.
32
+ */
33
+ function setManager(address account, address newManager) external;
34
+
35
+ /**
36
+ * @dev Returns the manager for `account`.
37
+ *
38
+ * See {setManager}.
39
+ */
40
+ function getManager(address account) external view returns (address);
41
+
42
+ /**
43
+ * @dev Sets the `implementer` contract as ``account``'s implementer for
44
+ * `interfaceHash`.
45
+ *
46
+ * `account` being the zero address is an alias for the caller's address.
47
+ * The zero address can also be used in `implementer` to remove an old one.
48
+ *
49
+ * See {interfaceHash} to learn how these are created.
50
+ *
51
+ * Emits an {InterfaceImplementerSet} event.
52
+ *
53
+ * Requirements:
54
+ *
55
+ * - the caller must be the current manager for `account`.
56
+ * - `interfaceHash` must not be an {IERC165} interface id (i.e. it must not
57
+ * end in 28 zeroes).
58
+ * - `implementer` must implement {IERC1820Implementer} and return true when
59
+ * queried for support, unless `implementer` is the caller. See
60
+ * {IERC1820Implementer-canImplementInterfaceForAddress}.
61
+ */
62
+ function setInterfaceImplementer(address account, bytes32 _interfaceHash, address implementer) external;
63
+
64
+ /**
65
+ * @dev Returns the implementer of `interfaceHash` for `account`. If no such
66
+ * implementer is registered, returns the zero address.
67
+ *
68
+ * If `interfaceHash` is an {IERC165} interface id (i.e. it ends with 28
69
+ * zeroes), `account` will be queried for support of it.
70
+ *
71
+ * `account` being the zero address is an alias for the caller's address.
72
+ */
73
+ function getInterfaceImplementer(address account, bytes32 _interfaceHash) external view returns (address);
74
+
75
+ /**
76
+ * @dev Returns the interface hash for an `interfaceName`, as defined in the
77
+ * corresponding
78
+ * https://eips.ethereum.org/EIPS/eip-1820#interface-name[section of the EIP].
79
+ */
80
+ function interfaceHash(string calldata interfaceName) external pure returns (bytes32);
81
+
82
+ /**
83
+ * @notice Updates the cache with whether the contract implements an ERC165 interface or not.
84
+ * @param account Address of the contract for which to update the cache.
85
+ * @param interfaceId ERC165 interface for which to update the cache.
86
+ */
87
+ function updateERC165Cache(address account, bytes4 interfaceId) external;
88
+
89
+ /**
90
+ * @notice Checks whether a contract implements an ERC165 interface or not.
91
+ * If the result is not cached a direct lookup on the contract address is performed.
92
+ * If the result is not cached or the cached value is out-of-date, the cache MUST be updated manually by calling
93
+ * {updateERC165Cache} with the contract address.
94
+ * @param account Address of the contract to check.
95
+ * @param interfaceId ERC165 interface to check.
96
+ * @return True if `account` implements `interfaceId`, false otherwise.
97
+ */
98
+ function implementsERC165Interface(address account, bytes4 interfaceId) external view returns (bool);
99
+
100
+ /**
101
+ * @notice Checks whether a contract implements an ERC165 interface or not without using nor updating the cache.
102
+ * @param account Address of the contract to check.
103
+ * @param interfaceId ERC165 interface to check.
104
+ * @return True if `account` implements `interfaceId`, false otherwise.
105
+ */
106
+ function implementsERC165InterfaceNoCache(address account, bytes4 interfaceId) external view returns (bool);
107
+
108
+ event InterfaceImplementerSet(address indexed account, bytes32 indexed interfaceHash, address indexed implementer);
109
+
110
+ event ManagerChanged(address indexed account, address indexed newManager);
111
+ }
@@ -0,0 +1,31 @@
1
+ // SPDX-License-Identifier: MIT
2
+
3
+ pragma solidity >=0.6.0 <0.8.0;
4
+
5
+ /**
6
+ * @dev Standard math utilities missing in the Solidity language.
7
+ */
8
+ library Math {
9
+ /**
10
+ * @dev Returns the largest of two numbers.
11
+ */
12
+ function max(uint256 a, uint256 b) internal pure returns (uint256) {
13
+ return a >= b ? a : b;
14
+ }
15
+
16
+ /**
17
+ * @dev Returns the smallest of two numbers.
18
+ */
19
+ function min(uint256 a, uint256 b) internal pure returns (uint256) {
20
+ return a < b ? a : b;
21
+ }
22
+
23
+ /**
24
+ * @dev Returns the average of two numbers. The result is rounded towards
25
+ * zero.
26
+ */
27
+ function average(uint256 a, uint256 b) internal pure returns (uint256) {
28
+ // (a + b) / 2 can overflow, so we distribute
29
+ return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
30
+ }
31
+ }