Ejemplo n.º 1
0
def test_rivine_types():
    e = RivineBinaryEncoder()

    # in the rivine_basic test we saw we can
    # serialise anything using the add method.
    # One can however also directly encode the specific type as desired,
    # which allows for example the encoding of an integer as a specific byte size.
    e.add_int8(1)
    e.add_int16(2)
    e.add_int24(3)
    e.add_int32(4)
    e.add_int64(5)

    # a single byte can be added as well
    e.add_byte(6)
    e.add_byte('4')
    e.add_byte(b'2')

    # array are like slices, but have no length prefix,
    # therefore this is only useful if there is a fixed amount of elements,
    # known by all parties
    e.add_array([False, True, True])

    # the result is a single bytearray
    jsass.equals(e.data, b'\x01\x02\x00\x03\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x0642\x00\x01\x01')
Ejemplo n.º 2
0
def test_sia_custom():
    e = SiaBinaryEncoder()

    # a class that provides a custom encoding logic for its types,
    # required in order to be able to encode Python objects
    class Answer(SiaBinaryObjectEncoderBase):
        def __init__(self, number=0):
            self._number = number

        def sia_binary_encode(self, encoder):
            if self._number == 42:
                return encoder.add(True)
            return encoder.add(False)

    # when we add our objects they will be encoded
    # using the method as provided by its type
    e.add(Answer())
    e.add(Answer(42))

    # this works for slices and arrays as well
    e.add_array([Answer(5), Answer(2)])

    # the result is a single bytearray
    jsass.equals(e.data, b'\x00\x01\x00\x00')

    # everything has limits, so do types,
    # that is what this test is about

    # no integer can be negative
    jsass.raises(IntegerOutOfRange, lambda _: e.add(-1))
Ejemplo n.º 3
0
def test_basic_merkletree():
    tree = Tree(hash_func=blake2b)
    tree.push(bytearray([1]))
    tree.push(bytearray([2]))
    tree.push(bytearray([3]))
    tree.push(bytearray([4]))
    tree.push(bytearray([5]))
    root = tree.root()
    jsass.equals(
        root,
        '0002789a97a9feee38af3709f06377ef0ad7d91407cbcad1ccb8605556b6578e')
Ejemplo n.º 4
0
def test_rivine_basic_encoding():
    e = RivineBinaryEncoder()

    # you can add integers, booleans, iterateble objects, strings,
    # bytes and byte arrays. Dictionaries and objects are not supported.
    e.add(False)
    e.add("a")
    e.add([1, True, "foo"])
    e.add(b"123")

    # the result is a single bytearray
    jsass.equals(e.data, b'\x00\x02a\x06\x01\x00\x00\x00\x00\x00\x00\x00\x01\x06foo\x06123')
Ejemplo n.º 5
0
def test_assymetric_key_pair_generate():
    entropy = __bip39.to_entropy(
        'usage video strategy recall chair brown enforce leopard liar captain churn pill usage column save copper kitten panel heavy bless history business attack sauce'
    )
    pair = tfwallet.assymetric_key_pair_generate(entropy, 0)
    jsass.equals(
        pair.key_secret,
        '68a5d13bd36777e09aaa38c99f269e3753276cc960a80447157614a2b71b76290e2975598765b233a73cdc7cf467468454788701b3dc6fa7bb05e0d332b5551c'
    )
    uh = tfwallet.unlockhash_from_assymetric_key_pair(pair)
    address = uh.__str__()
    jsass.equals(
        address,
        '01ed90bee1d6d50b730a1aacf2890ac6fc0f7718849fba5f7c5719e3cfcc4641be09c5607b0210'
    )
Ejemplo n.º 6
0
def test_sia_types():
    e = SiaBinaryEncoder()

    # in the sia_basic test we saw we can
    # serialise anything using the add method.

    # by default strings, byte arrays and iterateable objects
    # are encoded as slices.
    #
    # array are like slices, but have no length prefix,
    # therefore this is only useful if there is a fixed amount of elements,
    # known by all parties
    e.add_array([False, True, True])

    # a single byte can be added as well
    e.add_byte(6)
    e.add_byte('4')
    e.add_byte(b'2')

    # the result is a single bytearray
    jsass.equals(e.data, b'\x00\x01\x01\x0642')
