Exemplo n.º 1
0
def decode_metadata(metadata_json):
    mp2019 = MerkleProof2019()
    if isinstance(metadata_json, dict) and 'proof' in metadata_json:
        encoded_value = metadata_json['proof']['proofValue']
    else:
        encoded_value = metadata_json[0]['proof']['proofValue']
    decoded_json = mp2019.decode(encoded_value)
    return decoded_json['anchors'][0].split(':')[-1]
Exemplo n.º 2
0
def decode_proof(proofEncoded):
    try:
        mp2019 = MerkleProof2019()
        check_decoded = mp2019.decode(proofEncoded)
    except Exception as e:
        print(e)
        raise HTTPException(status_code=400,
                            detail="Invalid Proof Value, could not decode")
    return check_decoded
    def test_encoding_incorrect(self):
        mp2019 = MerkleProof2019()

        error = False
        try:
            check_encoded = mp2019.encode('not a valid decoding')
        except:
            error = True

        self.assertTrue(error)
Exemplo n.º 4
0
from lds_merkle_proof_2019.merkle_proof_2019 import MerkleProof2019

mp2019 = MerkleProof2019()

#decoded_value = mp2019.decode('asdfasdf')
#print(decoded_value)

json_string = '{' \
              '"path": [{ "right": "51b4e22ed024ec7f38dc68b0bf78c87eda525ab0896b75d2064bdb9fc60b2698" },{ "right": "61c56cca660b2e616d0bd62775e728f50275ae44adf12d1bfb9b9c507a14766b" }],' \
              '"merkleRoot": "3c9ee831b8705f2fbe09f8b3a92247eed88cdc90418c024924be668fdc92e781",' \
              '"targetHash": "c65c6184e3d5a945ddb5437e93ea312411fd33aa1def22b0746d6ecd4aa30f20",' \
              '"anchors": ["blink:btc:testnet:582733d7cef8035d87cecc9ebbe13b3a2f6cc52583fbcd2b9709f20a6b8b56b3"]' \
              '' \
              '}'
encoded_value = mp2019.encode(json_string)

print('\nresults::')
print(encoded_value)

