Skip to content
This repository has been archived by the owner on Nov 16, 2022. It is now read-only.

Pyband: Added the address to account dataclass #2897

Merged
merged 4 commits into from
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG_UNRELEASED.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@

### Helpers

- (impv) [\#2897](https://github.com/bandprotocol/bandchain/pull/2897) pyband: Added the address to account dataclass
- (impv) [\#2862](https://github.com/bandprotocol/bandchain/pull/2862) bandchain.js: Implement `getSignData`, `getTxData` and all `with_*` methods on Transaction module
- (feat) [\#2872](https://github.com/bandprotocol/bandchain/pull/2872) Bandchain.js: Added new Address class and added more fn on PublicKey class
- (feat) [\#2865](https://github.com/bandprotocol/bandchain/pull/2865) bandchain.js: Add Private Key and verify on Public Key on Wallet
Expand Down
10 changes: 7 additions & 3 deletions helpers/pyband/pyband/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@
from typing import List, Optional, NewType
from dacite import Config
from pyband.utils import parse_datetime
from pyband.wallet import Address

HexBytes = NewType("HexBytes", bytes)
Timestamp = NewType("Timestamp", int)


DACITE_CONFIG = Config(
type_hooks={
int: int,
bytes: base64.b64decode,
HexBytes: bytes.fromhex,
Timestamp: parse_datetime,
Address: Address.from_acc_bech32,
}
)


@dataclass
class DataSource(object):
owner: str
owner: Address
name: str = ""
description: str = ""
filename: str = ""


@dataclass
class OracleScript(object):
owner: str
owner: Address
name: str = ""
description: str = ""
filename: str = ""
Expand Down Expand Up @@ -120,12 +123,13 @@ def validate(self) -> bool:

@dataclass
class Account(object):
address: str
address: Address
coins: List[dict]
public_key: Optional[dict]
account_number: int
sequence: int


@dataclass
class TransactionSyncMode(object):
tx_hash: HexBytes
Expand Down
15 changes: 10 additions & 5 deletions helpers/pyband/pyband/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def from_mnemonic(cls, words: str, path=DEFAULT_DERIVATION_PATH) -> "PrivateKey"
def from_hex(cls, priv: str) -> "PrivateKey":
self = cls(_error_do_not_use_init_directly=True)
self.signing_key = SigningKey.from_string(
bytes.fromhex(priv), curve=SECP256k1, hashfunc=hashlib.sha256,
bytes.fromhex(priv),
curve=SECP256k1,
hashfunc=hashlib.sha256,
)
evilpeach marked this conversation as resolved.
Show resolved Hide resolved
return self

Expand Down Expand Up @@ -97,7 +99,9 @@ def sign(self, msg: bytes) -> bytes:
:return: a signature of this private key over the given message
"""
return self.signing_key.sign_deterministic(
msg, hashfunc=hashlib.sha256, sigencode=sigencode_string_canonize,
msg,
hashfunc=hashlib.sha256,
sigencode=sigencode_string_canonize,
)


Expand All @@ -124,9 +128,7 @@ def _from_bech32(cls, bech: str, prefix: str) -> "PublicKey":
raise ValueError("Cannot decode bech32")
bz = convertbits(bz, 5, 8, False)
self = cls(_error_do_not_use_init_directly=True)
self.verify_key = VerifyingKey.from_string(
bytes(bz[5:]), curve=SECP256k1, hashfunc=hashlib.sha256
)
self.verify_key = VerifyingKey.from_string(bytes(bz[5:]), curve=SECP256k1, hashfunc=hashlib.sha256)
return self

@classmethod
Expand Down Expand Up @@ -194,6 +196,9 @@ class Address:
def __init__(self, addr: bytes) -> None:
self.addr = addr

def __eq__(self, o: "Address") -> bool:
return self.addr == o.addr

@classmethod
def _from_bech32(cls, bech: str, prefix: str) -> "Address":
hrp, bz = bech32_decode(bech)
Expand Down
6 changes: 3 additions & 3 deletions helpers/pyband/tests/client/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_get_account(requests_mock):
)

assert client.get_account(Address.from_acc_bech32("band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte")) == Account(
address="band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte",
address=Address.from_acc_bech32("band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte"),
coins=[{"denom": "uband", "amount": "104082359107"}],
public_key={
"type": "tendermint/PubKeySecp256k1",
Expand Down Expand Up @@ -207,7 +207,7 @@ def test_get_data_source(requests_mock):
)

assert client.get_data_source(1) == DataSource(
owner="band1m5lq9u533qaya4q3nfyl6ulzqkpkhge9q8tpzs",
owner=Address.from_acc_bech32("band1m5lq9u533qaya4q3nfyl6ulzqkpkhge9q8tpzs"),
name="CoinGecko Cryptocurrency Price",
description="Retrieves current price of a cryptocurrency from https://www.coingecko.com",
filename="c56de9061a78ac96748c83e8a22330accf6ee8ebb499c8525613149a70ec49d0",
Expand All @@ -234,7 +234,7 @@ def test_get_oracle_script(requests_mock):
)

assert client.get_oracle_script(1) == OracleScript(
owner="band1m5lq9u533qaya4q3nfyl6ulzqkpkhge9q8tpzs",
owner=Address.from_acc_bech32("band1m5lq9u533qaya4q3nfyl6ulzqkpkhge9q8tpzs"),
name="Cryptocurrency Price in USD",
description="Oracle script that queries the average cryptocurrency price using current price data from CoinGecko, CryptoCompare, and Binance",
filename="a1f941e828bd8d5ea9c98e2cd3ff9ba8e52a8f63dca45bddbb2fdbfffebc7556",
Expand Down