mirror of
https://github.com/efecini/mooniversity_examples.git
synced 2025-06-06 01:12:04 +00:00
commit
874888a595
62
1_notes.py
Normal file
62
1_notes.py
Normal file
@ -0,0 +1,62 @@
|
||||
|
||||
# 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
|
||||
|
||||
N = 115792089237316195423570985008687907852837564279074904382605163141518161494337
|
||||
big_number = 105936749493397165751943248390023977685888340619776708311600296471039819517671
|
||||
|
||||
class RPC:
|
||||
|
||||
def __init__(self, uri):
|
||||
self.rpc = AuthServiceProxy(uri)
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""Hack to establish a new AuthServiceProxy every time this is called"""
|
||||
return getattr(self.rpc, name)
|
||||
|
||||
rpc_template = "http://%s:%s@%s:%s"
|
||||
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())
|
||||
"""
|
26
3_digital_signatures.py
Normal file
26
3_digital_signatures.py
Normal file
@ -0,0 +1,26 @@
|
||||
### Digital Signatures ###
|
||||
|
||||
from helpers import mainnet,testnet
|
||||
from pprint import pprint
|
||||
|
||||
#sign and verify messages like this: signmessage "address" "message"
|
||||
|
||||
#First we have to get an address:
|
||||
address = testnet.getnewaddress('','legacy')
|
||||
print(f"Address created with private key is:{address}")
|
||||
|
||||
#Create our message. Just a basic string
|
||||
msg = 'this is a message'
|
||||
print(f"Message is:{msg}")
|
||||
|
||||
#Create the signature by signing the address with a message. It returns you a signature
|
||||
signature = testnet.signmessage(address, msg)
|
||||
print(f"Signature created with address for the message {msg} is:{signature}")
|
||||
|
||||
#Verify that our signature is valid with my address for 'my message' msg
|
||||
verified = testnet.verifymessage(address, signature, 'this is a message')
|
||||
print(f"Verified?:{verified}")
|
||||
|
||||
#If you try the worng msg to sign, it will print false.
|
||||
verified2 = testnet.verifymessage(address, signature, 'this is a message too')
|
||||
print(f"Verified?:{verified2}")
|
36
4_hashing.py
Normal file
36
4_hashing.py
Normal file
@ -0,0 +1,36 @@
|
||||
### HASHING ###
|
||||
|
||||
from helpers import mainnet,testnet
|
||||
from pprint import pprint
|
||||
|
||||
# Hash functions takes data and gives you a fingerprint.You CANT reverse it
|
||||
|
||||
# This is a built in library in python
|
||||
# To get more information on hashlib: help(hashlib)
|
||||
import hashlib
|
||||
|
||||
# hashlib onjects return a hash obj
|
||||
msg = b'hello, i am efe'
|
||||
hash1 = hashlib.sha256(msg)
|
||||
print(hash1,'\n')
|
||||
|
||||
# with digest we get the data in that hash object
|
||||
hash2 = hashlib.sha256(msg).digest()
|
||||
print(hash2,'\n')
|
||||
|
||||
# with hexdigest we get the hexadecimal data in that hash object
|
||||
# Hexadecimal(means 16) characters: 0123456789ABCDEF
|
||||
hash3 = hashlib.sha256(msg).hexdigest()
|
||||
print(hash3,'\n')
|
||||
|
||||
# Example
|
||||
# Get a string, turn it into bytes and hash it.
|
||||
msg2 = input('Enter a string:')
|
||||
print(msg2.encode()) #turns into byte
|
||||
|
||||
#You must change msg to the bytes with encode befre hashing
|
||||
#hash5 = hashlib.sha256(msg2).hexdigest() #fail.
|
||||
#print(hash5)
|
||||
|
||||
hash4 = hashlib.sha256(msg2.encode()).hexdigest() #get the hash with encoded string
|
||||
print(hash4)
|
17
5_proof_of_work.py
Normal file
17
5_proof_of_work.py
Normal file
@ -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))
|
17
6_bitcoin_blockreward_calculator.py
Normal file
17
6_bitcoin_blockreward_calculator.py
Normal file
@ -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.")
|
20
7_transactions.py
Normal file
20
7_transactions.py
Normal file
@ -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))
|
@ -1,15 +0,0 @@
|
||||
from helpers import mainnet
|
||||
from decimal import Decimal
|
||||
|
||||
def calculate_bitcoin_cycle_and_block_reward():
|
||||
#This little program tells you 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.")
|
24
my2.py
Normal file
24
my2.py
Normal file
@ -0,0 +1,24 @@
|
||||
"""
|
||||
_ = set(map(int, input().split()))
|
||||
arr = input().split()
|
||||
s1 = set(map(int, input().split()))
|
||||
s2 = set(map(int, input().split()))
|
||||
|
||||
print(s1)
|
||||
print(s2)
|
||||
print(s3)
|
||||
print(sum([(i in s1)-(i in s2) for i in arr]))
|
||||
|
||||
n, m = raw_input().split()
|
||||
|
||||
sc_ar = raw_input().split()
|
||||
|
||||
A = set(raw_input().split())
|
||||
B = set(raw_input().split())
|
||||
print sum([(i in A) - (i in B) for i in sc_ar])
|
||||
"""
|
||||
A = set(input().split())
|
||||
B = set(input().split())
|
||||
print(A)
|
||||
print(B)
|
||||
print(A.issuperset(B))
|
60
notes_01.py
60
notes_01.py
@ -1,60 +0,0 @@
|
||||
'''
|
||||
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
|
||||
/
|
||||
Code:
|
||||
from bitcoinrpc.authproxy import AuthServiceProxy
|
||||
|
||||
N = 115792089237316195423570985008687907852837564279074904382605163141518161494337
|
||||
big_number = 105936749493397165751943248390023977685888340619776708311600296471039819517671
|
||||
|
||||
class RPC:
|
||||
|
||||
def __init__(self, uri):
|
||||
self.rpc = AuthServiceProxy(uri)
|
||||
|
||||
def __getattr__(self, name):
|
||||
"""Hack to establish a new AuthServiceProxy every time this is called"""
|
||||
return getattr(self.rpc, name)
|
||||
|
||||
rpc_template = "http://%s:%s@%s:%s"
|
||||
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())
|
||||
'''
|
Loading…
x
Reference in New Issue
Block a user