-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkristapi.lua
65 lines (63 loc) · 1.92 KB
/
kristapi.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
local api = {}
--local json = require("json")
local url = "https://krist.dev"
api.makeTransaction = function(privateKey, targetAddress, value, metadata)
if metadata == nil then
metadata = ""
end
local kutyus = {
["privatekey"]=privateKey,
["to"]=targetAddress,
["amount"]=value,
["metadata"]=metadata
}
local request = http.post(url.."/transactions/", --[[json.encode(kutyus)]]textutils.serialiseJSON(kutyus), {["Content-Type"] = "application/json"})
return request.readAll()
end
api.getAddress = function(address)
local request = http.get(url.."/addresses/"..address)
--local data = json.decode(request.readAll())
local data = textutils.unserialiseJSON(request.readAll())
return data.address
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
api.parseMeta = function(meta)
local out = {}
local a = mysplit(meta, ";")
for k,v in ipairs(a) do
local b = mysplit(v, "=")
if b[2] ~= nil then
out[b[1]] = b[2]
else
-- if matches the format of a krist address with metaname (ie [email protected]), we get the metaname, aka the test
if(b[1]:match("^.+@.+%.kst$")) then
local c = mysplit(b[1], "@")
out["metaname"] = c[1]
end
--out[b[1]] = true
end
end
return out
end
api.websocket = function()
local fr = http.post(url.."/ws/start","")
--local data = json.decode(fr.readAll())
local data = textutils.unserialiseJSON(fr.readAll())
local soc = data.url
local socket = http.websocket(soc)
--[[print(socket.send('{"type":"subscribe","event":"transactions","id":1}'))
while true do
print(socket.receive())
end]]
return socket
end
return api