A golang client library for Libra blockchain.
Thanks to gopherjs, go-libra is also available as a Javascript client library: gopherjs-libra. It works for both NodeJS and browsers.
It has all cryptographic verification algorithms, including validator-signature-based consensus verification, ledger history accumulator proof, and account state sparse Merkle tree proof, etc.
Compatible with testnet 2020/4/8 (commit hash 718ace82).
- Data models with all necessary cryptographic verification algorithms
- Ledger state: signature-based consensus verification
- Ledger consistency verification: detects reset or hard-forks
- Transaction info and event: Merkle tree accumulator proof
- Transaction list: Merkle tree accumulator proof on a range of transactions
- Transaction signature: ed25519 signature
- Account state: sparse Merkle tree proof
- Events: event list hash based on Merkle tree accumulator
- RPC functions
- Query account states
- Make P2P transaction, and wait for ledger inclusion
- Query transaction list by ledger version
- Query account transaction by sequence number
- Query sent or received event list by account
$ # download the code
$ go get -u github.com/the729/go-libra
$ # build example client
$ cd example/cli_client && go build
$ # see example/cli_client/README.md
$ ./cli_client a ls
Godoc reference to client package and types package.
package main
import (
"context"
"log"
"github.com/the729/go-libra/client"
)
const (
defaultServer = "ac.testnet.libra.org:8000"
waypoint = "0:4d4d0feaa9378069f8fcee71980e142273837e108702d8d7f93a8419e2736f3f"
)
func main() {
c, err := client.New(defaultServer, waypoint)
if err != nil {
log.Fatal(err)
}
defer c.Close()
addrStr := "42f5745128c05452a0c68272de8042b1"
addr := client.MustToAddress(addrStr)
// provenState is cryptographically proven state of account
provenState, err := c.QueryAccountState(context.TODO(), addr)
if err != nil {
log.Fatal(err)
}
if provenState.IsNil() {
log.Printf("Account %s does not exist at version %d.", addrStr, provenState.GetVersion())
return
}
ar, br, err := provenState.GetAccountBlob().GetLibraResources()
if err != nil {
log.Fatal(err)
}
log.Printf("Balance: %d", br.Coin)
log.Printf("Sequence Number: %d", ar.SequenceNumber)
log.Printf("Authentication key: %v", hex.EncodeToString(ar.AuthenticationKey))
}
log.Printf("Get current account sequence of sender...")
seq, err := c.QueryAccountSequenceNumber(context.TODO(), senderAddr)
if err != nil {
log.Fatal(err)
}
log.Printf("... is %d", seq)
rawTxn, err := client.NewRawP2PTransaction(
senderAddr, recvAddr, recvAuthKeyPrefix, seq,
amountMicro, maxGasAmount, gasUnitPrice, expiration,
)
if err != nil {
log.Fatal(err)
}
log.Printf("Submit transaction...")
expectedSeq, err := c.SubmitRawTransaction(context.TODO(), rawTxn, priKey)
if err != nil {
log.Fatal(err)
}
log.Printf("Waiting until transaction is included in ledger...")
err = c.PollSequenceUntil(context.TODO(), senderAddr, expectedSeq, expiration)
if err != nil {
log.Fatal(err)
}
log.Printf("done.")
Several examples are included in example
folder.
- cli_client: A fully functional Libra CLI client
- query_account: Query specific account states
- query_txn_range: Query a range of transactions
- query_txn_by_seq: Query a transaction by specific account and sequence number
- p2p_transaction: Make P2P transaction
- NodeJS examples based on gopherjs-libra
- web_client: a pure front-end libra explorer based on gopherjs-libra. See it in action