def main(source, dest, loop): bitcoin.SelectParams('testnet') rpc = bitcoin.rpc.Proxy('http://*****:*****@localhost:18332') script_pub_key = rpc.validateaddress(source)["scriptPubKey"] output_script = bitcoin.core.x(script_pub_key) script_pub_key_dest = rpc.validateaddress(dest)["scriptPubKey"] output_script_dest = bitcoin.core.x(script_pub_key_dest) transaction_provider = asyncio.coroutine(rpc.getrawtransaction) engine = openassets.protocol.ColoringEngine( transaction_provider, openassets.protocol.OutputCache(), loop) unspent_outputs = [] am = 0 for output in listunspent(rpc): if output['scriptPubKey'] == output_script: unspent_outputs.append( openassets.transactions.SpendableOutput( bitcoin.core.COutPoint(output['outpoint'].hash, output['outpoint'].n), (yield from engine.get_output(output['outpoint'].hash, output['outpoint'].n)))) am += output['amount'] builder = openassets.transactions.TransactionBuilder(600) issuance_parameters = openassets.transactions.TransferParameters( unspent_outputs= unspent_outputs, # Unspent outputs the coins are issued from to_script= output_script_dest, # The issued coins are sent back to the same address change_script= output_script, # The bitcoin change is sent back to the same address amount=1) # Issue 1,500 units of the asset transaction = builder.issue(issuance_parameters, metadata=b'Gianpiero_07/2018', fees=1000) send(rpc, transaction)
def create_bill(source,dest,loop,cliente,data,kwh,euro): res = [] #c.liststreamkeyitems("cambio",cliente+"_"+data) if (len(res)>0): print("Bolletta già emessa") exit(-1) euro_chain = kwh*euro bitcoin.SelectParams('testnet') rpc = bitcoin.rpc.Proxy('http://*****:*****@localhost:18332') script_pub_key = rpc.validateaddress(source)["scriptPubKey"] output_script = bitcoin.core.x(script_pub_key) script_pub_key_dest = rpc.validateaddress(dest)["scriptPubKey"] output_script_dest = bitcoin.core.x(script_pub_key_dest) transaction_provider = asyncio.coroutine(rpc.getrawtransaction) engine = openassets.protocol.ColoringEngine(transaction_provider, openassets.protocol.OutputCache(), loop) unspent_outputs = [] am = 0 for output in listunspent(rpc): if output['scriptPubKey'] == output_script: unspent_outputs.append(openassets.transactions.SpendableOutput( bitcoin.core.COutPoint(output['outpoint'].hash, output['outpoint'].n), (yield from engine.get_output(output['outpoint'].hash, output['outpoint'].n)) )) am += output['amount'] builder = openassets.transactions.TransactionBuilder(600) issuance_parameters = openassets.transactions.TransferParameters( unspent_outputs=unspent_outputs, # Unspent outputs the coins are issued from to_script=output_script_dest, # The issued coins are sent back to the same address change_script=output_script, # The bitcoin change is sent back to the same address amount=int(round(euro_chain*100))) # Issue euro_chain units of the asset str_meta = 'EURO_' + cliente + '_' + data transaction = builder.issue(issuance_parameters, metadata=str_meta.encode('utf-8'), fees=1000) send(rpc,transaction)
def pay_bill(source,dest,loop,cliente,data): bitcoin.SelectParams('testnet') rpc = bitcoin.rpc.Proxy('http://*****:*****@localhost:18332') script_pub_key = rpc.validateaddress(source)["scriptPubKey"] output_script = bitcoin.core.x(script_pub_key) script_pub_key_dest = rpc.validateaddress(dest)["scriptPubKey"] output_script_dest = bitcoin.core.x(script_pub_key_dest) cache = openassets.protocol.OutputCache() transaction_provider = asyncio.coroutine(rpc.getrawtransaction) engine = openassets.protocol.ColoringEngine(transaction_provider, cache, loop) find = False unspent_outputs = [] for output in listunspent(rpc): if output['scriptPubKey'] == output_script: transaction_hash = bitcoin.core.lx(output['txid']) output_index = output['outpoint'].n err = False try: colored_output = yield from engine.get_output(transaction_hash, output_index) except bitcoin.core.serialize.DeserializationExtraDataError: print("Un errore") err = True if ((colored_output.asset_id == None) or (err==True)): try: unspent_outputs.append(openassets.transactions.SpendableOutput( bitcoin.core.COutPoint(output['outpoint'].hash, output['outpoint'].n), (yield from engine.get_output(output['outpoint'].hash, output['outpoint'].n)) )) except bitcoin.core.serialize.DeserializationExtraDataError: pass else: output_index=1 colored_output_meta = yield from engine.get_output(transaction_hash, output_index) if (colored_output_meta.script.find(b'EURO')!=-1): meta_out = colored_output_meta.script[colored_output_meta.script.find(b'EURO')+5:] str_cmp = (cliente+"_"+data).encode('utf-8') if (meta_out==str_cmp): find = True assetid = colored_output.asset_id unspent_outputs.append(openassets.transactions.SpendableOutput( bitcoin.core.COutPoint(output['outpoint'].hash, output['outpoint'].n), (yield from engine.get_output(output['outpoint'].hash, output['outpoint'].n)) )) asset_quantity = colored_output.asset_quantity if (find == False): print("Bolletta non emessa o già pagata") exit(-1) builder = openassets.transactions.TransactionBuilder(600) issuance_parameters = openassets.transactions.TransferParameters( unspent_outputs=unspent_outputs, # Unspent outputs the coins are issued from to_script=output_script_dest, # The issued coins are sent back to the same address change_script=output_script, # The bitcoin change is sent back to the same address amount=asset_quantity) # Issue euro_chain units of the asset transaction = builder.transfer_assets(assetid,issuance_parameters,output_script, fees=1000) send(rpc,transaction)
txinfo = rpc.getrawtransaction(args.txid, True) tx = CMutableTransaction.from_tx(txinfo['tx']) if 'confirmations' in txinfo and txinfo['confirmations'] > 0: parser.exit("Transaction already mined; %d confirmations." % txinfo['confirmations']) # Find a txout that was being used for change change_txout = None for vout in tx.vout: try: addr = CBitcoinAddress.from_scriptPubKey(vout.scriptPubKey) except ValueError: continue if rpc.validateaddress(addr)['ismine']: change_txout = vout break if change_txout is None: # No suitable change txout; no txout was an address in our wallet. # # Create a new txout for use as change. addr = rpc.getrawchangeaddress() change_txout = CMutableTxOut(0, addr.to_scriptPubKey()) tx.vout.append(change_txout) # Find total value in value_in = 0 for vin in tx.vin:
txinfo = rpc.getrawtransaction(args.txid, True) tx = CMutableTransaction.from_tx(txinfo['tx']) if 'confirmations' in txinfo and txinfo['confirmations'] > 0: parser.exit("Transaction already mined; %d confirmations." % txinfo['confirmations']) # Find a txout that was being used for change change_txout = None for vout in tx.vout: try: addr = CBitcoinAddress.from_scriptPubKey(vout.scriptPubKey) except ValueError: continue if rpc.validateaddress(addr)['ismine']: change_txout = vout break if change_txout is None: # No suitable change txout; no txout was an address in our wallet. # # Create a new txout for use as change. addr = rpc.getrawchangeaddress() change_txout = CMutableTxOut(0, addr.to_scriptPubKey()) tx.vout.append(change_txout) min_change_txout_nValue = 0 if args.first_seen_safe: min_change_txout_nValue = change_txout.nValue logging.debug('First-seen-safe enabled: will not reduce change txout value below %s BTC' % \
# Unfortunately there isn't any way to ask Bitcoin Core if a given address # is a change address; if you're sending yourself funds to test the feature # it's not possible to distinguish change from send-to-self outputs. # # So instead we always build transactions such that the first output is # change, and we delete only that output. Not pretty - you don't want to do # something that dumb and anti-privacy in a real wallet - but without a way # of keeping state this is the best we've got. try: addr = CBitcoinAddress.from_scriptPubKey(tx2.vout[0].scriptPubKey) except ValueError: pass else: # There is an edge case not handled: if we have multiple outputs but # didn't need a change output. But whatever, this is just a demo! if len(tx2.vout) > 1 and rpc.validateaddress(addr)['ismine']: tx2.vout = tx2.vout[1:] # Add the new output payment_txout = CMutableTxOut(args.amount, args.address.to_scriptPubKey()) tx2.vout.append(payment_txout) r = rpc.fundrawtransaction(tx2) tx2 = CMutableTransaction.from_tx(r['tx']) tx2_fee = r['fee'] # Set nSequnce on all inputs appropriately to opt-in to full-RBF for txin in tx2.vin: txin.nSequence = 0
# Unfortunately there isn't any way to ask Bitcoin Core if a given address # is a change address; if you're sending yourself funds to test the feature # it's not possible to distinguish change from send-to-self outputs. # # So instead we always build transactions such that the first output is # change, and we delete only that output. Not pretty - you don't want to do # something that dumb and anti-privacy in a real wallet - but without a way # of keeping state this is the best we've got. try: addr = CBitcoinAddress.from_scriptPubKey(tx2.vout[0].scriptPubKey) except ValueError: pass else: # There is an edge case not handled: if we have multiple outputs but # didn't need a change output. But whatever, this is just a demo! if len(tx2.vout) > 1 and rpc.validateaddress(addr)["ismine"]: tx2.vout = tx2.vout[1:] # Add the new output payment_txout = CMutableTxOut(args.amount, args.address.to_scriptPubKey()) tx2.vout.append(payment_txout) r = rpc.fundrawtransaction(tx2) tx2 = CMutableTransaction.from_tx(r["tx"]) tx2_fee = r["fee"] # Set nSequnce on all inputs appropriately to opt-in to full-RBF for txin in tx2.vin: txin.nSequence = 0xFFFFFFFF - 2
# Unfortunately there isn't any way to ask Bitcoin Core if a given address # is a change address; if you're sending yourself funds to test the feature # it's not possible to distinguish change from send-to-self outputs. # # So instead we always build transactions such that the first output is # change, and we delete only that output. Not pretty - you don't want to do # something that dumb and anti-privacy in a real wallet - but without a way # of keeping state this is the best we've got. try: addr = CBitcoinAddress.from_scriptPubKey(tx2.vout[0].scriptPubKey) except ValueError: pass else: # There is an edge case not handled: if we have multiple outputs but # didn't need a change output. But whatever, this is just a demo! if len(tx2.vout) > 1 and rpc.validateaddress(addr)['ismine']: tx2.vout = tx2.vout[1:] # Add the new output payment_txout = CMutableTxOut(args.amount, args.address.to_scriptPubKey()) tx2.vout.append(payment_txout) r = rpc.fundrawtransaction(tx2) tx2 = CMutableTransaction.from_tx(r['tx']) tx2_fee = r['fee'] # Set nSequnce on all inputs appropriately to opt-in to full-RBF for txin in tx2.vin: txin.nSequence = 0 # Move change txout to 0th slot