From 81913f12325915db8ffd17145471668edcc14ace Mon Sep 17 00:00:00 2001 From: linfeng-crypto Date: Wed, 26 Jan 2022 13:36:07 +0800 Subject: [PATCH] merge main --- README.md | 10 +++++----- chainlibpy/grpc_client.py | 19 +++++++++---------- chainlibpy/multisign/bits.py | 2 +- example/multi_sig/main.py | 17 +++++++++++++---- example/secure_channel_example.py | 30 ++++++++++-------------------- example/transaction.py | 17 +++++++++++------ tests/test_grpc_client.py | 9 +++------ 7 files changed, 52 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 57b2428..38d2022 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ print(wallet.address) ```python from chainlibpy.generated.cosmos.base.v1beta1.coin_pb2 import Coin -from chainlibpy.grpc_client import GrpcClient +from chainlibpy.grpc_client import GrpcClient, CRO_NETWORK, get_packed_send_msg from chainlibpy.transaction import sign_transaction from chainlibpy.wallet import Wallet @@ -61,18 +61,18 @@ DENOM = "basecro" MNEMONIC_PHRASE = "first ... last" TO_ADDRESS = "cro...add" AMOUNT = [Coin(amount="10000", denom=DENOM)] -CHAIN_ID = "chainmaind" -GRPC_ENDPOINT = "0.0.0.0:26653" wallet = Wallet(MNEMONIC_PHRASE) -client = GrpcClient(CHAIN_ID, DENOM, GRPC_ENDPOINT) + +config = CRO_NETWORK["mainnet"] +client = GrpcClient(config) from_address = wallet.address account_number = client.query_account_data(wallet.address).account_number msg = get_packed_send_msg(wallet.address, TO_ADDRESS, AMOUNT) tx = client.generate_tx([msg], [wallet.address], [wallet.public_key]) -sign_transaction(tx, wallet.private_key, CHAIN_ID, account_number) +sign_transaction(tx, wallet.private_key, config.chain_id, account_number) client.broadcast_tx(tx) ``` diff --git a/chainlibpy/grpc_client.py b/chainlibpy/grpc_client.py index eefed2b..f84b59a 100644 --- a/chainlibpy/grpc_client.py +++ b/chainlibpy/grpc_client.py @@ -6,7 +6,7 @@ from typing import List, Optional from google.protobuf.any_pb2 import Any as ProtoAny -from grpc import ChannelCredentials, RpcError, insecure_channel, secure_channel +from grpc import ChannelCredentials, insecure_channel, secure_channel from chainlibpy.generated.cosmos.auth.v1beta1.auth_pb2 import BaseAccount from chainlibpy.generated.cosmos.auth.v1beta1.query_pb2 import QueryAccountRequest @@ -97,7 +97,7 @@ class NetworkConfig: coin_base_denom="basecro", derivation_path="m/44'/394'/0'/0/0", ), - "testnet_croeseid": NetworkConfig( + "testnet": NetworkConfig( grpc_endpoint="testnet-croeseid-4.crypto.org:9090", chain_id="testnet-croeseid-4", address_prefix="tcro", @@ -113,27 +113,26 @@ class GrpcClient: def __init__( self, - network: NetworkConfig, - account_number: int = None, + network_config: NetworkConfig, credentials: ChannelCredentials = None, ) -> None: if credentials is None: - channel = insecure_channel(network.grpc_endpoint) + channel = insecure_channel(network_config.grpc_endpoint) else: - channel = secure_channel(network.grpc_endpoint, credentials) + channel = secure_channel(network_config.grpc_endpoint, credentials) self.bank_client = BankGrpcClient(channel) self.tx_client = TxGrpcClient(channel) self.auth_client = AuthGrpcClient(channel) - self.chain_id = network.chain_id - self.account_number = account_number + self.chain_id = network_config.chain_id + self.denom = network_config.coin_base_denom def query_bank_denom_metadata(self) -> QueryDenomMetadataResponse: res = self.bank_client.DenomMetadata(QueryDenomMetadataRequest(denom=self.network.coin_base_denom)) return res - def get_balance(self, address: str, denom: str) -> QueryBalanceResponse: - res = self.bank_client.Balance(QueryBalanceRequest(address=address, denom=denom)) + def get_balance(self, address: str) -> QueryBalanceResponse: + res = self.bank_client.Balance(QueryBalanceRequest(address=address, denom=self.denom)) return res def query_account_data(self, address: str) -> BaseAccount: diff --git a/chainlibpy/multisign/bits.py b/chainlibpy/multisign/bits.py index 3924c21..ef58d44 100644 --- a/chainlibpy/multisign/bits.py +++ b/chainlibpy/multisign/bits.py @@ -1,5 +1,5 @@ class Bits(object): - po8tab = [ + pop8tab = [ 0x00, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, 0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04, 0x02, 0x03, 0x03, 0x04, 0x03, 0x04, 0x04, 0x05, diff --git a/example/multi_sig/main.py b/example/multi_sig/main.py index 419673c..2fdeb59 100644 --- a/example/multi_sig/main.py +++ b/example/multi_sig/main.py @@ -14,7 +14,7 @@ from chainlibpy.generated.cosmos.base.v1beta1.coin_pb2 import Coin as ProtoCoin from chainlibpy.generated.cosmos.crypto.multisig.keys_pb2 import LegacyAminoPubKey from chainlibpy.generated.cosmos.crypto.secp256k1.keys_pb2 import PubKey as ProtoPubKey -from chainlibpy.grpc_client import GrpcClient, gen_multi_tx, get_packed_send_msg +from chainlibpy.grpc_client import GrpcClient, gen_multi_tx, get_packed_send_msg, NetworkConfig from chainlibpy.multisign.signature import SingleSignatureV2 from chainlibpy.wallet import Wallet @@ -139,12 +139,21 @@ def main(): # multi_wallet_addr = get_multi_addr(multi_wallet_name) # account_num, sequence = chain_maind_client.get_account_info(multi_wallet_addr) - grpc_client = GrpcClient(chain_id, "basecro", "0.0.0.0:26653", None) + config = NetworkConfig( + grpc_endpoint="0.0.0.0:26653", + chain_id=chain_id, + address_prefix="cro", + coin_denom="cro", + coin_base_denom="basecro", + derivation_path="m/44'/394'/0'/0/0", + ) + + grpc_client = GrpcClient(config) res = grpc_client.get_balance(multi_address) print(f"get multi address balance: {res.balance.amount}") account_info = grpc_client.query_account_data(multi_address) - account_num = account_info.account_num - sequence = account_info + account_num = account_info.account_number + sequence = account_info.sequence print(f"account_num {account_num}, sequence: {sequence}") # account_num, sequence = 12, 0 diff --git a/example/secure_channel_example.py b/example/secure_channel_example.py index 39755a6..1c43b88 100644 --- a/example/secure_channel_example.py +++ b/example/secure_channel_example.py @@ -5,23 +5,15 @@ import grpc -from chainlibpy.grpc_client import GrpcClient +from chainlibpy.grpc_client import GrpcClient, CRO_NETWORK 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) + config = CRO_NETWORK["testnet_croeseid"] + wallet = Wallet(MNEMONIC_PHRASE, config.derivation_path, config.address_prefix) # 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 @@ -29,27 +21,25 @@ def example_with_certificate_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(CHAIN_ID, DENOM, GRPC_ENDPOINT, creds) - + client = GrpcClient(config, creds) from_address = wallet.address res = client.get_balance(from_address) 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) + config = CRO_NETWORK["testnet_croeseid"] + wallet = Wallet(MNEMONIC_PHRASE, config.derivation_path, config.address_prefix) # 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)) + (server_host, server_port) = config.grpc_endpoint.split(":") + conn = ssl.create_connection((server_host, server_port)) context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) - sock = context.wrap_socket(conn, server_hostname=SERVER_HOST) + 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(CHAIN_ID, DENOM, GRPC_ENDPOINT, creds) - + client = GrpcClient(config, creds) from_address = wallet.address res = client.get_balance(from_address) print(f"address {from_address} initial balance: {res.balance.amount}") diff --git a/example/transaction.py b/example/transaction.py index 1c9f62f..fe6bf8b 100644 --- a/example/transaction.py +++ b/example/transaction.py @@ -2,13 +2,12 @@ # -*- coding: utf-8 -*- from chainlibpy.generated.cosmos.base.v1beta1.coin_pb2 import Coin -from chainlibpy.grpc_client import GrpcClient +from chainlibpy.grpc_client import GrpcClient, NetworkConfig from chainlibpy.wallet import Wallet # NOTE: # Recommend to use pystarport(https://pypi.org/project/pystarport/) to setup a testnet locally -DENOM = "basecro" # Obtained from {directory_started_pystarport}/data/chainmaind/accounts.json # To recover one of the genesis account # MNEMONIC_PHRASE = "first ... last" @@ -18,20 +17,26 @@ TO_ADDRESS = "cro1hk220qwxp0c8m3pzazardmmfv8y0mg7ukdnn37" AMOUNT = [Coin(amount="10000", denom=DENOM)] # Obtained from {directory_started_pystarport}/data/chainmaind/genesis.json -CHAIN_ID = "chain_id_test" # Obtained from {directory_started_pystarport}/data/chainmaind/nodex/config/app.toml # Look for "gRPC Configuration" section -GRPC_ENDPOINT = "0.0.0.0:26653" def main(): wallet = Wallet(MNEMONIC_PHRASE) - client = GrpcClient(CHAIN_ID, DENOM, GRPC_ENDPOINT) + config = NetworkConfig( + grpc_endpoint="0.0.0.0:26653", + chain_id="chain_id_test", + address_prefix="cro", + coin_denom="cro", + coin_base_denom="basecro", + derivation_path="m/44'/394'/0'/0/0", + ) + client = GrpcClient(config) from_address = wallet.address res = client.get_balance(from_address) print(f"from_address initial balance: {res.balance.amount}") - # res = client.get_balance(TO_ADDRESS, DENOM) + # res = client.get_balance(TO_ADDRESS) # print(f"to_address initial balance: {res.balance.amount}") client.bank_send(wallet.address, wallet.private_key, wallet.public_key, TO_ADDRESS, AMOUNT) diff --git a/tests/test_grpc_client.py b/tests/test_grpc_client.py index c56d65a..5208734 100644 --- a/tests/test_grpc_client.py +++ b/tests/test_grpc_client.py @@ -10,17 +10,14 @@ @pytest.mark.parametrize("network_config", CRO_NETWORK.values()) def test_network_config(network_config: "NetworkConfig"): - wallet = Wallet.new(path=network_config.derivation_path, hrp=network_config.address_prefix) - (server_host, server_port) = network_config.grpc_endpoint.split(":") - 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, network_config, creds) + client = GrpcClient(network_config, creds) assert ( client.query_bank_denom_metadata(network_config.coin_base_denom).metadata.base @@ -40,5 +37,5 @@ def test_test_environment(blockchain_config_dict, blockchain_accounts, local_tes wallet_default_derivation = Wallet(alice_account["mnemonic"]) assert wallet_default_derivation.address == alice_account["address"] - client = GrpcClient(wallet_default_derivation, local_test_network_config) - print(client.get_balance(wallet_default_derivation.address, "basecro")) + client = GrpcClient(local_test_network_config) + print(client.get_balance(wallet_default_derivation.address))