{"id":327919,"date":"2022-01-13T09:00:48","date_gmt":"2022-01-13T09:00:48","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=327919"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=327919","title":{"rendered":"<span>EVM offchain random<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u0432\u044b \u043f\u043e\u043d\u044f\u043b\u0438 \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0451\u0442 \u043e \u0440\u0430\u043d\u0434\u043e\u043c\u0435 \u043d\u0430 solidity, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u043e \u043e\u0444\u0447\u0435\u0439\u043d \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0438 \u043d\u0435\u0442, \u044d\u0442\u043e \u043d\u0435 chainlink.\u00a0<\/p>\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" alt=\"\u041a\u043e\u0441\u0442\u044b\u043b\u0438 \u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b\" title=\"\u041a\u043e\u0441\u0442\u044b\u043b\u0438 \u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b\" height=\"720\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/61f\/cd3\/6c3\/61fcd36c3fea706fe02d56876b8084e5.png\" data-width=\"1280\"\/><figcaption>\u041a\u043e\u0441\u0442\u044b\u043b\u0438 \u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b<\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443: \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0430 evm \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0435 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0441\u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0438 <abbr title=\"\u041b\u0438\u0446\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0443\u0441\u043b\u0443\u0433\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446 \u0438\u0433\u0440\u044b.\">\u0441\u0435\u0440\u0432\u0435\u0440<\/abbr>.<\/p>\n<p><strong>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043b\u043e\u0431<\/strong>:<br \/>\u041f\u0440\u043e\u0441\u0442\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043d\u0430 \u0441\u043e\u043b\u0438\u0434\u0438\u0442\u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u0440\u0438\u043c\u0435\u0440<\/summary>\n<div class=\"spoiler__content\">\n<p><code>function random() external view returns (uint) {<\/code><\/p>\n<p><code>\u00a0 \u00a0 \u00a0 \u00a0 return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, additionalRandomParam)));<\/code><\/p>\n<p><code>\u00a0 \u00a0 }<\/code><\/p>\n<\/div>\n<\/details>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430:<\/p>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0443\u044e \u043c\u0430\u043b\u043e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u0443\u044e \u0443\u0433\u0440\u043e\u0437\u0443 \u0430\u0442\u0430\u043a\u0438 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043c\u0430\u0439\u043d\u0435\u0440\u043e\u0432, \u0442\u0443\u0442 \u0435\u0441\u0442\u044c \u0438 \u043c\u0435\u043d\u0435\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430\u044f \u0443\u0433\u0440\u043e\u0437\u0430, \u043d\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u0430\u044f. \u0410\u0442\u0430\u043a\u0443\u044e\u0449\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043c\u0430\u0440\u0442 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432.<\/p>\n<p>\u0412 \u0442\u0435\u043e\u0440\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043a\u043e\u0434\u0430 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043c\u0430\u0440\u0442 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u043e\u0432, \u043d\u043e \u0434\u0430\u0436\u0435 \u0442\u043e\u0433\u0434\u0430, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0430\u043a\u0438 \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p><a href=\"https:\/\/chain.link\/\" rel=\"noopener noreferrer nofollow\"><strong>Chainlink <\/strong><\/a><strong>\/ \u0414\u0432\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438<\/strong><\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"242\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/116\/d18\/a8c\/116d18a8c03a5f072b4ad8ce875b3444.png\" data-width=\"208\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0421\u0443\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f, \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0430 \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u043e\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u0430, \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043d\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 chainlink, \u0441\u0435\u0440\u0432\u0435\u0440 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u043e\u0439.\u00a0<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430:<\/p>\n<p>\u0417\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0441 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043b\u0430\u0442\u0438\u0442\u044c <abbr title=\"\u041b\u0438\u0446\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0443\u0441\u043b\u0443\u0433\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446 \u0438\u0433\u0440\u044b.\">\u0441\u0435\u0440\u0432\u0435\u0440<\/abbr>.\u00a0 \u0411\u0435\u0437\u0443\u0441\u043b\u043e\u0432\u043d\u043e, \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u044d\u0442\u043e \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043d\u043e, \u043d\u043e \u043d\u0435 \u0432 \u043d\u0430\u0448\u0435\u043c.<\/p>\n<p><strong>Offchain random<\/strong>  <\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"183\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/246\/6c8\/4fc\/2466c84fc139d3410ce59f2464eddaf9.png\" data-width=\"275\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0434\u0435\u044f: \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u0434\u043e\u0433\u043e\u0432\u0430\u0440\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043e \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u043e\u043c \u0447\u0438\u0441\u043b\u0435:<\/p>\n<ul>\n<li>\n<p>\u0441\u0435\u0440\u0432\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0435 \u0447\u0438\u0441\u043b\u043e(r) \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>\u0432 \u043e\u0442\u0432\u0435\u0442 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0445\u044d\u0448 \u044d\u0442\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430<\/p>\n<\/li>\n<li>\n<p>\u043a\u043b\u0438\u0435\u043d\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0447\u0438\u0441\u043b\u043e(c) \u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0435\u0433\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0440\u0442\u044b, \u043e\u0442\u0434\u0430\u0432\u0430\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0447\u0442\u043e \u0431\u044b \u043e\u043d \u0441\u0440\u0430\u0432\u043d\u0438\u043b \u0435\u0433\u043e \u0441 \u0445\u044d\u0448\u0435\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0447\u0438\u0441\u0435\u043b (\u0430 \u0442\u0430\u043a\u0436\u0435, seq, address, chain id, contract address, \u0434\u0430\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u0438).\u00a0<\/p>\n<\/li>\n<li>\n<p>\u041a\u043b\u0438\u0435\u043d\u0442 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0445\u044d\u0448 \u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043d\u0430 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d<\/p>\n<\/li>\n<\/ul>\n<details class=\"spoiler\">\n<summary>\u041b\u0443\u0447\u0448\u0435 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u043a\u0435\u043d<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" height=\"1551\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5a7\/84d\/73c\/5a784d73ce09180f3180b39422c6a50c.png\" data-width=\"961\"\/><figcaption><\/figcaption><\/figure>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>js ethers test<\/summary>\n<div class=\"spoiler__content\">\n<p>\u00a0\u00a0\u00a0\u00a0<code>it(\"Base success\", async function () {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var seed = 500;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var seq = 0;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var address = user.address;<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var hash = ethers.utils.keccak256(<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ethers.utils.solidityPack(['uint256', 'uint256','address' ],<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[seed, seq, address])\/\/add chain id, etc<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0);<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var signature = await hrv.web3.eth.sign(hash, master.address);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var item = await random.connect(user).next(seed, seq, signature);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await item.wait();<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0});<\/code><\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>solidity contract<\/summary>\n<div class=\"spoiler__content\">\n<p><code>\/\/SPDX-License-Identifier: Unlicense<\/code><\/p>\n<p><code>pragma solidity ^0.8.0;<\/code><\/p>\n<p><code>import \"@openzeppelin\/contracts\/utils\/cryptography\/ECDSA.sol\";<\/code><\/p>\n<p><code>import \"hardhat\/console.sol\";<\/code><\/p>\n<p><code>import \"@openzeppelin\/contracts\/access\/Ownable.sol\";<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>contract Random is Ownable {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0using ECDSA for bytes32;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0mapping(address => uint256) public SeqMapping;<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0function next(<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uint256 seed,<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uint256 seq,<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0bytes memory signature<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0) public returns (uint256) {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0require(SeqMapping[msg.sender] == seq, \"wrong seq\");<\/code><\/p>\n<p><code>\u00a0\u00a0\/\/add chain id<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0bytes32 hash = keccak256(abi.encodePacked(seed, seq, msg.sender));<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0bytes32 hashEth = hash.toEthSignedMessageHash();<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0require(hashEth.recover(signature) == owner(), \"wrong signature\");<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SeqMapping[msg.sender]++;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return uint256(hash);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0}<\/code><\/p>\n<p><code>}<\/code><\/p>\n<\/div>\n<\/details>\n<p>\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a:<\/p>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0437\u0434\u0435\u0441\u044c, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043a\u043b\u0438\u0435\u043d\u0442, \u0431\u0443\u0434\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d \u0438 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0432\u0432\u0438\u0434\u0443 \u0442\u043e\u0433\u043e \u0447\u0442\u043e, \u0448\u0430\u0433\u043e\u0432 \u0432 \u0438\u0433\u0440\u0435 \u043c\u043d\u043e\u0433\u043e, \u0438 \u043e\u0434\u0438\u043d \u0445\u043e\u0434 \u043d\u0435 \u043d\u0430 \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u0435\u0442. \u0410 \u043c\u044b \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0434\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e.<\/p>\n<p><strong>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u043f\u0440\u0438 \u0433\u0443\u0433\u043b\u0435\u043d\u0438\u0438 \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0430\u043d\u0434\u043e\u043c \u043d\u0430 \u0441\u043e\u043b\u0438\u0434\u0438\u0442\u0438, <a href=\"https:\/\/stackoverflow.com\/questions\/48848948\/how-to-generate-a-random-number-in-solidity\" rel=\"noopener noreferrer nofollow\">\u0432 \u043e\u0442\u0432\u0435\u0442\u0430\u0445 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b<\/a> \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0443\u043f\u043e\u043c\u044f\u043d\u0430\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u043f\u0443\u043d\u043a\u0442\u0430, \u0430 \u0442\u0440\u0435\u0442\u0438\u0439 \u043f\u0443\u0442\u044c \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u0435 \u0435\u0441\u0442\u044c \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438, \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0435\u043a\u044c\u044e\u0440\u0438\u0442\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0435\u0439. <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438, \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0433\u043e\u0434\u043d\u044b\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u043f\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443, \u0441 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435\u043c \u0438\u0445 \u043f\u043e\u0447\u0438\u0442\u0430\u044e \u0438 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/645353\/\"> https:\/\/habr.com\/ru\/post\/645353\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u0432\u044b \u043f\u043e\u043d\u044f\u043b\u0438 \u0438\u0437 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u0440\u0435\u0447\u044c \u043f\u043e\u0439\u0434\u0451\u0442 \u043e \u0440\u0430\u043d\u0434\u043e\u043c\u0435 \u043d\u0430 solidity, \u0430 \u0438\u043c\u0435\u043d\u043d\u043e \u043e \u043e\u0444\u0447\u0435\u0439\u043d \u0440\u0435\u0448\u0435\u043d\u0438\u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0438 \u043d\u0435\u0442, \u044d\u0442\u043e \u043d\u0435 chainlink.\u00a0<\/p>\n<figure class=\"full-width\"><figcaption>\u041a\u043e\u0441\u0442\u044b\u043b\u0438 \u0438 \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434\u044b<\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u043e \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443: \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043d\u0430 evm \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d\u0435 \u043a\u043e\u0442\u043e\u0440\u044b\u043c \u0441\u043c\u043e\u0436\u0435\u0442 \u0434\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0438 <abbr title=\"\u041b\u0438\u0446\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0443\u0441\u043b\u0443\u0433\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446 \u0438\u0433\u0440\u044b.\">\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446 \u0438\u0433\u0440\u044b.&#187;>\u0441\u0435\u0440\u0432\u0435\u0440<\/abbr>.<\/p>\n<p><strong>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0432 \u043b\u043e\u0431<\/strong>:<br \/>\u041f\u0440\u043e\u0441\u0442\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u043d\u0430 \u0441\u043e\u043b\u0438\u0434\u0438\u0442\u0438 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430<\/p>\n<details class=\"spoiler\">\n<summary>\u041f\u0440\u0438\u043c\u0435\u0440<\/summary>\n<div class=\"spoiler__content\">\n<p><code>function random() external view returns (uint) {<\/code><\/p>\n<p><code>\u00a0 \u00a0 \u00a0 \u00a0 return uint(keccak256(abi.encodePacked(block.difficulty, block.timestamp, additionalRandomParam)));<\/code><\/p>\n<p><code>\u00a0 \u00a0 }<\/code><\/p>\n<\/div>\n<\/details>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430:<\/p>\n<p>\u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u043a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0443\u044e \u043c\u0430\u043b\u043e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u0443\u044e \u0443\u0433\u0440\u043e\u0437\u0443 \u0430\u0442\u0430\u043a\u0438 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u043c\u0430\u0439\u043d\u0435\u0440\u043e\u0432, \u0442\u0443\u0442 \u0435\u0441\u0442\u044c \u0438 \u043c\u0435\u043d\u0435\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430\u044f \u0443\u0433\u0440\u043e\u0437\u0430, \u043d\u043e \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u0430\u044f. \u0410\u0442\u0430\u043a\u0443\u044e\u0449\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043c\u0430\u0440\u0442 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u043e\u0432\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0432\u0441\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0434\u043b\u044f \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432.<\/p>\n<p>\u0412 \u0442\u0435\u043e\u0440\u0438\u0438 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u043a\u043e\u0434\u0430 \u0438\u0437 \u0434\u0440\u0443\u0433\u0438\u0445 \u0441\u043c\u0430\u0440\u0442 \u043a\u043e\u043d\u0442\u0440\u0430\u043a\u0442\u043e\u0432, \u043d\u043e \u0434\u0430\u0436\u0435 \u0442\u043e\u0433\u0434\u0430, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u043e\u0441\u0442\u044c \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0430\u043a\u0438 \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p><a href=\"https:\/\/chain.link\/\" rel=\"noopener noreferrer nofollow\"><strong>Chainlink <\/strong><\/a><strong>\/ \u0414\u0432\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438<\/strong><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0421\u0443\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f, \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0437\u0430 \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u043e\u0441\u0442\u044c \u0447\u0438\u0441\u043b\u0430, \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u043d\u0435 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0430, \u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430. \u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 chainlink, \u0441\u0435\u0440\u0432\u0435\u0440 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u043e\u0439.\u00a0<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430:<\/p>\n<p>\u0417\u0430 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u0441 \u0442\u0440\u0435\u0442\u044c\u0435\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043b\u0430\u0442\u0438\u0442\u044c <abbr title=\"\u041b\u0438\u0446\u043e \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0449\u0435\u0435 \u0443\u0441\u043b\u0443\u0433\u0438, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446 \u0438\u0433\u0440\u044b.\">\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0432\u043b\u0430\u0434\u0435\u043b\u0435\u0446 \u0438\u0433\u0440\u044b.&#187;>\u0441\u0435\u0440\u0432\u0435\u0440<\/abbr>.\u00a0 \u0411\u0435\u0437\u0443\u0441\u043b\u043e\u0432\u043d\u043e, \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u044d\u0442\u043e \u043e\u043f\u0440\u0430\u0432\u0434\u0430\u043d\u043d\u043e, \u043d\u043e \u043d\u0435 \u0432 \u043d\u0430\u0448\u0435\u043c.<\/p>\n<p><strong>Offchain random<\/strong>  <\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0418\u0434\u0435\u044f: \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043a\u043b\u0438\u0435\u043d\u0442 \u0434\u043e\u0433\u043e\u0432\u0430\u0440\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043e \u0440\u0430\u043d\u0434\u043e\u043c\u043d\u043e\u043c \u0447\u0438\u0441\u043b\u0435:<\/p>\n<ul>\n<li>\n<p>\u0441\u0435\u0440\u0432\u0435\u0440 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043d\u0435\u043a\u043e\u0435 \u0447\u0438\u0441\u043b\u043e(r) \u043f\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0443 \u043a\u043b\u0438\u0435\u043d\u0442\u0430<\/p>\n<\/li>\n<li>\n<p>\u0432 \u043e\u0442\u0432\u0435\u0442 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u0445\u044d\u0448 \u044d\u0442\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430<\/p>\n<\/li>\n<li>\n<p>\u043a\u043b\u0438\u0435\u043d\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0447\u0438\u0441\u043b\u043e(c) \u043c\u0435\u043d\u044c\u0448\u0435\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438 \u043f\u043e\u0441\u044b\u043b\u0430\u0435\u0442 \u0435\u0433\u043e \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440<\/p>\n<\/li>\n<li>\n<p>\u0441\u0435\u0440\u0432\u0435\u0440 \u0432\u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442 \u043a\u0430\u0440\u0442\u044b, \u043e\u0442\u0434\u0430\u0432\u0430\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0443 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e, \u0447\u0442\u043e \u0431\u044b \u043e\u043d \u0441\u0440\u0430\u0432\u043d\u0438\u043b \u0435\u0433\u043e \u0441 \u0445\u044d\u0448\u0435\u043c, \u0430 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044f \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0441\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0447\u0438\u0441\u0435\u043b (\u0430 \u0442\u0430\u043a\u0436\u0435, seq, address, chain id, contract address, \u0434\u0430\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0434\u043f\u0438\u0441\u0438).\u00a0<\/p>\n<\/li>\n<li>\n<p>\u041a\u043b\u0438\u0435\u043d\u0442 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 \u0445\u044d\u0448 \u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e \u043d\u0430 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d<\/p>\n<\/li>\n<\/ul>\n<details class=\"spoiler\">\n<summary>\u041b\u0443\u0447\u0448\u0435 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0442\u044c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043e\u043a\u0435\u043d<\/summary>\n<div class=\"spoiler__content\">\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>js ethers test<\/summary>\n<div class=\"spoiler__content\">\n<p>\u00a0\u00a0\u00a0\u00a0<code>it(\"Base success\", async function () {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var seed = 500;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var seq = 0;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var address = user.address;<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var hash = ethers.utils.keccak256(<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ethers.utils.solidityPack(['uint256', 'uint256','address' ],<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0[seed, seq, address])\/\/add chain id, etc<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0);<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var signature = await hrv.web3.eth.sign(hash, master.address);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var item = await random.connect(user).next(seed, seq, signature);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0await item.wait();<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0});<\/code><\/p>\n<\/div>\n<\/details>\n<details class=\"spoiler\">\n<summary>solidity contract<\/summary>\n<div class=\"spoiler__content\">\n<p><code>\/\/SPDX-License-Identifier: Unlicense<\/code><\/p>\n<p><code>pragma solidity ^0.8.0;<\/code><\/p>\n<p><code>import \"@openzeppelin\/contracts\/utils\/cryptography\/ECDSA.sol\";<\/code><\/p>\n<p><code>import \"hardhat\/console.sol\";<\/code><\/p>\n<p><code>import \"@openzeppelin\/contracts\/access\/Ownable.sol\";<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>contract Random is Ownable {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0using ECDSA for bytes32;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0mapping(address => uint256) public SeqMapping;<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0function next(<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uint256 seed,<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0uint256 seq,<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0bytes memory signature<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0) public returns (uint256) {<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0require(SeqMapping[msg.sender] == seq, \"wrong seq\");<\/code><\/p>\n<p><code>\u00a0\u00a0\/\/add chain id<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0bytes32 hash = keccak256(abi.encodePacked(seed, seq, msg.sender));<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0bytes32 hashEth = hash.toEthSignedMessageHash();<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0require(hashEth.recover(signature) == owner(), \"wrong signature\");<\/code><\/p>\n<p><code>\u00a0<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0SeqMapping[msg.sender]++;<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return uint256(hash);<\/code><\/p>\n<p><code>\u00a0\u00a0\u00a0\u00a0}<\/code><\/p>\n<p><code>}<\/code><\/p>\n<\/div>\n<\/details>\n<p>\u041d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a:<\/p>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0437\u0434\u0435\u0441\u044c, \u044d\u0442\u043e \u0442\u043e, \u0447\u0442\u043e \u043a\u043b\u0438\u0435\u043d\u0442, \u0431\u0443\u0434\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043d\u0430 \u0431\u043b\u043e\u043a\u0447\u0435\u0439\u043d \u0438 \u043c\u043e\u0436\u0435\u0442 \u043d\u0435 \u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e. \u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u044d\u0442\u043e \u043d\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430, \u0432\u0432\u0438\u0434\u0443 \u0442\u043e\u0433\u043e \u0447\u0442\u043e, \u0448\u0430\u0433\u043e\u0432 \u0432 \u0438\u0433\u0440\u0435 \u043c\u043d\u043e\u0433\u043e, \u0438 \u043e\u0434\u0438\u043d \u0445\u043e\u0434 \u043d\u0435 \u043d\u0430 \u0447\u0442\u043e \u043d\u0435 \u043f\u043e\u0432\u043b\u0438\u044f\u0435\u0442. \u0410 \u043c\u044b \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0435 \u0447\u0438\u0441\u043b\u0430 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0434\u043e \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e.<\/p>\n<p><strong>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u043f\u0440\u0438 \u0433\u0443\u0433\u043b\u0435\u043d\u0438\u0438 \u043e \u0442\u043e\u043c \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0440\u0430\u043d\u0434\u043e\u043c \u043d\u0430 \u0441\u043e\u043b\u0438\u0434\u0438\u0442\u0438, <a href=\"https:\/\/stackoverflow.com\/questions\/48848948\/how-to-generate-a-random-number-in-solidity\" rel=\"noopener noreferrer nofollow\">\u0432 \u043e\u0442\u0432\u0435\u0442\u0430\u0445 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b<\/a> \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0443\u043f\u043e\u043c\u044f\u043d\u0430\u044e\u0442\u0441\u044f \u043f\u0435\u0440\u0432\u044b\u0435 \u0434\u0432\u0430 \u043f\u0443\u043d\u043a\u0442\u0430, \u0430 \u0442\u0440\u0435\u0442\u0438\u0439 \u043f\u0443\u0442\u044c \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u043d\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c. \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0432 \u044d\u0442\u043e\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u0435 \u0435\u0441\u0442\u044c \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0435 \u043a\u0430\u043c\u043d\u0438, \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0435\u043a\u044c\u044e\u0440\u0438\u0442\u0438 \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0446\u0438\u0435\u0439. <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438, \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0433\u043e\u0434\u043d\u044b\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438, \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f \u043f\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u043e\u043c\u0443 \u043c\u0435\u0442\u043e\u0434\u0443, \u0441 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435\u043c \u0438\u0445 \u043f\u043e\u0447\u0438\u0442\u0430\u044e \u0438 \u043f\u043e\u0441\u0442\u0430\u0440\u0430\u044e\u0441\u044c \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/645353\/\"> https:\/\/habr.com\/ru\/post\/645353\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-327919","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/327919","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=327919"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/327919\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=327919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=327919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=327919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}