Ejemplo n.º 7
0
def test_rivine_custom():
    e = RivineBinaryEncoder()

    # a class that provides a custom encoding logic for its types,
    # required in order to be able to encode Python objects
    class Answer(RivineBinaryObjectEncoderBase):
        def __init__(self, number=0):
            self._number = number

        def rivine_binary_encode(self, encoder):
            if self._number % 2 != 0:
                return encoder.add_int24(self._number - 1)
            return encoder.add_int24(self._number)

    # when we add our objects they will be encoded
    # using the method as provided by its type
    e.add(Answer())
    e.add(Answer(43))

    # this works for slices and arrays as well
    e.add_array([Answer(5), Answer(2)])

    # the result is a single bytearray
    jsass.equals(e.data, b'\x00\x00\x00\x2A\x00\x00\x04\x00\x00\x02\x00\x00')
Ejemplo n.º 8
0
 def test_encoded(encoder, obj, expected):
     encoder.add(obj)
     jsass.equals(encoder.data, expected)
Ejemplo n.º 9
0
def test_fulfillments():
    def test_encoded(encoder, obj, expected):
        encoder.add(obj)
        jsass.equals(encoder.data, expected)

    def test_sia_encoded(obj, expected):
        test_encoded(SiaBinaryEncoder(), obj, expected)

    def test_rivine_encoded(obj, expected):
        test_encoded(RivineBinaryEncoder(), obj, expected)

    # single signature fulfillments are supported
    ss_json = json_loads(
        '{"type":1,"data":{"publickey":"ed25519:dda39750fff2d869badc797aaad1dea8c6bcf943879421c58ac8d8e2072d5b5f","signature":"818dfccee2cb7dbe4156169f8e1c5be49a3f6d83a4ace310863d7b3b698748e3e4d12522fc1921d5eccdc108b36c84d769a9cf301e969bb05db1de9295820300"}}'
    )
    ssf = FulfillmentTypes.from_json(ss_json)
    jsass.equals(ssf.json(), ss_json)
    test_sia_encoded(
        ssf,
        '018000000000000000656432353531390000000000000000002000000000000000dda39750fff2d869badc797aaad1dea8c6bcf943879421c58ac8d8e2072d5b5f4000000000000000818dfccee2cb7dbe4156169f8e1c5be49a3f6d83a4ace310863d7b3b698748e3e4d12522fc1921d5eccdc108b36c84d769a9cf301e969bb05db1de9295820300'
    )
    test_rivine_encoded(
        ssf,
        '01c401dda39750fff2d869badc797aaad1dea8c6bcf943879421c58ac8d8e2072d5b5f80818dfccee2cb7dbe4156169f8e1c5be49a3f6d83a4ace310863d7b3b698748e3e4d12522fc1921d5eccdc108b36c84d769a9cf301e969bb05db1de9295820300'
    )
    jsass.is_true(ssf.is_fulfilled(ConditionUnlockHash()))

    # atomic swap fulfillments are supported
    as_json = json_loads(
        '{"type":2,"data":{"publickey":"ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","signature":"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab","secret":"def789def789def789def789def789dedef789def789def789def789def789de"}}'
    )
    asf = FulfillmentTypes.from_json(as_json)
    jsass.equals(asf.json(), as_json)
    test_sia_encoded(
        asf,
        '02a000000000000000656432353531390000000000000000002000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabdef789def789def789def789def789dedef789def789def789def789def789de'
    )
    test_rivine_encoded(
        asf,
        '02090201ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabdef789def789def789def789def789dedef789def789def789def789def789de'
    )
    jsass.is_true(asf.is_fulfilled(ConditionAtomicSwap()))
    # atomic swap fulfillments without secrets are supported
    as_json = json_loads(
        '{"type":2,"data":{"publickey":"ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","signature":"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab"}}'
    )
    asf = FulfillmentTypes.from_json(as_json)
    jsass.equals(asf.json(), as_json)
    test_sia_encoded(
        asf,
        '028000000000000000656432353531390000000000000000002000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab'
    )
    test_rivine_encoded(
        asf,
        '02c401ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab'
    )
    jsass.is_true(asf.is_fulfilled(ConditionAtomicSwap()))

    # multi signature fulfillments are supported
    ms_json = json_loads(
        '{"type":3,"data":{"pairs":[{"publickey":"ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","signature":"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab"},{"publickey":"ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff","signature":"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab"}]}}'
    )
    msf = FulfillmentTypes.from_json(ms_json)
    jsass.equals(msf.json(), ms_json)
    test_sia_encoded(
        msf,
        '0308010000000000000200000000000000656432353531390000000000000000002000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab656432353531390000000000000000002000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab'
    )
    test_rivine_encoded(
        msf,
        '0315030401ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab'
    )
    jsass.is_true(msf.is_fulfilled(ConditionMultiSignature(min_nr_sig=1)))
    jsass.is_true(msf.is_fulfilled(ConditionMultiSignature(min_nr_sig=2)))
