def _find_payment(self, msg, txid, txtype): """ search ix/tx for transaction matching current address """ addr = None value = 0 refund = 0 for vout in msg.tx.vout: try: addr = str(P2PKHBitcoinAddress.from_scriptPubKey(vout.scriptPubKey)) except CBitcoinAddressError: continue if addr == self.current_address: value = int(vout.nValue) break # current address received something if addr and value: # calculate under/overpay refund if value < self.vend.cost: refund = AmountToJSON(value) elif value > self.vend.cost: refund = AmountToJSON(value - self.vend.cost) if txid not in self.mempool: self.mempool[txid] = {} # set transaction type, tx refund self.mempool[txid][txtype] = True if txtype == "tx": tx = self.mempool[txid]["processed"] tx["refund"] = AmountToJSON(tx["value"]) self.mempool[txid]["processed"] = { "addr": addr, "value": value, "refund": refund, "sale": (value >= self.vend.cost and True or False), } self.vend.get_next_address(increment=True) return True
def _find_payment(self, msg, txid, txtype): """ search ix/tx for transaction matching current address """ addr = None value = 0 refund = 0 for vout in msg.tx.vout: try: addr = str( P2PKHBitcoinAddress.from_scriptPubKey(vout.scriptPubKey)) except CBitcoinAddressError: continue if addr == self.current_address: value = int(vout.nValue) break # current address received something if addr and value: # calculate under/overpay refund if value < self.vend.cost: refund = AmountToJSON(value) elif value > self.vend.cost: refund = AmountToJSON(value - self.vend.cost) if txid not in self.mempool: self.mempool[txid] = {} # set transaction type, tx refund self.mempool[txid][txtype] = True if txtype == 'tx': tx = self.mempool[txid]['processed'] tx['refund'] = AmountToJSON(tx['value']) self.mempool[txid]['processed'] = { "addr": addr, "value": value, "refund": refund, "sale": (value >= self.vend.cost and True or False) } self.vend.get_next_address(increment=True) return True
utxos = rpc_connection.listunspent() inputs = [] input_value = Decimal(0) for utxo in utxos: if input_value < price + fee: inputs.append({"txid": utxo["txid"], "vout": utxo["vout"]}) input_value += utxo["amount"] else: break # Create outputs my_addr = utxo["address"] change = input_value - price - fee p2pkh = payment_details.outputs[0].script # op_return = payment_details.outputs[1].script[2:].hex() payment_addr = str(P2PKHBitcoinAddress.from_scriptPubKey(p2pkh)) outputs = [ { payment_addr: price # Payment output }, # { # "data": op_return # }, { my_addr: change # Change output } ] # Create tx raw_tx_unsigned = rpc_connection.createrawtransaction(inputs, outputs) signed_raw_tx = bytes.fromhex(
#!/usr/bin/env python3 import bitcoin from bitcoin.wallet import CBitcoinSecret, P2PKHBitcoinAddress bitcoin.SelectParams('testnet') priv_key = 'cUHmPhwxCZN22yJP3orG1pgVSYrNcZtTB3KZMpPHXqyDnfFmzSF4' secret = CBitcoinSecret(priv_key) public_key = secret.pub p2pkh_address = P2PKHBitcoinAddress.from_pubkey(public_key) scriptPubKey = p2pkh_address.to_scriptPubKey() p2pkh_address2 = P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey) print('P2PKH address based on public key: %s' % p2pkh_address) print('P2PKH address based on scriptPubKey: %s' % p2pkh_address2)