-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponderInterface.py
499 lines (472 loc) · 28.1 KB
/
responderInterface.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
import json, os, json_tools, configparser
import time
from datetime import datetime
import AtomicityInterface
import SigmaParticleInterface
import ElGamalInterface
import file_tools
import price_tools
import config_tools
import swap_tools
from passwordFileEncryption import get_val_from_envdata_key, decrypt_file_return_contents
py = "python3 -u "
AtomicSwapECCPath = "Ergo/SigmaParticle/AtomicMultiSigECC/py/deploy.py " #TODO Ergo Specific
s_ = " "
MIN_CLAIM_LOCKTIME_ERGOTESTNET = int(config_tools.valFromConf(".env", "MIN_CLAIM_LOCKTIME_ERGOTESTNET"))
MIN_REFUND_LOCKTIME_SEPOLIA = int(config_tools.valFromConf(".env", "MIN_REFUND_LOCKTIME_SEPOLIA"))
SEPOLIA_EVM_GAS_CONTROL = int(config_tools.valFromConf(".env", "SEPOLIA_EVM_GAS_CONTROL"))
SEPOLIA_EVM_GASMOD_CONTROL = int(config_tools.valFromConf(".env", "SEPOLIA_EVM_GASMOD_CONTROL"))
# we need to assume we have the ENC file saved already
def process_initiation(ENC_filepath, DEC_filepath, SenderPubKey, UserKeyFileName): #this is generic to any enc_message actually
file_tools.clean_file_open(ENC_filepath, "r")
decrypt = ElGamalInterface.ElGamal_Decrypt(ENC_filepath, SenderPubKey, UserKeyFileName) #TODO verify initiation details including json sanity
file_tools.clean_file_open(DEC_filepath, "w", decrypt)
def response(DEC_initiation_filepath, responderMasterJSONPATH, response_filepath, SenderPubKey):
j_init = json.loads(file_tools.clean_file_open(DEC_initiation_filepath, "r"))
ksG = j_init["ksG"]
j_master = json.loads(file_tools.clean_file_open(responderMasterJSONPATH, "r"))
swapName = j_master["swapName"]
sr_filepath = swapName + "/sr"
x_filepath = swapName + "/x"
responderMessageContent = str(datetime.now())
command = \
py + AtomicSwapECCPath + "p2Respond " +\
"'" + ksG + "' " + "'" + responderMessageContent + "' " + \
sr_filepath + s_ + x_filepath
response = os.popen(command).read()
file_tools.clean_file_open(response_filepath, "w", response)
sr = file_tools.clean_file_open(sr_filepath, "r")
x = file_tools.clean_file_open(x_filepath, "r")
secretslist = [{"sr":sr}, {"x":x}]
json_tools.keyVal_list_update(secretslist, responderMasterJSONPATH)
def GeneralizeENC_ResponseSubroutine(\
swapName, responderCrossChainAccountName, responderLocalChainAccountName, \
ElGamalKey, ElGamalKeyPath, InitiatorChain, ResponderChain, swapAmount,
localChainAccountPassword="", crossChainAccountPassword="", reloadSwapState=""):
def setup(
swapName, responderCrossChainAccountName, responderLocalChainAccountName, \
ElGamalKey, ElGamalKeyPath, InitiatorChain, ResponderChain, swapAmount,
localChainAccountPassword="", crossChainAccountPassword=""
):
mi = {}
localChainAccountEnvData = ""
crossChainAccountEnvData = ""
crossChainEncAccount = False
localChainEncAccount = False
ErgoPath = "Ergo/SigmaParticle/" + responderCrossChainAccountName + "/.env.encrypted"
if os.path.isfile(ErgoPath):
if InitiatorChain == "TestnetErgo":
if crossChainAccountPassword != "":
crossChainAccountEnvData = decrypt_file_return_contents(ErgoPath, localChainAccountPassword)
crossChainEncAccount = True
else:
print("password required for encrypted env file!")
return
elif ResponderChain == "TestnetErgo":
if localChainAccountPassword != "":
localChainAccountEnvData = decrypt_file_return_contents(ErgoPath, crossChainAccountPassword)
localChainEncAccount = True
else:
print("password required for encrypted env file!")
return
#ergo encrypted
if os.path.isfile("EVM/Atomicity/" + responderLocalChainAccountName + "/.env.encrypted"):
path = "EVM/Atomicity/" + responderLocalChainAccountName + "/.env.encrypted"
if InitiatorChain == "Sepolia":
if crossChainAccountPassword != "":
crossChainAccountEnvData = decrypt_file_return_contents(path, localChainAccountPassword)
crossChainEncAccount = True
else:
print("password required for encrypted env file!")
return
elif ResponderChain == "Sepolia":
if localChainAccountPassword != "":
localChainAccountEnvData = decrypt_file_return_contents(path, crossChainAccountPassword)
localChainEncAccount = True
else:
print("password required for encrypted env file!")
return
#sepolia encrypted
if localChainEncAccount == False and crossChainEncAccount == False:
if InitiatorChain.strip("\"") == "TestnetErgo" and ResponderChain.strip("\"") == "Sepolia":
mi = {
"responderErgoAccountName": responderCrossChainAccountName,
"responderSepoliaAccountName": responderLocalChainAccountName,
"ElGamalKey" : ElGamalKey,
"ElGamalKeyPath" : ElGamalKeyPath,
"swapName" : swapName,
"InitiatorChain" : InitiatorChain,
"ResponderChain" : ResponderChain,
"responderLocalChain": ResponderChain,
"responderJSONPath" : swapName + "/responder.json",
"ResponderEVMAddr" : \
config_tools.valFromConf("EVM/Atomicity/" + responderLocalChainAccountName.replace('"', '') + "/.env", 'SepoliaSenderAddr').replace('"', ''),
"ResponderEIP3Secret" : \
config_tools.valFromConf("Ergo/SigmaParticle/" + responderCrossChainAccountName.replace('"', '') + "/.env", 'senderEIP3Secret').replace('"', ''),
"ResponderErgoAddr" : \
config_tools.valFromConf("Ergo/SigmaParticle/" + responderCrossChainAccountName.replace('"', '') + "/.env", 'senderPubKey').replace('"', ''),
"ENC_Init_PATH" : swapName + "/ENC_init.bin", #responder needs to save ENC init to this path to proceed
"DEC_Init_PATH" : swapName + "/DEC_init.json",
"responsePATH" : swapName + "/response_path.json",
"ENC_Response_PATH" : swapName + "/ENC_response_path.bin",
"ENC_finalizationPATH" : swapName + "/ENC_finalization.bin",
"DEC_finalizationPATH" : swapName + "/DEC_finalization.json",
"SwapAmount" : swapAmount
}
elif localChainEncAccount == True and crossChainEncAccount == True:
if InitiatorChain.strip("\"") == "TestnetErgo" and ResponderChain.strip("\"") == "Sepolia":
mi = {
"responderErgoAccountName": responderCrossChainAccountName,
"responderSepoliaAccountName": responderLocalChainAccountName,
"ElGamalKey" : ElGamalKey,
"ElGamalKeyPath" : ElGamalKeyPath,
"swapName" : swapName,
"InitiatorChain" : InitiatorChain,
"ResponderChain" : ResponderChain,
"responderLocalChain": ResponderChain,
"responderJSONPath" : swapName + "/responder.json",
"ResponderEVMAddr" : \
get_val_from_envdata_key('SepoliaSenderAddr', localChainAccountEnvData).replace('"', ''),
"ResponderEIP3Secret" : \
get_val_from_envdata_key('senderEIP3Secret', crossChainAccountEnvData).replace('"', ''),
"ResponderErgoAddr" : \
get_val_from_envdata_key('senderPubKey', crossChainAccountEnvData).replace('"', ''),
"ENC_Init_PATH" : swapName + "/ENC_init.bin", #responder needs to save ENC init to this path to proceed
"DEC_Init_PATH" : swapName + "/DEC_init.json",
"responsePATH" : swapName + "/response_path.json",
"ENC_Response_PATH" : swapName + "/ENC_response_path.bin",
"ENC_finalizationPATH" : swapName + "/ENC_finalization.bin",
"DEC_finalizationPATH" : swapName + "/DEC_finalization.json",
"SwapAmount" : swapAmount
}
file_tools.clean_mkdir(mi["swapName"])
file_tools.clean_file_open(mi["responderJSONPath"], "w", json.dumps(mi))
resp_J = json_tools.ojf(mi["responderJSONPath"])
ENC_Init_PATH = resp_J["ENC_Init_PATH"]
DEC_Init_PATH = resp_J["DEC_Init_PATH"]
ElGamalKey = resp_J["ElGamalKey"]
ElGamalKeyPath = resp_J["ElGamalKeyPath"]
responsePATH = resp_J["responsePATH"]
ENC_Response_PATH = resp_J["ENC_Response_PATH"]
ResponderChain = resp_J["ResponderChain"]
ResponderErgoAddr = resp_J["ResponderErgoAddr"]
responderJSONPath = mi["responderJSONPath"]
process_initiation(ENC_Init_PATH, DEC_Init_PATH, ElGamalKey, ElGamalKeyPath)
r_initiation_keyValList = json_tools.json_to_keyValList(DEC_Init_PATH)
json_tools.keyVal_list_update(r_initiation_keyValList, mi["responderJSONPath"])
resp_J = json_tools.ojf(mi["responderJSONPath"])
# InitiatorEVMAddr = resp_J["InitiatorEVMAddr"] TODO generalize this
temprenamelist = [{"InitiatorEVMAddr": resp_J["SepoliaChainPubkey"]}]
json_tools.keyVal_list_update(temprenamelist, mi["responderJSONPath"])
resp_J = json_tools.ojf(mi["responderJSONPath"])
InitiatorEVMAddr = resp_J["InitiatorEVMAddr"]
swap_tools.setSwapState(swapName, "initiated", setMap=True) #set swap state to initiated
return \
ENC_Init_PATH, DEC_Init_PATH, ElGamalKey, \
ElGamalKeyPath, responsePATH, ENC_Response_PATH, \
ResponderChain, ResponderErgoAddr, responderJSONPath, \
resp_J, InitiatorEVMAddr
def startResponse(
DEC_Init_PATH, responderJSONPath, responsePATH,
ElGamalKey, ResponderChain, InitiatorEVMAddr,
swapName, localChainAccountPassword=""
):
response(
DEC_Init_PATH, responderJSONPath, \
responsePATH, ElGamalKey
) #ElGamalKeyPath)
#TODO: replace sr and x paths with master json update
xG = json.loads(file_tools.clean_file_open(responsePATH, "r"))["xG"]
AtomicityInterface.Atomicity_buildScalarContract(ResponderChain, InitiatorEVMAddr, xG, MIN_REFUND_LOCKTIME_SEPOLIA, swapName)
swap_tools.setSwapState(swapName, "uploadingResponseContract", setMap=True)
addr = AtomicityInterface.Atomicity_deployEVMContract(
swapName, customGas=SEPOLIA_EVM_GAS_CONTROL,
customGasMod=SEPOLIA_EVM_GASMOD_CONTROL, password=localChainAccountPassword
)
if addr != "fail":
#ASSUMING ITS ENDING WITH \n
addr = addr[:-1]
else:
print("fail: deployContract() didnt return a contract addr")
return
swapAmount = json_tools.ojf(responderJSONPath)["SwapAmount"]
responderFundingAmountWei = price_tools.EthToWei(swapAmount)
#TODO save to resp_J?
swap_tools.setSwapState(swapName, "uploadedResponseContract", setMap=True)
return addr, responderFundingAmountWei
def fund(
addr, responderFundingAmountWei, ResponderChain, ResponderErgoAddr, responsePATH
):
swap_tools.setSwapState(swapName, "fundingResponseContract", setMap=True)
update_response_keyValList = [{"responderLocalChain":ResponderChain}, \
{"responderContractAddr":addr},\
{"ResponderErgoAddr":ResponderErgoAddr}]
json_tools.keyVal_list_update(update_response_keyValList, responsePATH)
j_response = json_tools.ojf(responsePATH)
AtomicityInterface.Atomicity_SendFunds(
addr, responderFundingAmountWei, swapName,
gas=SEPOLIA_EVM_GAS_CONTROL, gasMod=SEPOLIA_EVM_GASMOD_CONTROL, password=localChainAccountPassword
)
while int(AtomicityInterface.Atomicity_CheckContractFunds(swapName, j_response, password=localChainAccountPassword)) <= 0:
print("contract not funded yet waiting...")
time.sleep(5)
swap_tools.setSwapState(swapName, "fundedResponseContract", setMap=True)
def completeResponse(
addr, swapAmount,
responsePATH, responderJSONPath, ElGamalKey, ElGamalKeyPath,
ENC_Response_PATH, localChainAccountPassword=""
):
swap_tools.setSwapState(swapName, "responding", setMap=True)
responseLIST = json_tools.json_to_keyValList(responsePATH)
json_tools.keyVal_list_update(responseLIST, responderJSONPath)
encrypted_response = ElGamalInterface.ElGamal_Encrypt(ElGamalKey, ElGamalKeyPath, responsePATH, ENC_Response_PATH)
swap_tools.setSwapState(swapName, "responded_unsubmitted", setMap=True)
if reloadSwapState != "":
print("reloading swap: ", swapName)
if reloadSwapState in [swap_tools.PossibleSwapStates[0], swap_tools.PossibleSwapStates[1]]:
#initiated or uploadingResponseContract
#TODO if uploading make sure its not in progress aka that its a failed tx and ensure it should continue:
#things that might influence that are the time elapsed since the initiation and feedback from the responder's UI
resp_J = json_tools.ojf(swapName + "/responder.json")
DEC_Init_PATH = resp_J["DEC_Init_PATH"]
responderJSONPath = swapName + "/responder.json"
responsePATH = resp_J["responsePATH"]
ElGamalKey = resp_J["ElGamalKey"]
ElGamalKeyPath = resp_J["ElGamalKeyPath"]
ResponderChain = resp_J["ResponderChain"]
InitiatorEVMAddr = resp_J["InitiatorEVMAddr"]
ResponderErgoAddr = resp_J["ResponderErgoAddr"]
ENC_Response_PATH = resp_J["ENC_Response_PATH"]
addr, responderFundingAmountWei = startResponse(
DEC_Init_PATH, responderJSONPath, responsePATH,
ElGamalKey, ResponderChain, InitiatorEVMAddr,
swapName, localChainAccountPassword=localChainAccountPassword
)
fund(addr, responderFundingAmountWei, ResponderChain, ResponderErgoAddr,
responsePATH)
completeResponse(
addr, swapAmount, responsePATH, responderJSONPath, ElGamalKey, ElGamalKeyPath,
ENC_Response_PATH, localChainAccountPassword=localChainAccountPassword
)
return
#TODO return some data to indicate that this swap needs to be resumed from its next step
#inform UI or rest server that it needs to keep track of its state from that point forward
#maybe just give feedback to ui while running the claim loop after a response reload
if reloadSwapState in [swap_tools.PossibleSwapStates[2], swap_tools.PossibleSwapStates[3]] : #funding response contract #TODO verify it hasnt been funded yet
#w a contract balance call
resp_J = json_tools.ojf(swapName + "/responder.json")
addr = resp_J["responderContractAddr"]
swapAmount = resp_J["swapAmount"]
ResponderChain = resp_J["ResponderChain"]
ResponderErgoAddr = resp_J["ResponderErgoAddr"]
responsePATH = resp_J["responsePATH"]
responderJSONPath = resp_J["responderJSONPath"]
ElGamalKey = resp_J["ElGamalKey"]
ElGamalKeyPath = resp_J["ElGamalKeyPath"]
ENC_Response_PATH = resp_J["ENC_Response_PATH"]
responderFundingAmountWei = price_tools.EthToWei(swapAmount)
j_response = json.tools.ojf(responsePATH)
if int(AtomicityInterface.Atomicity_CheckContractFunds(swapName, j_response, password=localChainAccountPassword)) <= 0:
#TODO check against specific funding amount
fund(addr, responderFundingAmountWei, ResponderChain, ResponderErgoAddr,
responsePATH)
completeResponse(
addr, swapAmount, responsePATH, responderJSONPath, ElGamalKey, ElGamalKeyPath,
ENC_Response_PATH, localChainAccountPassword=localChainAccountPassword
)
return
if reloadSwapState in [swap_tools.PossibleSwapStates[4], swap_tools.PossibleSwapStates[5]]: #funded or responding
resp_J = json_tools.ojf(swapName + "/responder.json")
addr = resp_J["responderContractAddr"]
swapAmount = resp_J["swapAmount"]
ResponderChain = resp_J["ResponderChain"]
ResponderErgoAddr = resp_J["ResponderErgoAddr"]
responsePATH = resp_J["responsePATH"]
responderJSONPath = resp_J["responderJSONPath"]
ElGamalKey = resp_J["ElGamalKey"]
ElGamalKeyPath = resp_J["ElGamalKeyPath"]
ENC_Response_PATH = resp_J["ENC_Response_PATH"]
completeResponse(
addr, swapAmount, responsePATH, responderJSONPath, ElGamalKey, ElGamalKeyPath,
ENC_Response_PATH, localChainAccountPassword=localChainAccountPassword
)
return
ENC_Init_PATH, DEC_Init_PATH, ElGamalKey, \
ElGamalKeyPath, responsePATH, ENC_Response_PATH, \
ResponderChain, ResponderErgoAddr, responderJSONPath, \
resp_J, InitiatorEVMAddr = \
setup(\
swapName, responderCrossChainAccountName, responderLocalChainAccountName, \
ElGamalKey, ElGamalKeyPath, InitiatorChain, ResponderChain, swapAmount,
localChainAccountPassword=localChainAccountPassword, crossChainAccountPassword=crossChainAccountPassword\
)
addr, responderFundingAmountWei = \
startResponse(
DEC_Init_PATH, responderJSONPath, responsePATH,
ElGamalKey, ResponderChain, InitiatorEVMAddr,
swapName, localChainAccountPassword=localChainAccountPassword
)
fund(addr, responderFundingAmountWei, ResponderChain, ResponderErgoAddr,
responsePATH)
completeResponse(
addr, swapAmount, responsePATH, responderJSONPath, ElGamalKey, ElGamalKeyPath,
ENC_Response_PATH, localChainAccountPassword=localChainAccountPassword
)
return ENC_Response_PATH
def GeneralizedENC_ResponderClaimSubroutine(
responderJSONPath, localChainAccountPassword="", crossChainAccountPassword="", reloadSwapState=""
):
resp_J = json_tools.ojf(responderJSONPath)
if resp_J["InitiatorChain"] == "TestnetErgo" and resp_J["ResponderChain"] == "Sepolia":
def setup(resp_J):
swapName = resp_J["swapName"]
ENC_finalizationPATH = resp_J["ENC_finalizationPATH"]
ElGamalKey = resp_J["ElGamalKey"]
ElGamalKeyPath = resp_J["ElGamalKeyPath"]
DEC_finalizationPATH = resp_J["DEC_finalizationPATH"]
responderErgoAccountName = resp_J["responderErgoAccountName"]
DEC_finalization = ElGamalInterface.ElGamal_Decrypt(ENC_finalizationPATH, ElGamalKey, ElGamalKeyPath)
file_tools.clean_file_open(DEC_finalizationPATH, "w", DEC_finalization)
swap_tools.setSwapState(swapName, "finalized", setMap=True)
return swapName, ENC_finalizationPATH, ElGamalKey, \
ElGamalKeyPath, DEC_finalizationPATH, responderErgoAccountName, DEC_finalization
def verify(\
DEC_finalizationPATH, responderJSONPath, DEC_finalization,\
swapName, responderErgoAccountName, localChainAccountPassword="", crossChainAccountPassword=""\
):
swap_tools.setSwapState(swapName, "verifyingFinalizedContractValues", setMap=True)
finalization_list = json_tools.json_to_keyValList(DEC_finalizationPATH)
json_tools.keyVal_list_update(finalization_list, responderJSONPath)
boxID = json.loads(DEC_finalization)["boxId"]
boxValue = SigmaParticleInterface.checkBoxValue(
boxID,
swapName + "/testBoxValPath.bin",
swapName,
role="responder",
ergopassword=crossChainAccountPassword,
otherchainpassword=localChainAccountPassword
)
j_response = json_tools.ojf(resp_J["responsePATH"])
if boxValue == 0:
print("refund tried, swap aborting")
while True:
if AtomicityInterface.Atomicity_RemainingLockTimeAtomicMultisig_v_002(\
j_response, swapName, password=localChainAccountPassword\
) <= 0:
AtomicityInterface.Atomicity_Refund(\
swapName, "responder", gas=SEPOLIA_EVM_GAS_CONTROL, \
gasMod=SEPOLIA_EVM_GASMOD_CONTROL, password=localChainAccountPassword
)
break
time.sleep(3)
return
#other than just the box value responder should verify the scalars in the contract match those expected
SigmaParticleInterface.SigmaParticle_newFrame(swapName)
remoteLockTime = SigmaParticleInterface.SigmaParticle_CheckLockTimeAtomicSchnorr(\
swapName, boxID, password=crossChainAccountPassword\
)
if type(remoteLockTime) != int:
print("error in remoteLockTime value: ", remoteLockTime)
return
if remoteLockTime < MIN_CLAIM_LOCKTIME_ERGOTESTNET:
while True:
if AtomicityInterface.Atomicity_RemainingLockTimeAtomicMultisig_v_002(
j_response, swapName, password=localChainAccountPassword
) <= 0:
AtomicityInterface.Atomicity_Refund(
swapName, "responder", \
gas=SEPOLIA_EVM_GAS_CONTROL, gasMod=SEPOLIA_EVM_GASMOD_CONTROL, \
password=localChainAccountPassword
)
break
time.sleep(3)
print("lock time is below safe minimum for claiming, refunding swap")
return
minBoxValue = 1123841 #1123841
if int(boxValue) < int(minBoxValue):
while True:
if AtomicityInterface.Atomicity_RemainingLockTimeAtomicMultisig_v_002(\
j_response, swapName, password=localChainAccountPassword\
) <= 0:
AtomicityInterface.Atomicity_Refund(\
swapName, "responder", \
gas=SEPOLIA_EVM_GAS_CONTROL, gasMod=SEPOLIA_EVM_GASMOD_CONTROL\
)
break
time.sleep(3)
print("not enough nanoerg in contract, refunging swap")
return
SigmaParticleInterface.SigmaParticle_updateKeyEnv(swapName, responderErgoAccountName)
SigmaParticleInterface.responderGenerateAtomicSchnorr(swapName, DEC_finalizationPATH, responderJSONPath, boxValue)
expectedErgoTree = SigmaParticleInterface.SigmaParticle_getTreeFromBox(boxID, swapName, password=crossChainAccountPassword)
if SigmaParticleInterface.responderVerifyErgoScript(swapName, expectedErgoTree, password=crossChainAccountPassword) == False:
print("ergoScript verification returned false, wont fulfil swap")
return
swap_tools.setSwapState(swapName, "verifiedFinalizedContractValues", setMap=True)
def claim():
swap_tools.setSwapState(swapName, "claiming", setMap=True)
SigmaParticleInterface.responderClaimAtomicSchnorr(swapName, 2500, password=crossChainAccountPassword)
swap_tools.setSwapState(swapName, "claimed", setMap=True) #TODO ensure it was claimed w contract balance call
if reloadSwapState != "":
if reloadSwapState == swap_tools.PossibleSwapStates[7]: #responded_submitted
swapName, ENC_finalizationPATH, ElGamalKey, \
ElGamalKeyPath, DEC_finalizationPATH, \
responderErgoAccountName, DEC_finalization = \
setup(resp_J)
# print("ergo contract verification status:", responderVerifyErgoScript(swapName, expectedErgoTree))
verify(\
DEC_finalizationPATH, responderJSONPath, \
DEC_finalization, swapName, responderErgoAccountName, \
localChainAccountPassword=localChainAccountPassword, crossChainAccountPassword=crossChainAccountPassword\
)
claim()
return
if reloadSwapState in [swap_tools.PossibleSwapStates[8], swap_tools.PossibleSwapStates[9]]: #finalized or verifying
DEC_finalizationPATH = resp_J["DEC_finalizationPATH"]
responderJSONPath = resp_J["responderJSONPath"]
DEC_finalization = file_tools.clean_file_open(DEC_finalizationPATH, "r")
swapName = resp_J["swapName"]
responderErgoAccountName = resp_J["responderErgoAccountName"]
verify(\
DEC_finalizationPATH, responderJSONPath, \
DEC_finalization, swapName, responderErgoAccountName, \
localChainAccountPassword=localChainAccountPassword, crossChainAccountPassword=crossChainAccountPassword\
)
claim()
return
if reloadSwapState in [swap_tools.PossibleSwapStates[10], swap_tools.PossibleSwapStates[11]]: #verified or claiming
claim()
return
else:
swapName, ENC_finalizationPATH, ElGamalKey, ElGamalKeyPath, DEC_finalizationPATH, responderErgoAccountName, DEC_finalization = \
setup(resp_J)
# print("ergo contract verification status:", responderVerifyErgoScript(swapName, expectedErgoTree))
verify(\
DEC_finalizationPATH, responderJSONPath, \
DEC_finalization, swapName, responderErgoAccountName, \
localChainAccountPassword=localChainAccountPassword, crossChainAccountPassword=crossChainAccountPassword\
)
claim()
def Responder_CheckLockTimeRefund(swapName, password=""):
resp_J = json_tools.ojf(swapName + "/responder.json")
j_response = json_tools.ojf(resp_J["responsePATH"])
swapName = resp_J["swapName"]
while True:
if \
AtomicityInterface.Atomicity_RemainingLockTimeAtomicMultisig_v_002(j_response, swapName, password=password) <= 0 and \
int(AtomicityInterface.Atomicity_CheckContractFunds(swapName, j_response, password=password)) != 0:
swap_tools.setSwapState(swapName, "refunding", setMap=True)
print(AtomicityInterface.Atomicity_Refund(swapName, "responder", password=password))
swap_tools.setSwapState(swapName, "refunded", setMap=True) #TODO verify it was refunded w a contract balance call
time.sleep(10)
#we may want to alter the smart contract to enforce that funds are in the
#contract before sender reclaim can be called
#will completely prevent fee waste on mainnet
print("lock time expired, refund attempted")
else:
if int(AtomicityInterface.Atomicity_CheckContractFunds(swapName, j_response, password=password)) != 0:
time.sleep(3)
continue
else:
break
print("contract empty / timelock expired")