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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import datetime
|
4 |
+
import hashlib
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
import requests
|
8 |
+
from mychain import Blockchain
|
9 |
+
from mychain import MyChainSend
|
10 |
+
from mychain import MyChainRec
|
11 |
+
from mychain import MyChainTrans
|
12 |
+
import bc_utils
|
13 |
+
#import app_og
|
14 |
+
|
15 |
+
from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
|
16 |
+
|
17 |
+
main_chain='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/chains/'
|
18 |
+
main_balance='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/balance/'
|
19 |
+
main_trans='https://huggingface.co/datasets/Omnibus/blockchain-sim-2/raw/main/transact/'
|
20 |
+
|
21 |
+
#main_nodes='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/node_file1.json'
|
22 |
+
#main_pending='https://huggingface.co/datasets/Omnibus/blockchain-sim/raw/main/pending1.json'
|
23 |
+
token_self = os.environ['HF_TOKEN']
|
24 |
+
pa=os.environ['PASS']
|
25 |
+
|
26 |
+
trans_name = 'trans1.json'
|
27 |
+
#repo_d='Omnibus/static-bin'
|
28 |
+
chain_d='chain1.json'
|
29 |
+
#node_file='node_file.json'
|
30 |
+
#space='blockchain-simulator-dev1'
|
31 |
+
api = HfApi(token=token_self)
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
repo = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[0]
|
36 |
+
name = main_balance.split('datasets/',1)[1].split('/raw',1)[0].split('/',1)[1]
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
############################## BLOCKCHAIN END ###############################
|
42 |
+
|
43 |
+
def checkp(inp):
|
44 |
+
if inp == pa:
|
45 |
+
return gr.update(visible=False), gr.update(visible=True)
|
46 |
+
elif inp != pa:
|
47 |
+
return gr.update(visible=True), gr.update(visible=False)
|
48 |
+
|
49 |
+
############################## MYCHAIN START ###############################
|
50 |
+
|
51 |
+
|
52 |
+
|
53 |
+
def get_my_chain_send(sender_name=None):
|
54 |
+
global mychain_send
|
55 |
+
global mychain_trans
|
56 |
+
mes_error = ""
|
57 |
+
try:
|
58 |
+
r = requests.get(f'{main_balance}{sender_name}.json')
|
59 |
+
|
60 |
+
#print (f'r={r.text}')
|
61 |
+
mychain_send = MyChainSend(chain_load=main_balance,load=r.text)
|
62 |
+
|
63 |
+
response = {'chain': mychain_send.chain,
|
64 |
+
'length': len(mychain_send.chain)}
|
65 |
+
#print (f'response={response}')
|
66 |
+
try:
|
67 |
+
d = requests.get(f'{main_trans}{trans_name}')
|
68 |
+
mychain_trans = MyChainTrans(chain_load=main_trans,load=d.text)
|
69 |
+
except Exception as e:
|
70 |
+
print (f'error1:: {e}')
|
71 |
+
try:
|
72 |
+
mychain_trans = MyChainTrans(chain_load=main_trans,create=trans_name)
|
73 |
+
print ("this worked")
|
74 |
+
except Exception as ee:
|
75 |
+
print (f'error2:: {ee}')
|
76 |
+
pass
|
77 |
+
try:
|
78 |
+
response_trans = {'chain': mychain_trans.chain,
|
79 |
+
'length': len(mychain_trans.chain)}
|
80 |
+
print(f'response_trans:: {response_trans}')
|
81 |
+
except Exception as e:
|
82 |
+
print (e)
|
83 |
+
pass
|
84 |
+
|
85 |
+
message = f"Blockchain loaded from: {main_balance}{sender_name}.json"
|
86 |
+
return response,message
|
87 |
+
except Exception:
|
88 |
+
message = f"Error loading from: {sender_name}"
|
89 |
+
print (message)
|
90 |
+
return ["Error Loading Chain"],message
|
91 |
+
|
92 |
+
def get_my_chain_rec(recipient_name=None):
|
93 |
+
global mychain_rec
|
94 |
+
|
95 |
+
try:
|
96 |
+
r = requests.get(f'{main_balance}{recipient_name}.json')
|
97 |
+
mychain_rec = MyChainRec(chain_load=main_balance,load=r.text)
|
98 |
+
response = {'chain': mychain_rec.chain,
|
99 |
+
'length': len(mychain_rec.chain)}
|
100 |
+
message = f"Blockchain loaded from: {main_balance}{recipient_name}.json"
|
101 |
+
return response,message
|
102 |
+
except Exception:
|
103 |
+
try:
|
104 |
+
mychain_rec = MyChainRec(chain_load=main_balance,create=recipient_name)
|
105 |
+
response = {'chain': mychain_rec.chain,
|
106 |
+
'length': len(mychain_rec.chain)}
|
107 |
+
|
108 |
+
message = f"Blockchain loaded from: {main_balance}{recipient_name}.json"
|
109 |
+
return response,message
|
110 |
+
except Exception:
|
111 |
+
message = f"Error loading from: {recipient_name}"
|
112 |
+
return ["Error Loading Chain"],message
|
113 |
+
|
114 |
+
def display_chain_send():
|
115 |
+
response = {'chain': mychain_send.chain,
|
116 |
+
'length': len(mychain_send.chain)}
|
117 |
+
return response
|
118 |
+
|
119 |
+
def display_chain_rec():
|
120 |
+
response = {'chain': mychain_rec.chain,
|
121 |
+
'length': len(mychain_rec.chain)}
|
122 |
+
return response
|
123 |
+
|
124 |
+
def valid_send():
|
125 |
+
valid,ind,mes = mychain_send.chain_valid(mychain_send.chain)
|
126 |
+
if valid:
|
127 |
+
response = 'The Blockchain is valid.'
|
128 |
+
z=True
|
129 |
+
else:
|
130 |
+
response = f'Sender Blockchain is not valid. {mes} at Index {ind}'
|
131 |
+
z=False
|
132 |
+
return response,z,mes,ind
|
133 |
+
|
134 |
+
def valid_rec():
|
135 |
+
valid,ind,mes = mychain_rec.chain_valid(mychain_rec.chain)
|
136 |
+
if valid:
|
137 |
+
response = 'The Blockchain is valid.'
|
138 |
+
z=True
|
139 |
+
else:
|
140 |
+
response = f'Blockchain is not valid. {mes} at Index {ind}'
|
141 |
+
z=False
|
142 |
+
return response,z, mes, ind
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
def mychain_mine_block_send(balance, chain_r=None,chain_n=None):
|
147 |
+
previous_block = mychain_send.print_previous_block()
|
148 |
+
previous_proof = previous_block['proof']
|
149 |
+
proof = mychain_send.proof_of_work(previous_proof)
|
150 |
+
previous_hash = mychain_send.hash(previous_block)
|
151 |
+
block = mychain_send.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n)
|
152 |
+
|
153 |
+
response = {'message': 'A block is MINED',
|
154 |
+
'index': block['index'],
|
155 |
+
'timestamp': block['timestamp'],
|
156 |
+
'balance': block['balance'],
|
157 |
+
'proof': block['proof'],
|
158 |
+
'previous_hash': block['previous_hash']
|
159 |
+
}
|
160 |
+
|
161 |
+
message = "A block is MINED"
|
162 |
+
show_chain = display_chain_send()
|
163 |
+
|
164 |
+
if len(mychain_send.chain) > 1000:
|
165 |
+
mychain_send.reset()
|
166 |
+
response = None
|
167 |
+
show_chain=display_chain_send()
|
168 |
+
message = "New Chain Created at Max 20 Blocks"
|
169 |
+
|
170 |
+
return response, show_chain,message
|
171 |
+
|
172 |
+
def mychain_mine_block_trans(balance, chain_r=None,chain_n=None):
|
173 |
+
previous_block = mychain_trans.print_previous_block()
|
174 |
+
previous_proof = previous_block['proof']
|
175 |
+
proof = mychain_trans.proof_of_work(previous_proof)
|
176 |
+
previous_hash = mychain_trans.hash(previous_block)
|
177 |
+
block = mychain_trans.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n)
|
178 |
+
|
179 |
+
response = {'message': 'A block is MINED',
|
180 |
+
'index': block['index'],
|
181 |
+
'timestamp': block['timestamp'],
|
182 |
+
'balance': block['balance'],
|
183 |
+
'proof': block['proof'],
|
184 |
+
'previous_hash': block['previous_hash']
|
185 |
+
}
|
186 |
+
|
187 |
+
message = "A block is MINED"
|
188 |
+
show_chain = display_chain_send()
|
189 |
+
|
190 |
+
if len(mychain_trans.chain) > 1000:
|
191 |
+
mychain_trans.reset()
|
192 |
+
response = None
|
193 |
+
show_chain=display_chain_send()
|
194 |
+
message = "New Chain Created at Max 20 Blocks"
|
195 |
+
|
196 |
+
return response, show_chain,message
|
197 |
+
|
198 |
+
|
199 |
+
|
200 |
+
def mychain_mine_block_rec(balance, chain_r=None,chain_n=None):
|
201 |
+
previous_block = mychain_rec.print_previous_block()
|
202 |
+
previous_proof = previous_block['proof']
|
203 |
+
proof = mychain_rec.proof_of_work(previous_proof)
|
204 |
+
previous_hash = mychain_rec.hash(previous_block)
|
205 |
+
block = mychain_rec.create_block(balance, proof, previous_hash,len(bc_utils.blockchain.chain)+1,chain_r,chain_n)
|
206 |
+
|
207 |
+
response = {'message': 'A block is MINED',
|
208 |
+
'index': block['index'],
|
209 |
+
'timestamp': block['timestamp'],
|
210 |
+
'balance': block['balance'],
|
211 |
+
'proof': block['proof'],
|
212 |
+
'previous_hash': block['previous_hash']
|
213 |
+
}
|
214 |
+
message = "A block is MINED"
|
215 |
+
show_chain = display_chain_rec()
|
216 |
+
if len(mychain_rec.chain) > 1000:
|
217 |
+
mychain_rec.reset()
|
218 |
+
response = None
|
219 |
+
show_chain=display_chain_rec()
|
220 |
+
message = "New Chain Created at Max 20 Blocks"
|
221 |
+
return response, show_chain, message
|
222 |
+
|
223 |
+
def merge_trans():
|
224 |
+
trans_bx = []
|
225 |
+
previous_post_send = mychain_send.print_previous_block()
|
226 |
+
previous_post_rec = mychain_rec.print_previous_block()
|
227 |
+
|
228 |
+
send_block = {
|
229 |
+
'role': 'Sender',
|
230 |
+
'name': previous_post_send['transactions'][0]['sender'],
|
231 |
+
'index': previous_post_send['index'],
|
232 |
+
'timestamp': previous_post_send['timestamp'],
|
233 |
+
'recipient': previous_post_send['transactions'][0]['recipient'],
|
234 |
+
'amount': previous_post_send['transactions'][0]['amount'],
|
235 |
+
'balance': previous_post_send['balance'],
|
236 |
+
'proof': previous_post_send['proof'],
|
237 |
+
'previous_hash': previous_post_send['previous_hash'],
|
238 |
+
'block': len(bc_utils.blockchain.chain)+1}
|
239 |
+
rec_block = {
|
240 |
+
'role': 'Recipient',
|
241 |
+
'name': previous_post_rec['transactions'][0]['recipient'],
|
242 |
+
'index': previous_post_rec['index'],
|
243 |
+
'timestamp': previous_post_rec['timestamp'],
|
244 |
+
'sender': previous_post_rec['transactions'][0]['sender'],
|
245 |
+
'amount': previous_post_rec['transactions'][0]['amount'],
|
246 |
+
'balance': previous_post_rec['balance'],
|
247 |
+
'proof': previous_post_rec['proof'],
|
248 |
+
'previous_hash': previous_post_rec['previous_hash'],
|
249 |
+
'block': len(bc_utils.blockchain.chain)+1}
|
250 |
+
|
251 |
+
trans_bx.append(send_block)
|
252 |
+
trans_bx.append(rec_block)
|
253 |
+
return trans_bx
|
254 |
+
|
255 |
+
|
256 |
+
|
257 |
+
|
258 |
+
def issue_tokens(send,rec,amount):
|
259 |
+
response_send={}
|
260 |
+
response_rec={}
|
261 |
+
show_chain_send={}
|
262 |
+
show_chain_rec={}
|
263 |
+
data_send=None
|
264 |
+
data_rec=None
|
265 |
+
rec_send=None
|
266 |
+
rec_drop=None
|
267 |
+
message_send=None
|
268 |
+
message_rec=None
|
269 |
+
trans_data=None
|
270 |
+
trans_mes=None
|
271 |
+
balance_send = 0
|
272 |
+
balance_rec = 0
|
273 |
+
mes = "blank message"
|
274 |
+
try:
|
275 |
+
b_mes,z=bc_utils.valid()
|
276 |
+
if z == True:
|
277 |
+
try:
|
278 |
+
response,message = get_my_chain_send(send)
|
279 |
+
#print (f'response:{response}::message{message}')
|
280 |
+
#r = requests.get(f'{main_balance}{send}.json')
|
281 |
+
lod=response
|
282 |
+
#lod = json.loads(response)
|
283 |
+
#print (lod)
|
284 |
+
p=True
|
285 |
+
except Exception as e:
|
286 |
+
lod=[]
|
287 |
+
p=False
|
288 |
+
mes = f"Sender has no wallet {e}"
|
289 |
+
pass
|
290 |
+
mes1,val,mes2,ind = valid_send()
|
291 |
+
print (f'val:: {val}')
|
292 |
+
if val == False:
|
293 |
+
p=False
|
294 |
+
mes = f"Sender: {send} Blockchain is not valid. {mes2} at Index {ind}"
|
295 |
+
#mes = "Blockchain is not valid."
|
296 |
+
|
297 |
+
#if val == True:
|
298 |
+
if p==True:
|
299 |
+
try:
|
300 |
+
balance = lod["chain"][-1]["balance"]
|
301 |
+
except Exception:
|
302 |
+
balance = 0
|
303 |
+
p=False
|
304 |
+
#print (balance)
|
305 |
+
balance_send =int(balance)-int(amount)
|
306 |
+
if balance_send >=0:
|
307 |
+
p=True
|
308 |
+
response,message = get_my_chain_rec(rec)
|
309 |
+
lod_rec = response
|
310 |
+
mes1,val,mes2,ind = valid_rec()
|
311 |
+
print (f'val:: {val}')
|
312 |
+
if val == False:
|
313 |
+
p=False
|
314 |
+
mes = f"Recipient: {rec} Blockchain is not valid. {mes2} at Index {ind}"
|
315 |
+
#mes = "Blockchain is not valid."
|
316 |
+
#print (lod_rec)
|
317 |
+
if val == True:
|
318 |
+
try:
|
319 |
+
balance = lod_rec["chain"][-1]["balance"]
|
320 |
+
#print (balance)
|
321 |
+
except Exception:
|
322 |
+
p=False
|
323 |
+
balance = 0
|
324 |
+
balance_rec =int(balance)+int(amount)
|
325 |
+
|
326 |
+
if balance_send < 0:
|
327 |
+
mes ="Not enough tokens"
|
328 |
+
p = False
|
329 |
+
|
330 |
+
print(mes)
|
331 |
+
|
332 |
+
if z==False:
|
333 |
+
mes = b_mes
|
334 |
+
p=False
|
335 |
+
except Exception as e:
|
336 |
+
mes = f"Blockchain not loaded? {e}"
|
337 |
+
p=False
|
338 |
+
print (mes)
|
339 |
+
|
340 |
+
if p==False:
|
341 |
+
return (mes, p,None,None,None,None,None,None,None,None,None,None,None)
|
342 |
+
|
343 |
+
#return (mes, p,{},{},{},{},{},{},None,None,None,None,{})
|
344 |
+
#json, json, json, json, dataframe, dataframe, textbox, textbox, dropdown, dropdown, dataframe
|
345 |
+
if p==True:
|
346 |
+
|
347 |
+
|
348 |
+
mychain_send.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_send}")
|
349 |
+
mychain_trans.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_send}")
|
350 |
+
|
351 |
+
message_send = "Transaction Added to Pool"
|
352 |
+
data_send = pd.DataFrame(mychain_send.pending_transactions)
|
353 |
+
mychain_rec.new_transaction(f"{send}",f"{rec}",f"{amount}",f"{balance_rec}")
|
354 |
+
message_rec = "Transaction Added to Pool"
|
355 |
+
data_rec = pd.DataFrame(mychain_rec.pending_transactions)
|
356 |
+
response_send, show_chain_send, message_send = mychain_mine_block_send(balance_send, chain_r=None,chain_n=send)
|
357 |
+
|
358 |
+
mychain_mine_block_trans(balance_send, chain_r=None,chain_n=send)
|
359 |
+
|
360 |
+
response_rec, show_chain_rec, message_rec = mychain_mine_block_rec(balance_rec, chain_r=None,chain_n=rec)
|
361 |
+
mes = (f'Send: {message_send} :: Recieve: {message_rec}')
|
362 |
+
_,rec_send=update_send_list()
|
363 |
+
_,rec_drop=update_rec_list()
|
364 |
+
trans_bx = merge_trans()
|
365 |
+
trans_data, mes = bc_utils.bc_transactions(trans_bx)
|
366 |
+
return (mes, p, response_send, response_rec, show_chain_send, show_chain_rec, data_send, data_rec, message_send, message_rec, rec_send, rec_drop,trans_data)
|
367 |
+
|
368 |
+
#global send_list
|
369 |
+
#global rec_list
|
370 |
+
|
371 |
+
def create_new_chain(address):
|
372 |
+
address = str(address.strip("b").strip("'"))
|
373 |
+
mychain_rec = MyChainRec(chain_load=main_balance,create=address)
|
374 |
+
response = {'chain': mychain_rec.chain,
|
375 |
+
'length': len(mychain_rec.chain)}
|
376 |
+
|
377 |
+
message = f"Blockchain loaded from: {main_balance}{address}.json"
|
378 |
+
send_list,send_drop = update_send_list()
|
379 |
+
rec_list, rec_drop = update_rec_list()
|
380 |
+
|
381 |
+
return response,message,send_drop,rec_drop
|
382 |
+
############################## MYCHAIN END ###############################
|
383 |
+
|
384 |
+
def res_source():
|
385 |
+
block = {'index': 1,
|
386 |
+
'timestamp': str(datetime.datetime.now()),
|
387 |
+
'transactions': [],
|
388 |
+
'balance': 10000000000000000000,
|
389 |
+
'proof': 1,
|
390 |
+
'previous_hash': 0}
|
391 |
+
|
392 |
+
pending_transactions_x = []
|
393 |
+
pending_transactions_x.append(block)
|
394 |
+
json_object = json.dumps(pending_transactions_x, indent=4)
|
395 |
+
with open("tmp_send.json", "w") as outfile:
|
396 |
+
outfile.write(json_object)
|
397 |
+
try:
|
398 |
+
api.upload_file(
|
399 |
+
path_or_fileobj="tmp_send.json",
|
400 |
+
path_in_repo="balance/__Source__.json",
|
401 |
+
repo_id=main_balance.split('datasets/',1)[1].split('/raw',1)[0],
|
402 |
+
token=token_self,
|
403 |
+
repo_type="dataset",
|
404 |
+
)
|
405 |
+
os.remove("tmp_send.json")
|
406 |
+
return "__Source__ Reset"
|
407 |
+
except Exception as e:
|
408 |
+
return e
|
409 |
+
|
410 |
+
|
411 |
+
def update_send_list():
|
412 |
+
f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="dataset"))
|
413 |
+
send_list =[]
|
414 |
+
for i,ea in enumerate(f_ist):
|
415 |
+
if "balance/" in ea:
|
416 |
+
try:
|
417 |
+
send_list.append(ea.split("/",1)[1].split(".",1)[0])
|
418 |
+
except Exception:
|
419 |
+
pass
|
420 |
+
return send_list, gr.Dropdown.update(label="Sender", choices=[f for f in send_list])
|
421 |
+
send_list,send_drop = update_send_list()
|
422 |
+
|
423 |
+
def update_rec_list():
|
424 |
+
f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="dataset"))
|
425 |
+
rec_list =[]
|
426 |
+
for i,ea in enumerate(f_ist):
|
427 |
+
if "balance/" in ea:
|
428 |
+
try:
|
429 |
+
if not "__Source__" in ea:
|
430 |
+
rec_list.append(ea.split("/",1)[1].split(".",1)[0])
|
431 |
+
except Exception:
|
432 |
+
pass
|
433 |
+
return rec_list, gr.Dropdown.update(label="Recipient", choices=[f for f in rec_list])
|
434 |
+
rec_list, rec_drop = update_rec_list()
|
435 |
+
|
436 |
+
with gr.Blocks() as bc:
|
437 |
+
with gr.Row(visible=True) as invalid:
|
438 |
+
pass_box = gr.Textbox()
|
439 |
+
pass_btn = gr.Button()
|
440 |
+
|
441 |
+
with gr.Box(visible=False) as valida:
|
442 |
+
gr.Markdown("""<h1><center>Blockchain Simulator<br><h3>(Transactions have no value)<br><h4>Chain will reset at 20 blocks""")
|
443 |
+
#blockchain = gr.State()
|
444 |
+
with gr.Row():
|
445 |
+
with gr.Tab("OG"):
|
446 |
+
with gr.Row():
|
447 |
+
with gr.Column():
|
448 |
+
with gr.Accordion(label="Load",open=False):
|
449 |
+
with gr.Row():
|
450 |
+
chain_repo=gr.Textbox(label="repo/name")
|
451 |
+
chain_n=gr.Textbox(label="Chain file")
|
452 |
+
with gr.Row():
|
453 |
+
in_chain_btn=gr.Button("Load Chain")
|
454 |
+
create_bc = gr.Button("Create New Blockchain")
|
455 |
+
#send=gr.Textbox(label="Sender")
|
456 |
+
send = gr.Dropdown(label="Sender", choices=[f for f in send_list], value = "Bank")
|
457 |
+
rec=gr.Dropdown(label="Recipient", choices=[f for f in rec_list], allow_custom_value=True)
|
458 |
+
am=gr.Textbox(label="Amount")
|
459 |
+
send_trans=gr.Button("Post Transaction")
|
460 |
+
mine_b = gr.Button("Mine Block")
|
461 |
+
check = gr.Button("Check Chain")
|
462 |
+
check_trans = gr.Button("Check Transactions")
|
463 |
+
check_all = gr.Button("Check All")
|
464 |
+
with gr.Column():
|
465 |
+
block_text = gr.Textbox()
|
466 |
+
trans_data = gr.Dataframe()
|
467 |
+
json_out = gr.JSON()
|
468 |
+
chain_json = gr.JSON()
|
469 |
+
with gr.Accordion("Nodes", open=False):
|
470 |
+
with gr.Row():
|
471 |
+
this_space=gr.Textbox(label="This Repo/Space")
|
472 |
+
with gr.Row():
|
473 |
+
node_repo=gr.Textbox(label="Node Repo")
|
474 |
+
node_space=gr.Textbox(label="Node Space")
|
475 |
+
node_file=gr.Textbox(label="Node File")
|
476 |
+
node_add=gr.Button("Add Node")
|
477 |
+
with gr.Accordion("Tokens", open=False):
|
478 |
+
with gr.Row():
|
479 |
+
with gr.Column():
|
480 |
+
issue_btn=gr.Button()
|
481 |
+
with gr.Column():
|
482 |
+
with gr.Row():
|
483 |
+
reset_blockchain=gr.Button("Reset Main Blockchain")
|
484 |
+
reset_source=gr.Button("Reset __Source__")
|
485 |
+
with gr.Row():
|
486 |
+
reset_sender=gr.Button("Reset Sender")
|
487 |
+
reset_recieve=gr.Button("Reset Recipient")
|
488 |
+
out_box_bool=gr.Textbox()
|
489 |
+
with gr.Row():
|
490 |
+
with gr.Column():
|
491 |
+
block_text_send = gr.Textbox()
|
492 |
+
trans_data_send = gr.Dataframe()
|
493 |
+
json_out_send = gr.JSON()
|
494 |
+
chain_json_send = gr.JSON()
|
495 |
+
with gr.Column():
|
496 |
+
block_text_rec = gr.Textbox()
|
497 |
+
trans_data_rec = gr.Dataframe()
|
498 |
+
json_out_rec = gr.JSON()
|
499 |
+
chain_json_rec = gr.JSON()
|
500 |
+
with gr.Tab("BC"):
|
501 |
+
with gr.Row():
|
502 |
+
with gr.Tab("Gen Wal"):
|
503 |
+
gen_wal_btn=gr.Button()
|
504 |
+
seed = gr.Textbox(label='Seed Phrase')
|
505 |
+
img1=gr.Pil(label='Private Key')
|
506 |
+
out1 = gr.Textbox(label='Private Key',max_lines=4)
|
507 |
+
img2=gr.Pil(label='Public Key')
|
508 |
+
out2 = gr.Textbox(label='Public Key',max_lines=4)
|
509 |
+
img3=gr.Pil(label='Address')
|
510 |
+
out3 = gr.Textbox(label='Address')
|
511 |
+
with gr.Tab("Encrypt"):
|
512 |
+
rsa_to_enc = gr.Textbox(label="txt to encrypt")
|
513 |
+
pub_key_in = gr.Image(label="Public Key", type="filepath")
|
514 |
+
priv_key_in1 = gr.Image(label="Private Key(sig)", type="filepath")
|
515 |
+
rsa_enc_btn = gr.Button("RSA Encrypt")
|
516 |
+
rsa_enc_mes = gr.Textbox(label="encoded", max_lines=4)
|
517 |
+
qr_enc_mes = gr.Image(type="filepath")
|
518 |
+
with gr.Tab("Decrypt"):
|
519 |
+
mes_in = gr.Image(label="Message", type="filepath")
|
520 |
+
priv_key_in = gr.Image(label="Private Key", type="filepath")
|
521 |
+
rsa_dec_btn = gr.Button("RSA Decrypt")
|
522 |
+
rsa_dec_mes = gr.Textbox(label="decoded")
|
523 |
+
|
524 |
+
|
525 |
+
|
526 |
+
def res_bc():
|
527 |
+
bc_utils.blockchain.reset(create="chain1.json")
|
528 |
+
return ("Main Blockchain Reset")
|
529 |
+
def res_send(send):
|
530 |
+
mychain_send = MyChainSend(chain_load=main_balance,create=send)
|
531 |
+
return ("Sender Blockchain Reset")
|
532 |
+
|
533 |
+
def deep_trans():
|
534 |
+
chain1=mychain_send.chain
|
535 |
+
chain2=bc_utils.blockchain.chain
|
536 |
+
valid,ind,mes = mychain_send.deep_valid_send(chain1,chain2)
|
537 |
+
|
538 |
+
if valid:
|
539 |
+
response = 'The Blockchain is valid.'
|
540 |
+
z=True
|
541 |
+
else:
|
542 |
+
response = f'Blockchain is not valid. {mes} at Index {ind}'
|
543 |
+
z=False
|
544 |
+
return response,z
|
545 |
+
|
546 |
+
def mine_block(chain_r=None,chain_n=None):
|
547 |
+
try:
|
548 |
+
a,b,c,d = bc_utils.mine_block(chain_r=None,chain_n=None)
|
549 |
+
mychain_trans.reset(create=trans_name)
|
550 |
+
return a,b,c,d
|
551 |
+
except Exception as e:
|
552 |
+
print (e)
|
553 |
+
return e,None,None,e
|
554 |
+
|
555 |
+
|
556 |
+
gen_wal_btn.click(app_og.generate_keys,None,[out2,out1, img3,out3,img1,img2]).then(create_new_chain,out3,[json_out,block_text,send,rec]).then(app_og.test_fn,[img1,img2],[priv_key_in,pub_key_in,priv_key_in1])
|
557 |
+
rsa_enc_btn.click(app_og.encrypt_text,[rsa_to_enc,pub_key_in,priv_key_in1,out3],[rsa_enc_mes,qr_enc_mes]).then(app_og.test_fn2,qr_enc_mes,mes_in)
|
558 |
+
rsa_dec_btn.click(app_og.decrypt_text,[mes_in,priv_key_in],rsa_dec_mes)
|
559 |
+
|
560 |
+
check_trans.click(deep_trans,None,[block_text,out_box_bool])
|
561 |
+
|
562 |
+
reset_sender.click(res_send,send,block_text)
|
563 |
+
reset_blockchain.click(res_bc,None,block_text)
|
564 |
+
reset_source.click(res_source,None,block_text)
|
565 |
+
|
566 |
+
send_trans.click(issue_tokens,[send,rec,am],[block_text,out_box_bool,json_out_send,json_out_rec,chain_json_send,chain_json_rec,trans_data_send,trans_data_rec,block_text_send,block_text_rec,send,rec,trans_data])
|
567 |
+
issue_btn.click(issue_tokens,[send,rec,am],[block_text,out_box_bool,json_out_send,json_out_rec,chain_json_send,chain_json_rec,trans_data_send,trans_data_rec,block_text_send,block_text_rec,send,rec,trans_data])
|
568 |
+
#issue_btn.click(issue_tokens,[send,rec,am],[block_text,out_box_bool,json_out_send])
|
569 |
+
|
570 |
+
node_add.click(bc_utils.add_node,[this_space,node_repo,node_space,node_file],block_text)
|
571 |
+
pass_btn.click(checkp,pass_box,[invalid,valida])
|
572 |
+
in_chain_btn.click(bc_utils.get_chain,[chain_repo,chain_n],[chain_json,block_text])
|
573 |
+
create_bc.click(bc_utils.create_chain,[chain_n],[block_text,json_out,chain_json])
|
574 |
+
check.click(bc_utils.valid,None,[block_text,out_box_bool])
|
575 |
+
#check_all.click(sort_valid,None,block_text)
|
576 |
+
|
577 |
+
#send_trans.click(bc_transactions,[send,rec,am],[trans_data,block_text,send,rec,am])
|
578 |
+
mine_b.click(mine_block,[chain_repo,chain_n],[json_out,chain_json,trans_data,block_text])
|
579 |
+
|
580 |
+
|
581 |
+
bc.launch(enable_queue=False)
|