### Install cosmospy Source: https://github.com/hukkin/cosmospy/blob/master/README.md Install the cosmospy library using pip. This command installs the package from the PyPI repository. ```bash pip install cosmospy ``` -------------------------------- ### Transaction Class Usage Source: https://context7.com/hukkin/cosmospy/llms.txt Demonstrates how to initialize the Transaction class, add transfers, and obtain a pushable JSON payload for broadcasting to the Cosmos REST API. ```APIDOC ## `Transaction` — Build and sign a Cosmos token transfer transaction The `Transaction` class constructs an offline-signed Cosmos SDK `MsgSend` transaction. Initialize it with the sender's private key, on-chain account number, current sequence number, fee/gas parameters, and chain ID. Add one or more transfers with `add_transfer()`, then call `get_pushable()` to obtain a JSON string ready for the Cosmos REST API's `POST /txs` endpoint. ### Method ```python Transaction( privkey: bytes, account_num: int, sequence: int, fee: int, gas: int, fee_denom: str = "uatom", memo: str = "", chain_id: str, sync_mode: str = "sync", # "sync" | "async" | "block" ) ``` ### Parameters - **privkey** (bytes) - The sender's private key. - **account_num** (int) - The sender's on-chain account number (obtained from `GET /auth/accounts/{address}`). - **sequence** (int) - The current sequence number for the account (increment by 1 for each subsequent tx). - **fee** (int) - The transaction fee in the smallest denomination of the fee token (e.g., uatom). - **gas** (int) - The gas limit for the transaction. - **fee_denom** (str, optional) - The denomination of the fee token. Defaults to "uatom". - **memo** (str, optional) - An optional memo for the transaction. - **chain_id** (str) - The ID of the Cosmos chain. - **sync_mode** (str, optional) - The transaction synchronization mode. Options are "sync", "async", or "block". Defaults to "sync". ### Method `add_transfer( recipient: str, amount: int, denom: str = "uatom" )` Adds a transfer to the transaction. #### Parameters - **recipient** (str) - The recipient's Cosmos address. - **amount** (int) - The amount to transfer in the smallest denomination of the token. - **denom** (str, optional) - The denomination of the token to transfer. Defaults to "uatom". ### Method `get_pushable() -> str` Returns a JSON string representing the signed transaction, ready to be broadcast to the Cosmos REST API. ### Request Example ```python from cosmospy import Transaction tx = Transaction( privkey=bytes.fromhex( "26d167d549a4b2b66f766b0d3f2bdbe1cd92708818c338ff453abde316a2bd59" ), account_num=11335, sequence=0, fee=1000, gas=70000, chain_id="cosmoshub-4", memo="sent via cosmospy", ) tx.add_transfer( recipient="cosmos103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf", amount=387000, ) tx.add_transfer( recipient="cosmos1lzumfk6xvwf9k9rk72mqtztv867xyem393um48", amount=123, ) pushable_tx = tx.get_pushable() print(pushable_tx) ``` ### Response Example ```json { "tx": { "msg": [ { "type": "cosmos-sdk/MsgSend", "value": { "from_address": "cosmos1...", "to_address": "cosmos103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf", "amount": [ { "denom": "uatom", "amount": "387000" } ] } }, { "type": "cosmos-sdk/MsgSend", "value": { "from_address": "cosmos1...", "to_address": "cosmos1lzumfk6xvwf9k9rk72mqtztv867xyem393um48", "amount": [ { "denom": "uatom", "amount": "123" } ] } } ], "fee": { "amount": [ { "denom": "uatom", "amount": "1000" } ], "gas": "70000" }, "memo": "sent via cosmospy", "signatures": [ "..." ] }, "mode": "sync" } ``` ``` -------------------------------- ### privkey_to_pubkey Source: https://context7.com/hukkin/cosmospy/llms.txt Derives a compressed public key from a 32-byte SECP256k1 private key. ```APIDOC ## privkey_to_pubkey — Derive a compressed public key from a private key Takes a 32-byte SECP256k1 private key and returns the 33-byte compressed public key using the ECDSA signing key abstraction. ### Method Signature ```python privkey_to_pubkey(privkey: bytes) ``` ### Parameters #### Arguments - **privkey** (bytes) - Required - The 32-byte private key. ### Returns - **bytes** - The 33-byte compressed public key. ### Example ```python from cosmospy import privkey_to_pubkey privkey = bytes.fromhex( "6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa" ) pubkey = privkey_to_pubkey(privkey) print(pubkey.hex()) print(len(pubkey)) # 33 (compressed point) ``` ``` -------------------------------- ### Generate a Cosmos Wallet Source: https://github.com/hukkin/cosmospy/blob/master/README.md Generates a new Cosmos wallet, including seed phrase, private key, public key, and address. The generated wallet is returned as a dictionary. ```python from cosmospy import generate_wallet wallet = generate_wallet() ``` ```python { "seed": "arch skill acquire abuse frown reject front second album pizza hill slogan guess random wonder benefit industry custom green ill moral daring glow elevator", "derivation_path": "m/44'/118'/0'/0/0", "private_key": b"\xbb\xec^\xf6\xdcg\xe6\xb5\x89\xed\x8cG\x05\x03\xdf0:\xc9\x8b \x85\x8a\x14\x12\xd7\xa6a\x01\xcd\xf8\x88\x93", "public_key": b"\x03h\x1d\xae\xa7\x9eO\x8e\xc5\xff\xa3sAw\xe6\xdd\xc9\xb8b\x06\x0eo\xc5a%z\xe3\xff\x1e\xd2\x8e5\xe7", "address": "cosmos1uuhna3psjqfxnw4msrfzsr0g08yuyfxeht0qfh", } ``` -------------------------------- ### Convert Private Key to Address Source: https://github.com/hukkin/cosmospy/blob/master/README.md Derives a Cosmos address directly from a private key. The private key must be provided as bytes. ```python from cosmospy import privkey_to_address privkey = bytes.fromhex( "6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa" ) addr = privkey_to_address(privkey) ``` -------------------------------- ### Convert Public Key to Address Source: https://github.com/hukkin/cosmospy/blob/master/README.md Derives a Cosmos address from a given public key. The public key must be provided as bytes. ```python from cosmospy import pubkey_to_address pubkey = bytes.fromhex( "03e8005aad74da5a053602f86e3151d4f3214937863a11299c960c28d3609c4775" ) addr = pubkey_to_address(pubkey) ``` -------------------------------- ### Derive Compressed Public Key from Private Key Source: https://context7.com/hukkin/cosmospy/llms.txt Takes a 32-byte SECP256k1 private key and returns the corresponding 33-byte compressed public key. ```python from cosmospy import privkey_to_pubkey privkey = bytes.fromhex( "6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa" ) pubkey = privkey_to_pubkey(privkey) print(pubkey.hex()) print(len(pubkey)) ``` -------------------------------- ### Generate HD Wallet with Cosmospy Source: https://context7.com/hukkin/cosmospy/llms.txt Generates a BIP39 mnemonic, private key, public key, and bech32 address. Supports custom derivation paths and human-readable prefixes (hrp) for different Cosmos chains. ```python from cosmospy import generate_wallet # Default: cosmoshub derivation path m/44'/118'/0'/0/0, "cosmos" bech32 prefix wallet = generate_wallet() print(wallet["seed"]) print(wallet["derivation_path"]) print(wallet["private_key"]) print(wallet["public_key"]) print(wallet["address"]) # Generate a wallet for an alternate Cosmos chain (e.g. Osmosis uses "osmo" prefix) osmo_wallet = generate_wallet(hrp="osmo") print(osmo_wallet["address"]) # Custom derivation path custom_wallet = generate_wallet(path="m/44'/118'/0'/0/1") print(custom_wallet["derivation_path"]) ``` -------------------------------- ### Sign and Prepare a Cosmos Transaction Source: https://github.com/hukkin/cosmospy/blob/master/README.md Creates and signs a Cosmos transaction with multiple transfers. The signed transaction is returned as a JSON string, ready to be submitted to the Cosmos REST API. ```python from cosmospy import Transaction tx = Transaction( privkey=bytes.fromhex( "26d167d549a4b2b66f766b0d3f2bdbe1cd92708818c338ff453abde316a2bd59" ), account_num=11335, sequence=0, fee=1000, gas=70000, memo="", chain_id="cosmoshub-4", sync_mode="sync", ) tx.add_transfer( recipient="cosmos103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf", amount=387000 ) tx.add_transfer(recipient="cosmos1lzumfk6xvwf9k9rk72mqtztv867xyem393um48", amount=123) pushable_tx = tx.get_pushable() # Optionally submit the transaction using your preferred method. # This example uses the httpx library. import httpx api_base_url = "https://node.atomscan.com" httpx.post(api_base_url + "/txs", data=pushable_tx) ``` -------------------------------- ### generate_wallet Source: https://context7.com/hukkin/cosmospy/llms.txt Generates a new HD wallet including mnemonic, private key, public key, and bech32 address. Supports custom derivation paths and human-readable prefixes. ```APIDOC ## generate_wallet — Generate a new HD wallet Generates a cryptographically secure BIP39 mnemonic, derives a private key along the given BIP32 path, computes the compressed SECP256k1 public key, and encodes a bech32 Cosmos address. Returns a typed `Wallet` dictionary. Accepts optional `path` and `hrp` keyword arguments to target non-default derivation paths or alternate Cosmos-compatible chains. ### Method Signature ```python generate_wallet(path: str = "m/44'/118'/0'/0/0", hrp: str = "cosmos") ``` ### Parameters #### Keyword Arguments - **path** (str) - Optional - The BIP32 derivation path. Defaults to `"m/44'/118'/0'/0/0"`. - **hrp** (str) - Optional - The human-readable prefix for the bech32 address. Defaults to `"cosmos"`. ### Returns - **Wallet** (dict) - A dictionary containing: - **seed** (str): The 24-word BIP39 mnemonic. - **derivation_path** (str): The BIP32 derivation path used. - **private_key** (bytes): The 32-byte private key. - **public_key** (bytes): The 33-byte compressed public key. - **address** (str): The bech32 encoded Cosmos address. ### Example ```python from cosmospy import generate_wallet # Default wallet wallet = generate_wallet() print(wallet["address"]) # Wallet for Osmosis chain osmo_wallet = generate_wallet(hrp="osmo") print(osmo_wallet["address"]) # Wallet with custom derivation path custom_wallet = generate_wallet(path="m/44'/118'/0'/0/1") print(custom_wallet["derivation_path"]) ``` ``` -------------------------------- ### privkey_to_address Source: https://context7.com/hukkin/cosmospy/llms.txt Derives a bech32 Cosmos address directly from a private key, combining public key derivation and address encoding. ```APIDOC ## privkey_to_address — Derive a bech32 address directly from a private key Convenience function combining `privkey_to_pubkey` and `pubkey_to_address` in one call. Accepts an optional `hrp` keyword argument. ### Method Signature ```python privkey_to_address(privkey: bytes, hrp: str = "cosmos") ``` ### Parameters #### Arguments - **privkey** (bytes) - Required - The 32-byte private key. - **hrp** (str) - Optional - The human-readable prefix for the bech32 address. Defaults to `"cosmos"`. ### Returns - **str** - The bech32 encoded Cosmos address. ### Example ```python from cosmospy import privkey_to_address privkey = bytes.fromhex( "6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa" ) addr = privkey_to_address(privkey) print(addr) # For an alternate chain addr_osmo = privkey_to_address(privkey, hrp="osmo") print(addr_osmo) ``` ``` -------------------------------- ### Build and Sign Cosmos Transaction with cosmospy Source: https://context7.com/hukkin/cosmospy/llms.txt Initializes a Transaction object with sender details, fee, and chain information. Adds transfers to recipients and generates a JSON payload for the Cosmos REST API. Requires sender's private key, account number, sequence, fee, gas, chain ID, and memo. The recipient and amount for transfers are specified in uatom by default. ```python from cosmospy import Transaction import httpx # optional: any HTTP client works # Build the transaction tx = Transaction( privkey=bytes.fromhex( "26d167d549a4b2b66f766b0d3f2bdbe1cd92708818c338ff453abde316a2bd59" ), account_num=11335, # from GET /auth/accounts/{address} sequence=0, # increment by 1 for each subsequent tx from this account fee=1000, # in uatom (1000 uatom = 0.001 ATOM) gas=70000, fee_denom="uatom", # default, can be omitted memo="sent via cosmospy", chain_id="cosmoshub-4", sync_mode="sync", # "sync" | "async" | "block" ) # Add one or more recipients tx.add_transfer( recipient="cosmos103l758ps7403sd9c0y8j6hrfw4xyl70j4mmwkf", amount=387000, # in uatom ) tx.add_transfer( recipient="cosmos1lzumfk6xvwf9k9rk72mqtztv867xyem393um48", amount=123, denom="uatom", # default, can be omitted ) # Get the signed, broadcast-ready JSON payload pushable_tx = tx.get_pushable() print(pushable_tx) # {"tx":{"msg":[...],"fee":{...},"memo":"sent via cosmospy","signatures":[...]},"mode":"sync"} # Broadcast to a Cosmos node response = httpx.post("https://node.atomscan.com/txs", data=pushable_tx) print(response.json()) # {"height":"0","txhash":"ABC123...","raw_log":"[...]"} ``` -------------------------------- ### seed_to_privkey Source: https://context7.com/hukkin/cosmospy/llms.txt Derives a private key from a BIP39 mnemonic seed phrase using a specified BIP32 derivation path. ```APIDOC ## seed_to_privkey — Derive a private key from a mnemonic seed Converts a BIP39 mnemonic phrase into a raw private key (`bytes`) by deriving along the specified BIP32 path. Assumes no BIP39 passphrase. Raises `cosmospy.BIP32DerivationError` if the derived key is invalid for the given path (rare but possible with certain mnemonics). ### Method Signature ```python seed_to_privkey(seed: str, path: str = "m/44'/118'/0'/0/0") ``` ### Parameters #### Arguments - **seed** (str) - Required - The BIP39 mnemonic phrase. - **path** (str) - Optional - The BIP32 derivation path. Defaults to `"m/44'/118'/0'/0/0"`. ### Returns - **bytes** - The 32-byte private key. ### Raises - **BIP32DerivationError** - If the derived key is invalid for the given path. ### Example ```python from cosmospy import BIP32DerivationError, seed_to_privkey seed = "teach there dream chase fatigue abandon lava super senior artefact close upgrade" try: privkey = seed_to_privkey(seed, path="m/44'/118'/0'/0/0") print(privkey.hex()) except BIP32DerivationError: print("No valid private key in this derivation path!") # Derive a different account index privkey_account_1 = seed_to_privkey(seed, path="m/44'/118'/0'/0/1") print(privkey_account_1.hex()) ``` ``` -------------------------------- ### Convert Private Key to Public Key Source: https://github.com/hukkin/cosmospy/blob/master/README.md Derives the public key corresponding to a given private key. The private key must be provided as bytes. ```python from cosmospy import privkey_to_pubkey privkey = bytes.fromhex( "6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa" ) pubkey = privkey_to_pubkey(privkey) ``` -------------------------------- ### pubkey_to_address Source: https://context7.com/hukkin/cosmospy/llms.txt Encodes a bech32 Cosmos address from a public key, with support for custom human-readable prefixes. ```APIDOC ## pubkey_to_address — Encode a bech32 Cosmos address from a public key Hashes the compressed public key with SHA-256 then RIPEMD-160, converts to 5-bit groups, and encodes as a bech32 string with the given human-readable prefix (`hrp`). Defaults to `"cosmos"`. ### Method Signature ```python pubkey_to_address(pubkey: bytes, hrp: str = "cosmos") ``` ### Parameters #### Arguments - **pubkey** (bytes) - Required - The 33-byte compressed public key. - **hrp** (str) - Optional - The human-readable prefix for the bech32 address. Defaults to `"cosmos"`. ### Returns - **str** - The bech32 encoded Cosmos address. ### Example ```python from cosmospy import pubkey_to_address pubkey = bytes.fromhex( "03e8005aad74da5a053602f86e3151d4f3214937863a11299c960c28d3609c4775" ) # Cosmos Hub address addr = pubkey_to_address(pubkey) print(addr) # Osmosis address (same key, different prefix) osmo_addr = pubkey_to_address(pubkey, hrp="osmo") print(osmo_addr) ``` ``` -------------------------------- ### Encode Bech32 Cosmos Address from Public Key Source: https://context7.com/hukkin/cosmospy/llms.txt Hashes a compressed public key and encodes it as a bech32 string. Supports custom human-readable prefixes (hrp) for different Cosmos chains. ```python from cosmospy import pubkey_to_address pubkey = bytes.fromhex( "03e8005aad74da5a053602f86e3151d4f3214937863a11299c960c28d3609c4775" ) # Cosmos Hub address addr = pubkey_to_address(pubkey) print(addr) # Osmosis address (same key, different prefix) osmo_addr = pubkey_to_address(pubkey, hrp="osmo") print(osmo_addr) ``` -------------------------------- ### Derive Private Key from Mnemonic Seed Source: https://context7.com/hukkin/cosmospy/llms.txt Converts a BIP39 mnemonic phrase into a raw private key using a specified BIP32 derivation path. Handles potential BIP32 derivation errors. ```python from cosmospy import BIP32DerivationError, seed_to_privkey seed = "teach there dream chase fatigue abandon lava super senior artefact close upgrade" try: privkey = seed_to_privkey(seed, path="m/44'/118'/0'/0/0") print(privkey.hex()) except BIP32DerivationError: print("No valid private key in this derivation path!") # Derive a different account index privkey_account_1 = seed_to_privkey(seed, path="m/44'/118'/0'/0/1") print(privkey_account_1.hex()) ``` -------------------------------- ### Convert Mnemonic Seed to Private Key Source: https://github.com/hukkin/cosmospy/blob/master/README.md Derives a private key from a mnemonic seed phrase using a specified derivation path. Handles potential BIP32 derivation errors. ```python from cosmospy import BIP32DerivationError, seed_to_privkey seed = ( "teach there dream chase fatigue abandon lava super senior artefact close upgrade" ) try: privkey = seed_to_privkey(seed, path="m/44'/118'/0'/0/0") except BIP32DerivationError: print("No valid private key in this derivation path!") ``` -------------------------------- ### Derive Bech32 Address Directly from Private Key Source: https://context7.com/hukkin/cosmospy/llms.txt A convenience function that combines private key to public key derivation and public key to bech32 address encoding. Supports custom human-readable prefixes (hrp). ```python from cosmospy import privkey_to_address privkey = bytes.fromhex( "6dcd05d7ac71e09d3cf7da666709ebd59362486ff9e99db0e8bc663570515afa" ) addr = privkey_to_address(privkey) print(addr) # For an alternate chain addr_osmo = privkey_to_address(privkey, hrp="osmo") print(addr_osmo) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.