Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
smiled0g committed Jul 4, 2019
1 parent f749962 commit 54559ae
Show file tree
Hide file tree
Showing 98 changed files with 18,881 additions and 9,466 deletions.
5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Band Protocol <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
162 changes: 162 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<div align="center">
<h1>
🦄 Libra Web
</h1>

<p>
<strong>Unofficial Javascript Client for <a href="http://libra.org">Libra Blockchain</a></strong>

[![Code Style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)
[![Dependency Status](https://david-dm.org/bandprotocol/libra-web.svg)](https://david-dm.org/bandprotocol/libra-web)
[![devDependency Status](https://david-dm.org/bandprotocol/libra-web/dev-status.svg)](https://david-dm.org/bandprotocol/libra-web#info=devDependencies)

</p>
</div>

`libra-web` is an unofficial javascript client for [Libra blockchain](http://libra.org). The library allows javascript program to interact with Libra nodes with [protobuf](https://developers.google.com/protocol-buffers/) message through [grpc-web](https://grpc.io/). **It works in both Browser and Node.js environments**. Note that this library performs key management internally. No server required! (except HTTP/2 proxy served through TryLibra.org)

Much of this library borrows the code from [libra-core](https://github.com/perfectmak/libra-core), which does a lot of heavy-lifting already (kudos to [perfectmak](https://github.com/perfectmak)!)

# Installation

## via yarn

```sh
$ yarn add libra-web
```

## via CDN

`Link`

# 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](https://github.com/libra/libra/tree/master/client/src), so you cannot import mnemonic between the two libraries (yet).

```py
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](https://metamask.io) keeps track of account. An `Account` contains its `address`, `public_key`, and `private_key`.

```py
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.

```py
from pylibra import LibraClient

client1 = LibraClient() # Default client connecting to the official testnet through TryLibra.org
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`.

```py
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](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).

```py
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](https://github.com/libra/libra/blob/master/language/stdlib/transaction_scripts/peer_to_peer_transfer.mvir). In the future, this library 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!

```py
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)
```

# HTTP/2 Proxy

Currently [gRPC-web](https://github.com/grpc/grpc-web) requires a proxy to communicate with gRPC-enabled Libra nodes. To run a local proxy, you need [Docker](https://www.docker.com/) and run:

```sh
$ yarn proxy
```

# License

This software is created by [Band Protocol](https://bandprotocol.com) and is released under [the MIT License](https://opensource.org/licenses/MIT).

# Contributing

Any and all contributions are welcome! The process is simple: fork this repo, make your changes, and submit a pull request.
107 changes: 0 additions & 107 deletions Readme.md

This file was deleted.

1 change: 1 addition & 0 deletions example/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
23 changes: 23 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
68 changes: 68 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.<br>
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br>
You will also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.<br>
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.<br>
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br>
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (Webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: https://facebook.github.io/create-react-app/docs/code-splitting

### Analyzing the Bundle Size

This section has moved here: https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size

### Making a Progressive Web App

This section has moved here: https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app

### Advanced Configuration

This section has moved here: https://facebook.github.io/create-react-app/docs/advanced-configuration

### Deployment

This section has moved here: https://facebook.github.io/create-react-app/docs/deployment

### `npm run build` fails to minify

This section has moved here: https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify
Loading

0 comments on commit 54559ae

Please sign in to comment.