diff --git a/1_notes.py b/1_notes.py index 770ae1b..0517885 100644 --- a/1_notes.py +++ b/1_notes.py @@ -1,36 +1,37 @@ -''' -print(int('b',16), int('62ba1cd8a',16)) # Prints the hexadecimal representation of the value. -/ -testnet : A small replica of bitcoin for developers to test their app without real money. -We use bitcoin-cli command to communicate with a node. It is a command line utility. -In this course we use >testnet rather than >bitcoin-cli. -/ ->testnet help ->mainnet help -Prints all the commands to use in testnet or mainnet -/ ->testnet help getblockchaininfo -Gets the help for the related command -/ ->testnet getblockchaininfo -Makes the query with the method for the testnet. mainnet for the actual chain -/ ->mainnet getmininginfo -Gives the mining info -/ -Check how the nodes talk with each other from https://bitcoin.org/en/developer-reference#protocol-versions -This changes from version to version. ipv4 / ipv6 / onion -/ ->testnet getwalletinfo -Gives the wallet info -/ ->testnet listtransactions -The transactions related to wallet -/ -Code to connect to node -If you don't have bitcoinrpc install it with pip: -pip install python-bitcoinrpc -/ + +# Prints the hexadecimal representation of the value. +print(int('b',16), int('62ba1cd8a',16)) + +#testnet : A small replica of bitcoin for developers to test their app without real money. +#We use bitcoin-cli command to communicate with a node. It is a command line utility. +#In this course we use >testnet rather than >bitcoin-cli. +#Prints all the commands to use in testnet or mainnet +testnet help +mainnet help + +#Gets the help for the related command +testnet help getblockchaininfo + +#Makes the query with the method for the testnet. mainnet for the actual chain +testnet getblockchaininfo + +#Gives the mining info +mainnet getmininginfo + +#Check how the nodes talk with each other from https://bitcoin.org/en/developer-reference#protocol-versions +#This changes from version to version. ipv4 / ipv6 / onion + +#Gives the wallet info +testnet getwalletinfo + +#The transactions related to wallet +testnet listtransactions + +#Code to connect to node +#If you don't have bitcoinrpc install it with pip: +#pip install python-bitcoinrpc + +""" Code: from bitcoinrpc.authproxy import AuthServiceProxy @@ -51,10 +52,11 @@ mainnet = RPC(rpc_template % ('bitcoin', 'python', '68.183.110.103', 8332)) testnet = RPC(rpc_template % ('bitcoin', 'python', 'localhost', 18332)) -/ +""" +""" pprint : A package to print objects pretty.Especially JSON objects. from helpers import testnet from pprint import pprint pprint(testnet.getblockchaininfo()) -''' \ No newline at end of file +""" \ No newline at end of file diff --git a/4_hashing b/4_hashing.py similarity index 100% rename from 4_hashing rename to 4_hashing.py diff --git a/5_proof_of_work.py b/5_proof_of_work.py new file mode 100644 index 0000000..ce4cf3f --- /dev/null +++ b/5_proof_of_work.py @@ -0,0 +1,17 @@ +import hashlib + +#Try to find the hash (of a block) which starts with some amount of zeros. We are impersonating the miners acivities here. + +def hashed(d, n): + d += n.to_bytes(4, 'little') + return hashlib.sha256(d).hexdigest() + +data = b'hash of a block' +nonce = 0 +zeros_required = 4 + +while not (hashed(data, nonce)[:zeros_required] == ('0' * zeros_required)): + print (nonce, hashed(data, nonce)) + nonce +=1 + +print('Found:',nonce,hashed(data, nonce)) \ No newline at end of file diff --git a/6_bitcoin_blockreward_calculator.py b/6_bitcoin_blockreward_calculator.py new file mode 100644 index 0000000..7b7b904 --- /dev/null +++ b/6_bitcoin_blockreward_calculator.py @@ -0,0 +1,17 @@ +from helpers import mainnet, testnet +from decimal import Decimal +from pprint import pprint + +def calculate_bitcoin_cycle_and_block_reward(): + #This little program gives the bitcoin's cycle count and the bitcoin block reward amount + INITIAL_SUBSIDY = 50 + BLOCKS_PER_HALVENING = 210000 + last_block = mainnet.getblockchaininfo()["blocks"] + cycle_count = int(last_block/BLOCKS_PER_HALVENING) + block_reward = INITIAL_SUBSIDY / (cycle_count*2) + return cycle_count+1, block_reward + +if __name__ == "__main__": + cycle, block_reward = calculate_bitcoin_cycle_and_block_reward() + pprint(cycle) + print(f"Bitcoin is in the {cycle}. halvening cycle and the block reward is {block_reward} ₿ right now.") \ No newline at end of file diff --git a/7_transactions.py b/7_transactions.py new file mode 100644 index 0000000..3c28b5b --- /dev/null +++ b/7_transactions.py @@ -0,0 +1,20 @@ +### TRANSACTIONS ### +from helpers import mainnet, testnet +from decimal import Decimal +from pprint import pprint + +#We want to send bitcoin to an address +#help(testnet.sendtoaddress()) bitcoin-cli sendtoaddress "1M72Sfpbz1BPpXFHz9m3CdqATR44Jvaydd" 0.1 +#First send tbtc to your address from a tbtc faucet then eun the program +address = testnet.getnewaddress('','legacy') +address2 = testnet.getnewaddress('','legacy') +print(address) +print(address2) +print(testnet.getbalance()) + +#We have sent the tbtc to an adress Bize output olarak bir tx döndürdü. We have sent tbtc to ourselves actually. It returned the transaction id +tx = testnet.sendtoaddress(address2, 0.0000100) +print(tx) + +#We can get tha transaction and check the input and the outputs +pprint(testnet.gettransaction(tx)) \ No newline at end of file diff --git a/bitcoin_blockreward_calculator.py b/bitcoin_blockreward_calculator.py deleted file mode 100644 index 2d66e45..0000000 --- a/bitcoin_blockreward_calculator.py +++ /dev/null @@ -1,15 +0,0 @@ -from helpers import mainnet -from decimal import Decimal - -def calculate_bitcoin_cycle_and_block_reward(): - #This little program gives the bitcoin's cycle count and the bitcoin block reward amount - INITIAL_SUBSIDY = Decimal(50) - BLOCKS_PER_HALVENING = 210000 - last_block = mainnet.getblockchaininfo()["blocks"] - cycle_count = int(last_block/BLOCKS_PER_HALVENING) - block_reward = INITIAL_SUBSIDY / cycle_count*2 - return {"Cycle":cycle_count+1, "Block_reward":block_reward} - -if __name__ == "__main__": - cycle = calculate_bitcoin_cycle_and_block_reward() - print(f"Bitcoin is in the {cycle.get('Cycle')}. halvening cycle and the block reward is {cycle.get('Block_reward')} ₿ right now.") \ No newline at end of file