Ejemplo n.º 10
0
def test_standard_transactions():
    # v1 Transactions are supported
    v1_txn_json = jsjson.json_loads(
        '{"version":1,"data":{"coininputs":[{"parentid":"5b907d6e4d34cdd825484d2f9f14445377fb8b4f8cab356a390a7fe4833a3085","fulfillment":{"type":1,"data":{"publickey":"ed25519:bd5e0e345d5939f5f9eb330084c7f0ffb8fc7fc5bdb07a94c304620eb4e2d99a","signature":"55dace7ccbc9cdd23220a8ef3ec09e84ce5c5acc202c5f270ea0948743ebf52135f3936ef7477170b4f9e0fe141a61d8312ab31afbf926a162982247e5d2720a"}}}],"coinoutputs":[{"value":"1000000000","condition":{"type":1,"data":{"unlockhash":"010009a2b6a482da73204ccc586f6fab5504a1a69c0d316cdf828a476ae7c91c9137fd6f1b62bb"}}},{"value":"8900000000","condition":{"type":1,"data":{"unlockhash":"01b81f9e02d6be3a7de8440365a7c799e07dedf2ccba26fd5476c304e036b87c1ab716558ce816"}}}],"blockstakeinputs":[{"parentid":"782e4819d6e199856ba1bff3def5d7cc37ae2a0dabecb05359d6072156190d68","fulfillment":{"type":1,"data":{"publickey":"ed25519:95990ca3774de81309932302f74dfe9e540d6c29ca5cb9ee06e999ad46586737","signature":"70be2115b82a54170c94bf4788e2a6dd154a081f61e97999c2d9fcc64c41e7df2e8a8d4f82a57a04a1247b9badcb6bffbd238e9a6761dd59e5fef7ff6df0fc01"}}}],"blockstakeoutputs":[{"value":"99","condition":{"type":1,"data":{"unlockhash":"01fdf10836c119186f1d21666ae2f7dc62d6ecc46b5f41449c3ee68aea62337dad917808e46799"}}}],"minerfees":["100000000"],"arbitrarydata":"ZGF0YQ=="}}'
    )
    v1_txn = tftransactions.from_json(v1_txn_json)
    # assert v1_txn.json() == v1_txn_json
    jsass.equals(v1_txn.signature_hash_get(0), v1_txn.signature_hash_get(0))
    jsass.equals(v1_txn.binary_encode(), v1_txn.binary_encode())
    jsass.equals(
        v1_txn.coin_outputid_new(0).str(),
        v1_txn.coin_outputid_new(0).str())
    jsass.equals(
        v1_txn.blockstake_outputid_new(0).str(),
        v1_txn.blockstake_outputid_new(0).str())

    # v1 transactions do not have block stakes when used just for coins
    v1_txn_json = jsjson.json_loads(
        '{"version":1,"data":{"coininputs":[{"parentid":"5b907d6e4d34cdd825484d2f9f14445377fb8b4f8cab356a390a7fe4833a3085","fulfillment":{"type":1,"data":{"publickey":"ed25519:bd5e0e345d5939f5f9eb330084c7f0ffb8fc7fc5bdb07a94c304620eb4e2d99a","signature":"55dace7ccbc9cdd23220a8ef3ec09e84ce5c5acc202c5f270ea0948743ebf52135f3936ef7477170b4f9e0fe141a61d8312ab31afbf926a162982247e5d2720a"}}}],"coinoutputs":[{"value":"1000000000","condition":{"type":1,"data":{"unlockhash":"010009a2b6a482da73204ccc586f6fab5504a1a69c0d316cdf828a476ae7c91c9137fd6f1b62bb"}}},{"value":"8900000000","condition":{"type":1,"data":{"unlockhash":"01b81f9e02d6be3a7de8440365a7c799e07dedf2ccba26fd5476c304e036b87c1ab716558ce816"}}}],"minerfees":["100000000"],"arbitrarydata":"ZGF0YQ=="}}'
    )
    v1_txn = tftransactions.from_json(v1_txn_json)
    # assert v1_txn.json() == v1_txn_json
    jsass.equals(v1_txn.signature_hash_get(0), v1_txn.signature_hash_get(0))
    jsass.equals(v1_txn.binary_encode(), v1_txn.binary_encode())
    jsass.equals(
        v1_txn.coin_outputid_new(0).str(),
        v1_txn.coin_outputid_new(0).str())

    # v0 Transactions are supported, but are not recommend or created by this client
    v0_txn_json = jsjson.json_loads(
        '{"version":0,"data":{"coininputs":[{"parentid":"abcdef012345abcdef012345abcdef012345abcdef012345abcdef012345abcd","unlocker":{"type":1,"condition":{"publickey":"ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},"fulfillment":{"signature":"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab"}}}],"coinoutputs":[{"value":"3","unlockhash":"0142e9458e348598111b0bc19bda18e45835605db9f4620616d752220ae8605ce0df815fd7570e"},{"value":"5","unlockhash":"01a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc353bdcf54be7d8"}],"blockstakeinputs":[{"parentid":"dfd23dfd23dfd23dfd23dfd23dfd23dfd23dfd23dfd23dfd23dfd23dfd23dfde","unlocker":{"type":1,"condition":{"publickey":"ed25519:ef1234ef1234ef1234ef1234ef1234ef1234ef1234ef1234ef1234ef1234ef12"},"fulfillment":{"signature":"01234def01234def01234def01234def01234def01234def01234def01234def01234def01234def01234def01234def01234def01234def01234def01234def"}}}],"blockstakeoutputs":[{"value":"4","unlockhash":"0142e9458e348598111b0bc19bda18e45835605db9f4620616d752220ae8605ce0df815fd7570e"},{"value":"2","unlockhash":"01a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc353bdcf54be7d8"}],"minerfees":["1","2","3"],"arbitrarydata":"ZGF0YQ=="}}'
    )
    v0_txn = tftransactions.from_json(v0_txn_json)
    jsass.equals(v0_txn.signature_hash_get(0), v0_txn.signature_hash_get(0))
    jsass.equals(v0_txn.binary_encode(), v0_txn.binary_encode())
    jsass.equals(
        v0_txn.coin_outputid_new(0).str(),
        v0_txn.coin_outputid_new(0).str())
    jsass.equals(
        v0_txn.blockstake_outputid_new(0).str(),
        v0_txn.blockstake_outputid_new(0).str())

    # v0 Transactions do not have block stakes when used just for coins
    v0_txn_json = jsjson.json_loads(
        '{"version":0,"data":{"coininputs":[{"parentid":"abcdef012345abcdef012345abcdef012345abcdef012345abcdef012345abcd","unlocker":{"type":1,"condition":{"publickey":"ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"},"fulfillment":{"signature":"abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab"}}}],"coinoutputs":[{"value":"3","unlockhash":"0142e9458e348598111b0bc19bda18e45835605db9f4620616d752220ae8605ce0df815fd7570e"},{"value":"5","unlockhash":"01a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc353bdcf54be7d8"}],"minerfees":["1","2","3"],"arbitrarydata":"ZGF0YQ=="}}'
    )
    v0_txn = tftransactions.from_json(v0_txn_json)
    jsass.equals(v0_txn.signature_hash_get(0), v0_txn.signature_hash_get(0))
    jsass.equals(v0_txn.binary_encode(), v0_txn.binary_encode())
    jsass.equals(
        v0_txn.coin_outputid_new(0).str(),
        v0_txn.coin_outputid_new(0).str())

    # Coin Creation Transactions

    # v128 Transactions are supported
    v128_txn_json = jsjson.json_loads(
        '{"version":128,"data":{"nonce":"FoAiO8vN2eU=","mintfulfillment":{"type":1,"data":{"publickey":"ed25519:d285f92d6d449d9abb27f4c6cf82713cec0696d62b8c123f1627e054dc6d7780","signature":"bdf023fbe7e0efec584d254b111655e1c2f81b9488943c3a712b91d9ad3a140cb0949a8868c5f72e08ccded337b79479114bdb4ed05f94dfddb359e1a6124602"}},"mintcondition":{"type":1,"data":{"unlockhash":"01e78fd5af261e49643dba489b29566db53fa6e195fa0e6aad4430d4f06ce88b73e047fe6a0703"}},"minerfees":["1000000000"],"arbitrarydata":"YSBtaW50ZXIgZGVmaW5pdGlvbiB0ZXN0"}}'
    )
    v128_txn = tftransactions.from_json(v128_txn_json)
    jsass.equals(v128_txn.json(), v128_txn_json)
    jsass.equals(
        v128_txn.signature_hash_get(0),
        'c0b865dd6980f377c9bc6fb195bca3cf169ea06e6bc658b29639bdb6fc387f8d')
    jsass.equals(
        v128_txn.binary_encode(),
        '801680223bcbcdd9e5018000000000000000656432353531390000000000000000002000000000000000d285f92d6d449d9abb27f4c6cf82713cec0696d62b8c123f1627e054dc6d77804000000000000000bdf023fbe7e0efec584d254b111655e1c2f81b9488943c3a712b91d9ad3a140cb0949a8868c5f72e08ccded337b79479114bdb4ed05f94dfddb359e1a612460201210000000000000001e78fd5af261e49643dba489b29566db53fa6e195fa0e6aad4430d4f06ce88b73010000000000000004000000000000003b9aca00180000000000000061206d696e74657220646566696e6974696f6e2074657374'
    )

    # v129 Transactions are supported
    v129_txn_json = jsjson.json_loads(
        '{"version":129,"data":{"nonce":"1oQFzIwsLs8=","mintfulfillment":{"type":1,"data":{"publickey":"ed25519:d285f92d6d449d9abb27f4c6cf82713cec0696d62b8c123f1627e054dc6d7780","signature":"ad59389329ed01c5ee14ce25ae38634c2b3ef694a2bdfa714f73b175f979ba6613025f9123d68c0f11e8f0a7114833c0aab4c8596d4c31671ec8a73923f02305"}},"coinoutputs":[{"value":"500000000000000","condition":{"type":1,"data":{"unlockhash":"01e3cbc41bd3cdfec9e01a6be46a35099ba0e1e1b793904fce6aa5a444496c6d815f5e3e981ccf"}}}],"minerfees":["1000000000"],"arbitrarydata":"dGVzdC4uLiAxLCAyLi4uIDM="}}'
    )
    v129_txn = tftransactions.from_json(v129_txn_json)
    jsass.equals(v129_txn.json(), v129_txn_json)
    jsass.equals(
        v129_txn.signature_hash_get(0),
        '984ccc3da2107e86f67ef618886f9144040d84d9f65f617c64fa34de68c0018b')
    jsass.equals(
        v129_txn.binary_encode(),
        '81d68405cc8c2c2ecf018000000000000000656432353531390000000000000000002000000000000000d285f92d6d449d9abb27f4c6cf82713cec0696d62b8c123f1627e054dc6d77804000000000000000ad59389329ed01c5ee14ce25ae38634c2b3ef694a2bdfa714f73b175f979ba6613025f9123d68c0f11e8f0a7114833c0aab4c8596d4c31671ec8a73923f023050100000000000000070000000000000001c6bf5263400001210000000000000001e3cbc41bd3cdfec9e01a6be46a35099ba0e1e1b793904fce6aa5a444496c6d81010000000000000004000000000000003b9aca001100000000000000746573742e2e2e20312c20322e2e2e2033'
    )
    jsass.equals(
        v129_txn.coin_outputid_new(0).str(),
        'f4b8569f430a29af187a8b97e78167b63895c51339255ea5198a35f4b162b4c6')
