-
Notifications
You must be signed in to change notification settings - Fork 63
/
sandbox.lua
152 lines (138 loc) · 4.72 KB
/
sandbox.lua
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
Json = (loadfile("./json.lua"))()
local json = Json.new()
thisTransaction = json:decode(txJson)
thisInput = json:decode(thisInputJson)
function jsonStrToObj(str)
if str == "" then return nil end
return json:decode(str)
end
function getBlock(id)
local res = Blockchain.getBlock(id)
return jsonStrToObj(res)
end
function getTransaction(id)
local res = Blockchain.getTransaction(id)
return jsonStrToObj(res)
end
function getOutput(id)
local res = Blockchain.getOutput(id)
return jsonStrToObj(res)
end
function getInput(id)
local res = Blockchain.getInput(id)
return jsonStrToObj(res)
end
sandbox_env = {Crypto = {new = Crypto.new, getPublicKey = Crypto.getPublicKey, getPrivateKey = Crypto.getPrivateKey,
setPublicKey = Crypto.setPublicKey, setPrivateKey = Crypto.setPrivateKey,
getStatus = Crypto.getStatus, sign = Crypto.sign, verify = Crypto.verify,},
Json = {new = Json.new, decode = Json.decode,},
sha256 = sha256,
thisTransaction = thisTransaction,
thisInput = thisInput,
outputSetId = outputSetId,
Blockchain = {getBlock = getBlock, getTransaction = getTransaction,
getOutput = getOutput, getInput = getInput,},
assert = assert,
error = error,
ipairs = ipairs,
next = next,
pairs = pairs,
pcall = pcall,
select = select,
tonumber = tonumber,
tostring = tostring,
type = type,
xpcall = xpcall,
string = {len = string.len,
pack = string.pack,
packsize = string.packsize,
reverse = string.reverse,
sub = string.sub,
unpack = string.unpack,},
utf8 = {char = utf8.char,
charpattern = utf8.charpattern,
codes = utf8.codes,
codepoint = utf8.codepoint,
len = utf8.len,
offset = utf8.offset,},
table = {concat = table.concat,
insert = table.insert,
move = table.move,
pack = table.pack,
remove = table.remove,
sort = table.sort,
unpack = table.unpack,},
math = {abs = math.abs,
acos = math.acos,
asin = math.asin,
atan = math.atan,
ceil = math.ceil,
cos = math.cos,
deg = math.deg,
exp = math.exp,
floor = math.floor,
fmod = math.fmod,
huge = math.huge,
log = math.log,
max = math.max,
maxinteger = math.maxinteger,
min = math.min,
mininteger = math.mininteger,
modf = math.modf,
pi = math.pi,
rad = math.rad,
sin = math.sin,
sqrt = math.sqrt,
tan = math.tan,
tointeger = math.tointeger,
type = math.type,
ult = math.ult,},
}
local function setfenv(fn, env)
local i = 1
while true do
local name = debug.getupvalue(fn, i)
if name == "_ENV" then
debug.upvaluejoin(fn, i, (function()
return env
end), 1)
break
elseif not name then
break
end
i = i + 1
end
return fn
end
pc = 0
function programCounterHook()
pc = pc + 50
if pc > pcLimit then
error("Instruction limit reached")
end
end
function run_sandbox(sb_env, sb_func, ...)
if (not sb_func) then return "Not a valid function" end
setfenv(sb_func, sb_env)
collectgarbage("stop")
debug.sethook(programCounterHook, "", 50)
local sb_ret={_ENV.pcall(sb_func, ...)}
debug.sethook()
collectgarbage("restart")
return _ENV.table.unpack(sb_ret)
end
function verifyTransaction(bytecode)
local status, lz4 = pcall(require, "lz4")
if(status) then
f = load(lz4.decompress(bytecode))
pcall_rc, result_or_err_msg = run_sandbox(sandbox_env, f)
if type(result_or_err_msg) ~= "boolean" then
print(result_or_err_msg)
return false, ""
else
return result_or_err_msg, ""
end
else
return false, "Failed to load lz4"
end
end