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
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
def get160BitHashFromPublicAddress(publicAddress): # Obtains the Hash of the Public Address leadingOnes = utils.countLeadingChars(publicAddress, '1') addressWithChecksum = utils.base256encode(utils.base58decode(publicAddress)) addressWithLeadingZeros = '\0' * leadingOnes + addressWithChecksum[:-CHECKSUM_BYTE_LENGTH] publicAddress160BitHash = addressWithLeadingZeros[1:].encode("hex") # Parses the Public Address for Checksum providedChecksum = addressWithChecksum[-CHECKSUM_BYTE_LENGTH:].encode("hex") # Calculates the Checksum based on Parsed Address oneSHA256MainAddress = SHA256.new(addressWithLeadingZeros) twoSHA256MainAddress = SHA256.new(oneSHA256MainAddress.digest()).hexdigest() calculatedChecksum = twoSHA256MainAddress[0:CHECKSUM_LENGTH] # Checks to make sure the checksum provided matches the calculated checksum # of the 160bit of the public address assert providedChecksum == calculatedChecksum return publicAddress160BitHash
#net = 'test' #compressed='yes' #wif = "cR7UGQyRRJxnXUm3wLrUNWpi4mrRLB6vUQ797KYQrx5UUvPp473y" #Antonopolous #compressed='yes' #wif = "KyBsPXxTuVD82av65KZkrGrWi5qLMah5SdNq6uftawDbgKa2wv6S" #net = 'main' #compressed='no' #wif = "5JG9hT3beGTJuUAmCQEmNaxAuMacCTfXuw1R3FCXig23RQHMr4K" print('1 - Take a Wallet Import Format string') print(' ', wif) print('2 - Convert it to a byte string using Base58Check encoding') leadingOnes = utils.countLeadingChars(wif, '1') s = utils.base256encode(utils.base58decode(wif)) #print(codecs.decode( s[:-4], 'hex' ).decode()) result = '\0' * leadingOnes + binascii.hexlify(s[:-4]).decode() print(' ', result.upper()) print('3 - Drop the last 4 checksum bytes from the byte string') print(' ', result[:-4].upper()) chk = s[-4:] checksum = hashlib.sha256(hashlib.sha256( result.encode('utf-8')).digest()).hexdigest()[0:8] print('4 - Dropping first byte. This is the private key') private_key = result[1:-4].upper() print(' ', result[1:-4].upper()) private_key = codecs.encode(keyUtils.wifToPrivateKey(wif), 'hex').decode() print(' ', codecs.encode(keyUtils.wifToPrivateKey(wif), 'hex').decode())
#net = 'test' #compressed='yes' #wif = "cR7UGQyRRJxnXUm3wLrUNWpi4mrRLB6vUQ797KYQrx5UUvPp473y" #Antonopolous #compressed='yes' #wif = "KyBsPXxTuVD82av65KZkrGrWi5qLMah5SdNq6uftawDbgKa2wv6S" #net = 'main' #compressed='no' #wif = "5JG9hT3beGTJuUAmCQEmNaxAuMacCTfXuw1R3FCXig23RQHMr4K" print('1 - Take a Wallet Import Format string') print(' ',wif) print('2 - Convert it to a byte string using Base58Check encoding') leadingOnes = utils.countLeadingChars(wif, '1') s = utils.base256encode(utils.base58decode(wif)) #print(codecs.decode( s[:-4], 'hex' ).decode()) result = '\0' * leadingOnes + binascii.hexlify( s[:-4] ).decode() print(' ',result.upper()) print('3 - Drop the last 4 checksum bytes from the byte string') print(' ',result[:-4].upper()) chk = s[-4:] checksum = hashlib.sha256(hashlib.sha256(result.encode('utf-8')).digest()).hexdigest()[0:8] print('4 - Dropping first byte. This is the private key') private_key = result[1:-4].upper() print(' ',result[1:-4].upper()) private_key = codecs.encode(keyUtils.wifToPrivateKey(wif),'hex').decode() print(' ',codecs.encode(keyUtils.wifToPrivateKey(wif),'hex').decode()) print(' ',codecs.encode(keyUtils.wifToPrivateKey(wif),'hex'))