def test_private_key_sign(): priv_key = PrivateKey.from_mnemonic(TEST_MNEMONIC) sig = priv_key.sign(b"test msg") assert ( sig.hex() == "42a1e41012155ae2daa9b9a2e038f76463da4662564b4989f236ecb4d97f592c1190d92319363e2d1eb78fb98f0dac30c5e2a850f45bb4c44f1f6203ebe6efbe" )
def test_public_key_verify(): priv_key = PrivateKey.from_mnemonic(TEST_MNEMONIC) sig = priv_key.sign(b"test msg") pub_key = priv_key.to_pubkey() assert pub_key.verify(b"test msg", sig) == True # Invalid message assert pub_key.verify(b"another msg", sig) == False # Invalid signature assert pub_key.verify(b"test msg", sig[1:]) == False
def test_get_tx_data_success(): priv = PrivateKey.from_mnemonic("s") pubkey = priv.to_pubkey() addr = pubkey.to_address() t = ( Transaction() .with_messages( MsgRequest( oracle_script_id=1, calldata=bytes.fromhex("000000034254430000000000000001"), ask_count=4, min_count=3, client_id="from_pyband", sender=addr, ) ) .with_account_num(100) .with_sequence(30) .with_chain_id("bandchain") ) raw_data = t.get_sign_data() signature = priv.sign(raw_data) raw_tx = t.get_tx_data(signature, pubkey) assert raw_tx == { "msg": [ { "type": "oracle/Request", "value": { "oracle_script_id": "1", "calldata": "AAAAA0JUQwAAAAAAAAAB", "ask_count": "4", "min_count": "3", "client_id": "from_pyband", "sender": "band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte", }, } ], "fee": {"gas": "200000", "amount": [{"denom": "uband", "amount": "0"}]}, "memo": "", "signatures": [ { "signature": "aFvTgkY9F14dHfp2mtq8V2eCTKUtY1T9WKs99jmR8To3JB16cawbmpa1TRUdzfnqLXBh+o6XUuF4bHWR6xbCCw==", "pub_key": { "type": "tendermint/PubKeySecp256k1", "value": "A/5wi9pmUk/SxrzpBoLjhVWoUeA9Ku5PYpsF3pD1Htm8", }, "account_number": "100", "sequence": "30", } ], }
def main(): c = Client("http://poa-api.bandchain.org") req_info = c.get_latest_request( 6, bytes.fromhex("000000045041584700000003555344000000003b9aca00"), 4, 4 ) oracle_script = c.get_oracle_script(6) obi = PyObi(oracle_script.schema) print(obi.decode_output(req_info.result.response_packet_data.result)) _, priv = PrivateKey.generate() print(priv.to_pubkey().to_acc_bech32(), priv.to_pubkey().to_address().to_acc_bech32())
def test_address_to_bech32(): addr = PrivateKey.from_mnemonic(TEST_MNEMONIC).to_pubkey().to_address() assert addr.to_acc_bech32() == TEST_ACC_ADDRESS assert addr.to_val_bech32() == TEST_VAL_ADDRESS assert addr.to_cons_bech32() == TEST_CONS_ADDRESS
def test_public_key_to_bech32(): priv_key = PrivateKey.from_mnemonic(TEST_MNEMONIC) assert priv_key.to_pubkey().to_acc_bech32() == TEST_ACC_PUBKEY assert priv_key.to_pubkey().to_val_bech32() == TEST_VAL_PUBKEY assert priv_key.to_pubkey().to_cons_bech32() == TEST_CONS_PUBKEY
def test_private_key_to_public_key(): pub_key = PrivateKey.from_mnemonic(TEST_MNEMONIC).to_pubkey() assert (pub_key.to_hex( ) == "0351e98e1be097250f2ff4188c0aace0a716e69a992cd77f9dfe436b3e8b34280d")
def test_private_key_from_hex(): assert (PrivateKey.from_hex( "2159f40dda9e4c9d8ed9d6f8c353e247a2658993a9d53a94ff17410cd0ea471d"). to_hex() == "2159f40dda9e4c9d8ed9d6f8c353e247a2658993a9d53a94ff17410cd0ea471d")
def test_private_key_generate(): mnemonic, privkey = PrivateKey.generate("m/44'/494'/0'/0/5") assert (PrivateKey.from_mnemonic( mnemonic, "m/44'/494'/0'/0/5").to_hex() == privkey.to_hex())
def test_private_key_from_mnemonic(): assert (PrivateKey.from_mnemonic(TEST_MNEMONIC).to_hex() == "2159f40dda9e4c9d8ed9d6f8c353e247a2658993a9d53a94ff17410cd0ea471d") assert (PrivateKey.from_mnemonic(TEST_MNEMONIC, "m/44'/494'/0'/0/1").to_hex() == "987af53f91a09926274e5a2ef86223356112056f61b35a57df345d7b14176bb3")
from google.protobuf.any_pb2 import Any from pyband.exceptions import EmptyMsgError, UndefinedError, ValueTooLargeError from pyband.transaction import Transaction from pyband.wallet import PrivateKey from pyband.proto.cosmos.base.v1beta1.coin_pb2 import Coin from pyband.proto.cosmos.tx.v1beta1.tx_pb2 import Fee, SignDoc from pyband.proto.cosmos.auth.v1beta1.query_pb2 import QueryAccountRequest, QueryAccountResponse from pyband.proto.oracle.v1.tx_pb2 import MsgRequestData # Servicers from pyband.proto.cosmos.auth.v1beta1.query_pb2_grpc import QueryServicer as QueryServicerBase MNEMONIC = "s" PRIVATE_KEY = PrivateKey.from_mnemonic(MNEMONIC) PUBLIC_KEY = PRIVATE_KEY.to_public_key() ADDRESS = PUBLIC_KEY.to_address() SENDER = ADDRESS.to_acc_bech32() class QueryServicer(QueryServicerBase): def Account(self, request: QueryAccountRequest, context): return QueryAccountResponse(account=Any( type_url="/cosmos.auth.v1beta1.BaseAccount", value= b"\n+band1z2hwz2vn6ardpjzgfx2k3wh2zglknwavhw3v2r\022F\n\037/cosmos.crypto.secp256k1.PubKey\022#\n!\002\243\357\354\271\2712\330H\300F\342suhP\357^!\007\244&\365\t\314\274\312\034~\240\004A\341\030h \010", )) @pytest.fixture(scope="module")