-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSigmaParticleInterface.py
413 lines (370 loc) · 18.1 KB
/
SigmaParticleInterface.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import json, ast, os, time, subprocess, json_tools
import file_tools
import config_tools
import AtomicityInterface
import hashlib
py = "python3 -u "
SigmaParticlePath = "Ergo/SigmaParticle/"
AtomicSwapECCPath = "Ergo/SigmaParticle/AtomicMultiSigECC/py/deploy.py " #TODO Ergo Specific
AtomicSwapPath = "Ergo/SigmaParticle/AtomicMultiSig/"
#def SigmaParticle_addNodeEndpoint(nodeurl, accountName):
# nodesmax = 64 #arbitrary limit
# filepath = SigmaParticlePath + accountName + "/.env"
# apiURL_label = "apiURL"
# index = 0
# while index < nodesmax
# if os.path.isfile(filepath):
# if config_tools.valFromConf(filepath, apiURL_label) == None:
# cmd = \
# "echo " + "\'\""
# else:
def SigmaParticle_newFrame(swapName):
cmd = "cd " + str(SigmaParticlePath) + " && ./new_frame " + str(swapName)
file_tools.clean_file_open("newframesigmaparticlescriptdebug", "w" , cmd)
os.popen(cmd).read()
def is_json(myjson):
try:
json.loads(myjson)
except ValueError as e:
return False
return True
def SigmaParticle_box_to_addr(boxId, swapName, password=""):
tries = 20
file_tools.clean_mkdir(SigmaParticlePath + swapName) #this might need to be created here sooner
while tries > 0:
try:
tree = SigmaParticle_getTreeFromBox(boxId, swapName, password=password).replace("\n", "")
if tree != None and tree != "":
hashedtree = str(hashlib.sha256(tree.encode()).hexdigest())#for deterministic proper size filename
file_tools.clean_mkdir(SigmaParticlePath + "/treeAddrs")
treeToAddrCmd = \
"cd " + SigmaParticlePath + "treeToAddr && ./deploy.sh " + \
tree + " " + \
"../treeAddrs/" + hashedtree + " " + \
password
# file_tools.clean_file_open("treeToAddrScriptTestDebug", "w", treeToAddrCmd)
res = os.popen(treeToAddrCmd).read()
if is_json(res) == True:
addr = json.loads(res)["address"]
return addr
else:
tries = tries - 1
time.sleep(15)
continue
except ValueError as e:
print(e)
time.sleep(15)
tries = tries - 1
continue
return "attempts exhausted looking for this box: " + boxId
def is_file_contents_int(file_path):
try:
with open(file_path, 'r') as file:
contents = file.read().strip()
if not contents: #checks for empty file
return False
# Split contents into individual elements assuming they are space-separated
elements = contents.split()
# Check if all elements are integers
for element in elements:
if not element.isdigit() and not (element.startswith('-') and element[1:].isdigit()):
return False
return True
except Exception as e:
print(f"An error occurred: {e}")
return False
def SigmaParticle_CheckLockTimeAtomicSchnorr(swapName, boxId, password=""):
lockHeightPath = SigmaParticlePath + swapName + "/lockHeight"
if os.path.isfile(lockHeightPath) == False:
while is_file_contents_int(lockHeightPath) == False:
lockHeightCMD = \
"cd " + SigmaParticlePath + "boxConstantByIndex && ./deploy.sh " + boxId + \
" 8 ../" + swapName + "/lockHeight " + password
# file_tools.clean_file_open("lockHeightScriptDebug", "w" , lockHeightCMD)
print(os.popen(lockHeightCMD).read())
time.sleep(5)
currentHeightCMD = \
"cd " + SigmaParticlePath + "currentHeight && ./deploy.sh ../../../" + \
swapName + "/localChain_currentHeight " + password
# file_tools.clean_file_open("currentHeightCMDDeubug", "w", currentHeightCMD)
print(os.popen(currentHeightCMD).read())
if \
os.path.isfile(SigmaParticlePath + swapName + "/lockHeight") == True and \
os.path.isfile(swapName + "/localChain_currentHeight") == True:
lockHeight = file_tools.clean_file_open(SigmaParticlePath + swapName + "/lockHeight", "r")
currentHeight = file_tools.clean_file_open(swapName + "/localChain_currentHeight", "r")
if currentHeight.isnumeric() == True and lockHeight.isnumeric() == True:
if int(currentHeight) <= int(lockHeight):
return int(lockHeight) - int(currentHeight) + 1 #plus 1 because currently contract checks for GREATER THAN lock height
#TODO: verify this
else:
return 0
else:
print("error: currentHeight or lockHeight is not numeric.\n currentHeight:", currentHeight, "lockHeight:", lockHeight)
return "error: currentHeight or lockHeight is not numeric.\n currentHeight:" + currentHeight + "lockHeight:" + lockHeight
else:
print("/localChain_lockHeight or /localChain_currentHeight files not found")
return "/localChain_lockHeight or /localChain_currentHeight files not found"
#This function should be used for building an INITIATOR's atomic schnorr contract.
def BuildAtomicSchnorrContract(initiatorMasterJSONPath, refundDuration_BLOCKS, swapName, valueERGO):
j = json.loads(file_tools.clean_file_open(initiatorMasterJSONPath, "r"))
krG = ast.literal_eval(j["krG"])
srG = ast.literal_eval(j["srG"])
ssG = ast.literal_eval(j["ssG"])
ksG = ast.literal_eval(j["ksG"])
responderChainPubKey = j["ResponderErgoAddr"]
initiatorEIP3Secret = j["InitiatorEIP3Secret"]
cmd = "cd " + str(SigmaParticlePath) + " && ./new_frame " + str(swapName) + \
" && cd " + str(swapName) + " && echo " + \
"'" + \
"senderEIP3Secret=" + str(initiatorEIP3Secret) + "\n" + \
"receiverAddr=\"" + str(responderChainPubKey) + "\"\n" + \
"ergoAmount=" + str(valueERGO) + "\n" +\
"refundDuration=" + str(refundDuration_BLOCKS) + "\n" + \
"krGX=" + str(krG[0]) + "\n" +\
"krGY=" + str(krG[1]) + "\n" +\
"ksGX=" + str(ksG[0]) + "\n" +\
"ksGY=" + str(ksG[1]) + "\n" +\
"srGX=" + str(srG[0]) + "\n" +\
"srGY=" + str(srG[1]) + "\n" +\
"ssGX=" + str(ssG[0]) + "\n" +\
"ssGY=" + str(ssG[1]) + "\n" +\
"'" + \
" >> .env"
createContract = os.popen(cmd).read()
employScriptsCMD = "cp " + AtomicSwapPath + "py/main.py " + SigmaParticlePath + swapName + "/py/main.py"
os.popen(employScriptsCMD).read()
#This function should be used for RE-CONSTRUCTING the ErgoTree Address by the RESPONDER ONLY.
def responderGenerateAtomicSchnorr(swapName, DEC_finalizationPATH, responderMasterJSONPATH, boxValue):
if os.path.isfile(DEC_finalizationPATH) == False:
print("finalization path is not a file! check filepath")
else:
claimContractGeneration = "cd " + SigmaParticlePath + " && ./new_frame " + swapName
gen = os.popen(claimContractGeneration).read()
importBoilerplate = "cp " + SigmaParticlePath + "/AtomicMultiSig/py/main.py " + SigmaParticlePath + swapName + "/py/main.py"
imp = os.popen(importBoilerplate).read()
finalization = json.loads(file_tools.clean_file_open(DEC_finalizationPATH, "r"))
master = json.loads(file_tools.clean_file_open(responderMasterJSONPATH, "r"))
responderChainPubKey = master["ResponderErgoAddr"]
initiatorChainPubKey = master["InitiatorErgoAddr"]
lockHeight = master["InitiatorAtomicSchnorrLockHeight"]
ss = finalization["ss"]
sr = master["sr"]
srG = ast.literal_eval(master["srG"])
ssG = ast.literal_eval(master["ssG"])
krG = master["krG"]
ksG = master["ksG"]
boxID = finalization["boxId"]
nanoErgs = boxValue
echoVariablesCMD = \
"echo \"" + \
"sr=" + sr + "\n" + \
"ss=" + ss + "\n" + \
"srGX=" + str(srG[0]) + "\n" +\
"srGY=" + str(srG[1]) + "\n" +\
"ssGX=" + str(ssG[0]) + "\n" +\
"ssGY=" + str(ssG[1]) + "\n" +\
"receiverAddr=\"" + str(responderChainPubKey) + "\"\n" + \
"senderAddr=\"" + str(initiatorChainPubKey) + "\"\n" + \
"ksGX=" + str(ast.literal_eval(ksG)[0]) + "\n" + \
"ksGY=" + str(ast.literal_eval(ksG)[1]) + "\n" + \
"krGX=" + str(ast.literal_eval(krG)[0]) + "\n" + \
"krGY=" + str(ast.literal_eval(krG)[1]) + "\n" + \
"atomicBox=" + "\"" + boxID + "\"\n" + \
"ergoAmount=" + str(nanoErgs) + "\n" + \
"staticLockHeight=" + str(lockHeight) + "\n" + \
"\" >> " + SigmaParticlePath + swapName + "/.env"
os.popen(echoVariablesCMD).read()
def responderVerifyErgoScript(swapName, expectedErgoTree, password=""):
verifyCMD = \
"cd " + SigmaParticlePath + swapName + " && ./deploy.sh deposit verifyTreeOnly " + password
# file_tools.clean_file_open("responderVerifyErgoScriptdebug", "w", verifyCMD)
os.popen(verifyCMD).read()
ergoTreePath = SigmaParticlePath + swapName + "/ergoTree"
ergoTree = file_tools.clean_file_open(ergoTreePath, "r")
fmt = "onchain:", expectedErgoTree, "offchain:", ergoTree
print(fmt)
# file_tools.clean_file_open(swapName + "/ergoTreeCompareTest", "w", fmt)
if expectedErgoTree.strip() == ergoTree.strip():
return True
else:
SigmaParticle_CheckLockTimeAtomicSchnorr(swapName, json_tools.ojf(swapName + "/responder.json")["boxId"], password=password)
return False
def responderClaimAtomicSchnorr(swapName, tries=None, password=""):
rounds = 0
if tries == None:
rounds = 60
elif type(tries) != int:
print("tries must be int! defaulting to 60 tries")
rounds = 60
else:
rounds = tries
claimCMD = \
"cd " + SigmaParticlePath + swapName + " && ./deploy.sh claim " + password
# file_tools.clean_file_open("sigmaresponderclaimscriptdebug", "w", claimCMD)
while rounds > 0:
response = os.popen(claimCMD).read()
if response == None:
rounds = rounds - 1
time.sleep(5)
continue
else:
return response
break
def SigmaParticle_updateKeyEnv(swapName, targetKeyEnvDirName):
if os.path.isfile(SigmaParticlePath + targetKeyEnvDirName + "/.env"):
update = file_tools.clean_file_open(SigmaParticlePath + targetKeyEnvDirName + "/.env", "r")
update.replace("[default]", "")
cmd = \
"echo \"" + update + "\"" + " >> " + SigmaParticlePath + swapName + "/.env"
os.popen(cmd).read()
else:
encpath = SigmaParticlePath + targetKeyEnvDirName + "/.env.encrypted"
if os.path.isfile(encpath):
newpath = SigmaParticlePath + swapName + "/.env.encrypted"
cpcmd = "cp " + encpath + " " + newpath
os.popen(cpcmd).read()
def SigmaParticle_getTreeFromBox(boxID, swapName, password=""):
tries = 60
while tries > 0:
boxID = boxID.replace("\n", "")
cmd = \
"cd " + SigmaParticlePath + "getTreeFromBox && ./deploy.sh " + boxID + \
" ../" + swapName + "/expectedErgoTree" + " " + password
# file_tools.clean_file_open("getTreeFromBoxScriptDebug", "w", cmd)
result = os.popen(cmd).read()
if "notfound" in result.lower():
tries = tries - 1
time.sleep(5)
continue
else:
return result
def deployErgoContract(swapName, password=""):
command = "cd " + SigmaParticlePath + swapName + " && ./deploy.sh deposit " + password
# file_tools.clean_file_open("initiatorDeployErgoContractDebug", "w", command)
os.popen(command).read()
def getBoxID(swapName):
path = SigmaParticlePath + swapName + "/boxId"
if os.path.isfile(path):
return file_tools.clean_file_open(path, "r")
else:
print("Path:", path, "not found!")
return False
def is_numeric_string(value):
return isinstance(value, str) and value.isdigit()
def checkBoxValue(boxID, boxValPATH, swapName, role=None, ergopassword="", otherchainpassword=""):
tries = 200
while tries > 0:
cmd = \
"cd " + SigmaParticlePath + \
"/boxValue/ && ./deploy.sh " + \
boxID + " " + "../../../" + boxValPATH + \
" " + ergopassword
# file_tools.clean_file_open("checkBoxValueScriptDebug", "w", cmd)
devnull = open(os.devnull, 'wb')
while is_numeric_string(file_tools.clean_file_open(boxValPATH, "r")) == False:
pipe = subprocess.Popen(cmd, shell=True, stdout=devnull, stderr=devnull, close_fds=True)
pipe.wait()
time.sleep(5)
response = file_tools.clean_file_open(boxValPATH, "r")
tries = tries - 1
if str(response) in ["error"] or type(response) == type(None):
if role == "responder":
masterjsonpath = swapName + "/responder.json"
responderChain = json_tools.ojf(masterjsonpath)["ResponderChain"]
if responderChain == "Sepolia":
responsepath = swapName + "/response_path.json"
j_response = json_tools.ojf(responsepath)
if AtomicityInterface.Atomicity_RemainingLockTimeAtomicMultisig_v_002( \
j_response, swapName, password=otherchainpassword \
) == 0:
AtomicityInterface.Atomicity_Refund(swapName, "responder", gas=7000000, gasMod=3, password=otherchainpassword)
return 0
break
time.sleep(5)
continue
else:
return response #returns box value in nano Ergs (nΣ)
break
def key_path_exists(data, key_path):
"""
Check if a nested key path exists in a dictionary or list.
Args:
data (dict or list): The data structure to check.
key_path (list): The list of keys and/or indices representing the path.
Returns:
bool: True if the path exists, False otherwise.
"""
try:
for key in key_path:
if isinstance(data, list) and isinstance(key, int):
data = data[key]
elif isinstance(data, dict):
data = data[key]
else:
return False
return True
except (KeyError, IndexError, TypeError):
return False
def checkSchnorrTreeForClaim(boxID, swapName, initiatorMasterJSONPath, password=""):
while True:
tree = ""
if os.path.isfile(SigmaParticlePath + swapName + "/ergoTree") == True:
tree = file_tools.clean_file_open(SigmaParticlePath + swapName + "/ergoTree", "r").replace("\n", "")
else:
tree = SigmaParticle_getTreeFromBox(boxID, swapName, password=password)
treeToAddrCmd = \
"cd " + SigmaParticlePath + "treeToAddr && ./deploy.sh " + tree + " ../" + \
swapName + "/scriptAddr " + password
# file_tools.clean_file_open("treeToAddrScriptTestDebug", "w", treeToAddrCmd)
addr = json.loads(os.popen(treeToAddrCmd).read())["address"]
boxFilterCmd = \
"cd " + SigmaParticlePath + "boxFilter && " + \
"./deploy.sh " + addr + " " + boxID + " ../../../" + swapName + "/atomicClaim " + password
# file_tools.clean_file_open("boxFilterCmdDebug", "w", boxFilterCmd)
os.popen(boxFilterCmd).read()
if os.path.isfile(swapName + "/atomicClaim_tx1") == True:
j = json.loads(file_tools.clean_file_open(swapName + "/atomicClaim_tx1", "r"))
keypath = ["outputs", 0, "additionalRegisters", "R4"]
if key_path_exists(j, keypath):
R4 = j["outputs"][0]["additionalRegisters"]["R4"]
sr_list = [{"sr":R4}]
json_tools.keyVal_list_update(sr_list, initiatorMasterJSONPath)
# print("atomic swap was claimed by responder!")
return True
else:
# print("no atomic swap claim found...")
#check contract lock time here for refund
remainingLocalLockTime = SigmaParticle_CheckLockTimeAtomicSchnorr(swapName, boxID, password=password)
if type(remainingLocalLockTime) == int:
if remainingLocalLockTime <= 0:
#claim refund here
echoBoxIdCMD = \
"echo '\natomicBox=" + boxID + "' >> " + SigmaParticlePath + swapName + "/.env"
os.popen(echoBoxIdCMD).read()
cmd = "cd " + SigmaParticlePath + swapName + " && ./deploy.sh refund"
print(os.popen(cmd).read()) #TODO check for success
return False
time.sleep(5)
continue
#This function takes a Register from the Responder's claim transaction from the Atomic Schnorr contract.
#It applies a subtraction against a previously known partial equation
#which allows Initiator to learn x (responders ephemoral secret).
def deduceX_fromAtomicSchnorrClaim(initiatorMasterJSONPath, swapName, password=""):
masterJSON = json.loads(file_tools.clean_file_open(initiatorMasterJSONPath, "r"))
sr_ = masterJSON["sr_"]
responderContractAddr = masterJSON["responderContractAddr"]
responderLocalChain = masterJSON["responderLocalChain"]
enc_sr = masterJSON["sr"]
decode_sr_cmd = \
"cd " + SigmaParticlePath + "valFromHex && ./deploy.sh " + enc_sr + " ../../../" + swapName + "/decoded_sr.bin " + password
decode = os.popen(decode_sr_cmd).read()
sr = file_tools.clean_file_open(swapName + "/decoded_sr.bin", "r")
deduction_cmd = \
"cd " + SigmaParticlePath + "AtomicMultiSigECC && python3 -u py/deploy.py p1Deduce " + sr_ + " " + sr
deduction_response = os.popen(deduction_cmd).read()
deduction_j = json.loads(deduction_response)
x = deduction_j["x"]
deduction_list = [{"x":x}]
json_tools.keyVal_list_update(deduction_list, initiatorMasterJSONPath)
return x