def fundrawtransaction(self, given_transaction, *args, **kwargs): """ Make up some inputs for the given transaction. """ # just use any txid here vintxid = lx("99264749804159db1e342a0c8aa3279f6ef4031872051a1e52fb302e51061bef") if isinstance(given_transaction, str): given_bytes = x(given_transaction) elif isinstance(given_transaction, CMutableTransaction): given_bytes = given_transaction.serialize() else: raise FakeBitcoinProxyException("Wrong type passed to fundrawtransaction.") # this is also a clever way to not cause a side-effect in this function transaction = CMutableTransaction.deserialize(given_bytes) for vout_counter in range(0, self._num_fundrawtransaction_inputs): txin = CMutableTxIn(COutPoint(vintxid, vout_counter)) transaction.vin.append(txin) # also allocate a single output (for change) txout = make_txout() transaction.vout.append(txout) transaction_hex = b2x(transaction.serialize()) return {"hex": transaction_hex, "fee": 5000000}
def test_fundrawtransaction_adds_output(self): num_outputs = 5 unfunded_transaction = CMutableTransaction([], [make_txout() for x in range(0, num_outputs)]) proxy = FakeBitcoinProxy() funded_transaction_hex = proxy.fundrawtransaction(b2x(unfunded_transaction.serialize()))["hex"] funded_transaction = CMutableTransaction.deserialize(x(funded_transaction_hex)) self.assertTrue(len(funded_transaction.vout) > num_outputs) self.assertEqual(len(funded_transaction.vout), num_outputs + 1)
def test_fundrawtransaction_hex_hash(self): unfunded_transaction = CMutableTransaction([], [make_txout() for x in range(0, 5)]) proxy = FakeBitcoinProxy() funded_transaction_hex = proxy.fundrawtransaction(b2x(unfunded_transaction.serialize()))["hex"] funded_transaction = CMutableTransaction.deserialize(x(funded_transaction_hex)) self.assertTrue(unfunded_transaction is not funded_transaction) self.assertEqual(type(funded_transaction), type(unfunded_transaction)) self.assertNotEqual(len(funded_transaction.vin), 0) self.assertTrue(len(funded_transaction.vin) > len(unfunded_transaction.vin)) self.assertEqual(type(funded_transaction.vin[0]), CTxIn)
def sendrawtransaction(self, given_transaction): """ Pretend to broadcast and relay the transaction. Return the txid of the given transaction. """ if isinstance(given_transaction, str): given_bytes = x(given_transaction) elif isinstance(given_transaction, CMutableTransaction): given_bytes = given_transaction.serialize() else: raise FakeBitcoinProxyException("Wrong type passed to sendrawtransaction.") transaction = CMutableTransaction.deserialize(given_bytes) return b2lx(transaction.GetHash())
def signrawtransaction(self, given_transaction): """ This method does not actually sign the transaction, but it does return a transaction based on the given transaction. """ if isinstance(given_transaction, str): given_bytes = x(given_transaction) elif isinstance(given_transaction, CMutableTransaction): given_bytes = given_transaction.serialize() else: raise FakeBitcoinProxyException("Wrong type passed to signrawtransaction.") transaction = CMutableTransaction.deserialize(given_bytes) transaction_hex = b2x(transaction.serialize()) return {"hex": transaction_hex}
def test_signrawtransaction(self): num_outputs = 5 given_transaction = CMutableTransaction([], [make_txout() for x in range(0, num_outputs)]) proxy = FakeBitcoinProxy() result = proxy.signrawtransaction(given_transaction) self.assertEqual(type(result), dict) self.assertTrue("hex" in result.keys()) result_transaction_hex = result["hex"] result_transaction = CMutableTransaction.deserialize(x(result_transaction_hex)) self.assertTrue(result_transaction is not given_transaction) self.assertTrue(result_transaction.vin is not given_transaction.vin) self.assertTrue(result_transaction.vout is not given_transaction.vout) self.assertEqual(len(result_transaction.vout), len(given_transaction.vout)) self.assertEqual(result_transaction.vout[0].scriptPubKey, given_transaction.vout[0].scriptPubKey)
def get(cls, address): """Get a Channel with the specified address.""" row = g.dat.execute( "SELECT * from CHANNELS WHERE address = ?", (address,)).fetchone() if row is None: raise Exception("Unknown address", address) address, commitment = row commitment = CMutableTransaction.deserialize(commitment) commitment = CMutableTransaction.from_tx(commitment) assert len(commitment.vin) == 1 assert len(commitment.vout) == 2 commitment.vin[0].scriptSig = AnchorScriptSig.from_script( commitment.vin[0].scriptSig) for tx_out in commitment.vout: tx_out.scriptPubKey = CBitcoinAddress.from_scriptPubKey(tx_out.scriptPubKey) return cls(address, commitment.vin[0], commitment.vout[0], commitment.vout[1])
def from_dict(cls, data): """ Instantiate a planned transaction using data from a formatted dictionary. """ planned_transaction = cls(enable_cpfp_hook=False) planned_transaction.output_utxos = [] # remove CPFP hook transaction planned_transaction.name = data["name"] planned_transaction.internal_id = data["internal_id"] planned_transaction.id = data["counter"] planned_transaction.bitcoin_transaction = CMutableTransaction.deserialize(x(data["bitcoin_transaction"])) planned_transaction.is_finalized = True for (idx, some_input) in data["inputs"].items(): planned_input = PlannedInput.from_dict(some_input) planned_transaction.inputs.append(planned_input) for (idx, some_output) in data["outputs"].items(): planned_output = PlannedUTXO.from_dict(some_output) planned_transaction.output_utxos.append(planned_output) return planned_transaction