{"action":"created","issue":{"url":"https://api.github.com/repos/mylamour/blog/issues/40","repository_url":"https://api.github.com/repos/mylamour/blog","labels_url":"https://api.github.com/repos/mylamour/blog/issues/40/labels{/name}","comments_url":"https://api.github.com/repos/mylamour/blog/issues/40/comments","events_url":"https://api.github.com/repos/mylamour/blog/issues/40/events","html_url":"https://github.com/mylamour/blog/issues/40","id":358552178,"node_id":"MDU6SXNzdWUzNTg1NTIxNzg=","number":40,"title":"合约安全入个门","user":{"login":"mylamour","id":12653147,"node_id":"MDQ6VXNlcjEyNjUzMTQ3","avatar_url":"https://avatars3.githubusercontent.com/u/12653147?v=4","gravatar_id":"","url":"https://api.github.com/users/mylamour","html_url":"https://github.com/mylamour","followers_url":"https://api.github.com/users/mylamour/followers","following_url":"https://api.github.com/users/mylamour/following{/other_user}","gists_url":"https://api.github.com/users/mylamour/gists{/gist_id}","starred_url":"https://api.github.com/users/mylamour/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mylamour/subscriptions","organizations_url":"https://api.github.com/users/mylamour/orgs","repos_url":"https://api.github.com/users/mylamour/repos","events_url":"https://api.github.com/users/mylamour/events{/privacy}","received_events_url":"https://api.github.com/users/mylamour/received_events","type":"User","site_admin":false},"labels":[{"id":740181998,"node_id":"MDU6TGFiZWw3NDAxODE5OTg=","url":"https://api.github.com/repos/mylamour/blog/labels/%E5%AE%89%E5%85%A8","name":"安全","color":"33bc0d","default":false},{"id":740178166,"node_id":"MDU6TGFiZWw3NDAxNzgxNjY=","url":"https://api.github.com/repos/mylamour/blog/labels/%E5%B7%A5%E4%BD%9C","name":"工作","color":"ffcc7f","default":false},{"id":740189602,"node_id":"MDU6TGFiZWw3NDAxODk2MDI=","url":"https://api.github.com/repos/mylamour/blog/labels/%E8%B5%84%E6%BA%90","name":"资源","color":"96063d","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-09-10T10:00:45Z","updated_at":"2018-09-13T06:07:36Z","closed_at":null,"author_association":"OWNER","body":"不知道什么情况，老板准备做游戏？还是做博彩？不清楚。让看下合约安全，然后给大家分享一下。不好说，两天时间，还要分析下几个Dapp的合约代码。还好之前自己学习过一点。然后现学现卖，搞了一下。想说的都在PDF里了。\r\nPDF ->  [smart contract security basicly](https://github.com/mylamour/blog/files/2366085/basic.sharing.pdf)\r\n\r\n\r\n\r\n# Resources\r\n* Ethereum Safety: https://github.com/ethereum/wiki/wiki/Safety\r\n* Smart Contract Best Practices: https://consensys.github.io/smart-contract-best-practices/\r\n* DASP top10: https://github.com/CryptoServices/dasp\r\n* Safe Solidity: http://chriseth.github.io/notes/talks/safe_solidity/\r\n* Audit Smart Contract: https://medium.com/@merunasgrincalaitis/how-to-audit-a-smart-contract-most-dangerous-attacks-in-solidity-ae402a7e7868\r\n* Online Contract Scan: https://securify.ch/\r\n* Scan Contract Tools: https://github.com/meyer9/ethdasm\r\n* Malicious Backdoors In Ethereum Proxies: https://medium.com/nomic-labs-blog/malicious-backdoors-in-ethereum-proxies-62629adf3357"},"comment":{"url":"https://api.github.com/repos/mylamour/blog/issues/comments/420894312","html_url":"https://github.com/mylamour/blog/issues/40#issuecomment-420894312","issue_url":"https://api.github.com/repos/mylamour/blog/issues/40","id":420894312,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMDg5NDMxMg==","user":{"login":"mylamour","id":12653147,"node_id":"MDQ6VXNlcjEyNjUzMTQ3","avatar_url":"https://avatars3.githubusercontent.com/u/12653147?v=4","gravatar_id":"","url":"https://api.github.com/users/mylamour","html_url":"https://github.com/mylamour","followers_url":"https://api.github.com/users/mylamour/followers","following_url":"https://api.github.com/users/mylamour/following{/other_user}","gists_url":"https://api.github.com/users/mylamour/gists{/gist_id}","starred_url":"https://api.github.com/users/mylamour/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mylamour/subscriptions","organizations_url":"https://api.github.com/users/mylamour/orgs","repos_url":"https://api.github.com/users/mylamour/repos","events_url":"https://api.github.com/users/mylamour/events{/privacy}","received_events_url":"https://api.github.com/users/mylamour/received_events","type":"User","site_admin":false},"created_at":"2018-09-13T06:07:36Z","updated_at":"2018-09-13T06:07:36Z","author_association":"OWNER","body":"# 合约中生成随机数\r\n\r\n## 不安全的方式\r\n\r\n1. 任何使用以下示例的决策将会给用户带来不公平机会\r\n> * 使用`blockhash`, timestamp`或者其他矿工可以定义的值。始终记住，每个矿工有权利选择是否打包一个块，所以可想而知，他们都有可能获取他们挖到每个块中奖金？？ what fuck ? \r\n> * 使用了任何用户提交的随机数。用户可能泄露预先提交了任何一个数值。\r\n\r\n2. 任何其他合约可见，或者公共的。\r\n> * 这意味着这个数字不应该在开奖前公布\r\n\r\n3. EVM将不会追踪外部计算机\r\n> * 任何合约生成的数字都可以在块结束前被知晓。所以需要在数字的产生和使用之间留出时间\r\n\r\n原文: \r\n> 1. Any decision that a user makes which affects the outcome gives that user an unfair advantage. Examples include:\r\n>> * Using a blockhash, timestamp, or other miner-defined value. Keep in mind that the miner has a choice of whether to publish a block or not, so they could conceivably have one chance at the prize per block they mine.\r\n>> * Any user-submitted random number. Even if the user pre-commits to a number, they have a choice in whether or not to reveal the number.\r\n>2. Everything that the contract sees, the public sees.\r\n>> * This means that the number should not be generated until after entry into the lottery has been closed.\r\n>3. The EVM will not outrace a physical computer.\r\n>> * Any number that the contract generates may be known before the end of that block. Leave time between the generation of the number and its use.\r\n\r\n## 安全的方式： 设计一个不可欺骗的分布式彩票\r\n\r\n1. The Casino sets aside a reward for a random number\r\n赌场为随机数留出奖池\r\n2. Each user generates their own secret random number N\r\n每个用户生成自己的随机数`N`\r\n3. Users create their commitment by hashing their N with their address: `bytes32 hash = sha3(N,msg.sender)` <sup>1</sup> \r\n用户通过散列他们的随机数值和钱包地址创建他们的唯一凭证\r\n* note: steps 2 & 3 should be performed locally, in secret\r\n这一步和上一步最好应该在本地完成，为了安全\r\n\r\n4. Users send their hash to the contract, along with ether greater than or equal in value to the value of the random number. <sup>2</sup> \r\n用户发送他们的哈希值(这个就是你的彩票)到合约地址, 随着`Ether`的值逐渐大于等于随机数的值\r\n\r\n5. Submissions continue for some number of blocks, or until sufficient participants have joined. Once the submission round has ended, the reveal round begins.\r\n继续提交新的块，直到有足够的参与者进来。一旦一轮提交结束，就开始开奖。\r\n\r\n6. Each user submits their random number N to the contract\r\n每个用户提交他们的随机数`N`到合约\r\n\r\n7. The contract verifies that the `sha3(N,msg.sender)` matches the original submission\r\n合约去验证这个`sha3(N, msg.sender)`是不是和原来的提交一致。\r\n* If the user does not submit a valid `N` in time, his deposit is forfeit.\r\n如果用户的没有及时提交或者提交的是无效的，他的存款将会被没收。\r\n\r\n8. The `N`s are all `XOR`'d together to generate the resulting random number.\r\n中奖的那个`N`是所有的一起生成所产生的随机数的异或。\r\n\r\n9. This number is used to determine which of the participants receives the reward.` (N % numUsers)` <sup>3</sup>\r\n这个数字决定了哪个参与者将接受奖金。\r\n\r\n注解: \r\n1. The users must concatenate their address to their `N` before hashing. Otherwise, another user could simply submit an identical hash, then wait for `N` to be revealed, then submit that. `N XOR N` equals `0`, so this could be used to cancel out all submissions except the attacker's.\r\n用户在哈希前吧随机数`N`和他们的地址连接到一起。否则，另一个用户可以简单地提交一个相同的哈希，然后等待`N`被显示，然后提交。`N XOR N`等于`0`，这可以用来抵消除了攻击者之外的所有提交。\r\n\r\n2. This is where the trade-offs come in. The last person to reveal their N has a choice whether to reveal or to not reveal. This essentially gives them a double chance at winning. Enter enough times, and you get a new choice for each entry. Hint: Miners chose the order of transactions in a block. In order to discourage this, users must put up a large security deposit, equal to the value they would gain by manipulating the random number. This could be a problem for many users, especially for large jackpots, even with game-theoretic optimizations.\r\n\r\n* A commonly employed alternative is a semi-centralized system. This requires the \"house\" to submit the first hash and last reveal. If the house does not fulfill their duty, everyone's ether is returned. This has issues, such as the house choosing to flake if a jackpot payout is imminent. The idea is that the house's reputation is at stake.\r\n** Note that this essentially centralizes the whole system. One simply needs to take down the house for the whole operation to be shut down. This risk can be reduced by hiring multiple trusted non-players to be the first/last commiters.\r\n* Another trick is to use hired professional third parties to \"mine\" randomness for you, so that the players need not be bothered with the process.\r\n3. A reward is necessary in order to foster competition among participants. It causes a classic tragedy-of-the-commons/prisoner's dilemma situation. Collusion between participants would allow them to win a large pot and split it among themselves, but if everyone knows what everyone else will submit, they know what they themselves should submit to win the reward. Thus, if the reward is larger than the value of the random number divided by the number of players, then everyone is incentivised to keep their own number a secret in order to have a better shot at the reward. Note that only one of the participants needs to submit a good random number, and the result will be unpredictable.\r\n\r\n\r\n\r\n# References\r\n* [生成安全的随机数](https://ethereum.stackexchange.com/questions/191/how-can-i-securely-generate-a-random-number-in-my-smart-contract)"}}
{"actor":{"display_login":"mylamour"}}