Skip to content

Introduction to Bitcoin

changwu edited this page Mar 8, 2016 · 3 revisions

Hash

SHA256 簡單示例

$ echo "hello world" > hello.txt
$ cat hello.txt
hello world
$ sha256sum hello.txt
a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447  hello.txt

沒有這個指令 sha256sum 的處理方式

$ brew install coreutils
$ sudo ln -s /usr/local/bin/gsha256sum /usr/local/bin/sha256sum

簽章

下載 Bitcoin Core lead developer Wladimir van der Laan 的公鑰

$ wget https://bitcoin.org/laanwj-releases.asc
$ gpg --import laanwj-releases.asc
gpg: directory `/Users/user/.gnupg' created
gpg: new configuration file `/Users/user/.gnupg/gpg.conf' created
gpg: WARNING: options in `/Users/user/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/Users/user/.gnupg/secring.gpg' created
gpg: keyring `/Users/user/.gnupg/pubring.gpg' created
gpg: /Users/user/.gnupg/trustdb.gpg: trustdb created
gpg: key 36C2E964: public key "Wladimir J. van der Laan (Bitcoin Core binary release signing key) <laanwj@gmail.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
gpg: no ultimately trusted keys found

如果沒有 gpg

$ brew install gnupg gnupg2

找一份過去他簽過的檔案, 進行驗證

$ wget https://bitcoin.org/bin/bitcoin-core-0.11.0/SHA256SUMS.asc -O shasums.asc
$ gpg --verify shasums.asc
gpg: Signature made Sun Jul 12 10:21:13 2015 CDT using RSA key ID 36C2E964
gpg: Good signature from "Wladimir J. van der Laan (Bitcoin Core binary release signing key) <laanwj@gmail.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 01EA 5486 DE18 A882 D4C2  6845 90C8 019E 36C2 E964

至於這個公鑰是不是真的由 Wladimir van der Laan 本人放出來, gpg 無法幫你驗證

挑其中一個檔案來驗證其完整性

$ wget https://bitcoin.org/bin/bitcoin-core-0.11.0/bitcoin-0.11.0.tar.gz
$ sha256sum bitcoin-0.11.0.tar.gz
51ba1756addfa71567559e3f22331c1d908a63571891287689fff7113035d09f  bitcoin-0.11.0.tar.gz
$ grep 51ba shasums.asc
51ba1756addfa71567559e3f22331c1d908a63571891287689fff7113035d09f  bitcoin-0.11.0.tar.gz

Bitcoin 當中很重要的兩個技術

  1. Cryptographic hashes
  2. Cryptographic signatures

在 Botcoin 的錢包, 包含公鑰, 通常作為收款地址

交易的順序對於錢幣是否重複消費扮演重要的角色, 尤其在去中心化的系統中, 讓多數人對於某筆交易達成已經支付的共識

$ brew install hashcash
$ time hashcash -m -r test > mined.txt
hashcash -m -r test > mined.txt  0.03s user 0.01s system 71% cpu 0.058 total
$ cat mined.txt
1:20:160216:test::Va0JnXwIWkZut1cu:00000000000000eTt
$ cat mined.txt | tr -d "\n" | shasum
00000ac8238b579aeaee384f99c9bd00327962ee  -

difficulty (default = 20)

$ time hashcash -b 25 -m -r test2 > mined2.txt
hashcash -b 25 -m -r test2 > mined2.txt  7.90s user 0.03s system 95% cpu 8.282 total
$ echo -n $( cat mined2.txt ) | shasum
000000386e58f3f37df13d0c38401127e7082779  -

安裝 bitcoin

$ brew install autoconf automake berkeley-db4 libtool boost miniupnpc openssl pkg-config protobuf qt5 libevent
$ git clone https://github.com/bitcoin/bitcoin.git
$ cd bitcoin
$ ./autogen.sh
$ ./configure --without-gui
$ make
$ make check
$ make install

啟動 bitcoin, get genesis block

$ ./bitcoind -daemon
Bitcoin server starting
$ bitcoin-cli getblockhash 0
000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

Use hash to get the contents of the genesis block decoded into JSON

$ bitcoin-cli getblock $( bitcoin-cli getblockhash 0 )
{
  "hash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
  "confirmations": 1865,
  "size": 285,
  "height": 0,
  "version": 1,
  "merkleroot": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
  "tx": [
    "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b"
  ],
  "time": 1231006505,
  "mediantime": 1231006505,
  "nonce": 2083236893,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "0000000000000000000000000000000000000000000000000000000100010001",
  "nextblockhash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048"
}

raw block data (represented as hexadecimal)

$ bitcoin-cli getblock $( bitcoin-cli getblockhash 0 ) false
0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000

Get block 300000

$ bitcoin-cli getblock $( bitcoin-cli getblockhash 300000 ) | less

Clone this wiki locally