forked from apache/incubator-resilientdb-graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
188 lines (162 loc) · 5.9 KB
/
Copy pathapp.py
File metadata and controls
188 lines (162 loc) · 5.9 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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 filter import filter_by_keys
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
@strawberry.type
class CommitTransaction:
id: str
@strawberry.input
class PrepareAsset:
operation: str
amount: int
signerPublicKey: str
signerPrivateKey: str
recipientPublicKey: str
asset: str
@strawberry.input
class UpdateAsset:
id: str
operation: typing.Optional["str"]
amount: typing.Optional["int"]
signerPublicKey: str
signerPrivateKey: str
recipientPublicKey: typing.Optional["str"]
asset: typing.Optional["str"]
@strawberry.input
class FilterKeys:
ownerPublicKey: Optional[str]
recipientPublicKey: Optional[str]
@strawberry.type
class Keys:
publicKey: str
privateKey: str
def update(data):
record = db.transactions.retrieve(data.id)
prepared_token_tx = db.transactions.prepare(
operation=record["operation"] if data.operation == "" else data.operation,
signers=data.signerPublicKey,
recipients=[([record["outputs"][0]["condition"]["details"]["public_key"] if data.recipientPublicKey == "" else data.recipientPublicKey], record["outputs"][0]["amount"] if data.amount == "" else data.amount)],
asset=record["asset"] if data.asset == "" else 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
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"],
operation=data["operation"],
metadata=data["metadata"],
asset=str(data["asset"])
)
return payload
@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"],
operation=data["operation"],
metadata=data["metadata"],
asset=str(data["asset"])
)
return payload
@strawberry.field
def getFilteredTransactions(self, filter: Optional[FilterKeys]) -> List[RetrieveTransaction]:
url = f"{protocol}{db_root_url}{fetch_all_endpoint}"
if filter.ownerPublicKey != None:
filter.ownerPublicKey = filter.ownerPublicKey if filter.ownerPublicKey.strip() else None
if filter.recipientPublicKey != None:
filter.recipientPublicKey = filter.recipientPublicKey if filter.recipientPublicKey.strip() else None
json_data = filter_by_keys(url, filter.ownerPublicKey, filter.recipientPublicKey)
records = []
for data in json_data:
try:
records.append(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"],
operation=data["operation"],
metadata=data["metadata"],
asset=str(data["asset"])
))
except Exception as e:
print(e)
return records
@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
@strawberry.mutation
def updateTransaction(self, data: UpdateAsset) -> RetrieveTransaction:
return update(data)
@strawberry.mutation
def updateMultipleTransaction(self, data: List[UpdateAsset]) -> List[RetrieveTransaction]:
result = []
for transaction in data:
result.append(update(transaction))
return result
@strawberry.mutation
def generateKeys(self) -> Keys:
keys = generate_keypair()
payload = Keys(
publicKey=keys.public_key,
privateKey=keys.private_key
)
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")