Package: pylibra
PyLibra 🐍SummaryPyLibra is an unofficial python client forLibra blockchain. The library allows Python program to interact with Libra nodes withprotobufmessage throughgrpc. Note that this library performs key management internally. No server required!Installation$pipinstallpylibraUsageBelow 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.LibraWalletYou can create a wallet usingLibraWalletclass. 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 ofLibra's CLI, so you cannot import mnemonic between the two libraries (yet).frompylibraimportLibraWallet# Create a new random walletwallet1=LibraWallet()print(wallet1.to_mnemonic())# Regenerate wallet from an existing Mnemonicwallet2=LibraWallet("student deliver dentist cat gorilla sleep proud naive gown fiber awkward weasel")print(wallet2.to_mnemonic())AccountAnAccountcan be created by callingget_accountfunction 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 howMetaMaskkeeps track of account. AnAccountcontains itsaddress,public_key, andprivate_key.frompylibraimportLibraWalletwallet=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)LibraClientALibraClientmust be created in order to send protobuf message to a Libra node. You can create a client with the following code.frompylibraimportLibraClientclient1=LibraClient()# Default client connecting to the official testnetclient2=LibraClient('localhost:80')# Client connecting to a local nodeGet Account State of an AddressYou can query an account's state by usingget_account_statefunction onLibraClient. The function returns anAccountState, which contains the address' sequence number, balance, and more. If an account has not been created yet (never received any funds), the function will returnNone.frompylibraimportLibraClient,LibraWalletclient=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 addressaccount_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 passedaccount_state=client.get_account_state(account)Mint Testnet Libra TokenYou can mint testnet libra withmint_with_faucetfunction, which sends a HTTP GET request tohttp://faucet.testnet.libra.org. You can customize this URL by passing a key-value argumentfaucetwhen creating aLibraClient(for example, when you want to have your own faucet service). The second argument is the mini-libra amount which is10^6times the amount of Libra token. (e.g.10000mini-libra is0.01Libra token).frompylibraimportLibraClient,LibraWalletclient=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 addressclient.mint_with_faucet("4988ceb593200955bf64a024907a94206518d6ac2f624eec569abce38f98da86",10000)# Or the given accountclient.mint_with_faucet(account,10000)Creating a Transfer Transaction Script and Sending the TransactionNote that in the official testnet, the Libra node ONLY allows sendingthe 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!frompylibraimportLibraClient,LibraWalletfrompylibra.transactionimportTransferTransactionclient=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 account2tx1=TransferTransaction(account2,1000)# Or to send to a plain hex addresstx2=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)LicenseThis software is created byBand Protocoland is released underthe MIT License.ContributingAny and all contributions are welcome! The process is simple: fork this repo, make your changes, and submit a pull request.Running Unit TestsPyLibra usespytestto run unit tests.$ PYTHONPATH=. pytest