비트코인 개념 - NomadJin/Blockchain-Study GitHub Wiki
Bitcoins the hard way : Using the raw Bitcoin protocol
A quick overview of Bitcoin
- A relatively new digital currency that can be transmitted across the Internet
- Consist of entries in a distributed database
- Bitcoins are owned by a Bitcoin address
Bitcoin transactions
- A key innovation of Bitcoin is how transactions are recorded in the distributed database through mining.
- Transactions are grouped into blocks and about every 10 minutes.
- Bitcoin mining is the process that puts transactions into a block.
- Mining is also the mechanism for new bitcoins to enter the system.
- The difficulty and competitiveness of mining is a key part of Bitcoin security, since it ensures that nobody cand flood the system with bad blocks.
The peer-to-peer network
- There is no centralized Bitcoin server.
- Bitcoin runs on a peer-to-peer network.
- The nodes on the network exchange transactions, blocks, and addresses of other peers with each other.
Cryptography(암호학?)
- Bitcoin uses digital signatures to ensure that only the owner of bitcoins can spend them.
- The owner of a Bitcoin address has the private key associated with the address.
- To spend bitcoins, they sign the transaction with this private key, with proves they are the owner
- A public key is associated with each Bitcoin address, and anyone can use it to verify the digital signature.
Diving into the raw Bitcoin protocol
- Bitcoin protocol is harder than I expected.
- Using the protocol directly is that being cryptographic, it is very unforgiving.
- The process of signing a transaction is much more difficult than necessary.
- The public key hash is the Bitcoin address you see published.
Bitcoin address and keys
- The private key is needed to sign a transaction and thus transfer(spend) bitcoins.
- The public key is used to verify the signature on a transaction.
- The public key is not revealed until a transaction is signed.
Generate a private key in WIF format and an address
def privateKeyToWif(key_hex):
return utils.base58CheckEncode(0x80, key_hex.decode('hex'))
def privateKeyToPublicKey(s):
sk = ecdsa.SigningKey.from_string(s.decode('hex'), curve=ecdsa.SECP256k1)
vk = sk.verifying_key
return ('\04' + sk.verifying_key.to_string()).encode('hex')
def pubKeyToAddr(s):
ripemd160 = hashlib.new('ripemd160')
ripemd160.update(hashlib.sha256(s.decode('hex')).digest())
return utils.base58CheckEncode(0, ripemd160.digest())
def keyToAddr(s):
return pubKeyToAddr(privateKeyToPublicKey(s))
# Warning: this random function is not cryptographically strong and is just for example
private_key = ''.join(['%x' % random.randrange(16) for x in range(0, 64)])
print keyUtils.privateKeyToWif(private_key)
print keyUtils.keyToAddr(private_key)
Inside a transaction
- A transaction is the basic operation in the Bitcoin system.
- A Bitcoin transaction moves bitcoins between one or more inputs and outputs.
- Transactions can also include fees.
- If there are any bitcoins left over after adding up the inputs and subtracting the outputs, the remainder is a fee paid to the miner.
Manually creating a transaction
How Bitcoin transactions are signed
The Bitcoin scripting language
- A small program inside each transaction that gets executed to decide if a transaction is valid
- This program is written in Script, the stack-based Bitcoin scripting language.
- In order to ensure that scripts terminate, the language does not contain any looping operation(As a consequence, it is not Turing-complete)
Signing the transaction
- The biggest complication is the signature appears in the middle of the transaction, which raises the question of how to sign the transaction before you have the signature.