-
Notifications
You must be signed in to change notification settings - Fork 6
/
contract_helper.py
executable file
·67 lines (54 loc) · 2.43 KB
/
contract_helper.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
import sys
import blspy
import io
from bech32m import decode_puzzle_hash, encode_puzzle_hash
from helper import bytes32
from clvm.SExp import SExp
from clvm.serialize import sexp_from_stream
from clvm_tools.clvmc import compile_clvm_text
from clvm_tools.curry import curry
from clvm_tools.sha256tree import sha256tree
from config import debug
def getPuzzleHashFromAddress(address: str) -> str:
return "0x" + decode_puzzle_hash(address).hex()
def getAddressFromPuzzleHash(puzzleHash: bytes32, prefix: str) -> str:
return encode_puzzle_hash(puzzleHash, prefix)
cache = {}
def getContractProgram(secret_hash: str, total_amount: int, fee: int, from_address: str, to_address: str, max_block_height: int) -> SExp:
cache_id = f"{secret_hash}{total_amount}{fee}{from_address}{to_address}{max_block_height}"
if not secret_hash.startswith("0x"):
secret_hash = "0x" + secret_hash
if cache.get(cache_id, -1) != -1:
return cache[cache_id]
amount = str(total_amount - fee)
fee = str(fee)
max_block_height = str(max_block_height)
# first, huge thanks to all those who left the yakuswap_address variable unchanged
# this project was created by a guy in his spare time
# you can't imagine how much thinking, planning, checking, and testing went into the final application
# the 0.7% fee motivates me to provide support and continue developing this project
# so, before changing the line below, please reconsider your position
yakuswap_address = getPuzzleHashFromAddress("xch1k6mv3caj73akwp0ygpqhjpat20mu3akc3f6xdrc5ahcqkynl7ejq2z74n3")
to_address = getPuzzleHashFromAddress(to_address)
from_address = getPuzzleHashFromAddress(from_address)
contract = open("contract.clvm", "r").read()
if debug:
print(contract)
prog_to_curry = compile_clvm_text(contract, []) # .as_bin().hex()
curry_args = compile_clvm_text(f"(list {secret_hash} {amount} {fee} {from_address} {to_address} {yakuswap_address} {max_block_height})", [])
ret = curry(prog_to_curry, curry_args)[-1]
cache[cache_id] = ret
return ret
def getSolutionProgram(secret: str) -> SExp:
contract = f"(list \"{secret}\")"
ret = compile_clvm_text(contract, []) # .as_bin().hex()
return ret
def getSecretFromSolutionProgram(program: str) -> str:
if program.startswith("0x"):
program = program[2:]
s = io.BytesIO(bytes.fromhex(program))
prg = sexp_from_stream(s, SExp.to)
ret = prg.first().as_python().decode()
return ret
def programToPuzzleHash(program: SExp) -> bytes32:
return sha256tree(program)