Ejemplo n.º 1
0
    def __init__(self, client: ExonumClient):
        self.client = client
        cryptocurrency_service_name = "exonum-cryptocurrency-advanced"
        cryptocurrency_service_version = "0.13.0-rc.2"
        self.loader = client.protobuf_loader()
        self.loader.initialize()
        self.loader.load_main_proto_files()
        self.loader.load_service_proto_files(
            runtime_id=0,
            artifact_name="exonum-supervisor",
            artifact_version="0.13.0-rc.2",
        )
        self.loader.load_service_proto_files(
            runtime_id=0,
            artifact_name=cryptocurrency_service_name,
            artifact_version=cryptocurrency_service_version,
        )

        self.cryptocurrency_module = ModuleManager.import_service_module(
            cryptocurrency_service_name, cryptocurrency_service_version,
            "service")
        self.types_module = ModuleManager.import_service_module(
            cryptocurrency_service_name, cryptocurrency_service_version,
            "types")
        instance_id = client.public_api.get_instance_id_by_name("crypto")
        self.msg_generator = MessageGenerator(
            instance_id=instance_id,
            artifact_name=cryptocurrency_service_name,
            artifact_version=cryptocurrency_service_version)
Ejemplo n.º 2
0
    def __init__(self,
                 client: ExonumClient,
                 instance_name: str = "crypto",
                 version: str = "0.2.0"):
        self.client = client
        service_name = "exonum-cryptocurrency"
        self.service_version = version
        self.instance_name = instance_name
        self.loader = client.protobuf_loader()
        self.loader.initialize()
        self.loader.load_main_proto_files()
        self.loader.load_service_proto_files(runtime_id=0,
                                             artifact_name="exonum-supervisor",
                                             artifact_version="1.0.0")
        self.loader.load_service_proto_files(
            runtime_id=0,
            artifact_name=service_name,
            artifact_version=self.service_version)

        self.cryptocurrency_module = ModuleManager.import_service_module(
            service_name, self.service_version, "service")
        self.types_module = ModuleManager.import_service_module(
            service_name, self.service_version, "exonum.crypto.types")
        instance_id = client.public_api.get_instance_id_by_name(
            self.instance_name)
        self.msg_generator = MessageGenerator(
            instance_id=instance_id,
            artifact_name=service_name,
            artifact_version=self.service_version)
Ejemplo n.º 3
0
def run() -> None:
    """This example creates two wallets (for Alice and Bob) and performs several
    transactions between these wallets."""
    client = ExonumClient(hostname="127.0.0.1",
                          public_api_port=8080,
                          private_api_port=8081)

    with client.protobuf_loader() as loader:
        # Load and compile proto files:
        loader.load_main_proto_files()
        loader.load_service_proto_files(RUST_RUNTIME_ID,
                                        CRYPTOCURRENCY_ARTIFACT_NAME,
                                        CRYPTOCURRENCY_ARTIFACT_VERSION)

        instance_id = get_cryptocurrency_instance_id(client)

        cryptocurrency_message_generator = MessageGenerator(
            instance_id, CRYPTOCURRENCY_ARTIFACT_NAME,
            CRYPTOCURRENCY_ARTIFACT_VERSION)

        alice_keypair = create_wallet(client, cryptocurrency_message_generator,
                                      "Alice")
        bob_keypair = create_wallet(client, cryptocurrency_message_generator,
                                    "Bob")

        alice_balance = get_balance(client, alice_keypair.public_key)
        bob_balance = get_balance(client, bob_keypair.public_key)
        print("Created the wallets for Alice and Bob. Balance:")
        print(f" Alice => {alice_balance}")
        print(f" Bob => {bob_balance}")

        amount = 10
        alice_balance, bob_balance = transfer(
            client, cryptocurrency_message_generator, alice_keypair,
            bob_keypair.public_key, amount)

        print(f"Transferred {amount} tokens from Alice's wallet to Bob's one")
        print(f" Alice => {alice_balance}")
        print(f" Bob => {bob_balance}")

        amount = 25
        bob_balance, alice_balance = transfer(
            client, cryptocurrency_message_generator, bob_keypair,
            alice_keypair.public_key, amount)

        print(f"Transferred {amount} tokens from Bob's wallet to Alice's one")
        print(f" Alice => {alice_balance}")
        print(f" Bob => {bob_balance}")
