Libra Block Chain Wallet Development Appendix Source Code – LearnDapp Series

Time:2019-8-7

Facebook Libra has recently attracted a lot of attention, and as a blockchain app developer who likes Libra, it’s natural to try something out. This paper records the specific process of developing a Libra wallet, using RPC call scheme and chain for interaction. The process description is more detailed. Please skip what you know as appropriate. Finally, the Github warehouse address of Libra wallet source code is attached. Welcome clone.

1. Installing Libra, Compiling Client, Connecting Test Network

install

git clone https://github.com/libra/libra.git && cd libra
./scripts/dev_setup.sh

Compile client

cargo build

If you encounter google/protobuf/wrappers.proto: File not found (macos environment), execute before cargo build

export PATH="/usr/local/opt/protobuf/bin:$PATH"

See the figure below for completion, estimated 5 minutes
Libra Block Chain Wallet Development Appendix Source Code - LearnDapp Series

Connect test network

./scripts/cli/start_cli_testnet.sh

Enter the Interactive Terminal
Libra Block Chain Wallet Development Appendix Source Code - LearnDapp Series

2. Experience: creating accounts, recharging, initiating transactions, querying transactions

Creating Accounts

account create

implement

account list

List the two accounts you just created

Libra Block Chain Wallet Development Appendix Source Code - LearnDapp Series

Refill #0 account

account mint 0 10000

Then query the balance

query balance 0

The results are as follows:Balance is: 10000

Initiate a transaction

transfer 0 1 2

Then query the transaction

query txn_acc_seq 0 0 true

The return includes Committed transaction and Events. From amount, we can see that the amount can be kept at least six decimal places, which is enough to stabilize the currency.

At this point, try to exit and re-enter. Execute account list and return to empty. It seems that the data has been cleared. But when you execute account creation, you create the same account as before, and then query the balance, the impact of the previous recharge is still there, in fact, the data has been tested on the network.

3. Local Running Node

After experiencing the basic operations of Libra, we need to run an authentication node locally.

cargo run -p libra_swarm -- -s 

(Note: Please close the local agent in advance, otherwise you will make an error.)

If the execution is successful, it will be carried out as above../scripts/cli/start_cli_testnet.shEnter the interactive terminal as well.

You can see the nodes running locally, the data is independent of the test environment, and the data will be reset after exit.

4. API Implementation of Call Chain

Because Libra provides RPC calls, we can easily choose a language for development. Here I developed an NPM package based on nodejslibra-weightThe basic API used to encapsulate RPC methods to provide front-end calls

Libra-weight did these things before implementing the interface:

Copy the proto file from the rust source code into the project, and then execute the following code:

protoc --proto_path=./ --proto_path=/usr/local/Cellar/protobuf/3.7.1/include/ --js_out=import
_style=commonjs,binary:. *.proto

Every. proto file gets compiled *. pb. js:

Libra Block Chain Wallet Development Appendix Source Code - LearnDapp Series

Searching for request in proto implements only these interfaces:

Libra Block Chain Wallet Development Appendix Source Code - LearnDapp Series

The interface is then invoked in the wallet application. At this time, the libra-weight publication is put aside and entered into the Libra-wallet. The code structure and the implementation of the call are as follows:
Libra Block Chain Wallet Development Appendix Source Code - LearnDapp Series

Then go to the browser and call the interface, you can see the transaction information of the account.
Libra Block Chain Wallet Development Appendix Source Code - LearnDapp Series

The example uses the official test network address. Of course, you can build your own validation node locally and run the sample code intuitively as described in the previous article. As for the front end of the wallet, many people have done it. You can go to https://github.com/learndapp/… to see the part of the wallet.

Account creation can be done within the Libra terminal, because Libra has not mined, and recharge can be considered as a special kind of transfer transaction or can be completed in the terminal. If you’re going to create an account in your wallet application right now, take a look at this case https://medium.com/kulapoffic…. Of course, it’s just for demonstration. It’s not safe to create an account for someone else or to transfer it over the network. The reasonable solution is to create account + API call in local environment for broadcasting transfer transactions.

Later words

The Libra project has a lot to explore. For example, the MVIR suffix file written by Move, like the sol suffix of Etherfang Solidity, can be called the intelligent contract in Libra. At present, there are many attempts on the application side, such as Block Browser, which can be viewed in the Block Chain Browser section of https://github.com/learndapp/…. Next, I will do more practice and share new discoveries.

The wallet source code mentioned in this article: https://github.com/learndapp/…

This article has been sorted out to the warehouse: https://github.com/learndapp/…

Remember to click Star, which is the best support for me. If you have any questions, please feel free to contact my Wechat Public Number “Block Chain Tile Worker”.

Reference connection

Official documents:

https://developers.libra.org/…
https://developers.libra.org/…
https://developers.libra.org/…
https://developers.libra.org/…

Recommended Today

Implementation of PHP Facades

Example <?php class RealRoute{ public function get(){ Echo’Get me’; } } class Facade{ public static $resolvedInstance; public static $app; public static function __callStatic($method,$args){ $instance = static::getFacadeRoot(); if(!$instance){ throw new RuntimeException(‘A facade root has not been set.’); } return $instance->$method(…$args); } // Get the Facade root object public static function getFacadeRoot() { return static::resolveFacadeInstance(static::getFacadeAccessor()); } protected […]