This repository has been archived by the owner on Mar 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Problem: Missing secure gRPC channel #24
Merged
linfeng-crypto
merged 1 commit into
crypto-org-chain:master
from
macong-cdc:secure-grpc
Jan 10, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
|
||
import ssl | ||
|
||
import grpc | ||
|
||
from chainlibpy.grpc_client import GrpcClient | ||
from chainlibpy.wallet import Wallet | ||
|
||
DENOM = "basetcro" | ||
MNEMONIC_PHRASE = "first ... last" | ||
CHAIN_ID = "testnet-croeseid-4" | ||
|
||
SERVER_HOST = "testnet-croeseid-4.crypto.org" | ||
SERVER_PORT = "9090" | ||
GRPC_ENDPOINT = f"{SERVER_HOST}:{SERVER_PORT}" | ||
|
||
DEFAULT_DERIVATION_PATH = "m/44'/1'/0'/0/0" | ||
DEFAULT_BECH32_HRP = "tcro" | ||
|
||
|
||
def example_with_certificate_file(): | ||
wallet = Wallet(MNEMONIC_PHRASE, DEFAULT_DERIVATION_PATH, DEFAULT_BECH32_HRP) | ||
|
||
# 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 # noqa501 | ||
# 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(wallet, CHAIN_ID, GRPC_ENDPOINT, creds) | ||
|
||
from_address = wallet.address | ||
res = client.get_balance(from_address, DENOM) | ||
print(f"address {from_address} initial balance: {res.balance.amount}") | ||
|
||
|
||
def example_with_certificate_request(): | ||
wallet = Wallet(MNEMONIC_PHRASE, DEFAULT_DERIVATION_PATH, DEFAULT_BECH32_HRP) | ||
|
||
# if server does not use Server Name Indication (SNI), commented code below is enough: | ||
# creds = ssl.get_server_certificate((SERVER_HOST, SERVER_PORT)) | ||
conn = ssl.create_connection((SERVER_HOST, SERVER_PORT)) | ||
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) | ||
sock = context.wrap_socket(conn, server_hostname=SERVER_HOST) | ||
certificate = ssl.DER_cert_to_PEM_cert(sock.getpeercert(True)) | ||
creds = grpc.ssl_channel_credentials(str.encode(certificate)) | ||
|
||
client = GrpcClient(wallet, CHAIN_ID, GRPC_ENDPOINT, creds) | ||
|
||
from_address = wallet.address | ||
res = client.get_balance(from_address, DENOM) | ||
print(f"address {from_address} initial balance: {res.balance.amount}") | ||
|
||
|
||
if __name__ == "__main__": | ||
example_with_certificate_file() | ||
example_with_certificate_request() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this may be better:
channel = insecure_channel(grpc_endpoint, credentials) if credentials else insecure_channel(grpc_endpoint)
or
or using
if not credentials:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to explicitly check for
None
to avoid confusion withFalse
.In this case, it does not matter that much though, as the variable is not a Boolean type.