Skip to content

Instantly share code, notes, and snippets.

@znepb
Created August 4, 2020 02:32
Show Gist options
  • Save znepb/2cc5e39773df0498f27112a957752e52 to your computer and use it in GitHub Desktop.
Save znepb/2cc5e39773df0498f27112a957752e52 to your computer and use it in GitHub Desktop.
local api = {}
local function formPostString(data)
local out = ""
for i, v in pairs(data) do
if i == 1 then
out = textutils.urlEncode(i) .. "=" .. textutils.urlEncode(v)
else
out = out .. "&" .. textutils.urlEncode(i) .. "=" .. textutils.urlEncode(v)
end
end
return out
end
function api:new(address, key)
local o = {
address = address,
key = key
}
setmetatable(o, self)
self.__index = self
return o
end
function api:makeTransaction(to, amount, metadata)
local out = http.post("http://krist.ceriat.net/transactions/", formPostString({
privatekey = self.key,
to = to,
amount = amount,
metadata = metadata
}))
local data = json.decode(out.readAll())
out.close()
return data
end
local function parseMeta(meta)
asserttype(meta, "meta", "string")
local tbl = {meta={}}
for m in meta:gmatch("[^;]+") do
if m:match(domainMatch) then
-- print("Matched domain")
local p1, p2 = m:match("([%l%d-_]*)@"), m:match("@?([%l%d-]+).kst")
tbl.name = p1
tbl.domain = p2
elseif m:match(commonMetaMatch) then
-- print("Matched common meta")
local p1, p2 = m:match(commonMetaMatch)
tbl.meta[p1] = p2
else
-- print("Unmatched standard meta")
table.insert(tbl.meta, m)
end
-- print(m)
end
-- print(textutils.serialize(tbl))
return tbl
end
function api:initWesocket()
local out = http.post("http://krist.ceriat.net/ws/start", formPostString({privatekey = self.key}))
local data = json.decode(out.readAll())
out.close()
local ws, err = http.websocket(data.url)
self.ws = ws
while true do
local e = {os.pullEvent()}
if e[1] == "websocket_message" then
local url, contents = e[2], e[3]
local jsonData = json.decode(contents)
if jsonData.type == "hello" then
os.queueEvent("krist", "hello", {
serverTime = jsonData["server_time"],
motd = jsonData.motd,
motdSet = jsonData["motd_set"],
lastBlock = {
height = jsonData["last_block"].height,
address = jsonData["last_block"].address,
hash = jsonData["last_block"].hash,
shortHash = jsonData["last_block"]["short_hash"],
value = jsonData["last_block"].value,
time = jsonData["last_block"].time,
difficulty = jsonData["last_block"] .difficulty
},
work = jsonData.work
})
elseif e[1] == "event" then
local jsonData = json.decode(contents)
if jsonData.event == "block" then
local b = jsonData.block
os.queueEvent("krist", "block", {
height = b.height,
address = b.address,
hash = b.hash,
shortHash = b["short_hash"],
value = b.value,
time = b.time,
difficulty = b.difficulty,
newWork = jaonData["new_work"]
})
elseif jsonData.event == "transaction" then
local t = jsonData.transaction
os.queueEvent("krist", "transaction", {
id = t.id,
from = t.from,
to = t.to,
value = t.value,
time = t.time,
metadata = parseMeta(t.metadata)
})
end
print(contents)
end
end
end
end
return api
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment