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_encrypt(private_key, passphrase, n=16384, r=8, p=8, compressed=False): # determine the flagbyte if compressed: flagbyte = '\xe0' else: flagbyte = '\xc0' # get the private key's address and equivalent in hex format and wif format k = BitcoinKeypair(private_key) address = k.address() hex_private_key = k.private_key() wif_private_key = k.wif_pk() # calculate the address's checksum address_checksum = sha256(sha256(address).digest()).digest()[0:4] # calculate the scrypt hash and split it in half 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] # combine parts of the private key and scrypt hash and AES encrypt them aes = AES.new(derived_half_2) encrypted_half_1 = aes.encrypt( binascii.unhexlify( '%0.32x' % (long(hex_private_key[0:32], 16) ^ long(binascii.hexlify(derived_half_1[0:16]), 16)) ) ) encrypted_half_2 = aes.encrypt( binascii.unhexlify( '%0.32x' % (long(hex_private_key[32:64], 16) ^ long(binascii.hexlify(derived_half_1[16:32]), 16)) ) ) # build the encrypted private key from the checksum and encrypted parts encrypted_private_key = ('\x42' + flagbyte + address_checksum + encrypted_half_1 + encrypted_half_2) # base 58 encode the encrypted private key b58check_encrypted_private_key = b58check_encode(encrypted_private_key, version_byte=1) # return the encrypted private key return b58check_encrypted_private_key
def broadcast(name, recipient_address, update_hash, private_key, blockchain_client, blockchain_broadcaster=None, tx_only=False, testset=False): if blockchain_broadcaster is None: blockchain_broadcaster = blockchain_client nulldata = build(name, testset=testset) # convert update_hash from a hex string so it looks like an address update_hash_b58 = b58check_encode( unhexlify(update_hash) ) # get inputs and from address private_key_obj, from_address, inputs = analyze_private_key(private_key, blockchain_client) # NAME_IMPORT outputs outputs = make_outputs(nulldata, inputs, recipient_address, from_address, update_hash_b58, format='hex') if tx_only: unsigned_tx = serialize_transaction( inputs, outputs ) return {"unsigned_tx": unsigned_tx} else: # serialize, sign, and broadcast the tx response = serialize_sign_and_broadcast(inputs, outputs, private_key_obj, blockchain_broadcaster) # response = {'success': True } response.update({'data': nulldata}) # return the response return response
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 broadcast(name, recipient_address, update_hash, private_key, blockchain_client, testset=False): nulldata = build(name, testset=testset) # get inputs and from address private_key_obj, from_address, inputs = analyze_private_key( private_key, blockchain_client) # convert update_hash from a hex string so it looks like an address update_hash_b58 = b58check_encode(unhexlify(update_hash)) # build custom outputs here outputs = make_outputs(nulldata, inputs, recipient_address, from_address, update_hash_b58, format='hex') # serialize, sign, and broadcast the tx response = serialize_sign_and_broadcast(inputs, outputs, private_key_obj, blockchain_client) # response = {'success': True } response.update({'data': nulldata}) # return the response return response
def make_transaction(name, recipient_address, update_hash, payment_addr, blockchain_client, tx_fee=0): name = str(name) recipient_address = str(recipient_address) update_hash = str(update_hash) payment_addr = str(payment_addr) tx_fee = int(tx_fee) assert is_name_valid(name) assert len(update_hash) == LENGTH_VALUE_HASH * 2 nulldata = build(name) # convert update_hash from a hex string so it looks like an address update_hash_b58 = pybitcoin.b58check_encode( unhexlify(update_hash), version_byte=virtualchain.version_byte) inputs = tx_get_unspents(payment_addr, blockchain_client) outputs = make_outputs(nulldata, inputs, recipient_address, payment_addr, update_hash_b58, tx_fee) return (inputs, outputs)
def make_transaction(name, recipient_address, update_hash, payment_addr, blockchain_client, tx_fee=0): name = str(name) recipient_address = str(recipient_address) update_hash = str(update_hash) payment_addr = str(payment_addr) tx_fee = int(tx_fee) assert is_name_valid(name) assert len(update_hash) == LENGTHS['update_hash'] * 2 nulldata = build(name) # convert update_hash from a hex string so it looks like an address update_hash_b58 = b58check_encode( unhexlify(update_hash) ) inputs = get_unspents( payment_addr, blockchain_client ) outputs = make_outputs(nulldata, inputs, recipient_address, payment_addr, update_hash_b58, tx_fee) return (inputs, outputs)
def broadcast(name, recipient_address, update_hash, private_key, blockchain_client, testset=False): nulldata = build(name, testset=testset) # get inputs and from address private_key_obj, from_address, inputs = analyze_private_key(private_key, blockchain_client) # convert update_hash from a hex string so it looks like an address update_hash_b58 = b58check_encode( unhexlify(update_hash) ) # build custom outputs here outputs = make_outputs(nulldata, inputs, recipient_address, from_address, update_hash_b58, format='hex') # serialize, sign, and broadcast the tx response = serialize_sign_and_broadcast(inputs, outputs, private_key_obj, blockchain_client) # response = {'success': True } response.update({'data': nulldata}) # return the response return response
def btc_decoderawtransaction_compat( tx_hex ): """ Implementation of bitcoind's decoderawtransaction JSONRPC method. Tries to be faithful enough to bitcoind for virtualchain's sake. Does NOT handle coinbase transactions """ inputs, outputs, locktime, version = tx_deserialize( tx_hex ) txid = make_txid( tx_hex ) vin = [] vout = [] for inp in inputs: vin_inp = { "txid": inp['transaction_hash'], "vout": inp['output_index'], } if inp.has_key("script_sig"): scriptsig_hex = inp['script_sig'] scriptsig_asm = btc_decoderawtransaction_script_hex_to_asm( scriptsig_hex ) vin_inp['scriptSig'] = { 'asm': scriptsig_asm, 'hex': scriptsig_hex } if inp.has_key("sequence"): vin_inp['sequence'] = inp['sequence'] vin.append( vin_inp ) for i in xrange( 0, len(outputs) ): out = outputs[i] script_type = btc_decoderawtransaction_get_script_type( out['script_hex'] ) addresses = [] if script_type == "pubkeyhash": addresses.append( pybitcoin.script_hex_to_address( out['script_hex'] ) ) elif script_type == "pubkey": pubkey = btc_decoderawtransaction_get_pubkey_from_script( out['script_hex'] ) addr = pybitcoin.BitcoinPublicKey( pubkey ).address() addresses.append( addr ) elif script_type == "scripthash": script_hash = btc_decoderawtransaction_get_script_hash_from_script( out['script_hex'] ) addr = pybitcoin.b58check_encode( binascii.unhexlify( script_hash ), version_byte=5 ) addresses.append( addr ) vout_out = { "value": float(out['value']) / 10e7, "mock_bitcoind_value_satoshi": out['value'], # NOTE: extra "n": i, "scriptPubKey": { 'asm': btc_decoderawtransaction_script_hex_to_asm( out['script_hex'] ), 'hex': out['script_hex'], "type": script_type }, } if script_type in ["pubkeyhash", "pubkey", "scripthash"]: vout_out['scriptPubKey']['reqSigs'] = 1 if len(addresses) > 0: vout_out['scriptPubKey']['addresses'] = addresses vout.append( vout_out ) tx_decoded = { "txid": txid, "version": version, "locktime": locktime, "vin": vin, "vout": vout } return tx_decoded
def addr_reencode( addr ): """ Encode addr to testnet """ return pybitcoin.b58check_encode( pybitcoin.b58check_decode( addr ), version_byte=111 )