newuser1
newuser1

Reputation: 11

adding a generated phrase into a url

I have a script I am playing with obtained on the net It works but there is another step I am trying to figure out

import ecdsa
import ecdsa.der
import ecdsa.util
import hashlib
import os
import re
import struct

b58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

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
private_key = os.urandom(32).encode('hex')

print "%s"% privateKeyToWif(private_key)
print "%s"% keyToAddr(private_key)

import urllib

link = "http://127.0.0.1:3001/insight-api/addr/(ADDR)/balance"
f = urllib.urlopen(link)
myfile = f.read()
print myfile

the last part is what I am working on. I also found that on the net, I have tried a few options but nothing, only thing that is missing that I am working on next is print the results into a txt file.

Upvotes: 1

Views: 99

Answers (2)

Findlay Smith
Findlay Smith

Reputation: 109

with python you can treat URLs as strings, as such to modify a URL to add a certain address you can do the following link = str("http://127.0.0.1:3001/insight-api/addr/" + str(ADDR) + "/balance") assuming I've interpreted your question correctly

Upvotes: 1

M. Powell
M. Powell

Reputation: 69

Can you be more specific with the question?

My interpretation of your question is that you are trying to replace the (ADDR) in

link = "http://127.0.0.1:3001/insight-api/addr/(ADDR)/balance" 

with the value returned from keyToAddr(private_key), and in that case, you should be able to just use

link = "http://127.0.0.1:3001/insight-api/addr/"+keyToAddr(private_key)+"/balance"

Upvotes: 1

Related Questions