Example #1
0
def getWIFPrivateKey(privateKey, networkType):
    
    if networkType == "main":
        mainAddress = "".join(("%x" % MAINNET_ADDRESS_PREFIX, privateKey))
        
        #Calculates the checksum
        oneSHA256MainAddress = SHA256.new(mainAddress.decode("hex")).hexdigest()
        twoSHA256MainAddress = SHA256.new(oneSHA256MainAddress.decode("hex")).hexdigest()
        mainAddressCheckSum = twoSHA256MainAddress[0:CHECKSUM_LENGTH]
        
        mainAddressWithChecksum = "".join((mainAddress, mainAddressCheckSum))
        mainAddressWIF = utils.base58encode(utils.base256decode(mainAddressWithChecksum.decode("hex")))
        return mainAddressWIF
    
    elif networkType == "test":
        testAddress = "".join(("%x" % TESTNET_ADDRESS_PREFIX, privateKey))
        
        #Calculates the checksum
        oneSHA256TestAddress = SHA256.new(testAddress.decode("hex")).hexdigest()
        twoSHA256TestAddress = SHA256.new(oneSHA256TestAddress.decode("hex")).hexdigest()
        testAddressCheckSum = twoSHA256TestAddress[0:CHECKSUM_LENGTH]
        
        testAddressWithChecksum = "".join((testAddress, testAddressCheckSum))
        testAddressWIF = utils.base58encode(utils.base256decode(testAddressWithChecksum.decode("hex")))
        return testAddressWIF
        
    else:
        print sys.stdout.write("getWIFPrivateKey Error") 
    
    return 0
Example #2
0
def getAddress(RIPEMD160SHA256Hash, addressType):
    if addressType == "main_pubkey":
        address = "".join(
            ("%02x" % MAINNET_ADDRESS_PUBKEY_PREFIX, RIPEMD160SHA256Hash))

        #Calculates the checksum
        oneSHA256MainAddress = SHA256.new(address.decode("hex")).hexdigest()
        twoSHA256MainAddress = SHA256.new(
            oneSHA256MainAddress.decode("hex")).hexdigest()
        addressCheckSum = twoSHA256MainAddress[0:CHECKSUM_LENGTH]

        addressWithChecksum = "".join((address, addressCheckSum))
        #print "addr w/ checksum: " + addressWithChecksum
        addressPublicKey = utils.base58encode(
            utils.base256decode(addressWithChecksum.decode("hex")))

        #leading zero to account for adding the 1 prefix to the bitcoin address
        leadingZeros = utils.countLeadingChars(address.decode("hex"), '\0')

        return '1' * leadingZeros + addressPublicKey

    elif addressType == "main_script":
        address = "".join(
            ("%02x" % MAINNET_ADDRESS_SCRIPT_PREFIX, RIPEMD160SHA256Hash))

        #Calculates the checksum
        oneSHA256MainAddress = SHA256.new(address.decode("hex")).hexdigest()
        twoSHA256MainAddress = SHA256.new(
            oneSHA256MainAddress.decode("hex")).hexdigest()
        addressCheckSum = twoSHA256MainAddress[0:CHECKSUM_LENGTH]

        addressWithChecksum = "".join((address, addressCheckSum))
        addressPublicKey = utils.base58encode(
            utils.base256decode(addressWithChecksum.decode("hex")))
        return addressPublicKey

    elif addressType == "test":
        address = "".join(
            ("%02x" % TESTNET_ADDRESS_PREFIX, RIPEMD160SHA256Hash))

        #Calculates the checksum
        oneSHA256MainAddress = SHA256.new(address.decode("hex")).hexdigest()
        twoSHA256MainAddress = SHA256.new(
            oneSHA256MainAddress.decode("hex")).hexdigest()
        addressCheckSum = twoSHA256MainAddress[0:CHECKSUM_LENGTH]

        addressWithChecksum = "".join((address, addressCheckSum))
        addressPublicKey = utils.base58encode(
            utils.base256decode(addressWithChecksum.decode("hex")))
        return addressPublicKey

    else:
        print sys.stdout.write("getWIFPrivateKey Error")

    return 0
Example #3
0
def getAddress(RIPEMD160SHA256Hash, addressType):
    if addressType == "main_pubkey":
        address = "".join(("%02x" % MAINNET_ADDRESS_PUBKEY_PREFIX, RIPEMD160SHA256Hash))
        
        #Calculates the checksum
        oneSHA256MainAddress = SHA256.new(address.decode("hex")).hexdigest()
        twoSHA256MainAddress = SHA256.new(oneSHA256MainAddress.decode("hex")).hexdigest()
        addressCheckSum = twoSHA256MainAddress[0:CHECKSUM_LENGTH]
        
        addressWithChecksum = "".join((address, addressCheckSum))
        #print "addr w/ checksum: " + addressWithChecksum
        addressPublicKey = utils.base58encode(utils.base256decode(addressWithChecksum.decode("hex")))
        
        #leading zero to account for adding the 1 prefix to the bitcoin address
        leadingZeros = utils.countLeadingChars(address.decode("hex"), '\0')
        
        return '1' * leadingZeros + addressPublicKey
    
    elif addressType == "main_script":
        address = "".join(("%02x" % MAINNET_ADDRESS_SCRIPT_PREFIX, RIPEMD160SHA256Hash))
        
        #Calculates the checksum
        oneSHA256MainAddress = SHA256.new(address.decode("hex")).hexdigest()
        twoSHA256MainAddress = SHA256.new(oneSHA256MainAddress.decode("hex")).hexdigest()
        addressCheckSum = twoSHA256MainAddress[0:CHECKSUM_LENGTH]
        
        addressWithChecksum = "".join((address, addressCheckSum))
        addressPublicKey = utils.base58encode(utils.base256decode(addressWithChecksum.decode("hex")))
        return addressPublicKey
    
    elif addressType == "test":
        address = "".join(("%02x" % TESTNET_ADDRESS_PREFIX, RIPEMD160SHA256Hash))
        
        #Calculates the checksum
        oneSHA256MainAddress = SHA256.new(address.decode("hex")).hexdigest()
        twoSHA256MainAddress = SHA256.new(oneSHA256MainAddress.decode("hex")).hexdigest()
        addressCheckSum = twoSHA256MainAddress[0:CHECKSUM_LENGTH]
        
        addressWithChecksum = "".join((address, addressCheckSum))
        addressPublicKey = utils.base58encode(utils.base256decode(addressWithChecksum.decode("hex")))
        return addressPublicKey
        
    else:
        print sys.stdout.write("getWIFPrivateKey Error") 
    
    return 0
