def namecoin_to_bitcoin_address( nmc_address ): """ Convert a namecoin address to a bitcoin address. The only difference is the version number. """ return pybitcoin.b58check_encode( pybitcoin.b58check_decode( nmc_address ), version_byte=0 )
def address_reencode(address): """ Depending on whether or not we're in testnet or mainnet, re-encode an address accordingly. """ vb = pybitcoin.b58check_version_byte(address) if os.environ.get("ZONEFILEMANAGE_TESTNET") == "1": if vb == 0 or vb == 111: # convert to testnet p2pkh vb = 111 elif vb == 5 or vb == 196: # convert to testnet p2sh vb = 196 else: raise ValueError("unrecognized address %s" % address) else: if vb == 0 or vb == 111: # convert to mainnet p2pkh vb = 0 elif vb == 5 or vb == 196: # convert to mainnet p2sh vb = 5 else: raise ValueError("unrecognized address %s" % address) return pybitcoin.b58check_encode(pybitcoin.b58check_decode(address), vb)
def bip38_decrypt(b58check_encrypted_private_key, passphrase, n=16384, r=8, p=8): # decode private key from base 58 check to binary encrypted_private_key = b58check_decode(b58check_encrypted_private_key) # parse the encrypted key different byte sections bip38_key_identification_byte = encrypted_private_key[0:1] flagbyte = encrypted_private_key[1:2] address_checksum = encrypted_private_key[2:6] encrypted_half_1 = encrypted_private_key[6:6+16] encrypted_half_2 = encrypted_private_key[6+16:6+32] # derive a unique key from the passphrase and the address checksum scrypt_derived_key = scrypt.hash(passphrase, address_checksum, n, r, p) derived_half_1 = scrypt_derived_key[0:32] derived_half_2 = scrypt_derived_key[32:64] # decrypt the encrypted halves aes = AES.new(derived_half_2) decrypted_half_1 = aes.decrypt(encrypted_half_1) decrypted_half_2 = aes.decrypt(encrypted_half_2) # get the original private key from the encrypted halves + the derived half decrypted_private_key = '%064x' % (long(binascii.hexlify(decrypted_half_1 + decrypted_half_2), 16) ^ long(binascii.hexlify(derived_half_1), 16)) # get the address corresponding to the private key k = BitcoinKeypair(decrypted_private_key) address = k.address() # make sure the address matches the checksum in the original encrypted key if address_checksum != sha256(sha256(address).digest()).digest()[0:4]: raise ValueError('Invalid private key and password combo.') # return the decrypted private key return k.private_key()
def get_import_update_hash_from_outputs( outputs, recipient ): """ This is meant for NAME_IMPORT operations, which have five outputs: the OP_RETURN, the sender (i.e. the namespace owner), the name's recipient, the name's update hash, and the burn output. This method extracts the name update hash from the list of outputs. By construction, the update hash address in the NAME_IMPORT operation is the first non-OP_RETURN output that is *not* the recipient. """ ret = None count = 0 for output in outputs: output_script = output['scriptPubKey'] output_asm = output_script.get('asm') output_hex = output_script.get('hex') output_addresses = output_script.get('addresses') if output_asm[0:9] != 'OP_RETURN' and output_hex is not None and output_hex != recipient: ret = hexlify( b58check_decode( str(output_addresses[0]) ) ) break if ret is None: raise Exception("No update hash found") return ret
def get_script_pubkey_from_addr( address ): """ Make a p2pkh script from an address """ hash160 = pybitcoin.b58check_decode(address).encode('hex') script_pubkey = pybitcoin.script_to_hex( 'OP_DUP OP_HASH160 %s OP_EQUALVERIFY OP_CHECKSIG' % hash160) return script_pubkey
def get_import_update_hash_from_outputs(outputs, recipient): """ This is meant for NAME_IMPORT operations, which have five outputs: the OP_RETURN, the sender (i.e. the namespace owner), the name's recipient, the name's update hash, and the burn output. This method extracts the name update hash from the list of outputs. By construction, the update hash address in the NAME_IMPORT operation is the first non-OP_RETURN output that is *not* the recipient. """ ret = None count = 0 for output in outputs: output_script = output['scriptPubKey'] output_asm = output_script.get('asm') output_hex = output_script.get('hex') output_addresses = output_script.get('addresses') if output_asm[ 0: 9] != 'OP_RETURN' and output_hex is not None and output_hex != recipient: ret = b58check_decode(str(output_addresses[0])) break if ret is None: raise Exception("No update hash found") return ret
def bip38_decrypt(b58check_encrypted_private_key, passphrase, n=16384, r=8, p=8): # decode private key from base 58 check to binary encrypted_private_key = b58check_decode(b58check_encrypted_private_key) # parse the encrypted key different byte sections bip38_key_identification_byte = encrypted_private_key[0:1] flagbyte = encrypted_private_key[1:2] address_checksum = encrypted_private_key[2:6] encrypted_half_1 = encrypted_private_key[6:6 + 16] encrypted_half_2 = encrypted_private_key[6 + 16:6 + 32] # derive a unique key from the passphrase and the address checksum scrypt_derived_key = scrypt.hash(passphrase, address_checksum, n, r, p) derived_half_1 = scrypt_derived_key[0:32] derived_half_2 = scrypt_derived_key[32:64] # decrypt the encrypted halves aes = AES.new(derived_half_2) decrypted_half_1 = aes.decrypt(encrypted_half_1) decrypted_half_2 = aes.decrypt(encrypted_half_2) # get the original private key from the encrypted halves + the derived half decrypted_private_key = '%064x' % ( long(binascii.hexlify(decrypted_half_1 + decrypted_half_2), 16) ^ long(binascii.hexlify(derived_half_1), 16)) # get the address corresponding to the private key k = BitcoinKeypair(decrypted_private_key) address = k.address() # make sure the address matches the checksum in the original encrypted key if address_checksum != sha256(sha256(address).digest()).digest()[0:4]: raise ValueError('Invalid private key and password combo.') # return the decrypted private key return k.private_key()
def address_reencode( address ): """ Depending on whether or not we're in testnet or mainnet, re-encode an address accordingly. """ vb = pybitcoin.b58check_version_byte( address ) if os.environ.get("BLOCKSTACK_TESTNET") == "1": if vb == 0 or vb == 111: # convert to testnet p2pkh vb = 111 elif vb == 5 or vb == 196: # convert to testnet p2sh vb = 196 else: raise ValueError("unrecognized address %s" % address) else: if vb == 0 or vb == 111: # convert to mainnet p2pkh vb = 0 elif vb == 5 or vb == 196: # convert to mainnet p2sh vb = 5 else: raise ValueError("unrecognized address %s" % address) return pybitcoin.b58check_encode( pybitcoin.b58check_decode(address), vb )
def addr_reencode( addr ): """ Encode addr to testnet """ return pybitcoin.b58check_encode( pybitcoin.b58check_decode( addr ), version_byte=111 )
def testnet_encode( pk_wif ): s = pybitcoin.b58check_decode(pk_wif ) s = '\xef' + s ret = base58.b58encode( s + pybitcoin.bin_double_sha256(s)[0:4] ) return ret