Mais conteúdo relacionado Semelhante a [ModernWeb2018] Web3.0 區塊鏈 DApp + 智能合約開發:你必要挑戰的坑坑洞洞 (20) [ModernWeb2018] Web3.0 區塊鏈 DApp + 智能合約開發:你必要挑戰的坑坑洞洞1. Web3.0 區塊鏈 DApp + 智能合約開發
你必要挑戰的坑坑洞洞
2018.07.18 @ ModernWeb 2018
Taien Wang <taien.wang@gmail.com>
2. • 安世泰共同合夥創辦人
• LEADBEST共同創辦人兼技術長
• TGO鯤鵬會台北分會會長
大型電商 friDay 購物前技術總監,微軟資安 MVP,2013
年拿下Facebook廣告API全球冠軍,擁有超過 10 年網路
研發和5年大型技術團隊管理。在技術架構與領導管理有
豐富實戰經驗,並於多項領域(資安、創新、雲端、網路
與廣告技術)獲得傑出成就。全球技術領導者高階社群
TGO 鯤鵬會台北分會發起人。知名白帽駭客,獲得多項國
際公司 Bug Bounty Award,包含:Yahoo、Line、
Synology 等。
https://www.linkedin.com/in/taienwang/
王志清 (Taien Wang)
14. Ethereum DApp 重要角色
● (身份)Key
● (程式)Solidity
● (虛擬機)EVM
● (執行環境)Main / Test / Private
EVM
Main
Blockchain
Test Blockchain
(Ropsten/Kovan/Rinkeby)
Main
Blockchain
Smart Contract
(Solidity)
Truffle
(on node.js)
Web3.js
Web App
(HTML/CSS/Javascript)
Ethereum core(Geth, cpp-ethereum, ethereumj, …)
RPC / TestRPC / JSONRPC
Account Address / Public Key / Private Key
21. 自建私有鍊
● 運行私有練 Ethereum
○ 創世區塊設定
○ 參考
■ Karien:建立 Go Ethereum 私有鍊
● 伺服器狀態 eth-netstats / eth-net-intelligence-api
○ 參考
■ Karien:監控 Go Ethereum 的區塊鏈狀況
● 鏈紀錄查詢 ethereumproject/explorer
○ 網頁主程式與同步程式
22. 自建私有鍊 – 常見問題
● Node.JS Mongo 函式庫方法更新
○ 解法:mongoose 连接警告问题(node:148572) DeprecationWarning: `open()` is deprecated
in mongoose >= 4.11.0
● Ethereum 限制連線節點
○ 設定子網路連線後,其他網路被拒絕( --netrestrict 10.1.0.0/16 )
■ DEBUG[05-11|10:38:36] Rejected conn (not whitelisted in NetRestrict)
addr=34.213.59.140:47536
■ DEBUG[05-11|10:38:39] Rejected conn (not whitelisted in NetRestrict)
addr=103.106.98.120:48290
24. EIP 與 ERC
● EIP:Stands for Ethereum Improvement Proposals (以太坊改进建议)
● ERC:Ethereum Request for Comment
● ERC-20 對應 EIP-20
25. Ethereum ERC20 代幣
● Functions
○ function totalSupply() constant returns (uint256 totalSupply)
○ function balanceOf(address _owner) constant returns (uint256 balance)
○ function transfer(address _to, uint256 _value) returns (bool success)
○ function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
○ function approve(address _spender, uint256 _value) returns (bool success)
○ function allowance(address _owner, address _spender) constant returns (uint256 remaining)
● Events
○ event Transfer(address indexed _from, address indexed _to, uint256 _value)
○ event Approval(address indexed _owner, address indexed _spender, uint256 _value)
33. SmartContract 安全原則
● 做好異常錯誤準備
● 謹慎發布智能合約
● 保持智能合約簡單
● 保持最新狀態
● 留意區塊鏈的特性
○ 外部調用、時間、同步
● 確保金鑰保護與權責分離
Ref: https://consensys.github.io/smart-contract-best-practices/general_philosophy/
34. SmartContract 已知攻擊(1/3)
● 外部調用
○ 盡量避免外部調用
○ 了解 send()、transfer()、以及 call.value() 差別
○ 處理外部調用錯誤
○ 不要假設你知道外部調用的控制流程
○ 外部合約優先使用 pull,而不是 push
● 鏈上的資料是公開的
● 正確使用 assert() 和 require()
Ref: https://consensys.github.io/smart-contract-best-practices/known_attacks/
35. SmartContract 已知攻擊(2/3)
● 注意使用整數除法
● 小心除0、整數溢位
○ SafeMath
● 保持 fallback 簡單(2300 GAS限制)
● 時間依賴性
● block.timestampe: 30秒規則
● Block.number: 12 分鐘
Ref: https://consensys.github.io/smart-contract-best-practices/known_attacks/
36. SmartContract 已知攻擊(3/3)
● 檢查 fallback 的資料長度限制
● 明確定義函數與變數可視性
● 將 pragmas 鎖定在某個特定版本
● 區分功能與事件
● 避免使用 tx.origin
Ref: https://consensys.github.io/smart-contract-best-practices/known_attacks/
37. Build Security In Agile
Code Review Meeting圖片來源:https://eternalsunshineoftheismind.wordpress.com/2013/02/04/agile-
methods-of-software-development-2/
38. development alpha beta gamma stage production
資料庫 docker docker 獨立 獨立 共用
乙太坊網路 testrpc testrpc privatenet testnet main
用途 開發 CI QA UAT 程式碼相容
是否對外 否 否 否 否
申請後開放
不對外 對外
Dapp 持續集成部署流程
commit to
master
static
analysis
checkout tag
vX.X.X
checkout tag
vX.X.X
create tag
vX.X.X
build
ABI/bytecode
checkout tag
vX.X.X
deploy
alpha
deploy
stage
build
ABI/bytecode
testcode
checkout tag
vX.X.X
checkout tag
vX.X.X
deploy
development
endendendendend
run test
deploy
beta
run test
deploy
gamma
run test manual test
deploy
production
end
verify? verify? verify? verify? verify?
manual test
40. Web 2.0 到 Web 3.0 轉換
工具 運算 服務介面 資料庫 儲存空間
Web2 App
瀏覽器 伺服器 HTTP API 資料庫
網頁伺服器
CDN
Web3 DApp
錢包
瀏覽器
點對點網路 智能合約 ABI
分散式儲存於
區塊鏈
分散式
CDNS
(IPFS/SWA
RM)