def test_fulfillments(): def test_encoded(encoder, obj, expected): encoder.add(obj) output = encoder.data.hex() if expected != output: msg = "{} != {}".format(expected, output) raise Exception("unexpected encoding result: " + msg) def test_sia_encoded(obj, expected): test_encoded(encoder_sia_get(), obj, expected) def test_rivine_encoded(obj, expected): test_encoded(encoder_rivine_get(), obj, expected) # single signature fulfillments are supported ss_json = {"type": 1, "data": {"publickey": "ed25519:dda39750fff2d869badc797aaad1dea8c6bcf943879421c58ac8d8e2072d5b5f", "signature": "818dfccee2cb7dbe4156169f8e1c5be49a3f6d83a4ace310863d7b3b698748e3e4d12522fc1921d5eccdc108b36c84d769a9cf301e969bb05db1de9295820300"}} ssf = FulfillmentTypes.from_json(ss_json) assert ssf.json() == ss_json test_sia_encoded(ssf, '018000000000000000656432353531390000000000000000002000000000000000dda39750fff2d869badc797aaad1dea8c6bcf943879421c58ac8d8e2072d5b5f4000000000000000818dfccee2cb7dbe4156169f8e1c5be49a3f6d83a4ace310863d7b3b698748e3e4d12522fc1921d5eccdc108b36c84d769a9cf301e969bb05db1de9295820300') test_rivine_encoded(ssf, '01c401dda39750fff2d869badc797aaad1dea8c6bcf943879421c58ac8d8e2072d5b5f80818dfccee2cb7dbe4156169f8e1c5be49a3f6d83a4ace310863d7b3b698748e3e4d12522fc1921d5eccdc108b36c84d769a9cf301e969bb05db1de9295820300') assert ssf.is_fulfilled(ConditionUnlockHash()) # atomic swap fulfillments are supported as_json = {"type": 2, "data": {"publickey": "ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "signature": "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab", "secret": "def789def789def789def789def789dedef789def789def789def789def789de"}} asf = FulfillmentTypes.from_json(as_json) assert asf.json() == as_json test_sia_encoded(asf, '02a000000000000000656432353531390000000000000000002000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabdef789def789def789def789def789dedef789def789def789def789def789de') test_rivine_encoded(asf, '02090201ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabdef789def789def789def789def789dedef789def789def789def789def789de') assert asf.is_fulfilled(ConditionAtomicSwap()) # atomic swap fulfillments without secrets are supported as_json = {"type": 2, "data": {"publickey": "ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "signature": "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab"}} asf = FulfillmentTypes.from_json(as_json) assert asf.json() == as_json test_sia_encoded(asf, '028000000000000000656432353531390000000000000000002000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab') test_rivine_encoded(asf, '02c401ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab') assert asf.is_fulfilled(ConditionAtomicSwap()) # multi signature fulfillments are supported ms_json = {"type": 3, "data": {"pairs": [{"publickey": "ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "signature": "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab"}, { "publickey": "ed25519:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", "signature": "abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab"}]}} msf = FulfillmentTypes.from_json(ms_json) assert msf.json() == ms_json test_sia_encoded(msf, '0308010000000000000200000000000000656432353531390000000000000000002000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab656432353531390000000000000000002000000000000000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4000000000000000abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab') test_rivine_encoded(msf, '0315030401ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefabcdefab') assert msf.is_fulfilled(ConditionMultiSignature(min_nr_sig=1)) assert msf.is_fulfilled(ConditionMultiSignature(min_nr_sig=2))
def from_coin_output(cls, co): if not isinstance(co, CoinOutput): raise TypeError( "invalid co parameter, expected value of type CoinOutput, not {}".format(type(co))) ci = cls( parentid=co.id, fulfillment=FulfillmentTypes.from_condition(co.condition)) ci.parent_output = co return ci
def from_blockstake_output(cls, bso): if not isinstance(bso, BlockstakeOutput): raise TypeError( "invalid type of bso {} (expected: BlockstakeOutput)".format(type(bso))) bsi = cls( parentid=bso.id, fulfillment=FulfillmentTypes.from_condition(bso.condition)) bsi.parent_output = bso return bsi
def _from_json_data_object(self, data): self._nonce = BinaryData.from_json(data.get('nonce', ''), strencoding='base64') self._mint_condition = ConditionTypes.from_json( data.get('mintcondition', {})) self._mint_fulfillment = FulfillmentTypes.from_json( data.get('mintfulfillment', {})) self._miner_fees = [ Currency.from_json(fee) for fee in data.get('minerfees', []) or [] ] self._data = BinaryData.from_json(data.get('arbitrarydata', None) or '', strencoding='base64')
def _from_json_data_object(self, data): self._nonce = BinaryData.from_json(data.get_or('nonce', ''), strencoding='base64') self._mint_fulfillment = FulfillmentTypes.from_json( data.get_or('mintfulfillment', jsobj.new_dict())) self._coin_outputs = [ CoinOutput.from_json(co) for co in data.get_or('coinoutputs', []) or [] ] self._miner_fees = [ Currency.from_json(fee) for fee in data.get_or('minerfees', []) or [] ] self._data = BinaryData.from_json(data.get_or('arbitrarydata', None) or '', strencoding='base64')
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)))
def from_json(cls, obj): return cls( parentid=Hash.from_json(obj['parentid']), fulfillment=FulfillmentTypes.from_json(obj['fulfillment']))