Last active
July 4, 2019 14:50
-
-
Save korrio/16f7ca4e3907bdb2c8c4de29d2bf5e69 to your computer and use it in GitHub Desktop.
libraservice.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!flask/bin/python | |
from flask import Flask, jsonify, abort, request, make_response, url_for | |
from flask_httpauth import HTTPBasicAuth | |
import simplejson as json | |
from pylibra import LibraClient, LibraWallet | |
from pylibra.transaction import TransferTransaction | |
app = Flask(__name__, static_url_path = "") | |
@app.route('/createWallet', methods = ['GET']) | |
def create_wallet(): | |
client = LibraClient() | |
wallet = LibraWallet() | |
account = wallet.get_account(0) | |
client.mint_with_faucet(account, 10000) | |
account_state = client.get_account_state(account) | |
return json.dumps({ | |
'address': account.address, | |
'mnemonic': wallet.to_mnemonic(), | |
'public_key': account.public_key, | |
'private_key': account.private_key, | |
#'authentication_key': account_state.authentication_key, | |
'balance': account_state.balance, | |
'sequence_number': account_state.sequence_number, | |
'received_events_count': account_state.received_events_count, | |
'sent_events_count': account_state.sent_events_count | |
}) | |
@app.route('/getBalance', methods = ['GET']) | |
def get_balance(): | |
address = request.args.get('address') | |
client = LibraClient() | |
account_state = client.get_account_state(address) | |
return json.dumps({ | |
# 'account': account.public_key, | |
# 'mnemonic': wallet.to_mnemonic(), | |
# 'priv_key': account.private_key, | |
#'authentication_key': account_state.authentication_key, | |
'address': address, | |
'balance': account_state.balance, | |
'sequence_number': account_state.sequence_number, | |
'received_events_count': account_state.received_events_count, | |
'sent_events_count': account_state.sent_events_count | |
}) | |
#@app.route('/transfer/<string:wallet_1>/<string:wallet_2>', methods = ['POST']) | |
@app.route('/transfer', methods = ['POST']) | |
def transfer(): | |
content = request.json | |
fromAddress = content['fromAddress'] | |
mnemonic = content['mnemonic'] | |
toAddress = content['fromAddress'] | |
amount = content['amount'] | |
client = LibraClient() | |
wallet = LibraWallet(mnemonic) | |
#wallet = LibraWallet(str("student deliver dentist cat gorilla sleep proud naive gown fiber awkward weasel")) | |
account1 = wallet.get_account(0) | |
#account2 = wallet.get_account(1) | |
account1_state = client.get_account_state(account1) | |
#account2_state = client.get_account_state(account2) | |
tx2 = TransferTransaction(toAddress, int(amount)) | |
x = client.send_transaction(account1, tx2, max_gas_amount=10000, gas_unit_price=0, expiration_time=17000000000) | |
return json.dumps(content) | |
if __name__ == '__main__': | |
app.run(debug = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment