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

Commit

Permalink
Merge pull request #21 from macong-cdc/grpc
Browse files Browse the repository at this point in the history
Migrate to gRPC + direct protobuf signing
  • Loading branch information
linfeng-crypto authored Dec 14, 2021
2 parents f90087e + 70f3fbf commit 30b1da0
Show file tree
Hide file tree
Showing 78 changed files with 14,632 additions and 1,029 deletions.
6 changes: 0 additions & 6 deletions .flake8

This file was deleted.

3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jobs:
matrix:
tox_env: [py37, py38, py39]
steps:
- uses: actions/checkout@v2
- name: Checkout
uses: actions/checkout@v2
- name: Run Tox tests
id: test
uses: fedora-python/tox-github-action@master
Expand Down
21 changes: 11 additions & 10 deletions .github/workflows/upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ name: upload

on:
push:
branches:
- master
tags:
- 'v*.*.*'

jobs:
upload:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: install dependencies
run: python3 -m pip install --user --upgrade setuptools wheel
run: python3 -m pip install --user --upgrade poetry
- name: build
run: python3 setup.py sdist bdist_wheel
- name: install jfrog
run: curl -fL https://getcli.jfrog.io | sh
- name: upload to jfrog
run: |
./jfrog rt config --url "${{ secrets.ARTIFACTORY_URL }}" --user "${{ secrets.ARTIFACTORY_USER }}" --access-token "${{ secrets.ARTIFACTORY_PASSWORD }}" --interactive=false
./jfrog rt u --build-name=Crypto-Blockchain-PYPI --build-number=${{ github.run_id }} --flat=true "dist/*" "${{ secrets.ARTIFACTORY_NAME }}"
run: poetry build
- name: release
uses: softprops/action-gh-release@v1
with:
files: |
dist/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ venv.bak/

# A script that can be used for quick testing
tester.py

# vscode
.vscode/
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

This log documents all public API breaking backwards incompatible changes.

## 1.0.0
*Dec 7, 2021*

- Added
- Code for address generation and transaction signing
## 2.1.0

