def sweep_btc_account(self, SENDER_ADDRESS='', email=''): addrs = [] addrs.append(SENDER_ADDRESS) transactions = [] transactions_with_key = [] sum = 0.0 utxo = rpc.listunspent(minconf=6, addrs=addrs) # Return if there are no transactions if len(utxo) == 0: return for txo in utxo: sum += float(txo['amount']) / COIN transaction = {} transaction['txid'] = b2lx(txo['outpoint'].hash) transaction['vout'] = txo['outpoint'].n transactions.append(transaction) transaction['scriptPubKey'] = hexlify(txo['scriptPubKey']) transactions_with_key.append(transaction) # Need to calculate transaction fee transaction_fee = 0.0001 addresses = {} addresses[EXCHANGE_BITCOIN_ADDRESS] = sum - transaction_fee # Pickup the private key from the database, hardcoded for now PRIVATE_KEY = manager_c.get('bitcoin_keypairs', SENDER_ADDRESS)['priv_key'] private_keys = [] private_keys.append(PRIVATE_KEY) try: raw_tx = rpc.createrawtransaction(transactions, addresses) signed_transaction = rpc.signrawtransaction( raw_tx, transactions_with_key, private_keys) except: raise return if not signed_transaction['complete'] or not signed_transaction: raise 'Transaction signing unsuccessful' return else: txid = rpc.sendrawtransaction(signed_transaction['tx']) print 'Sent %s from %s to %s' % ( addresses[EXCHANGE_BITCOIN_ADDRESS], SENDER_ADDRESS, PRIVATE_KEY) lxtxid = b2lx(txid) print lxtxid # Update the the users space in hyperdex, add 'sum' bitcoin to the # user's balance keyed on email-id. x = webserver_c.begin_transaction() #x.put('users', email, {'Bitcoin': x.get('users', email)['Bitcoin'] + addresses[EXCHANGE_BITCOIN_ADDRESS]}) x.atomic_add('users', email, {'Bitcoin': addresses[EXCHANGE_BITCOIN_ADDRESS]}) x.commit()
def sweep_btc_account(self, SENDER_ADDRESS='', email=''): addrs = [] addrs.append(SENDER_ADDRESS) transactions = [] transactions_with_key = [] sum = 0.0 utxo = rpc.listunspent(minconf=6, addrs=addrs) # Return if there are no transactions if len(utxo) == 0: return for txo in utxo: sum += float(txo['amount'])/COIN transaction = {} transaction['txid'] = b2lx(txo['outpoint'].hash) transaction['vout'] = txo['outpoint'].n transactions.append(transaction) transaction['scriptPubKey'] = hexlify(txo['scriptPubKey']) transactions_with_key.append(transaction) # Need to calculate transaction fee transaction_fee = 0.0001 addresses = {} addresses[EXCHANGE_BITCOIN_ADDRESS] = sum - transaction_fee # Pickup the private key from the database, hardcoded for now PRIVATE_KEY = manager_c.get('bitcoin_keypairs', SENDER_ADDRESS)['priv_key'] private_keys = [] private_keys.append(PRIVATE_KEY) try: raw_tx = rpc.createrawtransaction( transactions, addresses) signed_transaction = rpc.signrawtransaction( raw_tx, transactions_with_key, private_keys) except: raise return if not signed_transaction['complete'] or not signed_transaction: raise 'Transaction signing unsuccessful' return else: txid = rpc.sendrawtransaction(signed_transaction['tx']) print 'Sent %s from %s to %s' % (addresses[EXCHANGE_BITCOIN_ADDRESS], SENDER_ADDRESS, PRIVATE_KEY) lxtxid = b2lx(txid) print lxtxid # Update the the users space in hyperdex, add 'sum' bitcoin to the # user's balance keyed on email-id. x = webserver_c.begin_transaction() #x.put('users', email, {'Bitcoin': x.get('users', email)['Bitcoin'] + addresses[EXCHANGE_BITCOIN_ADDRESS]}) x.atomic_add('users', email, {'Bitcoin': addresses[EXCHANGE_BITCOIN_ADDRESS]}) x.commit()
def send(rpc,transaction): signed = rpc.signrawtransaction(transaction) txid = rpc.sendrawtransaction(signed['tx']) print(txid)
tx.vin[-1].scriptSig = r['tx'].vin[-1].scriptSig r = rpc.signrawtransaction(tx) assert(r['complete']) tx = r['tx'] logging.debug('Payment tx %s' % b2x(tx.serialize())) logging.info('Payment tx size: %.3f KB, fees: %s, %s BTC/KB' % \ (len(tx.serialize()) / 1000, str_money_value(value_in-value_out), str_money_value((value_in-value_out) / len(tx.serialize()) * 1000))) if not args.dryrun: txid = rpc.sendrawtransaction(tx) logging.info('Sent payment tx: %s' % b2lx(txid)) if not args.dryrun: logging.info('Sleeping for %d seconds' % args.delay) time.sleep(args.delay) # Double-spend! Remove all but the change output tx.vout = tx.vout[0:1] change_txout = tx.vout[0] value_out = value_in change_txout.nValue = value_out # FIXME: need to modularize this code while (value_in - value_out) / len(tx.serialize()) < feeperbyte2:
add_privkey(CBitcoinSecret.from_secret_bytes(secret, False)) add_privkey(CBitcoinSecret.from_secret_bytes(secret, True)) logging.info('Added %d known passphrases' % n) known_txids = set() while True: mempool_txids = set(rpc.getrawmempool()) new_txids = mempool_txids.difference(known_txids) known_txids.update(mempool_txids) burn_txs = [] for new_txid in new_txids: try: new_tx = rpc.getrawtransaction(new_txid) except IndexError: continue burn_txs.extend(scan_tx_for_spendable_outputs(new_tx, new_txid)) for burn_tx in burn_txs: try: txid = rpc.sendrawtransaction(burn_tx) logging.info('Sent burn tx %s' % b2lx(txid)) except bitcoin.rpc.JSONRPCException as err: logging.info('Got error %s while sending %s' % (err, b2x(burn_tx.serialize()))) logging.info('Sleeping %f seconds' % args.delay) time.sleep(args.delay)
value_in += new_amount change_txout.nValue += new_amount value_out += new_amount tx.vin.append(new_txin) # re-sign the tx so we can figure out how large the new input's scriptSig will be. r = rpc.signrawtransaction(tx) assert (r['complete']) tx.vin[-1].scriptSig = r['tx'].vin[-1].scriptSig logging.debug('New size: %.3f KB, New fees: %s, %s BTC/KB' % \ (len(tx.serialize()) / 1000, str_money_value(value_in-value_out), str_money_value((value_in-value_out) / len(tx.serialize()) * 1000))) r = rpc.signrawtransaction(tx) assert (r['complete']) tx = r['tx'] if args.dryrun: print(b2x(tx.serialize())) else: logging.debug('Sending tx %s' % b2x(tx.serialize())) txid = rpc.sendrawtransaction(tx) print(b2lx(txid))
except IndexError: continue # The scriptSigs might not sign vout, in which case we can replace the # whole thing with OP_RETURN. if not (len(new_tx.vout) == 1 and new_tx.vout[0].nValue == 0 and new_tx.vout[0].scriptPubKey == CScript([OP_RETURN])): to_fees_tx = CTransaction(new_tx.vin, [CTxOut(0, CScript([OP_RETURN]))], nLockTime=new_tx.nLockTime, nVersion=new_tx.nVersion) try: to_fees_txid = rpc.sendrawtransaction(to_fees_tx, True) logging.info('Replaced tx %s with all-to-fees %s' % (b2lx(new_txid), b2lx(to_fees_txid))) except bitcoin.rpc.JSONRPCException as exp: # Couldn't replace; try spending individual outputs instead. burn_txs.extend(scan_tx_for_spendable_outputs(new_tx, new_txid)) for burn_tx in burn_txs: try: txid = rpc.sendrawtransaction(burn_tx, True) logging.info('Sent burn tx %s' % b2lx(txid)) except bitcoin.rpc.JSONRPCException as err: logging.info('Got error %s while sending %s' % (err, b2x(burn_tx.serialize()))) logging.debug('Sleeping %f seconds' % args.delay) time.sleep(args.delay)
new_tx = rpc.getrawtransaction(new_txid) except IndexError: continue # The scriptSigs might not sign vout, in which case we can replace the # whole thing with OP_RETURN. if not (len(new_tx.vout) == 1 and new_tx.vout[0].nValue == 0 and new_tx.vout[0].scriptPubKey == CScript([OP_RETURN])): to_fees_tx = CTransaction(new_tx.vin, [CTxOut(0, CScript([OP_RETURN]))], nLockTime=new_tx.nLockTime, nVersion=new_tx.nVersion) try: to_fees_txid = rpc.sendrawtransaction(to_fees_tx, True) logging.info('Replaced tx %s with all-to-fees %s' % (b2lx(new_txid), b2lx(to_fees_txid))) except bitcoin.rpc.JSONRPCException as exp: # Couldn't replace; try spending individual outputs instead. burn_txs.extend(scan_tx_for_spendable_outputs( new_tx, new_txid)) for burn_tx in burn_txs: try: txid = rpc.sendrawtransaction(burn_tx, True) logging.info('Sent burn tx %s' % b2lx(txid)) except bitcoin.rpc.JSONRPCException as err: logging.info('Got error %s while sending %s' % (err, b2x(burn_tx.serialize())))