|
@@ -0,0 +1,54 @@
|
|
|
|
|
+from flask import Flask, json, request, jsonify
|
|
|
|
|
+from json import JSONEncoder
|
|
|
|
|
+
|
|
|
|
|
+# Here are Iroha dependencies.
|
|
|
|
|
+# Python library generally consists of 3 parts:
|
|
|
|
|
+# Iroha, IrohaCrypto and IrohaGrpc which we need to import:
|
|
|
|
|
+import os
|
|
|
|
|
+from iroha import IrohaCrypto
|
|
|
|
|
+from iroha import Iroha, IrohaGrpc
|
|
|
|
|
+
|
|
|
|
|
+# Here is the information about the environment and admin account information:
|
|
|
|
|
+IROHA_HOST_ADDR = os.getenv('IROHA_HOST_ADDR', '127.0.0.1')
|
|
|
|
|
+IROHA_PORT = os.getenv('IROHA_PORT', '50051')
|
|
|
|
|
+ADMIN_ACCOUNT_ID = os.getenv('ADMIN_ACCOUNT_ID', 'admin@test')
|
|
|
|
|
+ADMIN_PRIVATE_KEY = os.getenv(
|
|
|
|
|
+ 'ADMIN_PRIVATE_KEY', 'f101537e319568c765b2cc89698325604991dca57b9716b58016b253506cab70')
|
|
|
|
|
+
|
|
|
|
|
+# Here we will create user keys
|
|
|
|
|
+user_private_key = IrohaCrypto.private_key()
|
|
|
|
|
+user_public_key = IrohaCrypto.derive_public_key(user_private_key)
|
|
|
|
|
+iroha = Iroha(ADMIN_ACCOUNT_ID)
|
|
|
|
|
+net = IrohaGrpc('{}:{}'.format(IROHA_HOST_ADDR, IROHA_PORT))
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+class MyEncoder(JSONEncoder):
|
|
|
|
|
+ def default(self, o):
|
|
|
|
|
+ return o.__dict__
|
|
|
|
|
+
|
|
|
|
|
+app = Flask(__name__)
|
|
|
|
|
+app.json_encoder = MyEncoder
|
|
|
|
|
+
|
|
|
|
|
+class Asset:
|
|
|
|
|
+ assetId = ""
|
|
|
|
|
+ balance = 0
|
|
|
|
|
+
|
|
|
|
|
+ def __init__(self, assetId = "", balance = 0):
|
|
|
|
|
+ self.assetId = assetId
|
|
|
|
|
+ self.balance = balance
|
|
|
|
|
+
|
|
|
|
|
+@app.get("/accounts/<string:accountId>/assets/")
|
|
|
|
|
+def get_account_assets(accountId): #TODO: add validation and error handling
|
|
|
|
|
+
|
|
|
|
|
+ query = iroha.query('GetAccountAssets', account_id=accountId)
|
|
|
|
|
+ IrohaCrypto.sign_query(query, ADMIN_PRIVATE_KEY)
|
|
|
|
|
+
|
|
|
|
|
+ iroha_response = net.send_query(query)
|
|
|
|
|
+ data = iroha_response.account_assets_response.account_assets
|
|
|
|
|
+
|
|
|
|
|
+ response = []
|
|
|
|
|
+
|
|
|
|
|
+ for asset in data:
|
|
|
|
|
+ response.append(Asset(asset.asset_id, asset.balance))
|
|
|
|
|
+
|
|
|
|
|
+ return jsonify(response)
|