def test_head(self): kid = 'did:ont:TRAtosUZHNSiLhzBdHacyxMX4Bg3cjWy3r#keys-1' claim_header = Header(kid) claim_header_dict = dict(claim_header) self.assertEqual(kid, claim_header_dict['kid']) self.assertTrue(isinstance(claim_header_dict, dict)) self.assertEqual('ONT-ES256', claim_header_dict['alg']) self.assertEqual(96, len(claim_header.to_json())) b64_head = claim_header.to_base64() claim_header_recv = Header.from_base64(b64_head) self.assertEqual(dict(claim_header), dict(claim_header_recv))
class TestClaim(unittest.TestCase): def setUp(self): self.gas_price = 500 self.gas_limit = 20000 self.kid = 'did:ont:TRAtosUZHNSiLhzBdHacyxMX4Bg3cjWy3r#keys-1' self.claim_header = Header(self.kid) def test_head_kid(self): self.assertEqual(self.kid, self.claim_header.kid) self.assertRaises(SDKException, setattr, self.claim_header, 'kid', 1) self.assertRaises(SDKException, setattr, self.claim_header, 'kid', 'test') self.assertRaises(SDKException, setattr, self.claim_header, 'kid', 'did:ont:test') self.kid = self.kid.replace('keys-1', 'keys-2') self.claim_header.kid = self.kid self.assertEqual(self.kid, self.claim_header.kid) def test_head_to_json(self): self.assertEqual(96, len(self.claim_header.to_json())) def test_claim_head_b64(self): self.assertEqual( dict(self.claim_header), dict(Header.from_base64(self.claim_header.to_base64()))) def test_head_dict(self): claim_header_dict = dict(self.claim_header) self.assertEqual(self.kid, claim_header_dict['kid']) self.assertTrue(isinstance(claim_header_dict, dict)) self.assertEqual('ONT-ES256', claim_header_dict['alg']) self.assertEqual(ClmType.witness_claim, self.claim_header.type) def test_from_str(self): clm_alg_lst = [ ClmAlg.ES224, ClmAlg.ES256, ClmAlg.ES384, ClmAlg.ES512, ClmAlg.ES3_224, ClmAlg.ES3_256, ClmAlg.ES3_384, ClmAlg.ES3_512, ClmAlg.ER160, ClmAlg.SM, ClmAlg.EDS512 ] alg_str_lst = [ 'ONT-ES224', 'ONT-ES256', 'ONT-ES384', 'ONT-ES512', 'ONT-ES3-224', 'ONT-ES3-256', 'ONT-ES3-384', 'ONT-ES3-512', 'ONT-ER160', 'ONT-SM', 'ONT-EDS512' ] for index, alg in enumerate(alg_str_lst): self.assertEqual(clm_alg_lst[index], ClmAlg.from_str(alg)) def test_payload(self): ver = '0.7.0' iss = 'did:ont:TRAtosUZHNSiLhzBdHacyxMX4Bg3cjWy3r' sub = 'did:ont:SI59Js0zpNSiPOzBdB5cyxu80BO3cjGT70' iat = 1525465044 exp = 1530735444 jti = '4d9546fdf2eb94a364208fa65a9996b03ba0ca4ab2f56d106dac92e891b6f7fc' context = 'https://example.com/template/v1' clm = dict(Name='Bob Dylan', Age='22') clm_rev = dict(typ='AttestContract', addr='8055b362904715fd84536e754868f4c8d27ca3f6') claim_payload = Payload(ver, iss, sub, iat, exp, context, clm, clm_rev, jti) claim_payload_dict = dict(claim_payload) self.assertEqual(ver, claim_payload_dict['ver']) self.assertEqual(iss, claim_payload_dict['iss']) self.assertEqual(sub, claim_payload_dict['sub']) self.assertEqual(iat, claim_payload_dict['iat']) self.assertEqual(exp, claim_payload_dict['exp']) self.assertEqual(jti, claim_payload_dict['jti']) self.assertEqual(64, len(claim_payload_dict['jti'])) self.assertEqual(context, claim_payload_dict['@context']) self.assertEqual(clm, claim_payload_dict['clm']) self.assertEqual(clm_rev, claim_payload_dict['clm-rev']) b64_payload = claim_payload.to_base64() claim_payload_recv = Payload.from_base64(b64_payload) self.assertEqual(dict(claim_payload), dict(claim_payload_recv)) @not_panic_exception def test_signature_info(self): try: pub_keys = sdk.native_vm.ont_id().get_public_keys(identity2.ont_id) pk = pub_keys[0] kid = pk['PubKeyId'] except IndexError: kid = '03eca83d4e4a0ed1e96d87942f76275c49a2f61a6b1e0f33be5a65f9beae71cd3f#keys-1' iss_ont_id = identity2.ont_id sub_ont_id = identity1.ont_id exp = int(time()) + 100 context = 'https://github.com/NashMiao' clm = dict(Name='NashMiao', Position='Mars', Birthday=str(time())) clm_rev = dict(type='AttestContract', addr='8055b362904715fd84536e754868f4c8d27ca3f6') claim = sdk.service.claim() claim.set_claim(kid, iss_ont_id, sub_ont_id, exp, context, clm, clm_rev) try: claim.generate_signature(identity2_ctrl_acct) except SDKException as e: if 'get key failed' not in e.args[1]: raise e claim.generate_signature(identity2_ctrl_acct, verify_kid=False) b64_claim = claim.to_base64() try: self.assertTrue(claim.validate_signature(b64_claim)) except SDKException as e: msg = 'invalid claim head parameter' self.assertTrue(msg in e.args[1]) @not_panic_exception def test_claim_demo(self): pub_keys = sdk.native_vm.ont_id().get_public_keys(identity1.ont_id) try: pk = pub_keys[0] kid = pk['PubKeyId'] except IndexError: kid = '03d0fdb54acba3f81db3a6e16fa02e7ea3678bd205eb4ed2f1cfa8ab5e5d45633e#keys-1' iss_ont_id = identity2.ont_id sub_ont_id = identity1.ont_id exp = int(time()) + 1000 context = 'https://example.com/template/v1' clm = dict(Name='NashMiao', JobTitle='SoftwareEngineer', HireData=str(time())) clm_rev = dict(type='AttestContract', addr='8055b362904715fd84536e754868f4c8d27ca3f6') claim = sdk.service.claim() claim.set_claim(kid, iss_ont_id, sub_ont_id, exp, context, clm, clm_rev) try: claim.generate_signature(identity2_ctrl_acct) except SDKException as e: msg = 'get key failed' self.assertTrue(msg in e.args[1]) claim.generate_signature(identity2_ctrl_acct, verify_kid=False) tx = claim.new_commit_tx(identity2_ctrl_acct.get_address_base58(), acct1.get_address_base58(), self.gas_price, self.gas_limit) tx.sign_transaction(identity2_ctrl_acct) tx.add_sign_transaction(acct1) tx_hash = sdk.rpc.send_raw_transaction(tx) sleep(12) blockchain_proof = claim.generate_blk_proof(tx_hash) self.assertTrue(claim.validate_blk_proof()) b64_claim = claim.to_base64() claim_list = b64_claim.split('.') self.assertEqual(4, len(claim_list)) claim_recv = sdk.service.claim() claim_recv.from_base64(b64_claim) self.assertEqual(dict(claim), dict(claim_recv)) proof_node = blockchain_proof.proof_node if proof_node[0]['Direction'] == 'Right': proof_node[0]['Direction'] = 'Left' else: proof_node[0]['Direction'] = 'Right' claim.blk_proof.proof_node = proof_node self.assertFalse(claim.validate_blk_proof())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lyZWN0aW9uIjoiTGVmdCJ9LHsiVGFyZ2V0SGFza' 'CI6IjkzOTgxNjAwMDViMDdlMDgyYzA0ZGU0ZDIzYmE5YjYxZDNhYzFiYjRlNzJiZWRhMmY2N2U5MWIwMGI3MzllOTkiLCJEaX' 'JlY3Rpb24iOiJMZWZ0In0seyJUYXJnZXRIYXNoIjoiM2IyNmQxMzA4NDFmMTQyNGYwMjFlODI2MzI5ODI2YjVlYTQxOTMzMWI' 'yMjlhYmRjZjMwYmU2MmIzNDgyMTBhZCIsIkRpcmVjdGlvbiI6IkxlZnQifSx7IlRhcmdldEhhc2giOiIwNTU5NmIzYTY1Yjg1' 'NjU4YmEyZDFjODcxYzMzYjY1ZGY3NDVlODA3YzBmNWVjZTU5YTIyNTE4N2YxZDIxYzRmIiwiRGlyZWN0aW9uIjoiTGVmdCJ9L' 'HsiVGFyZ2V0SGFzaCI6ImRlMzE0YzgxMmU5NTVkZDBhNTczYzJkNGJjMzlmMmI3MGQxMTllNTBlNTZlMWRhMjVjYzMzOTM1N2' 'QwOTA4OTAiLCJEaXJlY3Rpb24iOiJMZWZ0In1dLCJDb250cmFjdEFkZHIiOiIzNmJiNWMwNTNiNmI4MzljOGY2YjkyM2ZlODU' 'yZjkxMjM5YjlmY2NjIn0=') try: sdk.default_network.connect_to_main_net() claim = sdk.service.claim() try: self.assertTrue(claim.validate_signature(b64_claim)) except SDKException as e: if 'invalid claim head parameter' not in e.args[1]: raise e claim.from_base64(b64_claim, True) self.assertTrue(claim.validate_blk_proof()) self.assertTrue(isinstance(claim, Claim)) finally: sdk.default_network.connect_to_test_net()