10. 해시 함수
입력값이 같으면 해시값도 반드시 같다.
불가역성 일방향 함수
출력값은 입력값의 길이와 상관없이 고정된 길이
SHA-256(Secure Hash Algorithm): 32바이트
$ echo "abcd" | sha256sum
fc4b5fd6816f75a7c81fc8eaa9499d6a299bd803397166e8c4cf9280b8
01d62c -
$ echo "abcde" | sha256sum
0283da60063abfb3a87f1aed845d17fe2d9ba8c780b478dc4ae048f5ee
97a6d5 -
10
11. 디지털 서명
송신자
1. 송신할 데이터의 해시값 생성
2. 출력된 해시값을 송신자의 비밀키로 암호화
3. 데이터와 서명을 함께 수신자에게 송신
수신자
1. 수신된 데이터의 해시값(A) 생성
2. 서명을 송신자의 공개키로 복호화(B)
3. A와 B 두 해시값을 비교하여 검증
11
17. 테스트 네트워크
Testnet
인터네상에서 동작하는 테스트 네트워크
테스트용 BTC, 처음 시작할때 모든 데이터 취득
Regtest
로컬PC내에서의 테스트 네트워크
계정생성, 채굴, 블록체인 초기화 용이
$ bitcoind -regtest -daemon
Bitcoin server starting
17
18. 블록 생성
# 채굴자는 보상 후 100 블록 이후에 송금이 가능
$ bitcoin-cli -regtest generate 101
블록 수 확인
$ bitcoin-cli -regtest getblockcount
101
계좌 생성
$ bitcoin-cli -regtest getnewaddress testuser100
2N7EHkYKrLbKqDybPY6cFoXXJ3ts4ArAKGb
18
25. Ethereum is a decentralized platform that runs
smart contracts:
“
“
비트코인이 2G폰이라면 이더리움은 스마트폰“ “
26. 이더리움과 비트코인의 차이점
유통되는 통화: ETH
스마트 계약: Turing complete
계정: EOA(Externally Owned Account), CA
블록의 데이터 구조: 잔액정보
상태 변화
계정과 연결된 정보: 논스, 스토리지 루트, 코드 해시
거래, 메시지, 콜(ReadOnly)
가스: 수수료, 보안
27. Programmable Economy
EVM(Ethereum Virtual Machine)
Solidity, a new language for smart contracts
Design and issue your own cryptocurrency
Build a new kind of decentralized application
28. 코인(Coin) vs. 토큰(Token)
코인: 독립된 블록체인 네트워크를 소유한 경우
비트코인(BTC), 이더리움(ETH), 스팀(STEEM)
토큰: 독립된 블록체인 네트워크를 소유하지 않은 경우
이오스(EOS), 트론(TRX)
플 랫 폼(마켓) 어 플 포 인 트
이더리움 블록체인 DAPP 토큰
구글 안드로이드 앱 ?
이오스(EOS), 퀀텀(Qtum)
: ERC-20 -> 메인넷 -> 코인
28
29. ERC-20(Ethereum Request for Comment)
Ethereum Improvement Proposals(EIPs)
이더리움 블록체인에서 발행되는 토큰의 표준
스마트 컨트랙트 인터페이스 규약만 정의
이더리움 기반의 DAPP에서 사용할 수 있는 토큰
ERC20 개발의 정석 - OpenZeppelin
넓은 의미에서 화폐뿐만 아니라 자산의 기능“ “
29
30. https://github.com/ethereum/EIPs/issues/20
contract ERC20Basic {
function totalSupply() constant returns (uint totalSupply)
function balanceOf(address who) constant returns (uint);
function transfer(address to, uint value);
event Transfer(address indexed from, address indexed to,
uint value);
}
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender)
constant returns (uint);
function transferFrom(address from, address to,
uint value);
function approve(address spender, uint value);
event Approval(address indexed owner, address indexed
spender, uint value);
}
30
34. Go Ethereum
Of cial Go implementation of the Ethereum protocol
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo add-apt-repository -y ppa:ethereum/ethereum-dev
$ sudo apt-get update
$ sudo apt-get install ethereum
$ geth version
Geth Version: 1.8.2-stable
Git Commit: b8b9f7f4476a30a0aaf6077daade6ae77f969960
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.9.4
34
35. Private 네트워크 구축
Geth 초기화
$ mkdir testnet
$ cd testnet/
$ vi genesis.json
$ geth --datadir /home/vagrant/testnet/
init /home/vagrant/testnet/genesis.json
Genesis 블록(0번째 블록)
: 동일한 블록체인 네트워크에 참가하는 노드는 동일한
Genesis블록으로부터 연결되는 블록체인을 공유
35
60. Express 페이지 생성
$ express --view=pug web3
$ cd web3 && sudo npm install
$ DEBUG=web3 npm start
http://192.168.33.150:3000/
web3 demo
/home/vagrant/web3/public
js, monitor.html 업로드
http://192.168.33.150:3000/monitor.html
60
61. Monitor Example
monitor.html
<script src="./js/bignumber.min.js"></script>
<script src="./js/web3-light.js"></script>
<script>
var web3 = new Web3();
var provider = new web3.providers.HttpProvider(
"http://192.168.33.150:8545");
web3.setProvider(provider);
web3.eth.defaultAccount = web3.eth.accounts[0];
var stop = false;
var startBlockNo = web3.eth.blockNumber - 20;
61
62. Vote Example
VoteContract.sol
contract VoteContract {
//하나의 계정 한번만 투표
mapping(address => bool) voters;
//후보자의 득표수 저장
mapping(string => uint) candidates;
//후보자 리스트
mapping(uint => string) candidateList;
//전체 후보자 수
uint8 numberOfCandidates;
address contractOwner;
} 62
64. function showList() {
var table = doc ocument.getElementById("table1");
var length = vc.getNumOfCandidates();
alert(length);
for (var i = 0; i < length; i++) {
var candidate = vc.getCandidateString(i);
var row=table.insertRow();
var cell1=row.insertCell(0);
var cell2=row.insertCell(1);
cell1.innerHTML = candidate;
cell2.innerHTML = vc.getScore(candidate);
}
}
64
65. 가상 화폐 계약
기본적인 가상 화폐 계약과 추가기능
1. 코인(Coin) 계약
블랙리스트(Blacklist)
캐시백(Cashback)
회원관리(Member)
2. 크라우드 세일(Crowd Sale)
3. 에스크로(Escrow)
65
66. 클라우드 세일(Crowd Sale)
// (1) 소유자 관리용 계약
contract Owned {
...
}
// (2) 회원 관리용 계약
contract Members is Owned {
...
}
// (3) 회원 관리 기능이 구현된 가상 화폐
contract BluewindCoin is Owned {
...
}
// (4) 크라우드 세일
contract Crowdsale is Owned {
...
}
66
67. How to test
1. BluewindCoin
- Deploy: 10000, "BluewindCoin", "bc", 0
2. Clowdsale
- Deploy: 10, 5000, 100, "BluewindCoin CA"
3. Transfer
- "Clowdsale CA", 5000
4. BalanceOf
- BluewindCoin CA 계정 -> 5000
- Clowdsale CA 계정 -> 5000
5. Start
- 10
67
68. How to test (con't)
6. Fallback
- 1번 계정 선택, Value 5 ether, (fallback)
- 2번 계정 선택, Value 5 ether, (fallback)
7. checkGoalReached, fundingGoalReached
- 10분간 대기
- 0번 계정 선택
- checkGoalReached, fundingGoalReached
8. withdrawal
- 1번 계정선택, withdrawal
- 2번 계정선택, withdrawal
9. withdrawalOwner
- 0번 계정에서 withdrawalOwner
68
70. Truf e framework for Ethereum
TRUFFLE
Smart Contracts Made Sweeter
Ganache
One Click Blockchain
Drizzle
Fresh Chain-Data For Front-Ends
Truf e is the most popular development
framework for Ethereum with a mission to make
your life a whole lot easier.
“
“
70
71. 설치
$ npm install -g truffle
$ truffle version
Truffle v4.1.13 (core: 4.1.13)
Solidity v0.4.24 (solc-js)
프로젝트 생성
$ mkdir sample
$ cd sample
$ truffle init
71
72. 프로젝트 폴더 구조
파일/폴더명 비고
contracts 스마튼 컨트랙트 소스 파일 폴더
migrations 배포를위한 스크립트 파일 폴더
test
개발된 컨트랙트를 테스트 하기 위한
폴더
truf e.js truf e 설정 파일 (default)
truf e-
con g.js
truf e 설정 파일 (truf e.js 파일이 없
는경우)
72
74. 배포 스크립트
migrations/2_deploy_sample.js
var SimpleStorage = artifacts.require("SimpleStorage");
module.exports = function(deployer) {
deployer.deploy(SimpleStorage);
};
Note: 기본적으로 migrations 폴더 아래에 포함된 모든
파일을 실행 하기 때문에 배포 시 우선순위를 주기 위해
앞에 숫자를 붙여 준다.
74
75. 테스트 컨트랙트
# test/TestSimpleStorage.sol
pragma solidity ^0.4.17;
import "truffle/Assert.sol";
import "truffle/DeployedAddresses.sol";
import "../contracts/SimpleStorage.sol";
contract TestSimpleStorage {
function testSimpleStorage() public {
SimpleStorage ss = new SimpleStorage();
uint expected = 4;
ss.set(expected);
Assert.equal(ss.get(), expected,
"value equal test");
}
}
75
77. Private Keys:
(0) c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a
(1) ae6ae8e5ccbfb04590405997ee2d52d2b330726137b875053c36d94e974
(2) 0dbbe8e4ae425a6d2687f1a7e3ba17bc98c673636790f1b8ad91193
(3) c88b703fb08cbea894b6aeff5a544fb92e78a18e19814cd85da83b71
(4) 388c684f0ba1ef5017716adb5d21a053ea8e90277d0868337519f97
(5) 659cbb0e2411a44db63778987b1e22153c086a95eb6b18bdf89de078917
(6) 82d052c865f5763aad42add438569276c00d3d88a2d062d36b2bae914
(7) aa3680d5d48a8283413f7a108367c7299ca73f553735860a87b08f393956
(8) 0f62d96d6675f32685bbdb8ac13cda7c23436f63efbb9d07700d8669
(9) 8d5366123cb560bb606379f90a0bfd4769eecc0557f1b362dcae9012
Mnemonic: candy maple cake sugar pudding cream
honey rich smooth crumble sweet treat
⚠ Important ⚠ : This mnemonic was created for you by
Truffle. It is not secure. Ensure you do not use it on
production blockchains, or else you risk losing funds.
truffle(develop)>
77
79. 배포 및 테스트
컴파일
truffle(develop)> compile
Compiling .contractsMigrations.sol...
Writing artifacts to .buildcontracts
배포
truffle(develop)> migrate
Using network 'develop'.
79
80. 테스트
truffle(develop)> test
Using network 'develop'.
Compiling .contractsSimpleStorage.sol...
Compiling .testTestSimpleStorage.sol...
Compiling truffle/Assert.sol...
Compiling truffle/DeployedAddresses.sol...
TestSimpleStorage
√ testSimpleStorage (83ms)
1 passing (760ms)
80
81. Bank Example
트러플 프로젝트 생성
$ mkdir bank
$ cd bank
$ truffle init
Bank Contract
$ truffle create contract Bank
Truf e 과 Ganache를 이용하여 이더리움 은행을
개발
“
“
81
82. # contracts/Bank.sol
pragma solidity ^0.4.4;
contract Bank {
address public owner; //계좌의 소유주
function Bank(address _owner) public {
owner = _owner;
}
//입금
function deposit() public payable {
require(msg.value > 0);
}
//출금
function withdraw() public {
require(msg.sender == owner);
owner.transfer(address(this).balance);
}
} 82
83. What is a transaction?
The term “transaction” is used in Ethereum to refer
to the signed data package that stores a message to
be sent from an externally owned account to another
account on the blockchain.
83
94. 과일가게 DApp
프로젝트 생성
Truf e Boxes를 이용하여 프로젝트를 생성
$ mkdir fruitshop
$ cd fruitshop
$ truffle unbox react
Shop Contract
$ truffle create contract Shop
contracts/Shop.sol
94