-
Notifications
You must be signed in to change notification settings - Fork 2
/
addresses.py
227 lines (188 loc) · 5.16 KB
/
addresses.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import hashlib
import os
import ecdsa
import ecdsa.der
import ecdsa.util
from bitcoin import *
import json
import requests
import node
b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
secure_key_length=60
def base58encode(n):
result = ''
while n > 0:
result = b58[n%58] + result
n /= 58
return result
def base256decode(s):
result = 0
for c in s:
result = result * 256 + ord(c)
return result
def countLeadingChars(s, ch):
count = 0
for c in s:
if c == ch:
count += 1
else:
break
return count
# https://en.bitcoin.it/wiki/Base58Check_encoding
def base58CheckEncode(version, payload):
s = chr(version) + payload
checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
result = s + checksum
leadingZeros = countLeadingChars(result, '\0')
return '1' * leadingZeros + base58encode(base256decode(result))
def privateKeyToWif(key_hex):
return 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 base58CheckEncode(0, ripemd160.digest())
def keyToAddr(s):
return pubKeyToAddr(privateKeyToPublicKey(s))
# Generate a random private key
def generate_subkeys():
a=[]
a.append(os.urandom(subkey_complexity).encode('hex')) #subkey1
a.append(os.urandom(subkey_complexity).encode('hex')) #subkey2
return a
def generate_privatekey(phrase):
keysum=phrase
secret_exponent=hashlib.sha256(keysum).hexdigest()
privkey=privateKeyToWif(secret_exponent)
return privkey
def generate_publicaddress(phrase):
keysum=phrase
secret_exponent=hashlib.sha256(keysum).hexdigest()
address=keyToAddr(secret_exponent)
return address
def getunspent(publicaddress): #REPLACE SOMEDAY WITH LOCAL
url= "https://blockchain.info/unspent?active="+publicaddress
a=requests.get(url)
return json.loads(a.content)['unspent_outputs']
def txs_received_by_address(publicaddress):
global transactions
url='http://blockchain.info/address/'+str(publicaddress)+'?format=json'
response=requests.get(url)
transactions=json.loads(response.content)
transactions=transactions['txs']
receivedtxs=[]
for tx in transactions:
tome=False
for outp in tx['out']:
if 'addr' in outp:
if outp['addr']==publicaddress:
tome=True
if tome:
receivedtxs.append(tx)
return receivedtxs
def txs_sent_by_address(publicaddress):
url='http://blockchain.info/address/'+str(publicaddress)+'?format=json'
response=requests.get(url)
transactions=json.loads(response.content)
transactions=transactions['txs']
senttxs=[]
for tx in transactions:
fromme=False
for inp in tx['inputs']:
if inp['prev_out']['addr']==publicaddress:
fromme=True
if fromme:
senttxs.append(tx)
return senttxs
def find_opreturns_sent_by_address(publicaddress):
txlist=txs_sent_by_address(publicaddress)
scriptlist=[]
for tx in txlist:
n=0
for out in tx['out']:
n=n+1
script=out['script']
if script[0:2]=='6a': #IS OP RETURN
#print script
txidentifier=str(tx['hash'])+":"+str(n)
r=[]
r.append(script[4:len(script)].decode('hex'))
r.append(txidentifier)
scriptlist.append(r)
return scriptlist
def read_opreturns_sent_by_address(publicaddress):
readdata=find_opreturns_sent_by_address(publicaddress)
text=[]
results=[]
for x in readdata:
text.append(x[0])
n=0
for x in text:
strin=x[2:len(x)]
x=x[0:2]
print x
try:
intversion=int(x)
#print intversion
results.append([intversion,strin])
except:
a=0
answer=''
sortedresults=['']*100
for x in results:
sortedresults[x[0]]=x[1]
for x in sortedresults:
answer=answer+x
return answer
def generate_secure_pair():
randomkey=os.urandom(secure_key_length)
public=generate_publicaddress(randomkey)
private=generate_privatekey(randomkey)
results = {}
results['public_address']=public
results['private_key']=private
return results
def unspent_value(public_address):
unspents=unspent(public_address)
value=0.0
for unsp in unspents:
value=value+float(unsp['value'])
value=value/100000000
return value
#a=read_opreturns_sent_by_address('173CJ9wxuZFbJyDbkJ89AfpAkqx5PatxMk')
b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
import cointools
def checkphrase(phrase):
a=generate_publicaddress(phrase)
r=cointools.unspent(a)
if len(r)>0:
print r
print phrase
return phrase
def int_to_phrase(intcheck):
r=intcheck
d=[]
while r>0:
a=r%58
d.append(a)
r=r-a
r=r/58
e=''
for x in d:
e=e+str(b58[x])
return e
import math
def check_int_range(loglimit):
a=math.pow(58,loglimit)
b=0
while b<a:
strin=int_to_phrase(b)
val=checkphrase(strin)
if not val is None:
b=a
else:
print str(b)+" "+str(strin)
b=b+1