print(mp2019.decode(encoded_value))
    def get_proof_generator(self,
                            tx_id,
                            app_config,
                            verification_method,
                            chain=Chain.bitcoin_mainnet):
        """
        Returns a generator (1-time iterator) of proofs in insertion order.

        :param tx_id: blockchain transaction id
        :return:
        """
        root = ensure_string(self.tree.get_merkle_root())
        node_count = len(self.tree.leaves)
        for index in range(0, node_count):
            proof = self.tree.get_proof(index)
            proof2 = []
            #Change back to smart contract proof & verification
            for p in proof:
                dict2 = dict()
                for key, value in p.items():
                    dict2[key] = ensure_string(value)
                proof2.append(dict2)
            target_hash = ensure_string(self.tree.get_leaf(index))
            """
            Add additional parameters for smart contract certification
            """
            if app_config.issuing_method == "smart_contract":
                from cert_issuer.blockchain_handlers.ethereum_sc.ens import ENSConnector

                ens = ENSConnector(app_config)
                abi = ens.get_abi()

                mp2019 = MerkleProof2019()
                print(helpers.tx_to_blink(chain, tx_id))
                merkle_json = {
                    "path": proof2,
                    "merkleRoot": root,
                    "targetHash": target_hash,
                    #Possibly adjust anchor to merkle_proof dict
                    "anchors": [helpers.tx_to_blink(chain, tx_id)]
                }
                logging.info('merkle_json: %s', str(merkle_json))

                proof_value = mp2019.encode(merkle_json)
                merkle_proof = {
                    "type": "MerkleProof2019",
                    "created": datetime.now().isoformat(),
                    "proofValue": proof_value.decode('utf8'),
                    "proofPurpose": "assertionMethod",
                    "verificationMethod": verification_method,
                    #Add ENS name for issuer validation
                    "ens_name": app_config.ens_name
                }
                #Uncomment after checking verification
                """
                "anchors": [{
                    #helpers.tx_to_blink(chain, tx_id),
                    "sourceId": to_source_id(tx_id, chain),
                    "type": "ETHSmartContract",
                    "contract_address": app_config.contract_address,
                    "ens_name": app_config.ens_name,
                    "contract_abi": abi
                }]
                """

            else:
                mp2019 = MerkleProof2019()
                merkle_json = {
                    "path": proof2,
                    "merkleRoot": root,
                    "targetHash": target_hash,
                    "anchors": [helpers.tx_to_blink(chain, tx_id)]
                }
                logging.info('merkle_json: %s', str(merkle_json))

                proof_value = mp2019.encode(merkle_json)
                merkle_proof = {
                    "type": "MerkleProof2019",
                    "created": datetime.now().isoformat(),
                    "proofValue": proof_value.decode('utf8'),
                    "proofPurpose": "assertionMethod",
                    "verificationMethod": verification_method
                }

            yield merkle_proof
    def test_encoding_correct_mocknet(self):
        mp2019 = MerkleProof2019()

        check_encoded = mp2019.encode(proofMocknetDecoded)
        self.assertIsNotNone(check_encoded)
    def test_encoding_correct(self):
        mp2019 = MerkleProof2019()

        check_encoded = mp2019.encode(proofDecoded)
        self.assertEqual(check_encoded, proofEncoded)
    def do_test_signature(self, chain, display_chain, type):
        self.maxDiff = None
        app_config = mock.Mock()
        app_config.issuing_method = "transaction"

        merkle_tree_generator = MerkleTreeGenerator()
        merkle_tree_generator.populate(get_test_data_generator())
        _ = merkle_tree_generator.get_blockchain_data()
        gen = merkle_tree_generator.get_proof_generator(
            '8087c03e7b7bc9ca7b355de9d9d8165cc5c76307f337f0deb8a204d002c8e582',
            'http://example.com', chain)
        p1 = next(gen)
        _ = next(gen)
        p3 = next(gen)

        p1_json_proof = {
            'path': [{
                'right':
                'd4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35'
            }, {
                'right':
                '4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce'
            }],
            'merkleRoot':
            '0932f1d2e98219f7d7452801e2b64ebd9e5c005539db12d9b1ddabe7834d9044',
            'targetHash':
            '6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b',
            'anchors': [
                helpers.tx_to_blink(
                    chain,
                    '8087c03e7b7bc9ca7b355de9d9d8165cc5c76307f337f0deb8a204d002c8e582'
                )
            ]
        }
        mp2019 = MerkleProof2019()
        proof_value = mp2019.encode(p1_json_proof)

        p1_expected = {
            "type": "MerkleProof2019",
            "created": p1['created'],
            "proofValue": proof_value.decode('utf8'),
            "proofPurpose": "assertionMethod",
            "verificationMethod": "http://example.com"
        }

        p3_json_proof = {
            'path': [{
                'left':
                '4295f72eeb1e3507b8461e240e3b8d18c1e7bd2f1122b11fc9ec40a65894031a'
            }],
            'merkleRoot':
            '0932f1d2e98219f7d7452801e2b64ebd9e5c005539db12d9b1ddabe7834d9044',
            'targetHash':
            '4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce',
            'anchors': [
                helpers.tx_to_blink(
                    chain,
                    '8087c03e7b7bc9ca7b355de9d9d8165cc5c76307f337f0deb8a204d002c8e582'
                )
            ]
        }
        mp2019 = MerkleProof2019()
        proof_value = mp2019.encode(p3_json_proof)

        p3_expected = {
            "type": "MerkleProof2019",
            "created": p3['created'],
            "proofValue": proof_value.decode('utf8'),
            "proofPurpose": "assertionMethod",
            "verificationMethod": "http://example.com"
        }

        self.assertEqual(p1, p1_expected)
        self.assertEqual(p3, p3_expected)