Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
linfeng-crypto committed Jan 26, 2022
1 parent ea957fc commit 358033b
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 52 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
```

Expand Down
19 changes: 9 additions & 10 deletions chainlibpy/grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion chainlibpy/multisign/bits.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
22 changes: 18 additions & 4 deletions example/multi_sig/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
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,
NetworkConfig,
gen_multi_tx,
get_packed_send_msg,
)
from chainlibpy.multisign.signature import SingleSignatureV2
from chainlibpy.wallet import Wallet

Expand Down Expand Up @@ -139,12 +144,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

Expand Down
30 changes: 10 additions & 20 deletions example/secure_channel_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,51 +5,41 @@

import grpc

from chainlibpy.grpc_client import GrpcClient
from chainlibpy.grpc_client import CRO_NETWORK, 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)
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
# 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(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}")
Expand Down
17 changes: 11 additions & 6 deletions example/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)

Expand Down
9 changes: 3 additions & 6 deletions tests/test_grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))

0 comments on commit 358033b

Please sign in to comment.