Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.54 KB | None | 0 0
  1.  
  2. -- Wallet
  3. -- by Connor Delaney
  4.  
  5. if not http then
  6. error("Wallet requires HTTP to talk to the Krist server, please enable HTTP.", 0)
  7. end
  8.  
  9. if not term.isColour() then
  10. error("Wallet requires a display that supports colour, try using an advanced computer.", 0)
  11. end
  12.  
  13. local uData = {
  14. balance = nil,
  15. pkey = nil,
  16. addr = nil
  17. }
  18.  
  19. local build = .5
  20. local running = true
  21. local autoUpdate = true
  22. local url = "http://65.26.252.225/quest/dia/krist/"
  23. local cBuild = http.get("https://raw.githubusercontent.com/connordelaneyy/Wallet/master/BUILD").readAll()
  24.  
  25. local MOD = 2^32
  26. local MODM = MOD-1
  27.  
  28. local function memoize(f)
  29. local mt = {}
  30. local t = setmetatable({}, mt)
  31. function mt:__index(k)
  32. local v = f(k)
  33. t[k] = v
  34. return v
  35. end
  36. return t
  37. end
  38.  
  39. local function make_bitop_uncached(t, m)
  40. local function bitop(a, b)
  41. local res,p = 0,1
  42. while a ~= 0 and b ~= 0 do
  43. local am, bm = a % m, b % m
  44. res = res + t[am][bm] * p
  45. a = (a - am) / m
  46. b = (b - bm) / m
  47. p = p*m
  48. end
  49. res = res + (a + b) * p
  50. return res
  51. end
  52. return bitop
  53. end
  54.  
  55. local function make_bitop(t)
  56. local op1 = make_bitop_uncached(t,2^1)
  57. local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  58. return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  59. end
  60.  
  61. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  62.  
  63. local function bxor(a, b, c, ...)
  64. local z = nil
  65. if b then
  66. a = a % MOD
  67. b = b % MOD
  68. z = bxor1(a, b)
  69. if c then z = bxor(z, c, ...) end
  70. return z
  71. elseif a then return a % MOD
  72. else return 0 end
  73. end
  74.  
  75. local function band(a, b, c, ...)
  76. local z
  77. if b then
  78. a = a % MOD
  79. b = b % MOD
  80. z = ((a + b) - bxor1(a,b)) / 2
  81. if c then z = bit32_band(z, c, ...) end
  82. return z
  83. elseif a then return a % MOD
  84. else return MODM end
  85. end
  86.  
  87. local function bnot(x) return (-1 - x) % MOD end
  88.  
  89. local function rshift1(a, disp)
  90. if disp < 0 then return lshift(a,-disp) end
  91. return math.floor(a % 2 ^ 32 / 2 ^ disp)
  92. end
  93.  
  94. local function rshift(x, disp)
  95. if disp > 31 or disp < -31 then return 0 end
  96. return rshift1(x % MOD, disp)
  97. end
  98.  
  99. local function lshift(a, disp)
  100. if disp < 0 then return rshift(a,-disp) end
  101. return (a * 2 ^ disp) % 2 ^ 32
  102. end
  103.  
  104. local function rrotate(x, disp)
  105. x = x % MOD
  106. disp = disp % 32
  107. local low = band(x, 2 ^ disp - 1)
  108. return rshift(x, disp) + lshift(low, 32 - disp)
  109. end
  110.  
  111. local k = {
  112. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  113. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  114. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  115. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  116. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  117. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  118. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  119. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  120. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  121. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  122. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  123. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  124. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  125. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  126. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  127. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  128. }
  129.  
  130. local function str2hexa(s)
  131. return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  132. end
  133.  
  134. local function num2s(l, n)
  135. local s = ""
  136. for i = 1, n do
  137. local rem = l % 256
  138. s = string.char(rem) .. s
  139. l = (l - rem) / 256
  140. end
  141. return s
  142. end
  143.  
  144. local function s232num(s, i)
  145. local n = 0
  146. for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  147. return n
  148. end
  149.  
  150. local function preproc(msg, len)
  151. local extra = 64 - ((len + 9) % 64)
  152. len = num2s(8 * len, 8)
  153. msg = msg .. "\128" .. string.rep("\0", extra) .. len
  154. assert(#msg % 64 == 0)
  155. return msg
  156. end
  157.  
  158. local function initH256(H)
  159. H[1] = 0x6a09e667
  160. H[2] = 0xbb67ae85
  161. H[3] = 0x3c6ef372
  162. H[4] = 0xa54ff53a
  163. H[5] = 0x510e527f
  164. H[6] = 0x9b05688c
  165. H[7] = 0x1f83d9ab
  166. H[8] = 0x5be0cd19
  167. return H
  168. end
  169.  
  170. local function digestblock(msg, i, H)
  171. local w = {}
  172. for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  173. for j = 17, 64 do
  174. local v = w[j - 15]
  175. local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  176. v = w[j - 2]
  177. w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  178. end
  179.  
  180. 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]
  181. for i = 1, 64 do
  182. local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  183. local maj = bxor(band(a, b), band(a, c), band(b, c))
  184. local t2 = s0 + maj
  185. local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  186. local ch = bxor (band(e, f), band(bnot(e), g))
  187. local t1 = h + s1 + ch + k[i] + w[i]
  188. h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  189. end
  190.  
  191. H[1] = band(H[1] + a)
  192. H[2] = band(H[2] + b)
  193. H[3] = band(H[3] + c)
  194. H[4] = band(H[4] + d)
  195. H[5] = band(H[5] + e)
  196. H[6] = band(H[6] + f)
  197. H[7] = band(H[7] + g)
  198. H[8] = band(H[8] + h)
  199. end
  200.  
  201. local function sha256(msg)
  202. msg = preproc(msg, #msg)
  203. local H = initH256({})
  204. for i = 1, #msg, 64 do digestblock(msg, i, H) end
  205. return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  206. num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  207. end
  208.  
  209. local function dectobase36(j)
  210. if j <= 6 then return "0"
  211. elseif j <= 13 then return "1"
  212. elseif j <= 20 then return "2"
  213. elseif j <= 27 then return "3"
  214. elseif j <= 34 then return "4"
  215. elseif j <= 41 then return "5"
  216. elseif j <= 48 then return "6"
  217. elseif j <= 55 then return "7"
  218. elseif j <= 62 then return "8"
  219. elseif j <= 69 then return "9"
  220. elseif j <= 76 then return "a"
  221. elseif j <= 83 then return "b"
  222. elseif j <= 90 then return "c"
  223. elseif j <= 97 then return "d"
  224. elseif j <= 104 then return "e"
  225. elseif j <= 111 then return "f"
  226. elseif j <= 118 then return "g"
  227. elseif j <= 125 then return "h"
  228. elseif j <= 132 then return "i"
  229. elseif j <= 139 then return "j"
  230. elseif j <= 146 then return "k"
  231. elseif j <= 153 then return "l"
  232. elseif j <= 160 then return "m"
  233. elseif j <= 167 then return "n"
  234. elseif j <= 174 then return "o"
  235. elseif j <= 181 then return "p"
  236. elseif j <= 188 then return "q"
  237. elseif j <= 195 then return "r"
  238. elseif j <= 202 then return "s"
  239. elseif j <= 209 then return "t"
  240. elseif j <= 216 then return "u"
  241. elseif j <= 223 then return "v"
  242. elseif j <= 230 then return "w"
  243. elseif j <= 237 then return "x"
  244. elseif j <= 244 then return "y"
  245. elseif j <= 251 then return "z"
  246. else return "e"
  247. end
  248. end
  249.  
  250. function makev2address(key)
  251. local protein = {}
  252. local stick = sha256(sha256(key))
  253. local n = 0
  254. local link = 0
  255. local v2 = "k"
  256. repeat
  257. if n < 9 then protein[n] = string.sub(stick,0,2)
  258. stick = sha256(sha256(stick)) end
  259. n = n + 1
  260. until n == 9
  261. n = 0
  262. repeat
  263. link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9
  264. if string.len(protein[link]) ~= 0 then
  265. v2 = v2 .. dectobase36(tonumber(protein[link],16))
  266. protein[link] = ''
  267. n = n + 1
  268. else
  269. stick = sha256(stick)
  270. end
  271. until n == 9
  272. return v2
  273. end
  274.  
  275.  
  276. local header = function()
  277. term.setBackgroundColour(colours.grey)
  278. term.setTextColour(colours.lightGrey)
  279. term.clear()
  280. term.setCursorPos(2, 2)
  281. print("Wallet - Created by CDEL")
  282. end
  283.  
  284. if autoUpdate == true then
  285. if build < tonumber(cBuild) then
  286. local file = fs.open(shell.getRunningProgram(), "w")
  287. file.write(http.get("https://raw.githubusercontent.com/connordelaneyy/Wallet/master/src/wallet.lua").readAll())
  288. file.close()
  289. end
  290. end
  291.  
  292. local login = function()
  293. header()
  294. term.setCursorPos(2, 4)
  295. print("Enter your private key to use Krist. If this is\n your first time using Krist, a wallet will be\n generated with the private key you enter.")
  296. term.setCursorPos(2, 8)
  297. write("Private Key: ")
  298. uData.pkey = sha256("KRISTWALLET"..read("*")).."-000"
  299. uData.addr = makev2address(uData.pkey)
  300.  
  301. uData.balance = http.get(url.."?getbalance="..uData.addr).readAll()
  302. if uData.balance == nil then
  303. uData.balance = 0
  304. end
  305. end
  306.  
  307. local balance = function()
  308. term.setCursorPos(17, 4)
  309. print(uData.addr)
  310. term.setCursorPos(17, 5)
  311. print(uData.balance.." KST")
  312. end
  313.  
  314. local transfer = function()
  315. term.setCursorPos(17, 4)
  316. print("Transfer Funds")
  317. term.setCursorPos(17, 6)
  318. write("Recipient: ")
  319. local rec = read()
  320. term.setCursorPos(17, 7)
  321. write("Amount: ")
  322. local amo = read()
  323.  
  324. local content = http.get(url.."?pushtx2&q="..rec.."&pkey="..uData.pkey.."&amt="..amo).readAll()
  325. term.setCursorPos(17, 9)
  326. print(content)
  327. sleep(1.5)
  328. end
  329.  
  330. local vault = function()
  331.  
  332. end
  333.  
  334. local menu = function()
  335. header()
  336. term.setCursorPos(2, 4)
  337. print("[ Overview ]")
  338. term.setCursorPos(2, 6)
  339. print("[ Transfer ]")
  340. term.setCursorPos(2, 8)
  341. print("[ Vault ]")
  342. term.setCursorPos(2, 10)
  343. print("[ Exit ]")
  344. end
  345.  
  346. login()
  347.  
  348. while running == true do
  349. menu()
  350. balance()
  351. local _, button, x, y = os.pullEvent("mouse_click")
  352. if button == 1 then
  353. if y == 4 and x >= 2 and x <= 13 then end
  354. if y == 6 and x >= 2 and x <= 13 then
  355. menu()
  356. transfer()
  357. elseif y == 8 and x >= 2 and x <= 10 then
  358. menu()
  359. vault()
  360. elseif y == 10 and x >= 2 and x <= 9 then
  361. running = false
  362. end
  363. end
  364. end
  365.  
  366. term.setBackgroundColour(colours.black)
  367. term.setTextColour(colours.white)
  368. term.clear()
  369. term.setCursorPos(1, 1)
  370. print("Thanks for using Wallet!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement