mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-20 18:44:20 +01:00
initial commit
This commit is contained in:
65
mint/app.py
Normal file
65
mint/app.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import hashlib
|
||||
|
||||
from ecc.curve import secp256k1, Point
|
||||
from flask import Flask, request
|
||||
import os
|
||||
import asyncio
|
||||
|
||||
from mint.ledger import Ledger
|
||||
from mint.migrations import m001_initial
|
||||
|
||||
# Ledger pubkey
|
||||
ledger = Ledger("supersecretprivatekey", "../data/mint")
|
||||
|
||||
|
||||
class MyFlaskApp(Flask):
|
||||
"""
|
||||
We overload the Flask class so we can run a startup script (migration).
|
||||
Stupid Flask.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
async def create_tasks_func():
|
||||
await asyncio.wait([m001_initial(ledger.db)])
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(create_tasks_func())
|
||||
loop.close()
|
||||
|
||||
return super().__init__(*args, **kwargs)
|
||||
|
||||
def run(self, *args, **options):
|
||||
super(MyFlaskApp, self).run(*args, **options)
|
||||
|
||||
|
||||
app = MyFlaskApp(__name__)
|
||||
|
||||
|
||||
@app.route("/keys")
|
||||
def keys():
|
||||
return ledger.get_pubkeys()
|
||||
|
||||
|
||||
@app.route("/mint", methods=["POST"])
|
||||
async def mint():
|
||||
amount = int(request.args.get("amount")) or 64
|
||||
x = int(request.json["x"])
|
||||
y = int(request.json["y"])
|
||||
B_ = Point(x, y, secp256k1)
|
||||
try:
|
||||
promise = await ledger.mint(B_, amount)
|
||||
return promise
|
||||
except Exception as exc:
|
||||
return {"error": str(exc)}
|
||||
|
||||
|
||||
@app.route("/split", methods=["POST"])
|
||||
async def split():
|
||||
proofs = request.json["proofs"]
|
||||
amount = request.json["amount"]
|
||||
output_data = request.json["output_data"]
|
||||
try:
|
||||
fst_promises, snd_promises = await ledger.split(proofs, amount, output_data)
|
||||
return {"fst": fst_promises, "snd": snd_promises}
|
||||
except Exception as exc:
|
||||
return {"error": str(exc)}
|
||||
Reference in New Issue
Block a user