0

Transaction.py

from TransactionInput import TransactionInput
import unittest
from io import BytesIO
from binascii import unhexlify
import json


class Transaction:
    def __init__(self, version, inputs):
        self.version = version
        self.inputs = inputs

    @classmethod
    def parse(cls, stream):
        version = int.from_bytes(stream.read(4), byteorder='little')

        inputs_size = int.from_bytes(stream.read(1), byteorder='big')
        tx_inputs = []
        for i in range(0, inputs_size):
            tx_input = TransactionInput.parse(stream)
            tx_inputs.append(tx_input)

        return cls(version, tx_inputs)


class TestTransaction(unittest.TestCase):
    def test_parse(self):
        raw_transaction = '0100000001280594d0869749bd0f0e76074637a41a534cf96b0f7787aafe36f2c466ae3c50000000006b48304502204b39b9d63d0718052bd64c120f768a5eb083a8184a31409520104bc2a508af4f022100f62d5ce8b74a61e0075b8f3dc14df8b2e4714a962a0644e458d56bd1cb92915e012103ca545c610051cb0cca1539d95b58dd87075f97e154e9c652bafe4379424c1ba9ffffffff02672df26d010000001976a91485c359dfc971723d6724d42c60b362cce7fe01d488ac4029a1d1000000001976a9147c228e5f129213301b84b3a28aa22bf3556568e588ac00000000';
        bytes = unhexlify(raw_transaction)
        transaction = Transaction.parse(BytesIO(bytes))
        print(json.dumps(vars(transaction), sort_keys=True, indent=4))


if __name__ == '__main__':
    unittest.main()

TransactionInput.py

from ScriptSig import ScriptSig
from binascii import hexlify


class TransactionInput:
    def __init__(self, prev_tx_hash):
        self.prev_tx_hash = prev_tx_hash

    @classmethod
    def parse(cls, stream):
        prev_tx_hash = hexlify(stream.read(32)).decode('ascii')
        return cls(prev_tx_hash)

error

TypeError: Object of type 'TransactionInput' is not JSON serializable

I have Transaction object which has nested objects of type TransactionInput. i am trying to convert this transaction object into JSON. when i do this without inner object, it worked. But upon adding inner object, it raised an error. can someone help me? I am open to better solutions/libraries for JSON coversion

1 Answer 1

1

The problem is that your transaction object contains a member of type TransactionInput (which is not serializable - only basic types, lists and dictionaries are):

{'version': 1, 'inputs': [<TransactionInput.TransactionInput object at 0x1082cd0f0>]}

There are several options to fix this, but since what you are using are relatively simple objects I would suggest handling the serialization by yourself:

class TransactionInput:
    def __init__(self, prev_tx_hash):
        self.prev_tx_hash = prev_tx_hash

    def to_json(self):
        """ This serializes each TransactionInput"""
        return "{{prev_tx_hash: {}}}".format(self.prev_tx_hash) 

 class Transaction:
    def __init__(self, version, inputs):
        self.version = version
        self.inputs = inputs


    def to_json(self):
        """ This serializes the whole Transaction object"""
        inputs_json = ','.join(_input.to_json() for _input in self.inputs)

        return '{{version: {}, inputs: [{}]}}'.format(self.version, self.inputs)

With that, you can just serialize your Transaction calling transaction.to_json() and your test will pass.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.