kp-bank/bank-client.lua

134 lines
3.7 KiB
Lua
Raw Normal View History

2023-08-29 16:27:38 +00:00
--[[
The bank-client is a library that applications can include to interact with
a central bank server. Note that it functions over the Rednet protocol, so you
should call `rednet.open("modem-name")` first.
]]--
local client = {}
client.state = {
auth = nil,
hostId = nil,
2023-08-29 19:36:53 +00:00
securityKey = nil,
2023-08-29 16:27:38 +00:00
timeout = 3
}
local function requestRaw(msg)
if not client.state.hostId or not rednet.isOpen() then
return {success = false, error = "Client not initialized"}
end
rednet.send(client.state.hostId, msg, "BANK")
local remoteId, response = rednet.receive("BANK", client.state.timeout)
if not remoteId then
return {success = false, error = "Request timed out"}
end
return response
end
local function request(command, data)
return requestRaw({command = command, data = data})
end
2023-08-29 19:36:53 +00:00
local function requestAuth(command, data, secure)
secure = secure or false
2023-08-29 16:27:38 +00:00
if not client.loggedIn() then
return {success = false, error = "Client not logged in"}
end
2023-08-29 19:36:53 +00:00
local authInfo = {
username = client.state.auth.username,
password = client.state.auth.password
}
if secure and not client.state.securityKey then
return {success = false, error = "Missing security key for secure request."}
end
2023-08-29 19:38:32 +00:00
authInfo.key = client.state.securityKey
2023-08-29 19:36:53 +00:00
return requestRaw({command = command, auth = authInfo, data = data})
2023-08-29 16:27:38 +00:00
end
-- Base functions
2023-08-29 19:36:53 +00:00
function client.init(modemName, host, securityKey)
2023-08-29 16:27:38 +00:00
rednet.open(modemName)
client.state.hostId = rednet.lookup("BANK", host)
2023-08-29 19:36:53 +00:00
client.state.securityKey = securityKey or nil
2023-08-29 16:27:38 +00:00
return client.state.hostId ~= nil
end
function client.logIn(username, password)
client.state.auth = {username = username, password = password}
end
function client.logOut()
client.state.auth = nil
end
function client.loggedIn()
return client.state.auth ~= nil
end
-- BANK functions
function client.getStatus()
local response = request("STATUS")
return response.success, response.error
end
function client.createUser(username, password)
local response = request("CREATE_USER", {username = username, password = password})
return response.success, response.error
end
function client.deleteUser()
local response = requestAuth("DELETE_USER")
return response.success
end
function client.renameUser(newUsername)
local response = requestAuth("RENAME_USER", {newUsername = newUsername})
return response.success, response.error
end
function client.getAccounts()
local response = requestAuth("GET_ACCOUNTS")
if not response.success then
return nil, response.error
end
return response.data
end
2023-09-17 14:14:56 +00:00
function client.getAccount(accountId)
local response = requestAuth("GET_ACCOUNT", {accountId = accountId})
if not response.success then
return nil, response.error
end
return response.data
end
2023-08-29 16:27:38 +00:00
function client.createAccount(accountName)
local response = requestAuth("CREATE_ACCOUNT", {name = accountName})
if not response.success then
return nil, response.error
end
return response.data
end
function client.deleteAccount(accountId)
local response = requestAuth("DELETE_ACCOUNT", {accountId = accountId})
return response.success
end
function client.renameAccount(accountId, newName)
local response = requestAuth("RENAME_ACCOUNT", {accountId = accountId, newName = newName})
return response.success, response.error
end
2023-08-29 19:36:53 +00:00
function client.recordTransaction(accountId, amount, description)
local response = requestAuth("RECORD_TRANSACTION", {accountId = accountId, amount = amount, description = description}, true)
if not response.success then
return nil, response.error
end
return response.data
end
2023-08-29 16:27:38 +00:00
return client