Advertisement
Sirshark10

kLua

Jun 24th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. --[[
  2.  
  3. Documentation
  4.  
  5. --Returns the privatekey of a password
  6. function ToPriv(input)
  7.  
  8.  
  9. --Get info on an address
  10. function getInfo(addr)
  11.  
  12.  
  13. --Send Funds
  14. function sendFunds(pass,to,amount)
  15.  
  16.  
  17. --Lookup a NAME.kst
  18. function nameLook(NAME)
  19.  
  20.  
  21. --Register NAME.kst to given password's private key address.
  22. function nameReg(NAME,InP)
  23.  
  24.  
  25. --List latest transactions
  26. function lateTrans()
  27.  
  28.  
  29. --Gets the Krist in circulation
  30. function getCirc()
  31.  
  32.  
  33. --Gets the Message of the day
  34. function MOTD()
  35.  
  36.  
  37. --Get the current work
  38. function work()
  39.  
  40.  
  41. --Authenticate an address with the server via the privatekey
  42. function authAddr(privateKey)
  43.  
  44.  
  45. --List all names
  46. function lNames()
  47.  
  48.  
  49. --Here's a little easter egg for 3d6 and the Kristfoundation team :P
  50. function myanmar()
  51.  
  52.  
  53. --]]
  54.  
  55. --Sha256 stuff here
  56. local MOD = 2^32
  57. local MODM = MOD-1
  58.  
  59. local function memoize(f)
  60. local mt = {}
  61. local t = setmetatable({}, mt)
  62. function mt:__index(k)
  63. local v = f(k)
  64. t[k] = v
  65. return v
  66. end
  67. return t
  68. end
  69.  
  70. local function make_bitop_uncached(t, m)
  71. local function bitop(a, b)
  72. local res,p = 0,1
  73. while a ~= 0 and b ~= 0 do
  74. local am, bm = a % m, b % m
  75. res = res + t[am][bm] * p
  76. a = (a - am) / m
  77. b = (b - bm) / m
  78. p = p*m
  79. end
  80. res = res + (a + b) * p
  81. return res
  82. end
  83. return bitop
  84. end
  85.  
  86. local function make_bitop(t)
  87. local op1 = make_bitop_uncached(t,2^1)
  88. local op2 = memoize(function(a) return memoize(function(b) return op1(a, b) end) end)
  89. return make_bitop_uncached(op2, 2 ^ (t.n or 1))
  90. end
  91.  
  92. local bxor1 = make_bitop({[0] = {[0] = 0,[1] = 1}, [1] = {[0] = 1, [1] = 0}, n = 4})
  93.  
  94. local function bxor(a, b, c, ...)
  95. local z = nil
  96. if b then
  97. a = a % MOD
  98. b = b % MOD
  99. z = bxor1(a, b)
  100. if c then z = bxor(z, c, ...) end
  101. return z
  102. elseif a then return a % MOD
  103. else return 0 end
  104. end
  105.  
  106. local function band(a, b, c, ...)
  107. local z
  108. if b then
  109. a = a % MOD
  110. b = b % MOD
  111. z = ((a + b) - bxor1(a,b)) / 2
  112. if c then z = bit32_band(z, c, ...) end
  113. return z
  114. elseif a then return a % MOD
  115. else return MODM end
  116. end
  117.  
  118. local function bnot(x) return (-1 - x) % MOD end
  119.  
  120. local function rshift1(a, disp)
  121. if disp < 0 then return lshift(a,-disp) end
  122. return math.floor(a % 2 ^ 32 / 2 ^ disp)
  123. end
  124.  
  125. local function rshift(x, disp)
  126. if disp > 31 or disp < -31 then return 0 end
  127. return rshift1(x % MOD, disp)
  128. end
  129.  
  130. local function lshift(a, disp)
  131. if disp < 0 then return rshift(a,-disp) end
  132. return (a * 2 ^ disp) % 2 ^ 32
  133. end
  134.  
  135. local function rrotate(x, disp)
  136. x = x % MOD
  137. disp = disp % 32
  138. local low = band(x, 2 ^ disp - 1)
  139. return rshift(x, disp) + lshift(low, 32 - disp)
  140. end
  141.  
  142. local k = {
  143. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
  144. 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  145. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
  146. 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  147. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
  148. 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  149. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
  150. 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  151. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
  152. 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  153. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
  154. 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  155. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
  156. 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  157. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
  158. 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
  159. }
  160.  
  161. local function str2hexa(s)
  162. return (string.gsub(s, ".", function(c) return string.format("%02x", string.byte(c)) end))
  163. end
  164.  
  165. local function num2s(l, n)
  166. local s = ""
  167. for i = 1, n do
  168. local rem = l % 256
  169. s = string.char(rem) .. s
  170. l = (l - rem) / 256
  171. end
  172. return s
  173. end
  174.  
  175. local function s232num(s, i)
  176. local n = 0
  177. for i = i, i + 3 do n = n*256 + string.byte(s, i) end
  178. return n
  179. end
  180.  
  181. local function preproc(msg, len)
  182. local extra = 64 - ((len + 9) % 64)
  183. len = num2s(8 * len, 8)
  184. msg = msg .. "\128" .. string.rep("\0", extra) .. len
  185. assert(#msg % 64 == 0)
  186. return msg
  187. end
  188.  
  189. local function initH256(H)
  190. H[1] = 0x6a09e667
  191. H[2] = 0xbb67ae85
  192. H[3] = 0x3c6ef372
  193. H[4] = 0xa54ff53a
  194. H[5] = 0x510e527f
  195. H[6] = 0x9b05688c
  196. H[7] = 0x1f83d9ab
  197. H[8] = 0x5be0cd19
  198. return H
  199. end
  200.  
  201. local function digestblock(msg, i, H)
  202. local w = {}
  203. for j = 1, 16 do w[j] = s232num(msg, i + (j - 1)*4) end
  204. for j = 17, 64 do
  205. local v = w[j - 15]
  206. local s0 = bxor(rrotate(v, 7), rrotate(v, 18), rshift(v, 3))
  207. v = w[j - 2]
  208. w[j] = w[j - 16] + s0 + w[j - 7] + bxor(rrotate(v, 17), rrotate(v, 19), rshift(v, 10))
  209. end
  210.  
  211. 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]
  212. for i = 1, 64 do
  213. local s0 = bxor(rrotate(a, 2), rrotate(a, 13), rrotate(a, 22))
  214. local maj = bxor(band(a, b), band(a, c), band(b, c))
  215. local t2 = s0 + maj
  216. local s1 = bxor(rrotate(e, 6), rrotate(e, 11), rrotate(e, 25))
  217. local ch = bxor (band(e, f), band(bnot(e), g))
  218. local t1 = h + s1 + ch + k[i] + w[i]
  219. h, g, f, e, d, c, b, a = g, f, e, d + t1, c, b, a, t1 + t2
  220. end
  221.  
  222. H[1] = band(H[1] + a)
  223. H[2] = band(H[2] + b)
  224. H[3] = band(H[3] + c)
  225. H[4] = band(H[4] + d)
  226. H[5] = band(H[5] + e)
  227. H[6] = band(H[6] + f)
  228. H[7] = band(H[7] + g)
  229. H[8] = band(H[8] + h)
  230. end
  231.  
  232. local function sha256(msg)
  233. msg = preproc(msg, #msg)
  234. local H = initH256({})
  235. for i = 1, #msg, 64 do digestblock(msg, i, H) end
  236. return str2hexa(num2s(H[1], 4) .. num2s(H[2], 4) .. num2s(H[3], 4) .. num2s(H[4], 4) ..
  237. num2s(H[5], 4) .. num2s(H[6], 4) .. num2s(H[7], 4) .. num2s(H[8], 4))
  238. end
  239.  
  240. local function makeaddressbyte(j)
  241. if j <= 6 then return "0"
  242. elseif j <= 13 then return "1"
  243. elseif j <= 20 then return "2"
  244. elseif j <= 27 then return "3"
  245. elseif j <= 34 then return "4"
  246. elseif j <= 41 then return "5"
  247. elseif j <= 48 then return "6"
  248. elseif j <= 55 then return "7"
  249. elseif j <= 62 then return "8"
  250. elseif j <= 69 then return "9"
  251. elseif j <= 76 then return "a"
  252. elseif j <= 83 then return "b"
  253. elseif j <= 90 then return "c"
  254. elseif j <= 97 then return "d"
  255. elseif j <= 104 then return "e"
  256. elseif j <= 111 then return "f"
  257. elseif j <= 118 then return "g"
  258. elseif j <= 125 then return "h"
  259. elseif j <= 132 then return "i"
  260. elseif j <= 139 then return "j"
  261. elseif j <= 146 then return "k"
  262. elseif j <= 153 then return "l"
  263. elseif j <= 160 then return "m"
  264. elseif j <= 167 then return "n"
  265. elseif j <= 174 then return "o"
  266. elseif j <= 181 then return "p"
  267. elseif j <= 188 then return "q"
  268. elseif j <= 195 then return "r"
  269. elseif j <= 202 then return "s"
  270. elseif j <= 209 then return "t"
  271. elseif j <= 216 then return "u"
  272. elseif j <= 223 then return "v"
  273. elseif j <= 230 then return "w"
  274. elseif j <= 237 then return "x"
  275. elseif j <= 244 then return "y"
  276. elseif j <= 251 then return "z"
  277. else return "e"
  278. end
  279. end
  280.  
  281. function makev2address(key)
  282. local protein = {}
  283. local stick = sha256(sha256(key))
  284. local n = 0
  285. local link = 0
  286. local v2 = "k"
  287. repeat
  288. if n < 9 then protein[n] = string.sub(stick,0,2)
  289. stick = sha256(sha256(stick)) end
  290. n = n + 1
  291. until n == 9
  292. n = 0
  293. repeat
  294. link = tonumber(string.sub(stick,1+(2*n),2+(2*n)),16) % 9
  295. if string.len(protein[link]) ~= 0 then
  296. v2 = v2 .. makeaddressbyte(tonumber(protein[link],16))
  297. protein[link] = ''
  298. n = n + 1
  299. else
  300. stick = sha256(stick)
  301. end
  302. until n == 9
  303. return v2
  304. end
  305.  
  306.  
  307.  
  308. --Rest of the code
  309.  
  310. --Creates a privatekey out of the password
  311. function ToPriv(input)
  312. return sha256("KRISTWALLET"..input).."-000"
  313. end
  314.  
  315. local site = "http://krist.ceriat.net/" --The site for all requests
  316.  
  317. --Get info on an address
  318. function getInfo(addr)
  319. return http.get(site.."addresses/"..addr).readAll()
  320. end
  321.  
  322. --Send Funds
  323. function sendFunds(InP,to,amount)
  324. return http.post(site.."transactions/","privatekey="..ToPriv(InP).."&to="..to.."&amount="..amount).readAll()
  325. end
  326.  
  327. --Lookup a NAME.kst
  328. function nameLook(NAME)
  329. return http.get(site.."names/"..NAME).readAll()
  330. end
  331.  
  332. --Register NAME.kst to given password's private key address.
  333. function nameReg(NAME,InP)
  334. return http.post(site.."names/"..NAME.."/","privatekey="..ToPriv(InP)).readAll()
  335. end
  336.  
  337. --List latest transactions
  338. function lateTrans()
  339. return http.get(site.."transactions/latest").readAll()
  340. end
  341.  
  342. --Gets the Krist in circulation
  343. function getCirc()
  344. return http.get(site.."supply").readAll()
  345. end
  346.  
  347. --Gets the Message of the day
  348. function MOTD()
  349. return http.get(site.."motd").readAll()
  350. end
  351.  
  352. --Get the current work
  353. function work()
  354. return http.get(site.."work").readAll()
  355. end
  356.  
  357. --Authenticate an address with the server via the privatekey
  358. function authAddr(privateKey)
  359. return http.post(site.."login","privatekey="..privateKey).readAll()
  360. end
  361.  
  362. --List all names
  363. function lNames()
  364. return http.get(site.."names").readAll()
  365. end
  366.  
  367. function myanmar()
  368. return "Marisa stole the entire country of Myanmar"
  369. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement