def create_transaction_output(address, transaction_fee): """ Create a single transaction output :param address: :param transaction_fee: :return: """ addr = CBitcoinAddress(address) tx_out = CMutableTxOut(transaction_fee, addr.to_scriptPubKey()) return tx_out
def create_transaction_output(address, output_value): """ Create a single transaction output :param address: :param output_value: :return: """ bitcoin_address = CBitcoinAddress(address) tx_out = CMutableTxOut(output_value, bitcoin_address.to_scriptPubKey()) return tx_out
def create_transaction_output(address, transaction_fee): """ Create a single transaction output :param address: :param transaction_fee: :return: """ bitcoin_address = CBitcoinAddress(address) tx_out = CMutableTxOut(transaction_fee, bitcoin_address.to_scriptPubKey()) return tx_out
class SpendScripts(object): def __init__(self, payto_addr): self.payto = CBitcoinAddress(payto_addr) self.proxy = bitcoin.rpc.Proxy() self.prevouts = [] def add_prevout(self, txid, vout, redeemer): outpoint = COutPoint(lx(txid), vout) try: prevout = self.proxy.gettxout(outpoint) except IndexError: raise Exception("Outpoint %s not found" % (outpoint, )) prevtx = prevout['txout'] if prevtx.scriptPubKey != redeemer.p2sh_scriptPubKey: raise Exception( "Outpoint %s has incorrect scriptPubKey (%s; expected %s)" % (outpoint, b2x( prevtx.scriptPubKey), b2x(redeemer.p2sh_scriptPubKey))) self.prevouts.append((outpoint, prevtx, redeemer)) def as_tx(self): sum_in = sum(prevtx.nValue for _, prevtx, _ in self.prevouts) sig_size = sum(redeemer.spendbytes for _, _, redeemer in self.prevouts) tx_size = ( 4 + # version field 2 + # # of txins len(self.prevouts) * 41 + # txins, excluding sigs sig_size + # txins, sigs only 1 + # # of txouts 34 + # txout 4 # nLockTime field ) feerate = int(self.proxy._call('estimatefee', 1) * COIN) # satoshi's per KB if feerate <= 0: feerate = 10000 fees = int(tx_size * feerate / 1000) tx = CMutableTransaction( [CTxIn(outpoint, nSequence=0) for outpoint, _, _ in self.prevouts], [CTxOut(sum_in - fees, self.payto.to_scriptPubKey())], 0) for n, (_, _, redeemer) in enumerate(self.prevouts): redeemer.mutate_spend(tx, n) unsigned_tx = CTransaction.from_tx(tx) for n, (_, _, redeemer) in enumerate(self.prevouts): txin = CMutableTxIn.from_txin(tx.vin[n]) txin.scriptSig = redeemer.sign_spend(unsigned_tx, n) tx.vin[n] = CTxIn.from_txin(txin) print(b2x(tx.serialize()))
class SpendScripts(object): def __init__(self, payto_addr): self.payto = CBitcoinAddress(payto_addr) self.proxy = bitcoin.rpc.Proxy() self.prevouts = [] def add_prevout(self, txid, vout, redeemer): outpoint = COutPoint(lx(txid), vout) try: prevout = self.proxy.gettxout(outpoint) except IndexError: raise Exception("Outpoint %s not found" % (outpoint,)) prevtx = prevout['txout'] if prevtx.scriptPubKey != redeemer.p2sh_scriptPubKey: raise Exception("Outpoint %s has incorrect scriptPubKey (%s; expected %s)" % (outpoint, b2x(prevtx.scriptPubKey), b2x(redeemer.p2sh_scriptPubKey))) self.prevouts.append((outpoint, prevtx, redeemer)) def as_tx(self): sum_in = sum(prevtx.nValue for _,prevtx,_ in self.prevouts) sig_size = sum(redeemer.spendbytes for _,_,redeemer in self.prevouts) tx_size = (4 + # version field 2 + # # of txins len(self.prevouts) * 41 + # txins, excluding sigs sig_size + # txins, sigs only 1 + # # of txouts 34 + # txout 4 # nLockTime field ) feerate = int(self.proxy._call('estimatefee', 1) * COIN) # satoshi's per KB if feerate <= 0: feerate = 10000 fees = int(tx_size * feerate / 1000) tx = CMutableTransaction( [CTxIn(outpoint, nSequence=0) for outpoint,_,_ in self.prevouts], [CTxOut(sum_in - fees, self.payto.to_scriptPubKey())], 0) for n,(_,_,redeemer) in enumerate(self.prevouts): redeemer.mutate_spend(tx, n) unsigned_tx = CTransaction.from_tx(tx) for n,(_,_,redeemer) in enumerate(self.prevouts): txin = CMutableTxIn.from_txin(tx.vin[n]) txin.scriptSig = redeemer.sign_spend(unsigned_tx, n) tx.vin[n] = CTxIn.from_txin(txin) print(b2x(tx.serialize()))
def do(self, args): genesis_outpoints = {} genesis_scriptPubKeys = set() for str_outpoint, str_qty in args.genesis_outpoints: outpoint = ParseCOutPointArg.str_to_COutPoint( str_outpoint, args.parser) qty = int(str_qty) if outpoint in genesis_outpoints: args.parser.exit('dup outpoint %r' % outpoint) logging.debug('Genesis outpoint: %s:%d %d' % (b2lx(outpoint.hash), outpoint.n, qty)) genesis_outpoints[outpoint] = qty for str_addr in args.genesis_addrs: addr = CBitcoinAddress(str_addr) scriptPubKey = addr.to_scriptPubKey() if scriptPubKey in genesis_scriptPubKeys: args.parser.exit('dup addr %s' % str_addr) logging.debug('Genesis scriptPubKey: %s (%s)' % (b2x(scriptPubKey), str(addr))) genesis_scriptPubKeys.add(scriptPubKey) for hex_scriptPubKey in args.genesis_scriptPubKeys: scriptPubKey = CScript(x(hex_scriptPubKey)) if scriptPubKey in genesis_scriptPubKeys: args.parser.exit('dup addr %s' % hex_scriptPubKey) logging.debug('Genesis scriptPubKey: %s' % b2x(scriptPubKey)) genesis_scriptPubKeys.add(scriptPubKey) stegkey = os.urandom(ColorDef.STEGKEY_LEN) if args.stegkey is not None: stegkey = x(args.stegkey) colordef = ColorDef(genesis_outpoints=genesis_outpoints, genesis_scriptPubKeys=genesis_scriptPubKeys, birthdate_blockheight=args.birthdate_blockheight, stegkey=stegkey) ColorDefFileSerializer.stream_serialize(colordef, args.fd)
def do(self, args): genesis_outpoints = {} genesis_scriptPubKeys = set() for str_outpoint, str_qty in args.genesis_outpoints: outpoint = ParseCOutPointArg.str_to_COutPoint(str_outpoint, args.parser) qty = int(str_qty) if outpoint in genesis_outpoints: args.parser.exit('dup outpoint %r' % outpoint) logging.debug('Genesis outpoint: %s:%d %d' % (b2lx(outpoint.hash), outpoint.n, qty)) genesis_outpoints[outpoint] = qty for str_addr in args.genesis_addrs: addr = CBitcoinAddress(str_addr) scriptPubKey = addr.to_scriptPubKey() if scriptPubKey in genesis_scriptPubKeys: args.parser.exit('dup addr %s' % str_addr) logging.debug('Genesis scriptPubKey: %s (%s)' % (b2x(scriptPubKey), str(addr))) genesis_scriptPubKeys.add(scriptPubKey) for hex_scriptPubKey in args.genesis_scriptPubKeys: scriptPubKey = CScript(x(hex_scriptPubKey)) if scriptPubKey in genesis_scriptPubKeys: args.parser.exit('dup addr %s' % hex_scriptPubKey) logging.debug('Genesis scriptPubKey: %s' % b2x(scriptPubKey)) genesis_scriptPubKeys.add(scriptPubKey) stegkey = os.urandom(ColorDef.STEGKEY_LEN) if args.stegkey is not None: stegkey = x(args.stegkey) colordef = ColorDef(genesis_outpoints=genesis_outpoints, genesis_scriptPubKeys=genesis_scriptPubKeys, birthdate_blockheight=args.birthdate_blockheight, stegkey=stegkey) ColorDefFileSerializer.stream_serialize(colordef, args.fd)
def create_transaction_object(inputs: List, outputs: Dict[str, Decimal]) -> CMutableTransaction: """ Create the transaction object to calculate the raw transaction. :param list inputs: an array of unspent objects :param dict outputs: a dictionary that maps addresses to amounts :return: a transaction object """ SelectParams(Config.NETWORK) tx_in: List[CMutableTxIn] = [] for i in inputs: tx_id = lx(i["tx_hash"]) vout = i["tx_index"] tx_in.append(CMutableTxIn(COutPoint(tx_id, vout))) tx_out: List[CMutableTxOut] = [] for addr, amount in outputs.items(): cv = CBitcoinAddress(addr) pk = cv.to_scriptPubKey() tx_out.append(CMutableTxOut(amount, pk)) return CMutableTransaction(tx_in, tx_out)
def recover_command(args): args.fee_per_kb = int(args.fee_per_kb * COIN) addr = CBitcoinAddress(args.addr) tx = CTransaction() sum_value_in = 0 dummy_scriptSig = CScript([b'\x00'*74]) inputs = {} for outpoint, txout in tuple(args.wallet.unspent_txouts.items())[0:500]: sum_value_in += txout.nValue tx.vin.append(CTxIn(outpoint, dummy_scriptSig)) inputs[outpoint] = txout tx.vout.append(CTxOut(-1, addr.to_scriptPubKey())) fees = int((len(tx.serialize())/1000) * args.fee_per_kb) tx.vout[0].nValue = sum_value_in - fees # Sign the transaction for (i, txin) in enumerate(tx.vin): prevout_scriptPubKey = inputs[txin.prevout].scriptPubKey sighash = SignatureHash(prevout_scriptPubKey, tx, i, SIGHASH_ALL) seckey = args.wallet.keypairs[prevout_scriptPubKey] sig = seckey.sign(sighash) + bytes([SIGHASH_ALL]) if prevout_scriptPubKey[-1] == OP_CHECKMULTISIG: txin.scriptSig = CScript([OP_0, sig]) elif prevout_scriptPubKey[-1] == OP_CHECKSIG and prevout_scriptPubKey[-2] == OP_EQUALVERIFY: txin.scriptSig = CScript([sig, seckey.pub]) VerifyScript(txin.scriptSig, prevout_scriptPubKey, tx, i) print(b2x(tx.serialize()))
def create_transaction_output(address, transaction_fee): """Create a transaction output""" addr = CBitcoinAddress(address) tx_out = CMutableTxOut(transaction_fee, addr.to_scriptPubKey()) return tx_out
logging.root.setLevel('DEBUG') if args.testnet: bitcoin.SelectParams('testnet') rpc = bitcoin.rpc.Proxy() args.dust = int(args.dust * COIN) feeperbyte1 = args.fee1 / 1000 * COIN feeperbyte2 = args.fee2 / 1000 * COIN # Construct payment tx payment_address = CBitcoinAddress(args.address) payment_txout = CTxOut(int(args.amount * COIN), payment_address.to_scriptPubKey()) change_txout = CTxOut(0, rpc.getnewaddress().to_scriptPubKey()) tx = CTransaction() tx.vout.append(change_txout) tx.vout.append(payment_txout) # Add all undesirable txouts meant to reduce propagation if args.op_return: op_ret_txout = CTxOut(0, CScript([OP_RETURN, b'\x00unsuccessful double-spend attempt\x00'])) tx.vout.append(op_ret_txout) if args.multisig: multisig_txout = CTxOut(args.dust, CScript([1, x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71'), b'\x00'*33,
def settle_to_single_addr(deposits: List[Deposit], addr: CBitcoinAddress): prevouts = [] for d in deposits: try: txid, n = d.txid, d.nout txid = lx(txid) n = int(n) outpoint = COutPoint(txid, n) except ValueError: raise ValueError('Invalid output: %s' % d) try: prevout = proxy.gettxout(outpoint) except IndexError: raise ValueError('Outpoint %s not found' % outpoint) prevout = prevout['txout'] if prevout.scriptPubKey != d.params.deposit_redeemScript.to_p2sh_scriptPubKey( ): raise Exception('Outpoint not correct scriptPubKey') prevouts.append((outpoint, prevout)) sum_in = sum(prev_txout.nValue for _, prev_txout in prevouts) tx_size = ( 4 + # version field 2 + # # of txins len(prevouts) * 153 + # txins, including sigs 1 + # # of txouts 34 + # txout 4 # nLockTime field ) estimated_fee = proxy._call('estimatesmartfee', 1) if 'errors' in estimated_fee: print(estimated_fee['errors']) feerate = -1 else: feerate = int(estimated_fee['feerate'] * COIN) # satoshi's per KB if feerate <= 0: feerate = 10000 fees = int(tx_size / 1000 * feerate) print('fee: %f' % fees) print('amount: %f' % (sum_in - fees)) # lock until the next block nLockTime = proxy.getblockcount() unsigned_tx = CTransaction( [CTxIn(outpoint, nSequence=0) for outpoint, _ in prevouts], [CTxOut(sum_in - fees, addr.to_scriptPubKey())], nLockTime=nLockTime) # sign the inputs signed_ins = [ CTxIn(unsigned_tx.vin[i].prevout, d.params.spend_redeemScript('exch', exch_seckey, unsigned_tx, i), nSequence=0) for i, d in enumerate(deposits) ] signed_tx = CTransaction(signed_ins, unsigned_tx.vout, unsigned_tx.nLockTime) print(b2x(signed_tx.serialize()))
rpc = bitcoin.rpc.Proxy() dust_amount = int(0.0001 * COIN) feeperbyte1 = 0.000011 / 1000 * COIN feeperbyte2 = 0.001 / 1000 * COIN optinrbf = True tx1_nSequence = 0xFFFFFFFF-2 if optinrbf else 0xFFFFFFFF tx2_nSequence = tx1_nSequence payment_address = CBitcoinAddress("n4Wux6bCxwFPvj7BYNb8REvtahhJ9fHJFv") payment_txout = CMutableTxOut(int(0.1 * COIN), payment_address.to_scriptPubKey()) change_txout = CMutableTxOut(0, rpc.getnewaddress().to_scriptPubKey()) tx = CMutableTransaction() tx.vout.append(change_txout) tx.vout.append(payment_txout) unspent = sorted(rpc.listunspent(1), key=lambda x: x['amount']) value_in = 0 value_out = sum([vout.nValue for vout in tx.vout]) while (value_in - value_out) / len(tx.serialize()) < feeperbyte1: delta_fee = math.ceil((feeperbyte1 * len(tx.serialize())) - (value_in - value_out))
if args.testnet: bitcoin.SelectParams('testnet') rpc = bitcoin.rpc.Proxy() args.dust = int(args.dust * COIN) feeperbyte1 = args.fee1 / 1000 * COIN feeperbyte2 = args.fee2 / 1000 * COIN # Construct payment tx payment_address = CBitcoinAddress(args.address) payment_txout = CMutableTxOut(int(args.amount * COIN), payment_address.to_scriptPubKey()) change_txout = CMutableTxOut(0, rpc.getnewaddress().to_scriptPubKey()) tx = CMutableTransaction() tx.vout.append(change_txout) tx.vout.append(payment_txout) # Add all undesirable txouts meant to reduce propagation if args.op_return: op_ret_txout = CMutableTxOut( 0, CScript([OP_RETURN, b'\x00unsuccessful double-spend attempt\x00'])) tx.vout.append(op_ret_txout) if args.multisig: multisig_txout = CMutableTxOut( args.dust,
logging.root.setLevel('DEBUG') if args.testnet: bitcoin.SelectParams('testnet') rpc = bitcoin.rpc.Proxy() args.dust = int(args.dust * COIN) feeperbyte1 = args.fee1 / 1000 * COIN feeperbyte2 = args.fee2 / 1000 * COIN # Construct payment tx payment_address = CBitcoinAddress(args.address) payment_txout = CMutableTxOut(int(args.amount * COIN), payment_address.to_scriptPubKey()) change_txout = CMutableTxOut(0, rpc.getnewaddress().to_scriptPubKey()) tx = CMutableTransaction() tx.vout.append(change_txout) tx.vout.append(payment_txout) # Add all undesirable txouts meant to reduce propagation if args.op_return: op_ret_txout = CMutableTxOut(0, CScript([OP_RETURN, b'\x00unsuccessful double-spend attempt\x00'])) tx.vout.append(op_ret_txout) if args.multisig: multisig_txout = CMutableTxOut(args.dust, CScript([1, x('0378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71'), b'\x00'*33,