Ejemplo n.º 11
0
def test_types():
    # currency values can be created from both
    # int and str values, but are never allowed to be negative
    jsass.equals(Currency().str(), '0')
    jsass.equals(Currency(value=123).str(), '123')
    jsass.equals(Currency(value='1').str(), '1')
    jsass.equals(Currency(value='0.1').json(), '100000000')
    # in the string versions you can also add the TFT currency notation,
    # or use decimal notation to express the currency in the TFT Currency Unit,
    # rather than the primitive unit
    jsass.equals(Currency(value='1 TFT').str(), '1')
    jsass.equals(Currency(value='0.123456789').str(), '0.123456789')
    jsass.equals(Currency(value='0.123456789').json(), '123456789')
    jsass.equals(Currency(value='9.123456789').str(), '9.123456789')
    jsass.equals(Currency(value='1234.34').str(), '1234.34')
    jsass.equals(Currency(value='1.00000').str(), '1')
    jsass.equals(Currency(value='1.0 tft').str(), '1')
    jsass.equals(Currency(value=1).str(), '1')
    jsass.equals(Currency(value=12344).str(), '12344')

    # hash values can be created directly from binary data,
    # or from a hex-encoded string, by default the nil hash will be created
    jsass.equals(Hash().str(), '0' * 64)
    jsass.equals(
        Hash(b'12345678901234567890123456789001').value,
        b'12345678901234567890123456789001')

    # binary data is very similar to a hash,
    # except that it doesn't have a fixed length and it is binary serialized
    # as a slice, not an array
    jsass.equals(BinaryData().str(), '')
    jsass.equals(BinaryData(b'1').str(), '31')
    jsass.equals(BinaryData(b'1', fixed_size=0).str(), '31')
    jsass.equals(BinaryData(b'1', fixed_size=1).str(), '31')

    # raw data is pretty much binary data, except that it is
    # base64 encoded/decoded for str/json purposes
    jsass.equals(BinaryData(b'data', strencoding='base64').str(), 'ZGF0YQ==')

    # block stake values can be created from both
    # int and str values, but are never allowed to be negative
    jsass.equals(Blockstake().str(), '0')
    jsass.equals(Blockstake(value=123).str(), '123')
    jsass.equals(Blockstake(value='1').str(), '1')
