Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. // cryptocurrency only using cookies
  2. // User security assumptions: trust service provider, connection, browser or other user software, and the baker or cookie maker.
  3. //
  4.  
  5. const express = require('express');
  6. const mustache = require('mustache');
  7. const app = express();
  8. const Keyv = require('keyv');
  9. const keyv = new Keyv('sqlite://./testdb.sqlite');
  10. const TOKEN_PREFIX = 'biscuit|';
  11. const LISTING_KEY = 'token_list';
  12. const css_style = `
  13. <style> *{ font-family: sans-serif; }
  14. th{ text-align: center; font-weight:normal; text-decoration: underline;
  15. padding: 0.33em 0.67em; background: #eef
  16. font-variant: small-caps; text-transform: lowercase; }
  17. td{ text-align: center; }
  18. a:visited{ color: blue; }
  19. </style>
  20. `
  21.  
  22. const home_html = `<!DOCTYPE html>
  23. <html>
  24. <head>
  25. <title> Cooke Jar Tokens </title>
  26. ${css_style}
  27. </head>
  28. <body>
  29. <!-- TODO privacy and unlisted settings, etc. -->
  30. <title> Cooke Jar Tokens </title>
  31. <a href='/create'>Create</a> &ndash;
  32. <a href='/list'>List</a>
  33. </body>
  34. </html>
  35. `
  36.  
  37. app.use(express.urlencoded({extended: true}));
  38. app.use(express.json());
  39. app.get('/', function(req, res){
  40. res.send(home_html);
  41. })
  42.  
  43. const create_html = `
  44. <!DOCTYPE html>
  45. <html>
  46. <head>
  47. <title> Create New Biscuits </title>
  48. ${css_style}
  49. </head>
  50. <body>
  51. <h1> Create New Biscuits </h1>
  52. <form action='' method="POST">
  53. <input type='hidden' name='author_name'
  54. value='***test-author***'/>
  55. <table>
  56. <tr><th><label for='biscuit_name'>Name</label></th>
  57. <td><input type='text' name='biscuit_name'></input></td></tr>
  58. <tr><th><label for='biscuit_quantity'>Quantity</label></th>
  59. <td><input type='number' name='biscuit_quantity'></input></td></tr>
  60. <tr><td></td><td><input type='submit'/></td></tr>
  61. </table>
  62. </form>
  63. </body>
  64. </html>
  65. `
  66.  
  67. app.get('/create', function(req, res){
  68. res.send(create_html);
  69. })
  70.  
  71.  
  72. const create_result_html = `
  73. <!DOCTYPE html>
  74. <html>
  75. <head>
  76. <title> New Biscuits Created </title>
  77. ${css_style}
  78. <!--style> *{ font-family: sans-serif; }
  79. th{ text-align: right; font-weight:normal; text-decoration: underline;
  80. padding: 0.33em 0.67em;
  81. font-variant: small-caps; text-transform: lowercase; }
  82. </style-->
  83. </head>
  84. <body>
  85. <h1> New Biscuits Created </h1>
  86. <table>
  87. <tr><th>Author</th><td>{{author}}</td></tr>
  88. <tr><th>Biscuit Name</th><td>{{name}}</td></tr>
  89. <tr><th>Created</th><td>{{quantity}}</td></tr>
  90. <tr><th>Total</th><td>{{total}}</td></tr>
  91. </table>
  92. </body>
  93. </html>
  94. `
  95.  
  96.  
  97. app.post('/create', async function(req, res){
  98. // console.log('serving request: ', req);
  99. const author = req.body.author_name;
  100. const name = req.body.biscuit_name;
  101. const quantity = req.body.biscuit_quantity;
  102. const biscuit_info = {
  103. 'author': author,
  104. 'name': name,
  105. 'quantity': quantity };
  106.  
  107.  
  108. const name_key = author + "|" + name;
  109. const token_key = TOKEN_PREFIX + name_key;
  110. let [listing, token_info] = await Promise.all([keyv.get(LISTING_KEY), keyv.get(token_key)]);
  111. if(!token_info){
  112. if(!listing) listing = [];
  113. listing.push(biscuit_info);
  114. await Promise.all([keyv.set(LISTING_KEY, listing), keyv.set(token_key, biscuit_info)]);
  115. res.send(mustache.render(create_result_html, biscuit_info));
  116. }
  117. else{
  118. res.send('token already exists.');
  119. }
  120. })
  121.  
  122. listing_html = `<!DOCTYPE html>
  123. <html>
  124. <header>
  125. ${css_style}
  126. </header>
  127. <body>
  128. <table>
  129. <tr><th> Author </th> <th> Name </th> <th> Quantity </th></tr>
  130. {{#listing}}
  131. <tr><td>{{author}}</td>
  132. <td>{{name}}</td>
  133. <td>{{quantity}}</td></tr>
  134. {{/listing}}
  135. </table>
  136. </body>
  137. </html>
  138. `
  139. app.get('/list', async function(req, res){
  140. //token_listing = [{'name': 'currency', 'quantity': '1000000'}];
  141. const token_list = await keyv.get(LISTING_KEY);
  142. const page_data = {'listing': token_list}
  143. console.log('listing', token_list);
  144. res.send(mustache.render(listing_html, page_data));
  145. })
  146.  
  147.  
  148. const server = app.listen(8089, function(){
  149. const host = server.address().address;
  150. const port = server.address().port;
  151.  
  152. console.log('Cookie service provider listening at %s:%s', host, port);
  153. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement