Esempio n. 1
0
def test_get_sign_data_with_sender_success(pyband_client):
    msg = MsgRequestData(
        oracle_script_id=1,
        calldata=bytes.fromhex("000000034254430000000000000001"),
        ask_count=4,
        min_count=3,
        client_id="from_pyband",
        fee_limit=[Coin(amount="100", denom="uband")],
        prepare_gas=30000,
        execute_gas=50000,
        sender="band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c",
    )
    fee = [Coin(amount="0", denom="uband")]

    t = (Transaction().with_messages(msg).with_sender(
        pyband_client,
        "band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c").with_chain_id(
            "bandchain").with_gas(50000).with_fee(fee))
    assert t.get_sign_doc(PUBLIC_KEY) == SignDoc(
        body_bytes=
        b"\n\204\001\n\031/oracle.v1.MsgRequestData\022g\010\001\022\017\000\000\000\003BTC\000\000\000\000\000\000\000\001\030\004 \003*\013from_pyband2\014\n\005uband\022\0031008\260\352\001@\320\206\003J+band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c",
        auth_info_bytes=
        b"\nP\nF\n\037/cosmos.crypto.secp256k1.PubKey\022#\n!\003\376p\213\332fRO\322\306\274\351\006\202\343\205U\250Q\340=*\356Ob\233\005\336\220\365\036\331\274\022\004\n\002\010\001\030\010\022\020\n\n\n\005uband\022\0010\020\320\206\003",
        chain_id="bandchain",
        account_number=104,
    )
Esempio n. 2
0
def main():
    # Step 1 Create a gRPC connection
    grpc_url = "rpc-laozi-testnet2.bandchain.org:9090"
    c = Client(grpc_url, insecure=True)

    # Step 2 Convert a menmonic to private key, public key, and sender
    MNEMONIC = os.getenv("MNEMONIC")
    private_key = PrivateKey.from_mnemonic(MNEMONIC)
    public_key = private_key.to_public_key()
    sender_addr = public_key.to_address()
    sender = sender_addr.to_acc_bech32()

    # Step 3 Prepare a transaction's properties
    request_msg = MsgRequestData(
        oracle_script_id=37,
        calldata=bytes.fromhex(
            "0000000200000003425443000000034554480000000000000064"),
        ask_count=4,
        min_count=3,
        client_id="BandProtocol",
        fee_limit=[Coin(amount="100", denom="uband")],
        prepare_gas=50000,
        execute_gas=200000,
        sender=sender,
    )

    account = c.get_account(sender)
    account_num = account.account_number
    sequence = account.sequence

    fee = [Coin(amount="0", denom="uband")]
    chain_id = c.get_chain_id()

    # Step 4 Construct a transaction
    txn = (Transaction().with_messages(request_msg).with_sequence(
        sequence).with_account_num(account_num).with_chain_id(
            chain_id).with_gas(2000000).with_fee(fee).with_memo(""))

    # Step 5 Sign a transaction by using private key
    sign_doc = txn.get_sign_doc(public_key)
    signature = private_key.sign(sign_doc.SerializeToString())
    tx_raw_bytes = txn.get_tx_data(signature, public_key)

    # Step 6 Broadcast a transaction
    tx_block = c.send_tx_block_mode(tx_raw_bytes)

    # Converting to JSON for readability
    print(MessageToJson(tx_block))
Esempio n. 3
0
def test_get_sign_doc_sequence_undefined():
    msg = MsgRequestData(
        oracle_script_id=1,
        calldata=bytes.fromhex("000000034254430000000000000001"),
        ask_count=4,
        min_count=3,
        client_id="from_pyband",
        fee_limit=[Coin(amount="100", denom="uband")],
        prepare_gas=30000,
        execute_gas=50000,
        sender="band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c",
    )

    t = Transaction().with_messages(msg).with_account_num(100).with_chain_id(
        "bandchain")
    with pytest.raises(UndefinedError, match="sequence should be defined"):
        t.get_sign_doc(PUBLIC_KEY)
Esempio n. 4
0
def test_invalid_memo():
    msg = MsgRequestData(
        oracle_script_id=1,
        calldata=bytes.fromhex("000000034254430000000000000001"),
        ask_count=4,
        min_count=3,
        client_id="from_pyband",
        fee_limit=[Coin(amount="100", denom="uband")],
        prepare_gas=30000,
        execute_gas=50000,
        sender="band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c",
    )

    t = Transaction().with_messages(msg).with_account_num(100).with_sequence(
        30).with_chain_id("bandchain")
    with pytest.raises(ValueTooLargeError, match="memo is too large"):
        t.with_memo(
            "This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world. This is the longest memo in the world.This is the longest memo in the world. This is the longest memo in the world.This is the longest memo in the world."
        )
Esempio n. 5
0
def test_get_sign_doc_no_public_key_success():
    msg = MsgRequestData(
        oracle_script_id=1,
        calldata=bytes.fromhex("000000034254430000000000000001"),
        ask_count=4,
        min_count=3,
        client_id="from_pyband",
        fee_limit=[Coin(amount="100", denom="uband")],
        prepare_gas=30000,
        execute_gas=50000,
        sender="band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c",
    )

    t = Transaction().with_messages(msg).with_account_num(100).with_sequence(
        30).with_chain_id("bandchain")
    assert t.get_sign_doc() == SignDoc(
        body_bytes=
        b"\n\204\001\n\031/oracle.v1.MsgRequestData\022g\010\001\022\017\000\000\000\003BTC\000\000\000\000\000\000\000\001\030\004 \003*\013from_pyband2\014\n\005uband\022\0031008\260\352\001@\320\206\003J+band13eznuehmqzd3r84fkxu8wklxl22r2qfmtlth8c",
        auth_info_bytes=
        b"\n\010\022\004\n\002\010\001\030\036\022\004\020\300\232\014",
        chain_id="bandchain",
        account_number=100,
    )
Esempio n. 6
0
def test_get_tx_data_no_public_key_success():
    msg = MsgRequestData(
        oracle_script_id=1,
        calldata=bytes.fromhex("000000034254430000000000000001"),
        ask_count=4,
        min_count=3,
        client_id="from_pyband",
        fee_limit=[Coin(amount="100", denom="uband")],
        prepare_gas=30000,
        execute_gas=50000,
        sender=SENDER,
    )

    t = Transaction().with_messages(msg).with_account_num(100).with_sequence(
        30).with_chain_id("bandchain")

    sign_doc = t.get_sign_doc()
    signature = PRIVATE_KEY.sign(sign_doc.SerializeToString())
    tx_raw_bytes = t.get_tx_data(signature)
    assert (
        tx_raw_bytes ==
        b'\n\x87\x01\n\x84\x01\n\x19/oracle.v1.MsgRequestData\x12g\x08\x01\x12\x0f\x00\x00\x00\x03BTC\x00\x00\x00\x00\x00\x00\x00\x01\x18\x04 \x03*\x0bfrom_pyband2\x0c\n\x05uband\x12\x031008\xb0\xea\x01@\xd0\x86\x03J+band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte\x12\x10\n\x08\x12\x04\n\x02\x08\x01\x18\x1e\x12\x04\x10\xc0\x9a\x0c\x1a@\xf3\xb5z\xa4\xea\xc6\x02\xff\\\x862x\x80\xba\xbf\xd8%t\x88X\xddrDY^"C\xd7\x9c\xf1\xb3\xbe]\xe7Z\x9a\x07\xedX\xf7r\xd4\xf8\x044\xf8\xda\x86\x80(~J\xb8\r\x12\x03\x17\xcb\x9f\xb95\xa4&\xa1'
    )
Esempio n. 7
0
def test_get_tx_data_tx_raw_bytes_no_public_key_success():
    msg = MsgRequestData(
        oracle_script_id=1,
        calldata=bytes.fromhex("000000034254430000000000000001"),
        ask_count=4,
        min_count=3,
        client_id="from_pyband",
        fee_limit=[Coin(amount="100", denom="uband")],
        prepare_gas=30000,
        execute_gas=50000,
        sender=SENDER,
    )

    t = Transaction().with_messages(msg).with_account_num(100).with_sequence(
        30).with_chain_id("bandchain")

    sign_doc = t.get_sign_doc(PUBLIC_KEY)
    signature = PRIVATE_KEY.sign(sign_doc.SerializeToString())
    tx_raw_bytes = t.get_tx_data(signature)
    assert (
        tx_raw_bytes ==
        b"\n\x87\x01\n\x84\x01\n\x19/oracle.v1.MsgRequestData\x12g\x08\x01\x12\x0f\x00\x00\x00\x03BTC\x00\x00\x00\x00\x00\x00\x00\x01\x18\x04 \x03*\x0bfrom_pyband2\x0c\n\x05uband\x12\x031008\xb0\xea\x01@\xd0\x86\x03J+band1jrhuqrymzt4mnvgw8cvy3s9zhx3jj0dq30qpte\x12\x10\n\x08\x12\x04\n\x02\x08\x01\x18\x1e\x12\x04\x10\xc0\x9a\x0c\x1a@/\xa2\xe7\xb3e\xf8\x13\x7f$\x162q\x11\xbaX\xa0\x93\xbeWeO\xc7~\x0e3\xe5F\xd0\xbd\xf1\xd5\xe1k\xefnxuv\x14l?\x8a3\x06Z_7\x93\xd7\xcc\xbf\xc0\xa2S\xff\xaa\x19\xfd}\xddO\xe3\xd9\xb1"
    )