def test_block(self): v = 42 tx_hash = hashlib.sha256(hashlib.sha256('enigmaenigmaenigma').digest()).digest() script = 'scriptscriptscript' idx = 75 seq = 42 txin = protocol.TxIn((tx_hash, idx), script, seq) txin = [txin] val = 12345 pksbytes = '\x32' * 40 pks = protocol.PubKeyScript(pksbytes) txout = protocol.TxOut(val, pks) txout = [txout] lock = 12345678 tx = protocol.Transaction(v, txin, txout, lock) msg = protocol.Block( 0xffed34, random_hash(), random_hash(), 948576, 738571, 975670, [tx, tx]) (parsed, bytes) = protocol.Block.parse(msg.bytes) self.assertEquals(bytes, '') self.assertEquals(msg.bytes, parsed.bytes) self.assertEquals(msg, parsed)
def test_transaction(self): v = 42 tx_hash = hashlib.sha256(hashlib.sha256('enigmaenigmaenigma').digest()).digest() script = 'scriptscriptscript' idx = 75 seq = 42 txin = protocol.TxIn((tx_hash, idx), script, seq) txin = [txin] val = 12345 pksbytes = '\x32' * 40 pks = protocol.PubKeyScript(pksbytes) txout = protocol.TxOut(val, pks) txout = [txout] lock = 12345678 tx1 = tx = protocol.TransactionMessage(protocol.Transaction(v, txin, txout, lock)) self.assertEquals(tx.tx.version, v) self.assertEquals(tx.tx.tx_in, txin) self.assertEquals(tx.tx.tx_out, txout) self.assertEquals(tx.tx.lock_time, lock) (tx, bytes) = protocol.TransactionMessage.parse(tx.bytes) self.assertEquals(bytes, '') self.assertEquals(tx.tx.version, v) self.assertEquals(tx.tx.tx_in, txin) self.assertEquals(tx.tx.tx_out, txout) self.assertEquals(tx.tx.lock_time, lock) self.assertEquals(tx1, tx)
def test_pks(self): script = '\x42' * 5 pks = protocol.PubKeyScript(script) self.assertEquals(pks.bytes, script) self.assertEquals(repr(pks), binascii.hexlify(script)) self.assertFalse(pks.is_standard_transaction) addr = '\x42' * 20 script = '\x76\xa9\x14' + addr + '\x88\xac' pks = protocol.PubKeyScript(script) self.assertEquals(pks.bytes, script) self.assertEquals( repr(pks), 'To Addr: ' + byte_util.base58_encode(key.address_from_pk_hash(addr))) self.assertTrue(pks.is_standard_transaction) self.assertEquals(pks, pks)
def to_address(self, value): if self.txout_unspent is not None: return self.txout_unspent.to_address if self._to_address is None: if self._pubkey is None: self._pubkey = protocol.PubKeyScript(self.pk_script) self._to_address = self._pubkey.to_address return self._to_address
def test_txout(self): val = 12345 pksbytes = '\x32' * 40 pks = protocol.PubKeyScript(pksbytes) txout1 = txout = protocol.TxOut(val, pks) self.assertEquals(txout.value, val) self.assertEquals(txout.pk_script.bytes, pksbytes) (txout, bytes) = protocol.TxOut.parse(txout.bytes) self.assertEquals(bytes, '') self.assertEquals(txout.value, val) self.assertEquals(txout.pk_script.bytes, pksbytes) self.assertEquals(txout1, txout)
def to_protocol(self): return protocol.TxOut(self.value, protocol.PubKeyScript(self.pk_script))