Example #4
0
def getWIFPrivateKey(privateKey, networkType):

    if networkType == "main":
        mainAddress = "".join(("%x" % MAINNET_ADDRESS_PREFIX, privateKey))

        #Calculates the checksum
        oneSHA256MainAddress = SHA256.new(
            mainAddress.decode("hex")).hexdigest()
        twoSHA256MainAddress = SHA256.new(
            oneSHA256MainAddress.decode("hex")).hexdigest()
        mainAddressCheckSum = twoSHA256MainAddress[0:CHECKSUM_LENGTH]

        mainAddressWithChecksum = "".join((mainAddress, mainAddressCheckSum))
        mainAddressWIF = utils.base58encode(
            utils.base256decode(mainAddressWithChecksum.decode("hex")))
        return mainAddressWIF

    elif networkType == "test":
        testAddress = "".join(("%x" % TESTNET_ADDRESS_PREFIX, privateKey))

        #Calculates the checksum
        oneSHA256TestAddress = SHA256.new(
            testAddress.decode("hex")).hexdigest()
        twoSHA256TestAddress = SHA256.new(
            oneSHA256TestAddress.decode("hex")).hexdigest()
        testAddressCheckSum = twoSHA256TestAddress[0:CHECKSUM_LENGTH]

        testAddressWithChecksum = "".join((testAddress, testAddressCheckSum))
        testAddressWIF = utils.base58encode(
            utils.base256decode(testAddressWithChecksum.decode("hex")))
        return testAddressWIF

    else:
        print sys.stdout.write("getWIFPrivateKey Error")

    return 0
Example #5
0
    '6 - Add the 4 checksum bytes from point 5 at the end of the extended key from point 2'
)
result = s + checksum
print('   ', codecs.encode(result, 'hex').decode().upper())
print(
    '   ',
    codecs.encode(result, 'hex').decode() ==
    '800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D'
    .lower())
leadingZeros = utils.countLeadingChars(result, '\0')

print(
    '7 - Convert the result from a byte string into a base58 string using Base58Check encoding. This is the Wallet Import Format'
)
#print(utils.base256decode( result ))
print('    WIF:', utils.base58encode(utils.base256decode(result)))
print('    WIF:',
      keyUtils.privateKeyToWif(private_key, net=net, compressed='yes'))
print('    WIF:',
      keyUtils.privateKeyToWif(private_key, net=net, compressed='no'))

#https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses
print('****************************************')
print('***** Private key to Bitcoin Address ***')
print('****************************************')

print('0 - Private ECDSA Key')
#private_key = '18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725'
#private_key = '1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD'
print('   ', private_key)
public_key = keyUtils.privateKeyToPublicKey(private_key, net=net)
Example #6
0
print( '4 - Perform SHA-256 hash on result of SHA-256 hash')
second_hash = hashlib.sha256(codecs.decode(first_hash.encode("utf-8"), "hex")).hexdigest()
print ('   ',second_hash.upper())
print('   ',second_hash == '507A5B8DFED0FC6FE8801743720CEDEC06AA5C6FCA72B07C49964492FB98A714'.lower())
print( '5 - Take the first 4 bytes of the second SHA-256 hash, this is the checksum')
checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
print ('   ',hashlib.sha256(hashlib.sha256(s).digest()).hexdigest()[0:8])
print('6 - Add the 4 checksum bytes from point 5 at the end of the extended key from point 2')
result = s + checksum
print ('   ',codecs.encode(result,'hex').decode().upper())
print ('   ',codecs.encode(result,'hex').decode()=='800C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D507A5B8D'.lower())
leadingZeros = utils.countLeadingChars(result, '\0')

print('7 - Convert the result from a byte string into a base58 string using Base58Check encoding. This is the Wallet Import Format')
#print(utils.base256decode( result ))
print ('    WIF:', utils.base58encode(utils.base256decode( result )))
print ('    WIF:', keyUtils.privateKeyToWif( private_key , net=net, compressed='yes'))
print ('    WIF:', keyUtils.privateKeyToWif( private_key , net=net, compressed='no'))


#https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses
print('****************************************')
print('***** Private key to Bitcoin Address ***')
print('****************************************')

print('0 - Private ECDSA Key')
#private_key = '18E14A7B6A307F426A94F8114701E7C8E774E7F9A47E2C2035DB29A206321725'
#private_key = '1E99423A4ED27608A15A2616A2B0E9E52CED330AC530EDCC32C8FFC6A526AEDD'
print('   ',private_key)
public_key  = keyUtils.privateKeyToPublicKey(private_key, net = net) 
print('1 - Public ECDSA Key')