-
Notifications
You must be signed in to change notification settings - Fork 7
/
bitcoin.py
executable file
·128 lines (109 loc) · 5.55 KB
/
bitcoin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
import glob, os, time, json, datetime
import urllib.request
from pprint import pprint
def convertTime(time):
return datetime.datetime.fromtimestamp(int(time)).strftime('%Y-%m-%d %H:%M:%S')
def price(balance):
web = urllib.request.urlopen("https://blockchain.info/tobtc?currency=USD&value=1").read()
rate = float(web)
price = ( 1 / rate ) * balance
return price
def SatoshiToBTC(balance):
balance = balance / 100000000.0
return(balance)
def checkBalance(address):
try:
web = urllib.request.urlopen("https://blockchain.info/q/addressbalance/" + address).read()
return int(web)
except Exception as e:
web = 0
print("Connection Error")
def checkBalance(address):
try:
web = urllib.request.urlopen("https://blockchain.info/q/addressbalance/" + address).read()
return int(web)
except Exception as e:
web = 0
print("Connection Error")
def singleAddress(address):
try:
with urllib.request.urlopen("https://blockchain.info/balance?active=" + address) as url:
data = json.loads(url.read().decode())
except Exception as e:
print("Connection Error")
print("Address: " + address)
print("Final balance: " + str(SatoshiToBTC(data.get(address).get('final_balance'))))
print("Equivalent as USD: " + str(price(SatoshiToBTC(data.get(address).get('final_balance')))))
print("Total received: " + str(SatoshiToBTC(data.get(address).get('total_received'))))
print("Number of transactions: " + str(data.get(address).get('n_tx')))
def allTransactions(address):
with urllib.request.urlopen("https://blockchain.info/rawaddr/" + address) as url:
data = json.loads(url.read().decode())
#pprint(data)
#pprint(data['txs'][0])
print("address: " + data['address'])
print("final balance: " + str(data['final_balance']))
print("hash160: " + data['hash160'])
print("n_tx: " + str(data['n_tx']))
print("total_received: " + str(data['total_received']))
print("total_sent: " + str(data['total_sent']))
for x in range(0,len(data['txs'])):
print("# --------- TX : " + str(x) + " -------")
print("block height: " + str(data['txs'][x]['block_height']))
print("hash: " + data['txs'][x]['hash'])
print("lock time: " + str(data['txs'][x]['lock_time']))
print("relayed by: " + str(data['txs'][x]['relayed_by']))
print("size: " + str(data['txs'][x]['size']))
print("time: " + str(data['txs'][x]['time']))
print("tx index: " + str(data['txs'][x]['tx_index']))
print("ver: " + str(data['txs'][x]['ver']))
print("vin sz: " + str(data['txs'][x]['vin_sz']))
print("vout sz: " + str(data['txs'][x]['vout_sz']))
print("weight: " + str(data['txs'][x]['weight']))
#print("inputs: " + str(data['txs'][x]['inputs']))
for y in range(0,len(data['txs'][x]['inputs'])):
print("# --------- RECEIVED : " + str(y) + " -------")
print("sequence: " + str(data['txs'][x]['inputs'][y].get('sequence')))
print("witness: " + data['txs'][x]['inputs'][y].get('witness'))
#print("prev out: " + str(data['txs'][0]['inputs'][0].get('prev_out')))
print("spent: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('spent')))
print("tx index: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('tx_index')))
print("type: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('type')))
print("addr: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('addr')))
print("value: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('value')))
print("n: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('n')))
print("script: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('script')))
#print("out: " + str(data['txs'][0]['out']))
for z in range(0,len(data['txs'][x]['out'])):
print("# --------- SENT : " + str(z) + " -------")
print("spent: " + str(data['txs'][x]['out'][z].get('spent')))
print("tx index: " + str(data['txs'][x]['out'][z].get('tx_index')))
print("type: " + str(data['txs'][x]['out'][z].get('type')))
print("addr: " + data['txs'][x]['out'][z].get('addr'))
print("value: " + str(data['txs'][x]['out'][z].get('value')))
print("n: " + str(data['txs'][x]['out'][z].get('n')))
print("script: " + data['txs'][x]['out'][z].get('script'))
def simpleTransactions(address):
with urllib.request.urlopen("https://blockchain.info/rawaddr/" + address) as url:
data = json.loads(url.read().decode())
print("Address: " + data['address'])
print("Final Balance: " + str(SatoshiToBTC(data['final_balance'])))
print("Transaction count: " + str(data['n_tx']))
print("Total Bitcoins Received: " + str(SatoshiToBTC(data['total_received'])))
print("Total Bitcoins Sent: " + str(SatoshiToBTC(data['total_sent'])))
for x in range(0,len(data['txs'])):
print("# --------- TRANSACTION " + str(x) + " -------")
print("Hash: " + data['txs'][x]['hash'])
print("Size: " + str(data['txs'][x]['size']) + " bytes")
print("Time: " + convertTime(data['txs'][x]['time']))
for y in range(0,len(data['txs'][x]['inputs'])):
print("---RECEIVED " + str(y))
print("Receive Address: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('addr')))
print("Amount: " + str(SatoshiToBTC(data['txs'][x]['inputs'][y].get('prev_out').get('value'))))
print("Spent: " + str(data['txs'][x]['inputs'][y].get('prev_out').get('spent')))
for z in range(0,len(data['txs'][x]['out'])):
print("---SENT " + str(z))
print("Sent Address: " + data['txs'][x]['out'][z].get('addr'))
print("Amount: " + str(SatoshiToBTC(data['txs'][x]['out'][z].get('value'))))
print("Spent: " + str(data['txs'][x]['out'][z].get('spent')))