Skip to main content

Tools for Crypto.org Chain wallet management and offline transaction signing

Project description

Build Status codecov.io PyPI version Code style: black

chainlibpy

Tools for Crypto.org Chain wallet management and offline transaction signing

Installing

Require Python >= 3.7, installing from PyPI repository (https://pypi.org/project/chainlibpy):

pip install chainlibpy

Usage

Generating a wallet

from chainlibpy import Wallet

# create a wallet
wallet = Wallet.new()
# or you can recover from an mnomic
# mnemonic = "dune car envelope chuckle elbow slight proud fury remove candy uphold puzzle call select sibling sport gadget please want vault glance verb damage gown"
# wallet = Wallet(mnemonic)

print(wallet.private_key)
print(wallet.public_key)
print(wallet.address)

Signing and broadcasting a transaction

from chainlibpy.generated.cosmos.base.v1beta1.coin_pb2 import Coin
from chainlibpy.grpc_client import GrpcClient
from chainlibpy.transaction import sign_transaction
from chainlibpy.wallet import Wallet

# Refer to example/transaction.py for how to obtain CONSTANT values below
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(wallet, CHAIN_ID, GRPC_ENDPOINT)

from_address = wallet.address
account_number = client.query_account_data(wallet.address).account_number

msg = client.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)
client.broadcast_tx(tx)

You may also refer to example/transaction.py on how to use a high level function bank_send() to sign and broadcast a transaction

Acknowledgement

Thanks cosmospy for the following:

  • referenced the packages to sign transaction and create hd wallet
  • python lint config file
  • use same sign method

Development

Set up development environment

More about poetry.

poetry install

Generate gRPC code

poetry shell
./generated_protos.sh

NOTE: By default, master branch of cosmos-sdk is used. Use command below to download a different version:

./generated_protos.sh -COSMOS_REF=v0.44.5

If more generated gRPC code is needed in the future, please add the .proto files needed here in generated_protos.sh:

# Add .proto files here to generate respective gRPC code
PROTO_FILES="
$COSMOS_SDK_DIR/proto/cosmos/auth/v1beta1/auth.proto
...

Tox

pyenv local 3.7.a 3.8.b 3.9.c

a, b and c is python versions installed on your computer by pyenv. More about pyenv.

After this command, a .python-version file will be generated at project root directory, which means python versions inside .python-version are presented for this project. So running tox command with py{37,38,39} configuration should succeed.
Then run to verify. This command is recommended to run before pushing a commit.

poetry run tox
# or
poetry shell
tox

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

chainlibpy-2.1.0-py3-none-any.whl (92.4 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page