mirror of openzeppelin-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openzeppelin-contracts/docs/modules/ROOT/pages/utilities.adoc

96 lines
5.2 KiB

migrate content to format for new docs site Squashed commit of the following: commit fcf35eb806100de300bd9803ce3150dde1ecc424 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:16:04 2019 -0300 remove all docsite dependency commit eeaee9a9d43d70704f6ab17b5126ddbd52b93a50 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:15:23 2019 -0300 update solidity-docgen commit f021ff951829ea0c155186749819403c6b76e803 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:05:06 2019 -0300 update docsite script for new setup commit ff887699d381cfbbe3acf1f1c0de8e22b58480f3 Merge: c938aa1d 84f85a41 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 16:46:46 2019 -0300 Merge branch 'master' into antora commit c938aa1d9ed05ac83a34e2cebd8353f8331ad6d6 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jul 16 18:24:29 2019 -0300 make component name shorter commit 5bbd6931e02cbbd8864c82655ad0f390ceead5f3 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 20:16:17 2019 -0300 add all info to docs templates commit 39682c4515d7cf0f0368ed557f50d2709174208a Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 20:13:49 2019 -0300 fix npm docsite script commit 7ae46bd4a0437abf66150d54d05adf46e3de2cab Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 18:48:05 2019 -0300 convert inline docs to asciidoc commit cfdfd3dee4b4bf582fde22c8cb6e17a603d6e0c8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:34:52 2019 -0300 add missing contract names in readmes commit 15b6a2f9bfb546cf1d3bf4f104278b118bf1b3f4 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:16:47 2019 -0300 fix script path commit 80d82b909f9460d1450d401f00b3f309da506b29 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:13:53 2019 -0300 update version of solidity-docgen commit a870b6c607b9c2d0012f8a60a4ed1a1c8b7e8ebd Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:03:53 2019 -0300 add nav generation of api ref commit 069cff4a25b83752650b54b86d85608c2f547e5e Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 16:32:14 2019 -0300 initial migration to asciidoc and new docgen version commit 55216eed0a6551da913c8d1da4b2a0d0d3faa1a8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 20:39:35 2019 -0300 add basic api doc example commit 0cbe50ce2173b6d1d9a698329d91220f58822a53 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 19:31:31 2019 -0300 add sidebars commit 256fc942845307258ac9dc25aace48117fa10f79 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:22:38 2019 -0300 add page titles commit f4d0effa70e1fc0662729863e8ee72a8821bc458 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:19:41 2019 -0300 add contracts index file commit b73b06359979f7d933df7f2b283c50cb1c31b2a0 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:14:52 2019 -0300 fix header levels commit fb57d9b820f09a1b7c04eed1a205be0e45866cac Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:11:47 2019 -0300 switch format to preferred asciidoctor format commit 032181d8804137332c71534753929d080a31a71f Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:05:38 2019 -0300 initialize antora component and convert docs to asciidoc
6 years ago
= Utilities
OpenZeppelin provides a ton of useful utilities that you can use in your project. Here are some of the more popular ones:
[[cryptography]]
== Cryptography
* link:api/cryptography#ecdsa[`ECDSA`] — provides functions for recovering and managing Ethereum account ECDSA signatures:
* to use it, declare: `using ECDSA for bytes32;`
* signatures are tightly packed, 65 byte `bytes` that look like `{v (1)} {r (32)} {s (32)}`
** this is the default from `web3.eth.sign` so you probably don't need to worry about this format
* recover the signer using link:api/cryptography#ECDSA.recover(bytes32,bytes)[`myDataHash.recover(signature)`]
* if you are using `eth_personalSign`, the signer will hash your data and then add the prefix `\x19Ethereum Signed Message:\n`, so if you're attempting to recover the signer of an Ethereum signed message hash, you'll want to use link:api/cryptography#ECDSA.toEthSignedMessageHash(bytes32)[`toEthSignedMessageHash`]
Use these functions in combination to verify that a user has signed some information on-chain:
[source,solidity]
----
keccack256(
abi.encodePacked(
someData,
moreData
)
)
.toEthSignedMessageHash()
.recover(signature)
----
* link:api/cryptography#merkleproof[`MerkleProof`] — provides link:api/cryptography#MerkleProof.verify(bytes32%5B%5D,bytes32,bytes32)[`verify`] for verifying merkle proofs.
[[introspection]]
== Introspection
In Solidity, it's frequently helpful to know whether or not a contract supports an interface you'd like to use. ERC165 is a standard that helps do runtime interface detection. OpenZeppelin provides some helpers, both for implementing ERC165 in your contracts and querying other contracts:
* link:api/introspection#ierc165[`IERC165`] — this is the ERC165 interface that defines link:api/introspection#IERC165.supportsInterface(bytes4)[`supportsInterface`]. When implementing ERC165, you'll conform to this interface.
* link:api/introspection#erc165[`ERC165`] — inherit this contract if you'd like to support interface detection using a lookup table in contract storage. You can register interfaces using link:api/introspection#ERC165._registerInterface(bytes4)[`_registerInterface(bytes4)`]: check out example usage as part of the ERC721 implementation.
* link:api/introspection#erc165checker[`ERC165Checker`] — ERC165Checker simplifies the process of checking whether or not a contract supports an interface you care about.
* include with `using ERC165Checker for address;`
* link:api/introspection#ERC165Checker._supportsInterface(address,bytes4)[`myAddress._supportsInterface(bytes4)`]
* link:api/introspection#ERC165Checker._supportsAllInterfaces(address,bytes4%5B%5D)[`myAddress._supportsAllInterfaces(bytes4[])`]
[source,solidity]
----
contract MyContract {
using ERC165Checker for address;
bytes4 private InterfaceId_ERC721 = 0x80ac58cd;
/**
* @dev transfer an ERC721 token from this contract to someone else
*/
function transferERC721(
address token,
address to,
uint256 tokenId
)
public
{
require(token.supportsInterface(InterfaceId_ERC721), "IS_NOT_721_TOKEN");
IERC721(token).transferFrom(address(this), to, tokenId);
}
}
----
[[math]]
== Math
The most popular math related library OpenZeppelin provides is link:api/math#safemath[`SafeMath`], which provides mathematical functions that protect your contract from overflows and underflows.
Include the contract with `using SafeMath for uint256;` and then call the functions:
* `myNumber.add(otherNumber)`
* `myNumber.sub(otherNumber)`
* `myNumber.div(otherNumber)`
* `myNumber.mul(otherNumber)`
* `myNumber.mod(otherNumber)`
Easy!
[[payment]]
== Payment
Want to split some payments between multiple people? Maybe you have an app that sends 30% of art purchases to the original creator and 70% of the profits to the current owner; you can build that with link:api/payment#paymentsplitter[`PaymentSplitter`]!
In solidity, there are some security concerns with blindly sending money to accounts, since it allows them to execute arbitrary code. You can read up on these security concerns in the https://consensys.github.io/smart-contract-best-practices/[Ethereum Smart Contract Best Practices] website. One of the ways to fix reentrancy and stalling problems is, instead of immediately sending Ether to accounts that need it, you can use link:api/payment#pullpayment[`PullPayment`], which offers an link:api/payment#PullPayment._asyncTransfer(address,uint256)[`_asyncTransfer`] function for sending money to something and requesting that they link:api/payment#PullPayment.withdrawPayments(address%20payable)[`withdrawPayments()`] it later.
If you want to Escrow some funds, check out link:api/payment#escrow[`Escrow`] and link:api/payment#conditionalescrow[`ConditionalEscrow`] for governing the release of some escrowed Ether.
[[misc]]
=== Misc
Want to check if an address is a contract? Use link:api/utils#address[`Address`] and link:api/utils#Address.isContract(address)[`Address.isContract()`].
Want to keep track of some numbers that increment by 1 every time you want another one? Check out link:api/drafts#counter[`Counter`]. This is especially useful for creating incremental ERC721 `tokenId`s like we did in the last section.