shop refactor
This commit is contained in:
@@ -356,43 +356,6 @@ function parseMeta(meta)
|
|||||||
return tbl
|
return tbl
|
||||||
end
|
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]<q then r[2]=r[2]+1;r[1]=q-(e-1-r[1])-1 else r[1]=r[1]+qVUrVSYs(t)Su=#t;t[#t+1]=0x80;while#t%64~=56Zt[#t+1]=0VSv=q({0,0},u*8)fWw=2,1,-1Zt[#t+1]=a(k(a(v[w]TFF000000),24)TFF)t[#t+1]=a(k(a(v[w]TFF0000),16)TFF)t[#t+1]=a(k(a(v[w]TFF00),8)TFF)t[#t+1]=a(v[w]TFF)VUtVSYx(y,w)Uc(y[w]W0,24)+c(y[w+1]W0,16)+c(y[w+2]W0,8)+(y[w+3]W0)VSYz(t,w,A)SB={}fWC=1,16ZB[C]=x(t,w+(C-1)*4)VfWC=17,64ZSD=B[C-15]SE=b(b(f(B[C-15],7),f(B[C-15],18)),k(B[C-15],3))SF=b(b(f(B[C-2],17),f(B[C-2],19)),k(B[C-2],10))B[C]=(B[C-16]+E+B[C-7]+F)%eVSG,h,H,I,J,j,K,L=d(A)fWC=1,64ZSM=b(b(f(J,6),f(J,11)),f(J,25))SN=b(a(J,j),a(Xbnot(J),K))SO=(L+M+N+p[C]+B[C])%eSP=b(b(f(G,2),f(G,13)),f(G,22))SQ=b(b(a(G,h),a(G,H)),a(h,H))SR=(P+Q)%e;L,K,j,J,I,H,h,G=K,j,J,(I+O)%e,H,h,G,(O+R)%eVA[1]=(A[1]+G)%e;A[2]=(A[2]+h)%e;A[3]=(A[3]+H)%e;A[4]=(A[4]+I)%e;A[5]=(A[5]+J)%e;A[6]=(A[6]+j)%e;A[7]=(A[7]+K)%e;A[8]=(A[8]+L)%eUAVUY(t)t=t W""t=type(t)=="string"and{t:byte(1,-1)}Wt;t=s(t)SA={d(o)}fWw=1,#t,64ZA=z(t,w,A)VU("%08x"):rep(8):format(d(A))V',"S"," local "),"T",",0x"),"U"," return "),"V"," end "),"W","or "),"X","bit32."),"Y","function "),"Z"," do "))()
|
|
||||||
|
|
||||||
function makeaddressbyte(byte)
|
|
||||||
local byte = 48 + math.floor(byte / 7)
|
|
||||||
return string.char(byte + 39 > 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 {
|
return {
|
||||||
init = init,
|
init = init,
|
||||||
address = address,
|
address = address,
|
||||||
@@ -407,7 +370,4 @@ return {
|
|||||||
connect = connect,
|
connect = connect,
|
||||||
parseMeta = parseMeta,
|
parseMeta = parseMeta,
|
||||||
sha256 = sha256,
|
sha256 = sha256,
|
||||||
makeaddressbyte = makeaddressbyte,
|
|
||||||
makev2address = makev2address,
|
|
||||||
toKristWalletFormat = toKristWalletFormat
|
|
||||||
}
|
}
|
||||||
45
swshop/apis/krist.lua
Normal file
45
swshop/apis/krist.lua
Normal file
@@ -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
|
||||||
209
swshop/json
209
swshop/json
@@ -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
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
local Config = require('opus.config')
|
local Config = require('opus.config')
|
||||||
|
local Krist = require('swshop.krist')
|
||||||
local Util = require('opus.util')
|
local Util = require('opus.util')
|
||||||
|
|
||||||
local fs = _G.fs
|
local fs = _G.fs
|
||||||
@@ -8,10 +9,10 @@ local shell = _ENV.shell
|
|||||||
local programDir = fs.getDir(shell.getRunningProgram())
|
local programDir = fs.getDir(shell.getRunningProgram())
|
||||||
os.loadAPI(programDir .. '/'.. 'json')
|
os.loadAPI(programDir .. '/'.. 'json')
|
||||||
|
|
||||||
local w = require("w")
|
local w = require("swshop.w")
|
||||||
local r = require("r")
|
local r = require("swshop.r")
|
||||||
local k = require("k")
|
local k = require("swshop.k")
|
||||||
local jua = require("jua")
|
local jua = require("swshop.jua")
|
||||||
|
|
||||||
local await = jua.await
|
local await = jua.await
|
||||||
local device = _G.device
|
local device = _G.device
|
||||||
@@ -30,8 +31,8 @@ k.init(jua, json, w, r)
|
|||||||
|
|
||||||
local node = ({ ... })[1] or error('Node name is required')
|
local node = ({ ... })[1] or error('Node name is required')
|
||||||
local config = storage[node]
|
local config = storage[node]
|
||||||
local privatekey = config.isPrivateKey and config.password or k.toKristWalletFormat(config.password)
|
local privatekey = config.isPrivateKey and config.password or Krist.toKristWalletFormat(config.password)
|
||||||
local address = k.makev2address(privatekey)
|
local address = Krist.makev2address(privatekey)
|
||||||
|
|
||||||
jua.on("terminate", function()
|
jua.on("terminate", function()
|
||||||
rs.setOutput('top', false)
|
rs.setOutput('top', false)
|
||||||
|
|||||||
Reference in New Issue
Block a user