Ejemplo n.º 4
0
    def __init__(self, client: ExonumClient):
        self.client = client
        cryptocurrency_service_name = 'exonum-cryptocurrency-advanced:0.13.0-rc.2'
        self.loader = client.protobuf_loader()
        self.loader.initialize()
        self.loader.load_main_proto_files()
        self.loader.load_service_proto_files(
            runtime_id=0, service_name='exonum-supervisor:0.13.0-rc.2')
        self.loader.load_service_proto_files(
            runtime_id=0, service_name=cryptocurrency_service_name)

        self.cryptocurrency_module = ModuleManager.import_service_module(
            cryptocurrency_service_name, 'service')
        self.types_module = ModuleManager.import_service_module(
            cryptocurrency_service_name, 'types')
        instance_id = client.get_instance_id_by_name("crypto")
        self.msg_generator = MessageGenerator(
            instance_id=instance_id, artifact_name=cryptocurrency_service_name)
Ejemplo n.º 5
0
def run() -> None:
    """This example creates a wallet in the Cryptocurrency service, retrieves
    proofs for the wallet and verifies them.
    For the example to work, be sure to have `exonum-cryptocurrency-advanced`
    service instance with name `XNM` deployed."""
    client = ExonumClient(hostname="127.0.0.1",
                          public_api_port=8080,
                          private_api_port=8081)

    with client.protobuf_loader() as loader:
        # Load and compile proto files:
        loader.load_main_proto_files()
        loader.load_service_proto_files(RUST_RUNTIME_ID,
                                        CRYPTOCURRENCY_ARTIFACT_NAME)

        instance_id = get_cryptocurrency_instance_id(client)

        cryptocurrency_message_generator = MessageGenerator(
            instance_id, CRYPTOCURRENCY_ARTIFACT_NAME)

        alice_keypair = create_wallet(client, cryptocurrency_message_generator,
                                      "Alice")

        wallet_info_response = client.get_service(
            CRYPTOCURRENCY_INSTANCE_NAME, "v1/wallets/info?pub_key={}".format(
                alice_keypair.public_key.hex()))
        ensure_status_code(wallet_info_response)
        wallet_info = wallet_info_response.json()

        # `MapProof` to the whole Exonum state hash:
        proof_to_table = wallet_info["wallet_proof"]["to_table"]
        # Expected hash of the proof to the table is a state hash of the block:
        expected_to_table_hash_raw = wallet_info["block_proof"]["block"][
            "state_hash"]
        expected_to_table_hash = Hash(
            bytes.fromhex(expected_to_table_hash_raw))

        # Verify the proof to the table:
        verify_proof_to_table(proof_to_table, expected_to_table_hash)

        # `MapProof` to the wallet as a part of the Cryptocurrency schema:
        proof_to_wallet = wallet_info["wallet_proof"]["to_wallet"]
        # Expected hash of the proof to the wallet is the value stored in the
        # proof to the table:
        expected_to_wallet_hash_raw = wallet_info["wallet_proof"]["to_table"][
            "entries"][0]["value"]
        expected_to_wallet_hash = Hash(
            bytes.fromhex(expected_to_wallet_hash_raw))

        # Verify the proof to the wallet:
        verify_proof_to_wallet(proof_to_wallet, expected_to_wallet_hash)

        # `ListProof` for the transactions associtated with the wallet:
        proof_wallet_history = wallet_info["wallet_history"]["proof"]
        # Expected hash for the wallet history is the hash stored in the proof
        # to the wallet:
        expected_history_hash_raw = wallet_info["wallet_proof"]["to_wallet"][
            "entries"][0]["value"]["history_hash"]
        expected_history_hash = Hash(bytes(expected_history_hash_raw["data"]))

        # Verify the proof for the wallet history:
        verify_wallet_history_proof(proof_wallet_history,
                                    expected_history_hash)