Ejemplo n.º 12
0
def test_conditiontypes():
    def test_encoded(encoder, obj, expected):
        encoder.add(obj)
        jsass.equals(encoder.data, expected)

    def test_sia_encoded(obj, expected):
        test_encoded(SiaBinaryEncoder(), obj, expected)

    def test_rivine_encoded(obj, expected):
        test_encoded(RivineBinaryEncoder(), obj, expected)

    # Nil conditions are supported
    for n_json in [
            '{}', '{"type": 0}', '{"type": 0, "data": null}',
            '{"type": 0, "data": {}}'
    ]:
        cn = ConditionTypes.from_json(json_loads(n_json))
        jsass.equals(cn.json(), {"type": 0})
        test_sia_encoded(cn, '000000000000000000')
        test_rivine_encoded(cn, '0000')
        jsass.equals(
            str(cn.unlockhash),
            '000000000000000000000000000000000000000000000000000000000000000000000000000000'
        )

    # UnlockHash conditions are supported
    uh_json_raw = '{"type":1,"data":{"unlockhash":"000000000000000000000000000000000000000000000000000000000000000000000000000000"}}'
    uh_json = json_loads(uh_json_raw)
    cuh = ConditionTypes.from_json(uh_json)
    jsass.equals(cuh.json(), uh_json)
    test_sia_encoded(
        cuh,
        '012100000000000000000000000000000000000000000000000000000000000000000000000000000000'
    )
    test_rivine_encoded(
        cuh,
        '0142000000000000000000000000000000000000000000000000000000000000000000'
    )
    jsass.equals(
        cuh.unlockhash,
        '000000000000000000000000000000000000000000000000000000000000000000000000000000'
    )

    # AtomicSwap conditions are supported
    as_json = json_loads(
        '{"type":2,"data":{"sender":"01e89843e4b8231a01ba18b254d530110364432aafab8206bea72e5a20eaa55f70b1ccc65e2105","receiver":"01a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc353bdcf54be7d8","hashedsecret":"abc543defabc543defabc543defabc543defabc543defabc543defabc543defa","timelock":1522068743}}'
    )
    cas = ConditionTypes.from_json(as_json)
    jsass.equals(cas.json(), as_json)
    test_sia_encoded(
        cas,
        '026a0000000000000001e89843e4b8231a01ba18b254d530110364432aafab8206bea72e5a20eaa55f7001a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc35abc543defabc543defabc543defabc543defabc543defabc543defabc543defa07edb85a00000000'
    )
    test_rivine_encoded(
        cas,
        '02d401e89843e4b8231a01ba18b254d530110364432aafab8206bea72e5a20eaa55f7001a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc35abc543defabc543defabc543defabc543defabc543defabc543defabc543defa07edb85a00000000'
    )
    jsass.equals(
        str(cas.unlockhash),
        '026e18a53ec6e571985ea7ed404a5d51cf03a72240065952034383100738627dbf949046789e30'
    )

    # MultiSig conditions are supported
    ms_json_raw = '{"type":4,"data":{"unlockhashes":["01e89843e4b8231a01ba18b254d530110364432aafab8206bea72e5a20eaa55f70b1ccc65e2105","01a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc353bdcf54be7d8"],"minimumsignaturecount":2}}'
    ms_json = json_loads(ms_json_raw)
    cms = ConditionTypes.from_json(ms_json)
    jsass.equals(cms.json(), ms_json)
    test_sia_encoded(
        cms,
        '0452000000000000000200000000000000020000000000000001e89843e4b8231a01ba18b254d530110364432aafab8206bea72e5a20eaa55f7001a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc35'
    )
    test_rivine_encoded(
        cms,
        '049602000000000000000401e89843e4b8231a01ba18b254d530110364432aafab8206bea72e5a20eaa55f7001a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc35'
    )
    jsass.equals(
        cms.unlockhash,
        '0313a5abd192d1bacdd1eb518fc86987d3c3d1cfe3c5bed68ec4a86b93b2f05a89f67b89b07d71'
    )

    # LockTime conditions are supported:
    # - wrapping a nil condition
    lt_n_json = json_loads(
        '{"type":3,"data":{"locktime":500000000,"condition":{"type":0}}}')
    clt_n = ConditionTypes.from_json(lt_n_json)
    jsass.equals(clt_n.json(), lt_n_json)
    test_sia_encoded(clt_n, '0309000000000000000065cd1d0000000000')
    test_rivine_encoded(clt_n, '03120065cd1d0000000000')
    jsass.equals(
        str(clt_n.unlockhash),
        '000000000000000000000000000000000000000000000000000000000000000000000000000000'
    )
    # - wrapping an unlock hash condition
    lt_uh_json = json_loads(
        '{"type":3,"data":{"locktime":500000000,"condition":' + uh_json_raw +
        '}}')
    clt_uh = ConditionTypes.from_json(lt_uh_json)
    jsass.equals(clt_uh.json(), lt_uh_json)
    test_sia_encoded(
        clt_uh,
        '032a000000000000000065cd1d0000000001000000000000000000000000000000000000000000000000000000000000000000'
    )
    test_rivine_encoded(
        clt_uh,
        '03540065cd1d0000000001000000000000000000000000000000000000000000000000000000000000000000'
    )
    jsass.equals(
        str(clt_uh.unlockhash),
        '000000000000000000000000000000000000000000000000000000000000000000000000000000'
    )
    # - wrapping a multi-sig condition
    lt_ms_json = json_loads(
        '{"type":3,"data":{"locktime":500000000,"condition":' + ms_json_raw +
        '}}')
    clt_ms = ConditionTypes.from_json(lt_ms_json)
    jsass.equals(clt_ms.json(), lt_ms_json)
    test_sia_encoded(
        clt_ms,
        '035b000000000000000065cd1d00000000040200000000000000020000000000000001e89843e4b8231a01ba18b254d530110364432aafab8206bea72e5a20eaa55f7001a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc35'
    )
    test_rivine_encoded(
        clt_ms,
        '03a80065cd1d000000000402000000000000000401e89843e4b8231a01ba18b254d530110364432aafab8206bea72e5a20eaa55f7001a6a6c5584b2bfbd08738996cd7930831f958b9a5ed1595525236e861c1a0dc35'
    )
    jsass.equals(
        str(clt_ms.unlockhash),
        '0313a5abd192d1bacdd1eb518fc86987d3c3d1cfe3c5bed68ec4a86b93b2f05a89f67b89b07d71'
    )

    # FYI, Where lock times are used, it should be known that these are pretty flexible in definition
    jsass.equals(OutputLock().value, 0)
    jsass.equals(OutputLock(value=0).value, 0)
    jsass.equals(OutputLock(value=1).value, 1)
    jsass.equals(OutputLock(value=1549483822).value, 1549483822)
    # if current_timestamp is not defined, the current time is used: int(datetime.now().timestamp)
    jsass.equals(OutputLock(value='+7d', current_timestamp=1).value, 604801)
    jsass.equals(
        OutputLock(value='+7d12h5s', current_timestamp=1).value, 648006)