diff --git a/_ucoinpy_test/documents/test_transaction.py b/_ucoinpy_test/documents/test_transaction.py index 9ca039465814f3f7f630337f55cce2947522407c..f5acbc5600c1bfd49b93f4f0732e08632cf4ddbe 100644 --- a/_ucoinpy_test/documents/test_transaction.py +++ b/_ucoinpy_test/documents/test_transaction.py @@ -8,7 +8,7 @@ from ucoinpy.documents.transaction import Transaction from mock import Mock -compact_transaction = """TX:1:1:3:1:0 +tx_compact = """TX:1:1:3:1:0 HsLShAtzXTVxeUtQd7yi5Z5Zh4zNvbu8sTEZ53nfKcqY 0:T:65:D717FEC1993554F8EAE4CEA88DE5FBB6887CFAE8:4 0:T:77:F80993776FB55154A60B3E58910C942A347964AD:15 @@ -17,11 +17,60 @@ BYfWYFrsyjpvpFysgu19rGK3VHBkz4MqmQbNyEuVU64g:30 42yQm4hGTJYWkPg39hQAUgP6S6EQ4vTfXdJuxKEHL1ih6YHiDL2hcwrFgBHjXLRgxRhj2VNVqqc6b4JayKqTE14r """ +tx_raw = """Version: 1 +Type: Transaction +Currency: beta_brousouf +Issuers: +HsLShAtzXTVxeUtQd7yi5Z5Zh4zNvbu8sTEZ53nfKcqY +Inputs: +0:T:65:D717FEC1993554F8EAE4CEA88DE5FBB6887CFAE8:4 +0:T:77:F80993776FB55154A60B3E58910C942A347964AD:15 +0:D:88:F4A47E39BC2A20EE69DCD5CAB0A9EB3C92FD8F7B:11 +Outputs: +BYfWYFrsyjpvpFysgu19rGK3VHBkz4MqmQbNyEuVU64g:30 +Comment: +42yQm4hGTJYWkPg39hQAUgP6S6EQ4vTfXdJuxKEHL1ih6YHiDL2hcwrFgBHjXLRgxRhj2VNVqqc6b4JayKqTE14r +""" + class Test_Transaction: + def test_fromcompact(self): + tx = Transaction.from_compact("zeta_brousouf", tx_compact) + assert tx.version == 1 + assert tx.currency == "zeta_brousouf" + assert len(tx.issuers) == 1 + assert len(tx.inputs) == 3 + assert len(tx.outputs) == 1 + + assert tx.issuers[0] == "HsLShAtzXTVxeUtQd7yi5Z5Zh4zNvbu8sTEZ53nfKcqY" + + assert tx.inputs[0].index == 0 + assert tx.inputs[0].source == 'T' + assert tx.inputs[0].number == 65 + assert tx.inputs[0].txhash == "D717FEC1993554F8EAE4CEA88DE5FBB6887CFAE8" + assert tx.inputs[0].amount == 4 + + assert tx.inputs[1].index == 0 + assert tx.inputs[1].source == 'T' + assert tx.inputs[1].number == 77 + assert tx.inputs[1].txhash == "F80993776FB55154A60B3E58910C942A347964AD" + assert tx.inputs[1].amount == 15 + + assert tx.inputs[2].index == 0 + assert tx.inputs[2].source == 'D' + assert tx.inputs[2].number == 88 + assert tx.inputs[2].txhash == "F4A47E39BC2A20EE69DCD5CAB0A9EB3C92FD8F7B" + assert tx.inputs[2].amount == 11 + + assert tx.outputs[0].pubkey == "BYfWYFrsyjpvpFysgu19rGK3VHBkz4MqmQbNyEuVU64g" + assert tx.outputs[0].amount == 30 + + assert tx.signatures[0] == "42yQm4hGTJYWkPg39hQAUgP6S6EQ4vTfXdJuxKEHL1ih6YHiDL2hcwrFgBHjXLRgxRhj2VNVqqc6b4JayKqTE14r" + def test_fromraw(self): - tx = Transaction.from_compact("zeta_brousouf", 2, compact_transaction) + tx = Transaction.from_signed_raw(tx_raw) assert tx.version == 1 + assert tx.currency == "beta_brousouf" assert len(tx.issuers) == 1 assert len(tx.inputs) == 3 assert len(tx.outputs) == 1 @@ -50,3 +99,41 @@ class Test_Transaction: assert tx.outputs[0].amount == 30 assert tx.signatures[0] == "42yQm4hGTJYWkPg39hQAUgP6S6EQ4vTfXdJuxKEHL1ih6YHiDL2hcwrFgBHjXLRgxRhj2VNVqqc6b4JayKqTE14r" + + + + def test_fromraw_toraw(self): + tx = Transaction.from_signed_raw(tx_raw) + rendered_tx = tx.signed_raw() + from_rendered_tx = Transaction.from_signed_raw(rendered_tx) + + assert from_rendered_tx.version == 1 + assert len(from_rendered_tx.issuers) == 1 + assert len(from_rendered_tx.inputs) == 3 + assert len(from_rendered_tx.outputs) == 1 + + assert from_rendered_tx.issuers[0] == "HsLShAtzXTVxeUtQd7yi5Z5Zh4zNvbu8sTEZ53nfKcqY" + + assert from_rendered_tx.inputs[0].index == 0 + assert from_rendered_tx.inputs[0].source == 'T' + assert from_rendered_tx.inputs[0].number == 65 + assert from_rendered_tx.inputs[0].txhash == "D717FEC1993554F8EAE4CEA88DE5FBB6887CFAE8" + assert from_rendered_tx.inputs[0].amount == 4 + + assert from_rendered_tx.inputs[1].index == 0 + assert from_rendered_tx.inputs[1].source == 'T' + assert from_rendered_tx.inputs[1].number == 77 + assert from_rendered_tx.inputs[1].txhash == "F80993776FB55154A60B3E58910C942A347964AD" + assert from_rendered_tx.inputs[1].amount == 15 + + assert from_rendered_tx.inputs[2].index == 0 + assert from_rendered_tx.inputs[2].source == 'D' + assert from_rendered_tx.inputs[2].number == 88 + assert from_rendered_tx.inputs[2].txhash == "F4A47E39BC2A20EE69DCD5CAB0A9EB3C92FD8F7B" + assert from_rendered_tx.inputs[2].amount == 11 + + assert from_rendered_tx.outputs[0].pubkey == "BYfWYFrsyjpvpFysgu19rGK3VHBkz4MqmQbNyEuVU64g" + assert from_rendered_tx.outputs[0].amount == 30 + + assert from_rendered_tx.signatures[0] == "42yQm4hGTJYWkPg39hQAUgP6S6EQ4vTfXdJuxKEHL1ih6YHiDL2hcwrFgBHjXLRgxRhj2VNVqqc6b4JayKqTE14r" + diff --git a/ucoinpy/documents/transaction.py b/ucoinpy/documents/transaction.py index 235a5a02d13b2bee732db3ce7d22bd5e0d9fc419..e5fd76ed6ca3cc6dbc9cc4717c66d8bf3788b281 100644 --- a/ucoinpy/documents/transaction.py +++ b/ucoinpy/documents/transaction.py @@ -40,13 +40,13 @@ SIGNATURE ... ''' - re_type = re.compile("Type: Transaction\n") + re_type = re.compile("Type: (Transaction)\n") re_header = re.compile("TX:([0-9])+:([0-9])+:([0-9])+:([0-9])+:(0|1)\n") re_issuers = re.compile("Issuers:\n") re_inputs = re.compile("Inputs:\n") re_outputs = re.compile("Outputs:\n") re_compact_comment = re.compile("-----@@@-----([^\n]+)\n") - re_comment = re.compile("Comment: ([^\n]+)\n") + re_comment = re.compile("Comment:(?:)?([^\n]*)\n") re_pubkey = re.compile("([1-9A-Za-z][^OIl]{42,45})\n") def __init__(self, version, currency, issuers, inputs, outputs, @@ -65,7 +65,7 @@ SIGNATURE self.comment = comment @classmethod - def from_compact(cls, currency, number, compact): + def from_compact(cls, currency, compact): lines = compact.splitlines(True) n = 0 @@ -112,7 +112,7 @@ SIGNATURE lines = raw.splitlines(True) n = 0 - version = Transaction.re_version.match(lines[n]).group(1) + version = int(Transaction.re_version.match(lines[n]).group(1)) n = n + 1 Transaction.re_type.match(lines[n]).group(1) @@ -127,42 +127,44 @@ SIGNATURE signatures = [] if Transaction.re_issuers.match(lines[n]): - lines = lines + 1 + n = n + 1 while Transaction.re_inputs.match(lines[n]) is None: issuer = Transaction.re_pubkey.match(lines[n]).group(1) issuers.append(issuer) - lines = lines + 1 + n = n + 1 if Transaction.re_inputs.match(lines[n]): - lines = lines + 1 + n = n + 1 while Transaction.re_outputs.match(lines[n]) is None: input_source = InputSource.from_inline(lines[n]) inputs.append(input_source) - lines = lines + 1 + n = n + 1 if Transaction.re_outputs.match(lines[n]) is not None: + n = n + 1 while not Transaction.re_comment.match(lines[n]): output = OutputSource.from_inline(lines[n]) outputs.append(output) - lines = lines + 1 + n = n + 1 comment = Transaction.re_comment.match(lines[n]).group(1) + n = n + 1 - if Transaction.re_sign.match(lines[n]) is not None: - while n < lines.len: - sign = Transaction.re_sign.match(lines[n]).group(1) + if Transaction.re_signature.match(lines[n]) is not None: + while n < len(lines): + sign = Transaction.re_signature.match(lines[n]).group(1) signatures.append(sign) - lines = lines + 1 + n = n + 1 return cls(version, currency, issuers, inputs, outputs, comment, signatures) def raw(self): - doc = """ -Version: {0} + doc = """Version: {0} Type: Transaction Currency: {1} -Issuers:""".format(self.version, +Issuers: +""".format(self.version, self.currency) for p in self.issuers: @@ -176,10 +178,10 @@ Issuers:""".format(self.version, for o in self.outputs: doc += "{0}\n".format(o.inline()) - doc += """ -COMMENT: -{0} -""".format(self.comment) + doc += "Comment: " + if self.comment: + doc += "{0}".format(self.comment) + doc += "\n" for signature in self.signatures: doc += "{0}\n".format(signature) @@ -211,7 +213,7 @@ COMMENT for o in self.outputs: doc += "{0}\n".format(o.inline()) if self.comment: - doc += "{0}\n".format(self.comment) + doc += "-----@@@----- {0}\n".format(self.comment) for s in self.signatures: doc += "{0}\n".format(s)