Exemple #1
0
def example_with_certificate_file():
    # 1. .cer certificate file could be obtained from the browser
    #    more details could be found here https://stackoverflow.com/questions/25940396/how-to-export-certificate-from-chrome-on-a-mac/59466184#59466184 # noqa: 501
    # 2. convert .cer file to .crt file
    #    `openssl x509 -inform DER -in cert.cer -out cert.crt`
    with open("./cert.crt", "rb") as f:
        creds = grpc.ssl_channel_credentials(f.read())

    client = GrpcClient(CRO_NETWORK["testnet_croeseid"], creds)

    print(client.query_bank_denom_metadata(CRO_NETWORK["testnet_croeseid"].coin_base_denom))
def test_2_msgs_in_1_tx(blockchain_accounts,
                        local_test_network_config: "NetworkConfig"):
    client = GrpcClient(local_test_network_config)
    alice_info = get_blockchain_account_info(blockchain_accounts, ALICE)
    alice_wallet = Wallet(alice_info["mnemonic"])
    alice_account = client.query_account(alice_info["address"])
    bob_info = get_blockchain_account_info(blockchain_accounts, BOB)
    bob_wallet = Wallet(bob_info["mnemonic"])
    alice_bal_init = client.query_account_balance(alice_wallet.address)
    bob_bal_init = client.query_account_balance(bob_wallet.address)
    alice_coin_init = CROCoin(alice_bal_init.balance.amount,
                              alice_bal_init.balance.denom,
                              local_test_network_config)
    bob_coin_init = CROCoin(bob_bal_init.balance.amount,
                            bob_bal_init.balance.denom,
                            local_test_network_config)

    ten_cro = CROCoin("10", CRO_DENOM, local_test_network_config)
    twnenty_cro = CROCoin("20", CRO_DENOM, local_test_network_config)
    one_cro_fee = CROCoin("1", CRO_DENOM, local_test_network_config)
    msg_send_10_cro = MsgSend(
        from_address=alice_info["address"],
        to_address=bob_info["address"],
        amount=[ten_cro.protobuf_coin_message],
    )
    msg_send_20_cro = MsgSend(
        from_address=alice_info["address"],
        to_address=bob_info["address"],
        amount=[twnenty_cro.protobuf_coin_message],
    )

    tx = Transaction(
        chain_id=local_test_network_config.chain_id,
        from_wallets=[alice_wallet],
        msgs=[msg_send_10_cro],
        account_number=alice_account.account_number,
        fee=[one_cro_fee.protobuf_coin_message],
        client=client,
    ).append_message(msg_send_20_cro)

    signature_alice = alice_wallet.sign(tx.sign_doc.SerializeToString())
    signed_tx = tx.set_signatures(signature_alice).signed_tx

    client.broadcast_transaction(signed_tx.SerializeToString())

    alice_bal_aft = client.query_account_balance(alice_wallet.address)
    bob_bal_aft = client.query_account_balance(bob_wallet.address)
    alice_coin_aft = CROCoin(alice_bal_aft.balance.amount,
                             alice_bal_aft.balance.denom,
                             local_test_network_config)
    bob_coin_aft = CROCoin(bob_bal_aft.balance.amount,
                           bob_bal_aft.balance.denom,
                           local_test_network_config)

    assert alice_coin_aft == alice_coin_init - ten_cro - twnenty_cro - one_cro_fee
    assert bob_coin_aft == bob_coin_init + ten_cro + twnenty_cro
Exemple #3
0
def simple_transaction():
    client = GrpcClient(LOCAL_NETWORK)

    sending_wallet = Wallet(
        MNEMONIC_PHRASE, LOCAL_NETWORK.derivation_path, LOCAL_NETWORK.address_prefix
    )
    sending_account = client.query_account(sending_wallet.address)
    sending_account_init_bal = client.query_account_balance(sending_wallet.address)
    receiving_account_init_bal = client.query_account_balance(TO_ADDRESS)

    print(
        f"sending account initial balance: {sending_account_init_bal.balance.amount}"
        f"{sending_account_init_bal.balance.denom}"
    )
    print(
        f"receiving account initial balance: {receiving_account_init_bal.balance.amount}"
        f"{receiving_account_init_bal.balance.denom}"
    )

    ten_cro = CROCoin("10", "cro", LOCAL_NETWORK)
    one_cro_fee = CROCoin("1", "cro", LOCAL_NETWORK)

    msg_send = MsgSend(
        from_address=sending_wallet.address,
        to_address=TO_ADDRESS,
        amount=[ten_cro.protobuf_coin_message],
    )
    tx = Transaction(
        chain_id=LOCAL_NETWORK.chain_id,
        from_wallets=[sending_wallet],
        msgs=[msg_send],
        account_number=sending_account.account_number,
        fee=[one_cro_fee.protobuf_coin_message],
        client=client,
    )

    signature_alice = sending_wallet.sign(tx.sign_doc.SerializeToString())
    signed_tx = tx.set_signatures(signature_alice).signed_tx

    client.broadcast_transaction(signed_tx.SerializeToString())

    sending_account_aft_bal = client.query_account_balance(sending_wallet.address)
    receiving_account_aft_bal = client.query_account_balance(TO_ADDRESS)

    print("After transaction of sending 10cro with a 1cro fee:")
    print(
        f"sending account after balance: {sending_account_aft_bal.balance.amount}"
        f"{sending_account_aft_bal.balance.denom}"
    )
    print(
        f"receiving account after balance: {receiving_account_aft_bal.balance.amount}"
        f"{receiving_account_aft_bal.balance.denom}"
    )
def test_network_config(network_config: "NetworkConfig"):
    (server_host, server_port) = network_config.grpc_endpoint.split(":")

    context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    with socket.create_connection((server_host, int(server_port))) as sock:
        with context.wrap_socket(sock, server_hostname=server_host) as ssock:
            certificate_DER = ssock.getpeercert(True)

    if certificate_DER is None:
        pytest.fail("no certificate returned from server")

    certificate_PEM = ssl.DER_cert_to_PEM_cert(certificate_DER)
    creds = grpc.ssl_channel_credentials(str.encode(certificate_PEM))
    client = GrpcClient(network_config, creds)

    assert (client.query_bank_denom_metadata(
        network_config.coin_base_denom).metadata.base ==
            network_config.coin_base_denom)
Exemple #5
0
def example_with_certificate_request():
    (server_host, server_port) = CRO_NETWORK["testnet_croeseid"].grpc_endpoint.split(":")

    # if server does not use Server Name Indication (SNI), commented code below is enough:
    # creds = ssl.get_server_certificate((SERVER_HOST, SERVER_PORT))
    context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
    with socket.create_connection((server_host, int(server_port))) as sock:
        with context.wrap_socket(sock, server_hostname=server_host) as ssock:
            certificate_DER = ssock.getpeercert(True)

    if certificate_DER is None:
        raise RuntimeError("no certificate returned from server")

    certificate_PEM = ssl.DER_cert_to_PEM_cert(certificate_DER)
    creds = grpc.ssl_channel_credentials(str.encode(certificate_PEM))

    client = GrpcClient(CRO_NETWORK["testnet_croeseid"], creds)

    print(client.query_bank_denom_metadata(CRO_NETWORK["testnet_croeseid"].coin_base_denom))