|
| 1 | +import hashlib |
| 2 | +import os |
| 3 | +import ecdsa |
| 4 | +import ecdsa.der |
| 5 | +import ecdsa.util |
| 6 | +from bitcoin import * |
| 7 | +import json |
| 8 | +import requests |
| 9 | +import node |
| 10 | + |
| 11 | +b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' |
| 12 | + |
| 13 | +secure_key_length=60 |
| 14 | + |
| 15 | +def base58encode(n): |
| 16 | + result = '' |
| 17 | + while n > 0: |
| 18 | + result = b58[n%58] + result |
| 19 | + n /= 58 |
| 20 | + return result |
| 21 | + |
| 22 | +def base256decode(s): |
| 23 | + result = 0 |
| 24 | + for c in s: |
| 25 | + result = result * 256 + ord(c) |
| 26 | + return result |
| 27 | + |
| 28 | +def countLeadingChars(s, ch): |
| 29 | + count = 0 |
| 30 | + for c in s: |
| 31 | + if c == ch: |
| 32 | + count += 1 |
| 33 | + else: |
| 34 | + break |
| 35 | + return count |
| 36 | + |
| 37 | +# https://en.bitcoin.it/wiki/Base58Check_encoding |
| 38 | +def base58CheckEncode(version, payload): |
| 39 | + s = chr(version) + payload |
| 40 | + checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4] |
| 41 | + result = s + checksum |
| 42 | + leadingZeros = countLeadingChars(result, '\0') |
| 43 | + return '1' * leadingZeros + base58encode(base256decode(result)) |
| 44 | + |
| 45 | +def privateKeyToWif(key_hex): |
| 46 | + return base58CheckEncode(0x80, key_hex.decode('hex')) |
| 47 | + |
| 48 | +def privateKeyToPublicKey(s): |
| 49 | + sk = ecdsa.SigningKey.from_string(s.decode('hex'), curve=ecdsa.SECP256k1) |
| 50 | + vk = sk.verifying_key |
| 51 | + return ('\04' + sk.verifying_key.to_string()).encode('hex') |
| 52 | + |
| 53 | +def pubKeyToAddr(s): |
| 54 | + ripemd160 = hashlib.new('ripemd160') |
| 55 | + ripemd160.update(hashlib.sha256(s.decode('hex')).digest()) |
| 56 | + return base58CheckEncode(0, ripemd160.digest()) |
| 57 | + |
| 58 | +def keyToAddr(s): |
| 59 | + return pubKeyToAddr(privateKeyToPublicKey(s)) |
| 60 | + |
| 61 | +# Generate a random private key |
| 62 | +def generate_subkeys(): |
| 63 | + a=[] |
| 64 | + a.append(os.urandom(subkey_complexity).encode('hex')) #subkey1 |
| 65 | + a.append(os.urandom(subkey_complexity).encode('hex')) #subkey2 |
| 66 | + return a |
| 67 | + |
| 68 | +def generate_privatekey(phrase): |
| 69 | + keysum=phrase |
| 70 | + secret_exponent=hashlib.sha256(keysum).hexdigest() |
| 71 | + |
| 72 | + privkey=privateKeyToWif(secret_exponent) |
| 73 | + return privkey |
| 74 | + |
| 75 | +def generate_publicaddress(phrase): |
| 76 | + keysum=phrase |
| 77 | + secret_exponent=hashlib.sha256(keysum).hexdigest() |
| 78 | + address=keyToAddr(secret_exponent) |
| 79 | + return address |
| 80 | + |
| 81 | +def getunspent(publicaddress): #REPLACE SOMEDAY WITH LOCAL |
| 82 | + url= "https://blockchain.info/unspent?active="+publicaddress |
| 83 | + a=requests.get(url) |
| 84 | + return json.loads(a.content)['unspent_outputs'] |
| 85 | + |
| 86 | +def txs_received_by_address(publicaddress): |
| 87 | + global transactions |
| 88 | + url='http://blockchain.info/address/'+str(publicaddress)+'?format=json' |
| 89 | + response=requests.get(url) |
| 90 | + transactions=json.loads(response.content) |
| 91 | + transactions=transactions['txs'] |
| 92 | + |
| 93 | + receivedtxs=[] |
| 94 | + |
| 95 | + for tx in transactions: |
| 96 | + tome=False |
| 97 | + for outp in tx['out']: |
| 98 | + if 'addr' in outp: |
| 99 | + if outp['addr']==publicaddress: |
| 100 | + tome=True |
| 101 | + if tome: |
| 102 | + receivedtxs.append(tx) |
| 103 | + |
| 104 | + return receivedtxs |
| 105 | + |
| 106 | +def txs_sent_by_address(publicaddress): |
| 107 | + url='http://blockchain.info/address/'+str(publicaddress)+'?format=json' |
| 108 | + response=requests.get(url) |
| 109 | + transactions=json.loads(response.content) |
| 110 | + transactions=transactions['txs'] |
| 111 | + |
| 112 | + senttxs=[] |
| 113 | + |
| 114 | + for tx in transactions: |
| 115 | + fromme=False |
| 116 | + for inp in tx['inputs']: |
| 117 | + if inp['prev_out']['addr']==publicaddress: |
| 118 | + fromme=True |
| 119 | + if fromme: |
| 120 | + senttxs.append(tx) |
| 121 | + return senttxs |
| 122 | + |
| 123 | + |
| 124 | +def find_opreturns_sent_by_address(publicaddress): |
| 125 | + txlist=txs_sent_by_address(publicaddress) |
| 126 | + scriptlist=[] |
| 127 | + for tx in txlist: |
| 128 | + |
| 129 | + n=0 |
| 130 | + for out in tx['out']: |
| 131 | + n=n+1 |
| 132 | + script=out['script'] |
| 133 | + if script[0:2]=='6a': #IS OP RETURN |
| 134 | + #print script |
| 135 | + txidentifier=str(tx['hash'])+":"+str(n) |
| 136 | + r=[] |
| 137 | + r.append(script[4:len(script)].decode('hex')) |
| 138 | + r.append(txidentifier) |
| 139 | + scriptlist.append(r) |
| 140 | + return scriptlist |
| 141 | + |
| 142 | +def read_opreturns_sent_by_address(publicaddress): |
| 143 | + readdata=find_opreturns_sent_by_address(publicaddress) |
| 144 | + text=[] |
| 145 | + results=[] |
| 146 | + for x in readdata: |
| 147 | + text.append(x[0]) |
| 148 | + n=0 |
| 149 | + for x in text: |
| 150 | + strin=x[2:len(x)] |
| 151 | + x=x[0:2] |
| 152 | + print x |
| 153 | + try: |
| 154 | + intversion=int(x) |
| 155 | + #print intversion |
| 156 | + results.append([intversion,strin]) |
| 157 | + except: |
| 158 | + a=0 |
| 159 | + answer='' |
| 160 | + |
| 161 | + sortedresults=['']*100 |
| 162 | + for x in results: |
| 163 | + sortedresults[x[0]]=x[1] |
| 164 | + |
| 165 | + for x in sortedresults: |
| 166 | + answer=answer+x |
| 167 | + |
| 168 | + return answer |
| 169 | + |
| 170 | + |
| 171 | +def generate_secure_pair(): |
| 172 | + randomkey=os.urandom(secure_key_length) |
| 173 | + public=generate_publicaddress(randomkey) |
| 174 | + private=generate_privatekey(randomkey) |
| 175 | + |
| 176 | + results = {} |
| 177 | + |
| 178 | + results['public_address']=public |
| 179 | + results['private_key']=private |
| 180 | + return results |
| 181 | + |
| 182 | +def unspent_value(public_address): |
| 183 | + unspents=unspent(public_address) |
| 184 | + value=0.0 |
| 185 | + for unsp in unspents: |
| 186 | + value=value+float(unsp['value']) |
| 187 | + value=value/100000000 |
| 188 | + return value |
| 189 | + |
| 190 | +#a=read_opreturns_sent_by_address('173CJ9wxuZFbJyDbkJ89AfpAkqx5PatxMk') |
| 191 | + |
| 192 | +b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' |
| 193 | + |
| 194 | +import cointools |
| 195 | +def checkphrase(phrase): |
| 196 | + a=generate_publicaddress(phrase) |
| 197 | + r=cointools.unspent(a) |
| 198 | + if len(r)>0: |
| 199 | + print r |
| 200 | + print phrase |
| 201 | + return phrase |
| 202 | + |
| 203 | +def int_to_phrase(intcheck): |
| 204 | + r=intcheck |
| 205 | + d=[] |
| 206 | + while r>0: |
| 207 | + a=r%58 |
| 208 | + d.append(a) |
| 209 | + r=r-a |
| 210 | + r=r/58 |
| 211 | + e='' |
| 212 | + for x in d: |
| 213 | + e=e+str(b58[x]) |
| 214 | + return e |
| 215 | + |
| 216 | +import math |
| 217 | +def check_int_range(loglimit): |
| 218 | + a=math.pow(58,loglimit) |
| 219 | + b=0 |
| 220 | + while b<a: |
| 221 | + strin=int_to_phrase(b) |
| 222 | + val=checkphrase(strin) |
| 223 | + if not val is None: |
| 224 | + b=a |
| 225 | + else: |
| 226 | + print str(b)+" "+str(strin) |
| 227 | + b=b+1 |
0 commit comments