-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
95 lines (80 loc) · 2.48 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from resdb_driver import Resdb
from resdb_driver.crypto import generate_keypair
db_root_url = "localhost:18000"
protocol = "http://"
fetch_all_endpoint = "/v1/transactions"
db = Resdb(db_root_url)
import strawberry
import typing
import ast
from typing import Optional, List
from flask import Flask
from flask_cors import CORS
app = Flask(__name__)
CORS(app) # This will enable CORS for all routes
from strawberry.flask.views import GraphQLView
@strawberry.type
class RetrieveTransaction:
id: str
version: str
amount: int
uri: str
type: str
publicKey: str
operation: str
metadata: typing.Optional["str"]
asset: str
signerPublicKey: str
@strawberry.type
class CommitTransaction:
id: str
@strawberry.input
class PrepareAsset:
operation: str
amount: int
signerPublicKey: str
signerPrivateKey: str
recipientPublicKey: str
asset: str
@strawberry.type
class Query:
@strawberry.field
def getTransaction(self, id: strawberry.ID) -> RetrieveTransaction:
data = db.transactions.retrieve(txid=id)
payload = RetrieveTransaction(
id=data["id"],
version=data["version"],
amount=data["outputs"][0]["amount"],
uri=data["outputs"][0]["condition"]["uri"],
type=data["outputs"][0]["condition"]["details"]["type"],
publicKey=data["outputs"][0]["condition"]["details"]["public_key"],
signerPublicKey=data["inputs"][0]["owners_before"][0],
operation=data["operation"],
metadata=data["metadata"],
asset=str(data["asset"])
)
return payload
@strawberry.type
class Mutation:
@strawberry.mutation
def postTransaction(self, data: PrepareAsset) -> CommitTransaction:
prepared_token_tx = db.transactions.prepare(
operation=data.operation,
signers=data.signerPublicKey,
recipients=[([data.recipientPublicKey], data.amount)],
asset=ast.literal_eval(data.asset),
)
# fulfill the tnx
fulfilled_token_tx = db.transactions.fulfill(prepared_token_tx, private_keys=data.signerPrivateKey)
id = db.transactions.send_commit(fulfilled_token_tx)[4:] # Extract ID
payload = CommitTransaction(
id=id
)
return payload
schema = strawberry.Schema(query=Query, mutation=Mutation)
app.add_url_rule(
"/graphql",
view_func=GraphQLView.as_view("graphql_view", schema=schema),
)
if __name__ == "__main__":
app.run(port="8000")