-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathsimple_script.py
113 lines (95 loc) · 3.25 KB
/
simple_script.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
from eopsin.prelude import *
@dataclass()
class RequireSignature(PlutusData):
CONSTR_ID = 0
vkeyhash: bytes # this is either a PubKeyHash or a VerificationKeyHash
@dataclass()
class RequireAllOf(PlutusData):
CONSTR_ID = 1
scripts: List[Datum] # "Script"
@dataclass()
class RequireAnyOf(PlutusData):
CONSTR_ID = 2
scripts: List[Datum] # "Script"
@dataclass()
class RequireMOf(PlutusData):
CONSTR_ID = 3
num: int
scripts: List[Datum] # "Script"
@dataclass()
class RequireBefore(PlutusData):
CONSTR_ID = 4
unixtimestamp: int
@dataclass()
class RequireAfter(PlutusData):
CONSTR_ID = 5
unixtimestamp: int
Script = Union[
RequireSignature,
RequireMOf,
RequireAnyOf,
RequireAllOf,
RequireAfter,
RequireBefore,
]
def validate_script(
script_raw: Script, signatories: List[bytes], valid_range: POSIXTimeRange
) -> bool:
script: Script = script_raw # cast to Script in the type system to avoid recursive type definition
if isinstance(script, RequireSignature):
res = script.vkeyhash in signatories
elif isinstance(script, RequireAllOf):
res = all(
[validate_script(s, signatories, valid_range) for s in script.scripts]
)
elif isinstance(script, RequireAnyOf):
res = any(
[validate_script(s, signatories, valid_range) for s in script.scripts]
)
elif isinstance(script, RequireMOf):
res = (
sum(
[
1 if validate_script(s, signatories, valid_range) else 0
for s in script.scripts
]
)
>= script.num
)
elif isinstance(script, RequireBefore):
upper_bound = valid_range.upper_bound
upper_limit = upper_bound.limit
if isinstance(upper_limit, FinitePOSIXTime):
upper_closed = upper_bound.closed
if isinstance(upper_closed, TrueData):
res = upper_limit.time <= script.unixtimestamp
else:
res = upper_limit.time < script.unixtimestamp
elif isinstance(upper_limit, PosInfPOSIXTime):
res = False
elif isinstance(upper_limit, NegInfPOSIXTime):
res = True
elif isinstance(script, RequireAfter):
lower_bound = valid_range.lower_bound
lower_limit = lower_bound.limit
if isinstance(lower_limit, FinitePOSIXTime):
lower_closed = lower_bound.closed
if isinstance(lower_closed, TrueData):
res = lower_limit.time >= script.unixtimestamp
else:
res = lower_limit.time > script.unixtimestamp
elif isinstance(lower_limit, PosInfPOSIXTime):
res = True
elif isinstance(lower_limit, NegInfPOSIXTime):
res = False
else:
assert False, "Invalid simple script passed"
return res
# to fully emulate simple script behaviour, compile with --force-three-params
# the script is a contract parameter, pass it into the build command
def validator(
script: Script, datum: None, redeemer: None, context: ScriptContext
) -> None:
assert validate_script(
script, context.tx_info.signatories, context.tx_info.valid_range
), "Simple Script validation failed!"