-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
38 lines (29 loc) · 997 Bytes
/
functions.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
from hashlib import new
from requests import get, post
from binascii import b2a_hex
OP_EQUAL = '87'
OP_HASH256 = 'aa'
OP_PUSHDATA1 = '0a'
COIN = 100000000
ADDRESS_PREFIX = 50
def broadcast(tx):
url = 'https://explorer.cha.terahash.cl/api/tx/send'
return post(url, data={'rawtx' : tx})
def getHexLen(s):
return '{:02x}'.format(int(len(s)/2))
def getScriptPubKey(s):
hash = new('sha256', new('sha256', s.encode()).digest()).hexdigest()
return OP_HASH256 + getHexLen(hash) + hash + OP_EQUAL
def getBalance(addr):
url = 'http://insight.chaucha.cl/api/addr/'
unspent = get(url + addr + '/utxo').json()
inputs = []
balance = 0
for i in unspent:
if i['confirmations'] >= 1:
input = {'output' : i['txid'] + ':' + str(i['vout']),
'value' : i['satoshis'],
'address' : i['address']}
balance += i['satoshis']
inputs.append(input)
return [inputs, round(balance/COIN, 8)]