diff --git a/swshop/jua.lua b/swshop/apis/jua.lua similarity index 100% rename from swshop/jua.lua rename to swshop/apis/jua.lua diff --git a/swshop/k.lua b/swshop/apis/k.lua similarity index 76% rename from swshop/k.lua rename to swshop/apis/k.lua index 7d0c61c..47ad499 100644 --- a/swshop/k.lua +++ b/swshop/apis/k.lua @@ -356,43 +356,6 @@ function parseMeta(meta) return tbl end -local g = string.gsub -sha256 = loadstring(g(g(g(g(g(g(g(g('Sa=XbandSb=XbxWSc=XlshiftSd=unpackSe=2^32SYf(g,h)Si=g/2^hSj=i%1Ui-j+j*eVSYk(l,m)Sn=l/2^mUn-n%1VSo={0x6a09e667Tbb67ae85T3c6ef372Ta54ff53aT510e527fT9b05688cT1f83d9abT5be0cd19}Sp={0x428a2f98T71374491Tb5c0fbcfTe9b5dba5T3956c25bT59f111f1T923f82a4Tab1c5ed5Td807aa98T12835b01T243185beT550c7dc3T72be5d74T80deb1feT9bdc06a7Tc19bf174Te49b69c1Tefbe4786T0fc19dc6T240ca1ccT2de92c6fT4a7484aaT5cb0a9dcT76f988daT983e5152Ta831c66dTb00327c8Tbf597fc7Tc6e00bf3Td5a79147T06ca6351T14292967T27b70a85T2e1b2138T4d2c6dfcT53380d13T650a7354T766a0abbT81c2c92eT92722c85Ta2bfe8a1Ta81a664bTc24b8b70Tc76c51a3Td192e819Td6990624Tf40e3585T106aa070T19a4c116T1e376c08T2748774cT34b0bcb5T391c0cb3T4ed8aa4aT5b9cca4fT682e6ff3T748f82eeT78a5636fT84c87814T8cc70208T90befffaTa4506cebTbef9a3f7Tc67178f2}SYq(r,q)if e-1-r[1] 122 and 101 or byte > 57 and byte + 39 or byte) -end - -function makev2address(key) - local protein = {} - local stick = sha256(sha256(key)) - local n = 0 - local link = 0 - local v2 = "k" - repeat - if n < 9 then protein[n] = string.sub(stick,0,2) - stick = sha256(sha256(stick)) end - n = n + 1 - until n == 9 - n = 0 - repeat - link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9 - if string.len(protein[link]) ~= 0 then - v2 = v2 .. makeaddressbyte(tonumber(protein[link],16)) - protein[link] = '' - n = n + 1 - else - stick = sha256(stick) - end - until n == 9 - return v2 -end - -function toKristWalletFormat(passphrase) - return sha256("KRISTWALLET"..passphrase).."-000" -end - return { init = init, address = address, @@ -407,7 +370,4 @@ return { connect = connect, parseMeta = parseMeta, sha256 = sha256, - makeaddressbyte = makeaddressbyte, - makev2address = makev2address, - toKristWalletFormat = toKristWalletFormat } \ No newline at end of file diff --git a/swshop/apis/krist.lua b/swshop/apis/krist.lua new file mode 100644 index 0000000..d250ed7 --- /dev/null +++ b/swshop/apis/krist.lua @@ -0,0 +1,45 @@ +local sha2 = require('opus.crypto.sha2') + +local Krist = { } + +local function sha256(key) + return sha2:digest(key):toHex() +end + +local function makeaddressbyte(byte) + byte = 48 + math.floor(byte / 7) + return string.char(byte + 39 > 122 and 101 or byte > 57 and byte + 39 or byte) +end + +function Krist.makev2address(key) + local protein = {} + local stick = sha256(sha256(key)) + local n = 0 + local v2 = "k" + repeat + if n < 9 then + protein[n] = string.sub(stick,0,2) + stick = sha256(sha256(stick)) + end + n = n + 1 + until n == 9 + + n = 0 + repeat + local link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9 + if string.len(protein[link]) ~= 0 then + v2 = v2 .. makeaddressbyte(tonumber(protein[link],16)) + protein[link] = '' + n = n + 1 + else + stick = sha256(stick) + end + until n == 9 + return v2 +end + +function Krist.toKristWalletFormat(passphrase) + return sha256('KRISTWALLET' .. passphrase) .. '-000' +end + +return Krist diff --git a/swshop/r.lua b/swshop/apis/r.lua similarity index 100% rename from swshop/r.lua rename to swshop/apis/r.lua diff --git a/swshop/w.lua b/swshop/apis/w.lua similarity index 100% rename from swshop/w.lua rename to swshop/apis/w.lua diff --git a/swshop/json b/swshop/json deleted file mode 100644 index 3bef482..0000000 --- a/swshop/json +++ /dev/null @@ -1,209 +0,0 @@ ------------------------------------------------------------------- utils -local controls = {["\n"]="\\n", ["\r"]="\\r", ["\t"]="\\t", ["\b"]="\\b", ["\f"]="\\f", ["\""]="\\\"", ["\\"]="\\\\"} - -local function isArray(t) - local max = 0 - for k,v in pairs(t) do - if type(k) ~= "number" then - return false - elseif k > max then - max = k - end - end - return max == #t -end - -local whites = {['\n']=true; ['\r']=true; ['\t']=true; [' ']=true; [',']=true; [':']=true} -function removeWhite(str) - while whites[str:sub(1, 1)] do - str = str:sub(2) - end - return str -end - ------------------------------------------------------------------- encoding - -local function encodeCommon(val, pretty, tabLevel, tTracking) - local str = "" - - -- Tabbing util - local function tab(s) - str = str .. ("\t"):rep(tabLevel) .. s - end - - local function arrEncoding(val, bracket, closeBracket, iterator, loopFunc) - str = str .. bracket - if pretty then - str = str .. "\n" - tabLevel = tabLevel + 1 - end - for k,v in iterator(val) do - tab("") - loopFunc(k,v) - str = str .. "," - if pretty then str = str .. "\n" end - end - if pretty then - tabLevel = tabLevel - 1 - end - if str:sub(-2) == ",\n" then - str = str:sub(1, -3) .. "\n" - elseif str:sub(-1) == "," then - str = str:sub(1, -2) - end - tab(closeBracket) - end - - -- Table encoding - if type(val) == "table" then - assert(not tTracking[val], "Cannot encode a table holding itself recursively") - tTracking[val] = true - if isArray(val) then - arrEncoding(val, "[", "]", ipairs, function(k,v) - str = str .. encodeCommon(v, pretty, tabLevel, tTracking) - end) - else - arrEncoding(val, "{", "}", pairs, function(k,v) - assert(type(k) == "string", "JSON object keys must be strings", 2) - str = str .. encodeCommon(k, pretty, tabLevel, tTracking) - str = str .. (pretty and ": " or ":") .. encodeCommon(v, pretty, tabLevel, tTracking) - end) - end - -- String encoding - elseif type(val) == "string" then - str = '"' .. val:gsub("[%c\"\\]", controls) .. '"' - -- Number encoding - elseif type(val) == "number" or type(val) == "boolean" then - str = tostring(val) - else - error("JSON only supports arrays, objects, numbers, booleans, and strings", 2) - end - return str -end - -function encode(val) - return encodeCommon(val, false, 0, {}) -end - -function encodePretty(val) - return encodeCommon(val, true, 0, {}) -end - ------------------------------------------------------------------- decoding - -local decodeControls = {} -for k,v in pairs(controls) do - decodeControls[v] = k -end - -function parseBoolean(str) - if str:sub(1, 4) == "true" then - return true, removeWhite(str:sub(5)) - else - return false, removeWhite(str:sub(6)) - end -end - -function parseNull(str) - return nil, removeWhite(str:sub(5)) -end - -local numChars = {['e']=true; ['E']=true; ['+']=true; ['-']=true; ['.']=true} -function parseNumber(str) - local i = 1 - while numChars[str:sub(i, i)] or tonumber(str:sub(i, i)) do - i = i + 1 - end - local val = tonumber(str:sub(1, i - 1)) - str = removeWhite(str:sub(i)) - return val, str -end - -function parseString(str) - str = str:sub(2) - local s = "" - while str:sub(1,1) ~= "\"" do - local next = str:sub(1,1) - str = str:sub(2) - assert(next ~= "\n", "Unclosed string") - - if next == "\\" then - local escape = str:sub(1,1) - str = str:sub(2) - - next = assert(decodeControls[next..escape], "Invalid escape character") - end - - s = s .. next - end - return s, removeWhite(str:sub(2)) -end - -function parseArray(str) - str = removeWhite(str:sub(2)) - - local val = {} - local i = 1 - while str:sub(1, 1) ~= "]" do - local v = nil - v, str = parseValue(str) - val[i] = v - i = i + 1 - str = removeWhite(str) - end - str = removeWhite(str:sub(2)) - return val, str -end - -function parseObject(str) - str = removeWhite(str:sub(2)) - - local val = {} - while str:sub(1, 1) ~= "}" do - local k, v = nil, nil - k, v, str = parseMember(str) - val[k] = v - str = removeWhite(str) - end - str = removeWhite(str:sub(2)) - return val, str -end - -function parseMember(str) - local k = nil - k, str = parseValue(str) - local val = nil - val, str = parseValue(str) - return k, val, str -end - -function parseValue(str) - local fchar = str:sub(1, 1) - if fchar == "{" then - return parseObject(str) - elseif fchar == "[" then - return parseArray(str) - elseif tonumber(fchar) ~= nil or numChars[fchar] then - return parseNumber(str) - elseif str:sub(1, 4) == "true" or str:sub(1, 5) == "false" then - return parseBoolean(str) - elseif fchar == "\"" then - return parseString(str) - elseif str:sub(1, 4) == "null" then - return parseNull(str) - end - return nil -end - -function decode(str) - str = removeWhite(str) - t = parseValue(str) - return t -end - -function decodeFromFile(path) - local file = assert(fs.open(path, "r")) - local decoded = decode(file.readAll()) - file.close() - return decoded -end \ No newline at end of file diff --git a/swshop/swshop.lua b/swshop/swshop.lua index 9e8aa0d..b7ae7e4 100644 --- a/swshop/swshop.lua +++ b/swshop/swshop.lua @@ -1,4 +1,5 @@ local Config = require('opus.config') +local Krist = require('swshop.krist') local Util = require('opus.util') local fs = _G.fs @@ -8,10 +9,10 @@ local shell = _ENV.shell local programDir = fs.getDir(shell.getRunningProgram()) os.loadAPI(programDir .. '/'.. 'json') -local w = require("w") -local r = require("r") -local k = require("k") -local jua = require("jua") +local w = require("swshop.w") +local r = require("swshop.r") +local k = require("swshop.k") +local jua = require("swshop.jua") local await = jua.await local device = _G.device @@ -30,8 +31,8 @@ k.init(jua, json, w, r) local node = ({ ... })[1] or error('Node name is required') local config = storage[node] -local privatekey = config.isPrivateKey and config.password or k.toKristWalletFormat(config.password) -local address = k.makev2address(privatekey) +local privatekey = config.isPrivateKey and config.password or Krist.toKristWalletFormat(config.password) +local address = Krist.makev2address(privatekey) jua.on("terminate", function() rs.setOutput('top', false)