Skip to main content

PyLibra 🐍

Summary

PyLibra is an unofficial python client for Libra blockchain. The library allows Python program to interact with Libra nodes with protobuf message through grpc. Note that this library performs key management internally. No server required!

Installation

$ pip install pylibra

Usage

Below you can find example usages together with explanation. Note that this is an alpha software and interface can change all the time, especially when Libra's interface itself is not yet settle.

LibraWallet

You can create a wallet using LibraWallet class. A wallet is like your masterkey and you can create almost infinitely many Libra accounts from it. Note that PyLibra's mnemonic scheme is not similar to that of Libra's CLI, so you cannot import mnemonic between the two libraries (yet).

from pylibra import LibraWallet

# Create a new random wallet
wallet1 = LibraWallet()
print(wallet1.to_mnemonic())

# Regenerate wallet from an existing Mnemonic
wallet2 = LibraWallet("student deliver dentist cat gorilla sleep proud naive gown fiber awkward weasel")
print(wallet2.to_mnemonic())

Account

An Account can be created by calling get_account function on a wallet, with a nonce integer. You use any number (0, 1, 2, ...) to generate a new account under your wallet. This is similar to how MetaMask keeps track of account. An Account contains its address, public_key, and private_key.

from pylibra import LibraWallet

wallet = LibraWallet()

account1 = wallet.get_account(0)
print(account1.address)
print(account1.public_key)
print(account1.private_key)

account2 = wallet.get_account(1)
print(account2.address)
print(account2.public_key)
print(account2.private_key)

LibraClient

A LibraClient must be created in order to send protobuf message to a Libra node. You can create a client with the following code.

from pylibra import LibraClient

client1 = LibraClient()  # Default client connecting to the official testnet
client2 = LibraClient('localhost:80')  # Client connecting to a local node

Get Account State of an Address

You can query an account's state by using get_account_state function on LibraClient. The function returns an AccountState, which contains the address' sequence number, balance, and more. If an account has not been created yet (never received any funds), the function will return None.

from pylibra import LibraClient, LibraWallet

client = LibraClient()
wallet = LibraWallet("student deliver dentist cat gorilla sleep proud naive gown fiber awkward weasel")
account = wallet.get_account(0)

# You can pass in a hex string address 
account_state = client.get_account_state("4988ceb593200955bf64a024907a94206518d6ac2f624eec569abce38f98da86")
print(account_state.balance)
print(account_state.sequence_number)
print(account_state.received_events_count)
print(account_state.sent_events_count)

# Account object can also be passed
account_state = client.get_account_state(account)

Mint Testnet Libra Token

You can mint testnet libra with mint_with_faucet function, which sends a HTTP GET request to http://faucet.testnet.libra.org. You can customize this URL by passing a key-value argument faucet when creating a LibraClient (for example, when you want to have your own faucet service). The second argument is the mini-libra amount which is 10^6 times the amount of Libra token. (e.g. 10000 mini-libra is 0.01 Libra token).

from pylibra import LibraClient, LibraWallet

client = LibraClient()
wallet = LibraWallet("student deliver dentist cat gorilla sleep proud naive gown fiber awkward weasel")
account = wallet.get_account(0)

# Mint 0.01 Libra to the given address
client.mint_with_faucet("4988ceb593200955bf64a024907a94206518d6ac2f624eec569abce38f98da86", 10000)  

# Or the given account
client.mint_with_faucet(account, 10000)

Creating a Transfer Transaction Script and Sending the Transaction

Note that in the official testnet, the Libra node ONLY allows sending the official transfer transaction script. In the future, this libra can be extended to support more transaction scripts as well, as you can see that the logic of creating and sending a transaction is completely independent!

from pylibra import LibraClient, LibraWallet
from pylibra.transaction import TransferTransaction

client = LibraClient()
wallet = LibraWallet("student deliver dentist cat gorilla sleep proud naive gown fiber awkward weasel")
account1 = wallet.get_account(0)
account2 = wallet.get_account(1)

# Create a transfer transaction object to send 0.001 Libra to account2
tx1 = TransferTransaction(account2, 1000)
# Or to send to a plain hex address
tx2 = TransferTransaction("4988ceb593200955bf64a024907a94206518d6ac2f624eec569abce38f98da86", 1000)

# You can send a transaction by calling `send_transaction` function, which takes a sender `Account` and a `Transaction` object. You can also optionally passed `max_gas_amount`, `gas_unit_price`, and `expiration_time`. 
client.send_transaction(account1, tx1)
# Specify gas limit, gas price, and expiration time (this case, it will expire in year 2508)
client.send_transaction(account1, tx2, max_gas_amount=10000, gas_unit_price=0, expiration_time=17000000000)

License

This software is created by Band Protocol and is released under the MIT License.

Contributing

Any and all contributions are welcome! The process is simple: fork this repo, make your changes, and submit a pull request.

Running Unit Tests

PyLibra uses pytest to run unit tests.

$ PYTHONPATH=. pytest

Metadata

Release files for pylibra 0.1.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pylibra 0.1.10
File Size Uploaded Provenance Actions
pylibra-0.1.10.tar.gz 29.3 kB Without provenance Details for pylibra-0.1.10.tar.gz

Release files / pylibra-0.1.10.tar.gz

Download URL pylibra-0.1.10.tar.gz
Size 29.3 kB
Tags Source
SHA-256 checksum
How to use checksums
4d295586a6d1779d79cabe6c7460380e2aca43c62b8ce5cb94fff79e1ff4bc47
BLAKE2b-256 checksum
How to use checksums
41f9b950f323977c6afaaeb1f6c5cd85fb62b0508a84d268f79b289ba0c8072a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Security for pylibra 0.1.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page