Sirshark10

Smallet

Jun 24th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --Sha256 stuff here
  2. local pass = nil
  3. local addr = nil
  4. local bal = nil
  5. local mkey = nil
  6.  
  7. local MOD = 2^32
  8. local MODM = MOD-1
  9.  
  10. local function memoize(f)
  11.     local mt = {}
  12.     local t = setmetatable({}, mt)
  13.     function mt:__index(k)
  14.         local v = f(k)
  15.         t[k] = v
  16.         return v
  17.     end
  18.     return t
  19. end
  20.  
  21. local function make_bitop_uncached(t, m)
  22.     local function bitop(a, b)
  23.         local res,p = 0,1
  24.         while a ~= 0 and b ~= 0 do
  25.             local am, bm = a % m, b % m
  26.             res = res + t[am][bm] * p
  27.             a = (a - am) / m
  28.             b = (b - bm) / m
  29.             p = p*m
  30.         end
  31.         res = res + (a + b) * p
  32.         return res
  33.     end
  34.     return bitop
  35. end
  36.  
  37. local function make_bitop(t)
  38.     local op1 = make_bitop_uncached(t,2^1)
  39.     local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  40.     return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  41. end
  42.  
  43. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  44.  
  45. local function bxor(a, b, c, ...)
  46.     local z = nil
  47.     if b then
  48.         a = a % MOD
  49.         b = b % MOD
  50.         z = bxor1(a, b)
  51.         if c then z = bxor(z, c, ...) end
  52.         return z
  53.     elseif a then return a % MOD
  54.     else return 0 end
  55. end
  56.  
  57. local function band(a, b, c, ...)
  58.     local z
  59.     if b then
  60.         a = a % MOD
  61.         b = b % MOD
  62.         z = ((a + b) - bxor1(a,b)) / 2
  63.         if c then z = bit32_band(z, c, ...) end
  64.         return z
  65.     elseif a then return a % MOD
  66.     else return MODM end
  67. end
  68.  
  69. local function bnot(x) return (-1 - x) % MOD end
  70.  
  71. local function rshift1(a, disp)
  72.     if disp < 0 then return lshift(a,-disp) end
  73.     return math.floor(a % 2 ^ 32 / 2 ^ disp)
  74. end
  75.  
  76. local function rshift(x, disp)
  77.     if disp > 31 or disp < -31 then return 0 end
  78.     return rshift1(x % MOD, disp)
  79. end
  80.  
  81. local function lshift(a, disp)
  82.     if disp < 0 then return rshift(a,-disp) end
  83.     return (a * 2 ^ disp) % 2 ^ 32
  84. end
  85.  
  86. local function rrotate(x, disp)
  87.     x = x % MOD
  88.     disp = disp % 32
  89.     local low = band(x, 2 ^ disp - 1)
  90.     return rshift(x, disp) + lshift(low, 32 - disp)
  91. end
  92.  
  93. local k = {
  94.     0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  95.     0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  96.     0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  97.     0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  98.     0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  99.     0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  100.     0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  101.     0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  102.     0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  103.     0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  104.     0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  105.     0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  106.     0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  107.     0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  108.     0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  109.     0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  110. }
  111.  
  112. local function str2hexa(s)
  113.     return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  114. end
  115.  
  116. local function num2s(l, n)
  117.     local s = ""
  118.     for i = 1, n do
  119.         local rem = l % 256
  120.         s = string.char(rem) .. s
  121.         l = (l - rem) / 256
  122.     end
  123.     return s
  124. end
  125.  
  126. local function s232num(s, i)
  127.     local n = 0
  128.     for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  129.     return n
  130. end
  131.  
  132. local function preproc(msg, len)
  133.     local extra = 64 - ((len + 9) % 64)
  134.     len = num2s(8 * len, 8)
  135.     msg = msg .. "\128" .. string.rep("\0", extra) .. len
  136.     assert(#msg % 64 == 0)
  137.     return msg
  138. end
  139.  
  140. local function initH256(H)
  141.     H[1] = 0x6a09e667
  142.     H[2] = 0xbb67ae85
  143.     H[3] = 0x3c6ef372
  144.     H[4] = 0xa54ff53a
  145.     H[5] = 0x510e527f
  146.     H[6] = 0x9b05688c
  147.     H[7] = 0x1f83d9ab
  148.     H[8] = 0x5be0cd19
  149.     return H
  150. end
  151.  
  152. local function digestblock(msg, i, H)
  153.     local w = {}
  154.     for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  155.     for j = 17, 64 do
  156.         local v = w[j - 15]
  157.         local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  158.         v = w[j - 2]
  159.         w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  160.     end
  161.  
  162.     local a, b, c, d, e, f, g, h = H[1], H[2], H[3], H[4], H[5], H[6], H[7], H[8]
  163.     for i = 1, 64 do
  164.         local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  165.         local maj = bxor(band(a, b), band(a, c), band(b, c))
  166.         local t2 = s0 + maj
  167.         local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  168.         local ch = bxor (band(e, f), band(bnot(e), g))
  169.         local t1 = h + s1 + ch + k[i] + w[i]
  170.         h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  171.     end
  172.  
  173.     H[1] = band(H[1] + a)
  174.     H[2] = band(H[2] + b)
  175.     H[3] = band(H[3] + c)
  176.     H[4] = band(H[4] + d)
  177.     H[5] = band(H[5] + e)
  178.     H[6] = band(H[6] + f)
  179.     H[7] = band(H[7] + g)
  180.     H[8] = band(H[8] + h)
  181. end
  182.  
  183. local function sha256(msg)
  184.     msg = preproc(msg, #msg)
  185.     local H = initH256({})
  186.     for i = 1, #msg, 64 do digestblock(msg, i, H) end
  187.     return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  188.         num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  189. end
  190.  
  191. local function makeaddressbyte(j)
  192.   if j <= 6 then return "0"
  193.   elseif j <= 13 then return "1"
  194.   elseif j <= 20 then return "2"
  195.   elseif j <= 27 then return "3"
  196.   elseif j <= 34 then return "4"
  197.   elseif j <= 41 then return "5"
  198.   elseif j <= 48 then return "6"
  199.   elseif j <= 55 then return "7"
  200.   elseif j <= 62 then return "8"
  201.   elseif j <= 69 then return "9"
  202.   elseif j <= 76 then return "a"
  203.   elseif j <= 83 then return "b"
  204.   elseif j <= 90 then return "c"
  205.   elseif j <= 97 then return "d"
  206.   elseif j <= 104 then return "e"
  207.   elseif j <= 111 then return "f"
  208.   elseif j <= 118 then return "g"
  209.   elseif j <= 125 then return "h"
  210.   elseif j <= 132 then return "i"
  211.   elseif j <= 139 then return "j"
  212.   elseif j <= 146 then return "k"
  213.   elseif j <= 153 then return "l"
  214.   elseif j <= 160 then return "m"
  215.   elseif j <= 167 then return "n"
  216.   elseif j <= 174 then return "o"
  217.   elseif j <= 181 then return "p"
  218.   elseif j <= 188 then return "q"
  219.   elseif j <= 195 then return "r"
  220.   elseif j <= 202 then return "s"
  221.   elseif j <= 209 then return "t"
  222.   elseif j <= 216 then return "u"
  223.   elseif j <= 223 then return "v"
  224.   elseif j <= 230 then return "w"
  225.   elseif j <= 237 then return "x"
  226.   elseif j <= 244 then return "y"
  227.   elseif j <= 251 then return "z"
  228.   else return "e"
  229.   end
  230. end
  231.  
  232. function makev2address(key)
  233.   local protein = {}
  234.   local stick = sha256(sha256(key))
  235.   local n = 0
  236.   local link = 0
  237.   local v2 = "k"
  238.   repeat
  239.     if n < 9 then protein[n] = string.sub(stick,0,2)
  240.     stick = sha256(sha256(stick)) end
  241.     n = n + 1
  242.   until n == 9
  243.   n = 0
  244.   repeat
  245.     link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9
  246.     if string.len(protein[link]) ~= 0 then
  247.       v2 = v2 .. makeaddressbyte(tonumber(protein[link],16))
  248.       protein[link] = ''
  249.       n = n + 1
  250.     else
  251.       stick = sha256(stick)
  252.     end
  253.   until n == 9
  254.   return v2
  255. end
  256. --Rest of the code
  257.  
  258. --Creates a privatekey out of the password
  259. function ToPriv(input)
  260.     return sha256("KRISTWALLET"..input).."-000"
  261. end
  262.  
  263. local site = "http://krist.ceriat.net/" --The site for all requests
  264.  
  265. --Get info on an address
  266. function getInfo(addr)
  267. return http.get(site.."addresses/"..addr).readAll()
  268. end
  269.  
  270. --Send Funds
  271. function sendFunds(InP,to,amount)
  272.     return http.post(site.."transactions/","privatekey="..ToPriv(InP).."&to="..to.."&amount="..amount).readAll()
  273. end
  274.  
  275. --Lookup a NAME.kst
  276. function nameLook(NAME)
  277.     return http.get(site.."names/"..NAME).readAll()
  278. end
  279.  
  280. --Register NAME.kst to given password's private key address.
  281. function nameReg(NAME,InP)
  282.     return http.post(site.."names/"..NAME.."/","privatekey="..ToPriv(InP)).readAll()
  283. end
  284.  
  285.  
  286.  
  287. --Graphics and non-kLua stuff
  288. local t = '"ok":false'
  289. local f = '"ok":true'
  290. local pass = "*"
  291. local clicks
  292.  
  293. local names
  294. local transactions
  295.  
  296. --Pulls clicks
  297. local function clicks()
  298. while true do
  299. sleep(0)
  300.     evnt, btn, x, y = os.pullEvent("mouse_click")
  301.     return x, y
  302. end
  303. end
  304.  
  305. --Gets the password and then creates a private key and address
  306. local function getPass()
  307. term.clear()
  308. term.setCursorPos(1,1)
  309. write("Enter Desired Password: ")
  310. pass = read("*")
  311. hashPass = sha256("KRISTWALLET"..pass)
  312. mKey = hashPass.."-000"
  313. http.post("http://krist.ceriat.net/login","privatekey="..mKey)
  314. addr = makev2address(mKey)
  315. end
  316.  
  317. --Gets the balance of the address
  318. local function getBal()
  319. ret = getInfo(addr)
  320. if string.find(ret,f) then
  321.     local x,y = string.find(ret,'"balance":')
  322.     local x2,y2 = string.find(ret,'"tot')
  323.     local nS = string.sub(ret,x,x2-1)
  324.     bal = string.sub(nS,11,string.len(nS)-1)
  325.     print(bal)
  326.     return bal
  327. else
  328. ret = getInfo(addr)
  329.     local x,y = string.find(ret,'"balance":')
  330.     local x2,y2 = string.find(ret,'"tot')
  331.     local nS = string.sub(ret,x,x2-1)
  332.     bal = string.sub(nS,11,string.len(nS)-1)
  333.     print(bal)
  334.    
  335.     return bal
  336. end
  337. end
  338.  
  339.  
  340. --Transactions Menu
  341. local function transactions()
  342. while true do
  343. mBox.clear()
  344. mBox.setCursorPos(1,1)
  345. mBox.write("Transactions")
  346. mBox.setCursorPos(1,4)
  347. mBox.write("Send")
  348. x,y = clicks()
  349.     if x >= 6 and x <= 9 and y == 4 then
  350.         mBox.clear()
  351.         mBox.setCursorPos(1,4)
  352.         mBox.write("Enter Recipient: ")
  353.         term.redirect(mBox)
  354.         Pers = read()
  355.         mBox.setCursorPos(1,5)
  356.         mBox.write("Enter Amount to Send: ")
  357.         Amt = read()
  358.         Ret = sendFunds(pass,Pers,Amt)
  359.         if string.find(Ret,"invalid_parameter") then
  360.             print("Invalid address or amount supplied")
  361.         else
  362.             if string.find(Ret,t) then
  363.                 print("Insufficient Funds")
  364.                 print("Click to Continue")
  365.                 clicks()
  366.                 transactions()
  367.             elseif string.find(Ret,f) then
  368.                 print("Transaction Complete")
  369.                 print("Click to Continue")
  370.                 clicks()
  371.                 transactions()
  372.             end
  373.         end
  374.     elseif x == 5 and y == 1 then
  375.         transactions()
  376.     elseif x == 5 and y == 2 then
  377.         names()
  378.     elseif x == 1 and y <= 7 then
  379.     init()
  380.     end
  381. end
  382. end
  383.  
  384. --Names menu
  385. local function names()
  386. while true do
  387.     mBox.clear()
  388.     mBox.setCursorPos(1,1)
  389.     mBox.write(addr.." ")
  390.     mBox.setCursorPos(1,3)
  391.     mBox.write("Register")
  392.     mBox.setCursorPos(1,4)
  393.     mBox.write("Lookup")
  394.     x,y = clicks()
  395.     if x >= 6 and x <= 13 and y == 3 then
  396.         mBox.clear()
  397.         mBox.setCursorPos(1,4)
  398.         mBox.write("Enter desired Name (without .kst): ")
  399.         term.redirect(mBox)
  400.         desName = read()
  401.         Ret = nameLook(desName)
  402.             if string.find(Ret,t) then
  403.                 print("Name Available")
  404.                 print("Attempting Registration")
  405.                 Ret = nameReg(desName,pass)
  406.                     if string.find(Ret,t) then
  407.                         print("Insufficient Funds")
  408.                         clicks()
  409.                         names()
  410.                     else
  411.                         print("Name "..desName.." Registered")
  412.                         clicks()
  413.                         names()
  414.                     end
  415.             else
  416.                 print("Name Unavailable")
  417.                 clicks()
  418.             end
  419.            
  420.     elseif x >= 6 and x <= 11 and y == 4 then
  421.         mBox.clear()
  422.         mBox.setCursorPos(1,1)
  423.         mBox.write("Name Lookup")
  424.         mBox.setCursorPos(1,4)
  425.         mBox.write("Enter desired Name (without .kst): ")
  426.         term.redirect(mBox)
  427.         desName = read()
  428.         Ret = nameLook(desName)
  429.             if string.find(Ret,'"ok":true') then
  430.                 if string.find(Ret,'"owner":') then
  431.                     x1,y1 = string.find(Ret,'"owner":')
  432.                     x2,y2 = string.find(Ret,'"reg')
  433.                     nS = string.sub(Ret,x1,x2-2)
  434.                     print(nS)
  435.                 end
  436.                 if string.find(Ret,'"registered":') then
  437.                     x1,y1 = string.find(Ret,'"registered":')
  438.                     x2,y2 = string.find(Ret,'"upd')
  439.                     nS = string.sub(Ret,x1,x2-2)
  440.                     print(nS)
  441.                     print("Click to continue")
  442.                     clicks()
  443.                     names()
  444.                 end
  445.             else
  446.                 print("Name Unregistered or Invalid")
  447.                 print("Click to continue")
  448.                 clicks()
  449.                 names()
  450.             end
  451.     elseif x == 5 and y == 1 then
  452.     transactions()
  453.     elseif x == 5 and y == 2 then
  454.     names()
  455.     elseif x == 1 and y <= 7 then
  456.     init()
  457.     end
  458.     end
  459. end
  460.  
  461.  
  462. --Initiate the Program
  463. function init()
  464. sBar.setBackgroundColor(colors.blue)
  465. sBar.setTextColor(colors.red)
  466. sBar.clear()
  467.   sBar.setCursorPos(1,1)
  468.   sBar.write("S")
  469.   sBar.setCursorPos(1,2)
  470.   sBar.write("M")
  471.   sBar.setCursorPos(1,3)
  472.   sBar.write("A")
  473.   sBar.setCursorPos(1,4)
  474.   sBar.write("L")
  475.   sBar.setCursorPos(1,5)
  476.   sBar.write("L")
  477.   sBar.setCursorPos(1,6)
  478.   sBar.write("E")
  479.   sBar.setCursorPos(1,7)
  480.   sBar.write("T")
  481.   sBar.setTextColor(colors.green)
  482.   sBar.setCursorPos(5,1)
  483.   sBar.write("T")
  484.   sBar.setCursorPos(5,2)
  485.   sBar.write("N")
  486.  
  487.   mBox.setBackgroundColor(colors.white)
  488.   mBox.setTextColor(colors.black)
  489.   mBox.clear()
  490.   mBox.setCursorPos(5,1)
  491.  
  492.   mBox.write(addr.."    "..getBal())
  493. x,y = clicks()
  494. if x == 5 and y == 1 then
  495. transactions()
  496. elseif x == 5 and y == 2 then
  497. names()
  498. else
  499. init()
  500. end
  501. end
  502.  
  503. local function make(IN)
  504. hashPass = sha256("KRISTWALLET"..IN)
  505. mKey = hashPass.."-000"
  506. addr = makev2address(mKey)
  507. http.post("http://krist.ceriat.net/login","privatekey="..mKey)
  508. end
  509.  
  510. function bInit()
  511. term.clear()
  512. term.setCursorPos(1,1)
  513. print("Welcome to Smallet")
  514. print("Enter your password: ")
  515. pass = read("*")
  516. make(pass)
  517. print("type ? to get started")
  518. while true do
  519. print("Your current balance is "..getBal())
  520. Cmd = read()
  521. if Cmd:lower() == "exit" then
  522. addr = nil
  523. pass = nil
  524. mKey = nil
  525. hashPass = nil
  526. term.clear()
  527. term.setCursorPos(1,1)
  528. return
  529. elseif Cmd == "?" then
  530.     print("type any of the following to access the respective menu's")
  531.     print("----")
  532.     print("Transactions")
  533.     print("Names")
  534.     print("Exit")
  535.     print("----")
  536. elseif Cmd:lower() == "transactions" then
  537.     print("Only one function currently implimented: Sending")
  538.     print("Type address or .kst name to send to: ")
  539.     local To = read()
  540.     print("Amount: ")
  541.     local amt = read()
  542.     if type(tonumber(amt)) ~= "number" then
  543.         print("Amount must be a number")
  544.         print("Press any key to continue")
  545.             sleep(0)
  546.             os.pullEvent("key")
  547.             term.setCursorPos(1,1)
  548.             term.clear()
  549.     end
  550.     if tonumber(amt) > tonumber(getBal()) then
  551.         print("Insufficient Funds")
  552.         print("Press any key to continue")
  553.             sleep(0)
  554.             os.pullEvent("key")
  555.             term.setCursorPos(1,1)
  556.             term.clear()
  557.     else
  558.    
  559.         ret = sendFunds(pass,To,amt)
  560.             if string.find(ret,"invalid_parameter") then
  561.             print("Invalid address or amount supplied")
  562.             print("Press any key to continue")
  563.             sleep(0)
  564.             os.pullEvent("key")
  565.             term.setCursorPos(1,1)
  566.             term.clear()
  567.             end
  568.    
  569.         end
  570. elseif Cmd:lower() == "names" then
  571.         print("Enter 'Lookup' or 'Register' (non case sensitive)")
  572.         nCmd = read()
  573.         if nCmd:lower() == "lookup" then
  574.         term.clear()
  575.         term.setCursorPos(1,1)
  576.         print("Enter name to lookup")
  577.         --lookup code
  578.        
  579.             desName = read()
  580.         Ret = nameLook(desName)
  581.             if string.find(Ret,'"ok":true') then
  582.                 if string.find(Ret,'"owner":') then
  583.                     x1,y1 = string.find(Ret,'"owner":')
  584.                     x2,y2 = string.find(Ret,'"reg')
  585.                     nS = string.sub(Ret,x1,x2-2)
  586.                     print(nS)
  587.                 end
  588.                 if string.find(Ret,'"registered":') then
  589.                     x1,y1 = string.find(Ret,'"registered":')
  590.                     x2,y2 = string.find(Ret,'"upd')
  591.                     nS = string.sub(Ret,x1,x2-2)
  592.                     print(nS)
  593.                     print("Press any key to continue")
  594.                     sleep(0)
  595.                     os.pullEvent("key")
  596.                     term.setCursorPos(1,1)
  597.                     term.clear()
  598.                 end
  599.             else
  600.                 print("Name Unregistered or Invalid")
  601.                 print("Press any key to continue")
  602.                     sleep(0)
  603.                     os.pullEvent("key")
  604.                     term.setCursorPos(1,1)
  605.                     term.clear()
  606.             end--lookup code end
  607.         elseif nCmd:lower() == "register" then
  608.         term.clear()
  609.         term.setCursorPos(1,1)
  610.         print("Enter name to register")
  611.             --Register Code Incoming
  612.             desName = read()
  613.         Ret = nameLook(desName)
  614.             if string.find(Ret,t) then
  615.                 print("Name Available")
  616.                 print("Attempting Registration")
  617.                 Ret = nameReg(desName,pass)
  618.                     if string.find(Ret,t) then
  619.                         print("Insufficient Funds")
  620.                         print("Press any key to continue")
  621.                         sleep(0)
  622.                         os.pullEvent("key")
  623.                         term.setCursorPos(1,1)
  624.                         term.clear()
  625.                     else
  626.                         print("Name "..desName.." Registered")
  627.                         print("Press any key to continue")
  628.                         sleep(0)
  629.                         os.pullEvent("key")
  630.                         term.setCursorPos(1,1)
  631.                         term.clear()
  632.                         end
  633.             else
  634.                 print("Name Unavailable")
  635.                 print("Press any key to continue")
  636.                     sleep(0)
  637.                     os.pullEvent("key")
  638.                     term.setCursorPos(1,1)
  639.                     term.clear()
  640.             end
  641.            
  642.            
  643.         end
  644. end
  645.  
  646. end
  647. end
  648.  
  649.  
  650. if term.isColor() then
  651. sBar = window.create(term.current(),1,1,5,19)
  652. mBox = window.create(term.current(),6,1,46,19)
  653. getPass()
  654. init()
  655. else
  656. bInit()
  657. end
Add Comment
Please, Sign In to add comment