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 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 __init__(self, nVersion=2, hashPrevBlock=b'\x00'*32, hashMerkleRoot=b'\x00'*32, nTime=0, nBits=0, nNonce=0, auxpow=None, vtx=()): """Create a new block""" super(CAltcoinBlock, self).__init__(nVersion, hashPrevBlock, hashMerkleRoot, nTime, nBits, nNonce, auxpow) vMerkleTree = tuple(bitcoin.core.CBlock.build_merkle_tree_from_txs(vtx)) object.__setattr__(self, 'vMerkleTree', vMerkleTree) object.__setattr__(self, 'vtx', tuple(CTransaction.from_tx(tx) for tx in vtx))
def __init__(self, nVersion=2, hashPrevBlock=b'\x00'*32, hashMerkleRoot=b'\x00'*32, nTime=0, nBits=0, nNonce=0, auxpow=None, vtx=()): """Create a new block""" super(CAltcoinBlock, self).__init__(nVersion, hashPrevBlock, hashMerkleRoot, nTime, nBits, nNonce, auxpow) vMerkleTree = tuple(bitcoin.core.CBlock.build_merkle_tree_from_txs(vtx)) object.__setattr__(self, 'vMerkleTree', vMerkleTree) object.__setattr__(self, 'vtx', tuple(CTransaction.from_tx(tx) for tx in vtx))
def form_unvault_spend(tx, sigs, pubkeys, pub_server): """Forms the transaction spending from an unvault using four signatures. :param tx: The unsigned transaction, a CMutableTransaction. :param sigs: The list of the four signatures in the same order as the following pubkeys. :param pubkeys: The pubkeys of the stakeholders, to form the script. :param pub_server: The pubkey of the cosigning server, to form the script. :return: The immutable signed transaction, a CTransaction. """ # Note that we use 4 sigs, but no CHECKMULTISIG, so no empty byte array at # the begining of this one! witness_script = [*sigs[::-1], unvault_script(*pubkeys, pub_server)] witness = CTxInWitness(CScriptWitness(witness_script)) tx.wit = CTxWitness([witness]) # Make it immutable return CTransaction.from_tx(tx)
def form_spend_vault_txout(tx, pubkeys, sigs): """Forms the final transaction spending a vault txout. :param tx: The unsigned transaction, as a CMutableTransaction. :param pubkeys: A list containing the pubkey of each of the four stakeholders. :param sigs: A list of the four signatures for the four pubkeys, in the same order. :return: The immutable CTransaction. """ # Spending a P2WSH, so the witness is <unlocking_script> <actual_script>. # Here, unlocking_script is the four signatures. Moreover note the empty # byte array for the CHECKMULTISIG bug. witness_script = [bytes(0), *sigs, vault_script(pubkeys)] witness = CTxInWitness(CScriptWitness(witness_script)) tx.wit = CTxWitness([witness]) # Make it immutable return CTransaction.from_tx(tx)
def form_spend_tx(tx, pubkeys, serv_pubkey, sigs): """Forms the tx spending the unvault_tx after the relative locktime, from three signatures. :param tx: The unsigned transaction, a CMutableTransaction. :param pubkeys: An *ordered* list of the four pubkeys of the stakeholders. :param serv_pubkey: The cosigning server pubkey. :param sigs: An *ordered* list of *four* bytearrays. Any of the first three can be empty (2of3). The first one is the first trader's signature, the second one the second trader signature, the third one the signature of the stakeholder's pubkey used in the unvaulting script, and the last one the cosigning server's signature. :return: The spending transaction, a CTransaction. """ # The sigs are reversed as we request them to be in the same order as the # pubkeys to keep the API simple. witness_script = [*sigs[::-1], unvault_script(*pubkeys, serv_pubkey)] witness = CTxInWitness(CScriptWitness(witness_script)) tx.wit = CTxWitness([witness]) # Make it immutable return CTransaction.from_tx(tx)