[#28](https://github.com/crypto-org-chain/chainlibpy/pull/21) Migrate to gRPC which supports `chain-main` using `Cosmos SDK` version v0.43/0.44

## 2.0.0

Expand All @@ -14,3 +15,8 @@ This log documents all public API breaking backwards incompatible changes.
- amino types including `Coin`, `StdFee`, `StdSignDoc` and many transaction messages
- simplified the transaction arguments
- require Python >= 3.7

## 1.0.0

- Added
- Code for address generation and transaction signing
10 changes: 0 additions & 10 deletions MANIFEST.in

This file was deleted.

119 changes: 83 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@

# chainlibpy

<!--- Don't edit the version line below manually. Let bump2version do it for you. -->

> Version 2.0.0
> Tools for [Crypto.org Chain](https://github.com/crypto-org-chain/chain-main) wallet management and offline transaction signing
<!-- mdformat-toc start --slug=github --maxlevel=6 --minlevel=2 -->

- [Installing](#installing)
- [Usage](#usage)
- [Generating a wallet](#generating-a-wallet)
- [Signing transactions](#signing-transactions)
- [thanks](#thanks)
- [Signing and broadcasting a transaction](#signing-and-broadcasting-a-transaction)
- [Acknowledgement](#acknowledgement)
- [Development](#development)
- [Set up development environment](#set-up-development-environment)
- [Generate gRPC code](#generate-grpc-code)
- [Tox](#tox)

<!-- mdformat-toc end -->

Expand Down Expand Up @@ -47,43 +47,90 @@ print(wallet.public_key)
print(wallet.address)
```

### Signing transactions<a name="signing-transactions"></a>
### Signing and broadcasting a transaction<a name="signing-and-broadcasting-a-transaction"></a>

```python
from chainlibpy import Transaction, Wallet
from chainlibpy.amino import StdFee, Coin
from chainlibpy.amino.message import MsgSend

wallet = Wallet.new()
fee = StdFee("300000", [Coin("100000")])
tx = Transaction(
wallet=wallet,
account_num=11335,
sequence=0,
fee=fee,
memo="",
chain_id="test",
sync_mode="sync",
)
from_add = wallet.address
msg = MsgSend(
from_address=wallet.address,
to_address="cro103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf",
amount="387000",
)
tx.add_msg(msg)
pushable_tx = tx.get_pushable()
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)
```

One or more token transfers can be added to a transaction by calling the `add_transfer` method.
You may also refer to `example/transaction.py` on how to use a high level function `bank_send()` to sign and broadcast a transaction

When the transaction is fully prepared, calling `get_pushable` will return a signed transaction in the form of a JSON string.
This can be used as request body when calling the `POST /txs` endpoint of rpc.
### Acknowledgement<a name="acknowledgement"></a>

### thanks<a name="thanks"></a>

thanks [cosmospy](https://github.com/hukkinj1/cosmospy) for the following:
Thanks [cosmospy](https://github.com/hukkinj1/cosmospy) for the following:

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

## Development<a name="development"></a>

### Set up development environment<a name="set-up-development-environment"></a>

More about [poetry](https://python-poetry.org/docs/).

```
poetry install
```

### Generate gRPC code<a name="generate-grpc-code"></a>

```
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`:

```bash
# Add .proto files here to generate respective gRPC code
PROTO_FILES="
$COSMOS_SDK_DIR/proto/cosmos/auth/v1beta1/auth.proto
...
```
### Tox<a name="tox"></a>
```
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](https://github.com/pyenv/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.
```sh
poetry run tox
# or
poetry shell
tox
```
9 changes: 0 additions & 9 deletions chainlibpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +0,0 @@
# Copyright (c) 2020, hukkinj1 (licensed under the MIT License)
# Modifications Copyright (c) 2020, Foris Limited (licensed under the Apache License, Version 2.0)

from chainlibpy.transaction import Transaction
from chainlibpy.wallet import Wallet

__version__ = "2.0.0" # DO NOT EDIT THIS LINE MANUALLY. LET bump2version UTILITY DO IT

__all__ = ["amino", "Transaction", "Wallet"]
21 changes: 17 additions & 4 deletions chainlibpy/amino/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
from .basic import (DEFAULT_BECH32_HRP_BASE, Coin, CommissionRates, Content,
Description, Input, Output, StdFee, SyncMode,
TimeoutHeight, VoteOptionAbstain, VoteOptionNo,
VoteOptionNoWithVeto, VoteOptionUnspecified, VoteOptionYes)
from .basic import (
DEFAULT_BECH32_HRP_BASE,
Coin,
CommissionRates,
Content,
Description,
Input,
Output,
StdFee,
SyncMode,
TimeoutHeight,
VoteOptionAbstain,
VoteOptionNo,
VoteOptionNoWithVeto,
VoteOptionUnspecified,
VoteOptionYes,
)
from .signdoc import StdSignDoc

__all__ = [
Expand Down
17 changes: 13 additions & 4 deletions chainlibpy/amino/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
from dataclasses import dataclass
from typing import List

from .basic import (Coin, CommissionRates, Content, Description, Input, Output,
TimeoutHeight, VoteOption, to_dict)
from .basic import (
Coin,
CommissionRates,
Content,
Description,
Input,
Output,
TimeoutHeight,
VoteOption,
to_dict,
)


class Msg:
Expand Down Expand Up @@ -253,7 +262,7 @@ def to_dict(self) -> dict:
"source_channel": self.source_channel,
"source_port": self.source_port,
"token": self.coin.to_dict(),
}
},
}
timeout_height = self.packet_timeout_height.to_dict()
if timeout_height:
Expand Down Expand Up @@ -286,7 +295,7 @@ def to_dict(self) -> dict:
"name": self.name,
"schema": schema,
"sender": self.sender,
}
},
}
return data

Expand Down
9 changes: 3 additions & 6 deletions chainlibpy/amino/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ class Pubkey(BasicObj):
value: str

def to_dict(self):
return {
"type": "tendermint/PubKeySecp256k1",
"value": self.value
}
return {"type": "tendermint/PubKeySecp256k1", "value": self.value}


@dataclass(init=True, repr=True, eq=True, order=True, frozen=True)
Expand All @@ -28,7 +25,7 @@ def to_dict(self):
"signature": self.signature,
"pub_key": self.pub_key.to_dict(),
"account_number": self.account_number,
"sequence": self.sequence
"sequence": self.sequence,
}


Expand All @@ -46,5 +43,5 @@ def to_dict(self):
"fee": self.fee.to_dict(),
"memo": self.memo,
"timeout_height": self.timeout_height,
"signatures": [s.to_dict() for s in self.signatures]
"signatures": [s.to_dict() for s in self.signatures],
}
4 changes: 4 additions & 0 deletions chainlibpy/generated/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))
Loading

0 comments on commit 30b1da0

Please sign in to comment.