-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkristapi.lua
More file actions
104 lines (96 loc) · 2.53 KB
/
kristapi.lua
File metadata and controls
104 lines (96 loc) · 2.53 KB
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
--[[
Kristapi
Made by: afonya2@github
]]
local api = {}
local server = "https://krist.dev"
function api.getAddress(address)
local requ = http.get(server.."/addresses/"..address)
local out = textutils.unserialiseJSON(requ.readAll())
if out.ok then
return out.address
else
error(out.message)
end
end
function api.getBalance(address)
local requ = http.get(server.."/addresses/"..address)
local out = textutils.unserialiseJSON(requ.readAll())
if out.ok then
return out.address.balance
else
error(out.message)
end
end
function api.getTransactions(address)
local requ = http.get(server.."/addresses/"..address.."/transactions")
local out = textutils.unserialiseJSON(requ.readAll())
if out.ok then
return out.transactions
else
error(out.message)
end
end
function mysplit (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function api.parseMeta(meta)
if (meta == nil) or (meta == "") then
return {}
end
local out = {}
local a = mysplit(meta, ";")
for k,v in ipairs(a) do
local b = mysplit(v, "=")
if b[1] ~= nil then
if b[2] ~= nil then
out[b[1]] = b[2]
else
-- if matches the format of a krist address with metaname (ie test@shop.kst), we get the metaname, aka the test
if(b[1]:match("^.+@.+%.kst$")) then
local c = mysplit(b[1], "@")
out["metaname"] = c[1]
else
out[b[1]] = true
end
end
end
end
return out
end
function api.makeTransaction(privKey, to, amount, meta)
if meta == nil then
meta = ""
end
local kutyus = {
privatekey = privKey,
to = to,
amount = amount,
metadata = meta
}
local requ = http.post(server.."/transactions", textutils.serialiseJSON(kutyus), {["Content-Type"] = "application/json"})
local out = textutils.unserialiseJSON(requ.readAll())
if out.ok then
return true
else
error(out.message)
end
end
function api.websocket()
local requ = http.post(server.."/ws/start","")
local out = textutils.unserialiseJSON(requ.readAll())
if out.ok then
local sock = http.websocket(out.url)
return sock
else
error(out.message)
end
end
return api