electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit 023cd9eeb0fc3dc01a0ffdc48612f57ccb7990bc
parent a056f9804be9584c7bc22ccef6e29a8dc82ed3c5
Author: ThomasV <electrumdev@gmail.com>
Date:   Tue, 25 Aug 2015 11:12:26 +0200

Merge pull request #1421 from romanz/tests

test_transaction: add unit tests for lib.transaction module
Diffstat:
Alib/tests/test_transaction.py | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 114 insertions(+), 0 deletions(-)

diff --git a/lib/tests/test_transaction.py b/lib/tests/test_transaction.py @@ -0,0 +1,114 @@ +import unittest +from lib import transaction + +import pprint + +unsigned_blob = '01000000012a5c9a94fcde98f5581cd00162c60a13936ceb75389ea65bf38633b424eb4031000000005701ff4c53ff0488b21e03ef2afea18000000089689bff23e1e7fb2f161daa37270a97a3d8c2e537584b2d304ecb47b86d21fc021b010d3bd425f8cf2e04824bfdf1f1f5ff1d51fadd9a41f9e3fb8dd3403b1bfe00000000ffffffff0140420f00000000001976a914230ac37834073a42146f11ef8414ae929feaafc388ac00000000' +signed_blob = '01000000012a5c9a94fcde98f5581cd00162c60a13936ceb75389ea65bf38633b424eb4031000000006c493046022100a82bbc57a0136751e5433f41cf000b3f1a99c6744775e76ec764fb78c54ee100022100f9e80b7de89de861dc6fb0c1429d5da72c2b6b2ee2406bc9bfb1beedd729d985012102e61d176da16edd1d258a200ad9759ef63adf8e14cd97f53227bae35cdb84d2f6ffffffff0140420f00000000001976a914230ac37834073a42146f11ef8414ae929feaafc388ac00000000' + + +class TestTransaction(unittest.TestCase): + + def test_tx_unsigned(self): + expected = { + 'inputs': [{ + 'address': '1446oU3z268EeFgfcwJv6X2VBXHfoYxfuD', + 'is_coinbase': False, + 'num_sig': 1, + 'prevout_hash': '3140eb24b43386f35ba69e3875eb6c93130ac66201d01c58f598defc949a5c2a', + 'prevout_n': 0, + 'pubkeys': ['02e61d176da16edd1d258a200ad9759ef63adf8e14cd97f53227bae35cdb84d2f6'], + 'scriptSig': '01ff4c53ff0488b21e03ef2afea18000000089689bff23e1e7fb2f161daa37270a97a3d8c2e537584b2d304ecb47b86d21fc021b010d3bd425f8cf2e04824bfdf1f1f5ff1d51fadd9a41f9e3fb8dd3403b1bfe00000000', + 'sequence': 4294967295, + 'signatures': [None], + 'x_pubkeys': ['ff0488b21e03ef2afea18000000089689bff23e1e7fb2f161daa37270a97a3d8c2e537584b2d304ecb47b86d21fc021b010d3bd425f8cf2e04824bfdf1f1f5ff1d51fadd9a41f9e3fb8dd3403b1bfe00000000']}], + 'lockTime': 0, + 'outputs': [{ + 'address': '14CHYaaByjJZpx4oHBpfDMdqhTyXnZ3kVs', + 'prevout_n': 0, + 'scriptPubKey': '76a914230ac37834073a42146f11ef8414ae929feaafc388ac', + 'type': 'address', + 'value': 1000000}], + 'version': 1 + } + tx = transaction.Transaction(unsigned_blob) + self.assertEquals(tx.deserialize(), expected) + self.assertEquals(tx.deserialize(), None) + + self.assertEquals(tx.as_dict(), {'hex': unsigned_blob, 'complete': False}) + self.assertEquals(tx.get_outputs(), [('14CHYaaByjJZpx4oHBpfDMdqhTyXnZ3kVs', 1000000)]) + self.assertEquals(tx.get_output_addresses(), ['14CHYaaByjJZpx4oHBpfDMdqhTyXnZ3kVs']) + + self.assertTrue(tx.has_address('14CHYaaByjJZpx4oHBpfDMdqhTyXnZ3kVs')) + self.assertTrue(tx.has_address('1446oU3z268EeFgfcwJv6X2VBXHfoYxfuD')) + self.assertFalse(tx.has_address('1CQj15y1N7LDHp7wTt28eoD1QhHgFgxECH')) + + self.assertEquals(tx.inputs_to_sign(), set(x_pubkey for i in expected['inputs'] for x_pubkey in i['x_pubkeys'])) + self.assertEquals(tx.serialize(), unsigned_blob) + + tx.update_signatures(signed_blob) + self.assertEquals(tx.raw, signed_blob) + + tx.update(unsigned_blob) + tx.raw = None + blob = str(tx) + self.assertEquals(transaction.deserialize(blob), expected) + + def test_tx_signed(self): + expected = { + 'inputs': [{ + 'address': '1446oU3z268EeFgfcwJv6X2VBXHfoYxfuD', + 'is_coinbase': False, + 'num_sig': 1, + 'prevout_hash': '3140eb24b43386f35ba69e3875eb6c93130ac66201d01c58f598defc949a5c2a', + 'prevout_n': 0, + 'pubkeys': ['02e61d176da16edd1d258a200ad9759ef63adf8e14cd97f53227bae35cdb84d2f6'], + 'scriptSig': '493046022100a82bbc57a0136751e5433f41cf000b3f1a99c6744775e76ec764fb78c54ee100022100f9e80b7de89de861dc6fb0c1429d5da72c2b6b2ee2406bc9bfb1beedd729d985012102e61d176da16edd1d258a200ad9759ef63adf8e14cd97f53227bae35cdb84d2f6', + 'sequence': 4294967295, + 'signatures': ['3046022100a82bbc57a0136751e5433f41cf000b3f1a99c6744775e76ec764fb78c54ee100022100f9e80b7de89de861dc6fb0c1429d5da72c2b6b2ee2406bc9bfb1beedd729d985'], + 'x_pubkeys': ['02e61d176da16edd1d258a200ad9759ef63adf8e14cd97f53227bae35cdb84d2f6']}], + 'lockTime': 0, + 'outputs': [{ + 'address': '14CHYaaByjJZpx4oHBpfDMdqhTyXnZ3kVs', + 'prevout_n': 0, + 'scriptPubKey': '76a914230ac37834073a42146f11ef8414ae929feaafc388ac', + 'type': 'address', + 'value': 1000000}], + 'version': 1 + } + tx = transaction.Transaction(signed_blob) + self.assertEquals(tx.deserialize(), expected) + self.assertEquals(tx.deserialize(), None) + self.assertEquals(tx.as_dict(), {'hex': signed_blob, 'complete': True}) + + self.assertEquals(tx.inputs_to_sign(), set()) + self.assertEquals(tx.serialize(), signed_blob) + + tx.update_signatures(signed_blob) + + def test_errors(self): + with self.assertRaises(TypeError): + transaction.Transaction.pay_script(output_type=None, addr='') + + with self.assertRaises(BaseException): + transaction.parse_xpub('') + + def test_parse_xpub(self): + res = transaction.parse_xpub('fe4e13b0f311a55b8a5db9a32e959da9f011b131019d4cebe6141b9e2c93edcbfc0954c358b062a9f94111548e50bde5847a3096b8b7872dcffadb0e9579b9017b01000200') + self.assertEquals(res, ('04ee98d63800824486a1cf5b4376f2f574d86e0a3009a6448105703453f3368e8e1d8d090aaecdd626a45cc49876709a3bbb6dc96a4311b3cac03e225df5f63dfc', '19h943e4diLc68GXW7G75QNe2KWuMu7BaJ')) + + res = transaction.parse_xpub('fd007d260305ef27224bbcf6cf5238d2b3638b5a78d5') + self.assertEquals(res, (None, '1CQj15y1N7LDHp7wTt28eoD1QhHgFgxECH')) + + def test_sign_tx(self): + tx = transaction.Transaction(unsigned_blob) + tx.deserialize() + + x_pubkey = 'ff0488b21e03ef2afea18000000089689bff23e1e7fb2f161daa37270a97a3d8c2e537584b2d304ecb47b86d21fc021b010d3bd425f8cf2e04824bfdf1f1f5ff1d51fadd9a41f9e3fb8dd3403b1bfe00000000' + privkey = 'L187zmkzzGgf9QdB23MrZvwJ52WoZuQHtkddjmePtbVjXxicJND2' + + tx.sign(keypairs={x_pubkey: privkey}) + self.assertEquals(tx.serialize(), signed_blob) + + tx.sign(keypairs={x_pubkey: privkey}) + self.assertEquals(tx.serialize(), signed_blob)