libra网络的python客户端

pylibra的Python项目详细描述


Pylibra

摘要

pylibra是Libra blockchain的非官方python客户机。库允许python程序通过grpc使用protobuf消息与libra节点交互。注意,这个库在内部执行密钥管理。不需要服务器!

安装

$ pip install pylibra

用法

下面您可以找到示例用法和解释。请注意,这是一个阿尔法软件和界面可以随时改变,特别是当天秤座的界面本身还没有解决。

Librawallet

您可以使用LibraWallet类创建钱包。钱包就像你的万能钥匙,你可以用它创建几乎无限多的天秤座账户。请注意,pylibra的助记符方案与Libra's CLI的助记符方案不相似,因此您还不能在两个库之间导入助记符。

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())

账户

可以通过调用钱包上的get_account函数来创建Account,该函数带有一个nonce整数。您可以使用任何号码(0、1、2,…)在您的钱包下生成新帐户。这类似于MetaMask如何跟踪帐户。一个Account包含它的addresspublic_keyprivate_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)

libraclient

为了向libra节点发送protobuf消息,必须创建一个LibraClient。您可以使用以下代码创建客户机。

frompylibraimportLibraClientclient1=LibraClient()# Default client connecting to the official testnetclient2=LibraClient('localhost:80')# Client connecting to a local node

获取地址

的帐户状态

您可以使用LibraClient上的get_account_state函数查询帐户的状态。函数返回一个AccountState,其中包含地址的序列号、余额等。如果尚未创建帐户(从未收到任何资金),则函数将返回None

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 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 passedaccount_state=client.get_account_state(account)

mint testnet libra token

您可以使用mint_with_faucet函数创建testnet libra,该函数向http://faucet.testnet.libra.org发送http get请求。在创建LibraClient时(例如,当您想要自己的水龙头服务时),可以通过传递键值参数faucet自定义此url。第二个参数是mini libra amount,它是libra令牌数量的10^6倍。(例如,10000mini libra是0.01libra令牌)。

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)

创建传输事务脚本并发送事务

注意,在正式的testnet中,libra节点只允许发送the official transfer transaction script。将来,这个libra也可以扩展以支持更多的事务脚本,因为您可以看到创建和发送事务的逻辑是完全独立的!

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)

许可证

此软件由Band Protocol创建,并在the MIT License下发布。

贡献

欢迎任何和所有的贡献!这个过程很简单:派生这个repo,进行更改,然后提交一个pull请求。

运行单元测试

pylibra使用pytest来运行单元测试。

$ PYTHONPATH=. pytest

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java JPanel不会对键绑定做出反应   当时间大于零时,不得在UI线程上调用java Await   JTextArea的java线程安全。追加   Java用户输入的字和行计数器   java以spreedsheat格式将数据保存到文件中   java构造函数的意义是什么?   java findViewById返回null,尽管组件的ID存在   java如何向按钮添加图像   java如何中断ExecutorService的线程   java如何将属性(例如枚举)绑定到不同类型的组件属性(例如每个枚举的映像)?   随机森林分类器的java实现   html使用java连接到一个站点并发布,HTTP状态代码200   从类访问属性时发生java编译错误   Java自动填充ArrayList,搜索更好的选项