Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // cryptocurrency only using cookies
- // User security assumptions: trust service provider, connection, browser or other user software, and the baker or cookie maker.
- //
- const express = require('express');
- const mustache = require('mustache');
- const app = express();
- const Keyv = require('keyv');
- const keyv = new Keyv('sqlite://./testdb.sqlite');
- const TOKEN_PREFIX = 'biscuit|';
- const LISTING_KEY = 'token_list';
- const css_style = `
- <style> *{ font-family: sans-serif; }
- th{ text-align: center; font-weight:normal; text-decoration: underline;
- padding: 0.33em 0.67em; background: #eef
- font-variant: small-caps; text-transform: lowercase; }
- td{ text-align: center; }
- a:visited{ color: blue; }
- </style>
- `
- const home_html = `<!DOCTYPE html>
- <html>
- <head>
- <title> Cooke Jar Tokens </title>
- ${css_style}
- </head>
- <body>
- <!-- TODO privacy and unlisted settings, etc. -->
- <title> Cooke Jar Tokens </title>
- <a href='/create'>Create</a> –
- <a href='/list'>List</a>
- </body>
- </html>
- `
- app.use(express.urlencoded({extended: true}));
- app.use(express.json());
- app.get('/', function(req, res){
- res.send(home_html);
- })
- const create_html = `
- <!DOCTYPE html>
- <html>
- <head>
- <title> Create New Biscuits </title>
- ${css_style}
- </head>
- <body>
- <h1> Create New Biscuits </h1>
- <form action='' method="POST">
- <input type='hidden' name='author_name'
- value='***test-author***'/>
- <table>
- <tr><th><label for='biscuit_name'>Name</label></th>
- <td><input type='text' name='biscuit_name'></input></td></tr>
- <tr><th><label for='biscuit_quantity'>Quantity</label></th>
- <td><input type='number' name='biscuit_quantity'></input></td></tr>
- <tr><td></td><td><input type='submit'/></td></tr>
- </table>
- </form>
- </body>
- </html>
- `
- app.get('/create', function(req, res){
- res.send(create_html);
- })
- const create_result_html = `
- <!DOCTYPE html>
- <html>
- <head>
- <title> New Biscuits Created </title>
- ${css_style}
- <!--style> *{ font-family: sans-serif; }
- th{ text-align: right; font-weight:normal; text-decoration: underline;
- padding: 0.33em 0.67em;
- font-variant: small-caps; text-transform: lowercase; }
- </style-->
- </head>
- <body>
- <h1> New Biscuits Created </h1>
- <table>
- <tr><th>Author</th><td>{{author}}</td></tr>
- <tr><th>Biscuit Name</th><td>{{name}}</td></tr>
- <tr><th>Created</th><td>{{quantity}}</td></tr>
- <tr><th>Total</th><td>{{total}}</td></tr>
- </table>
- </body>
- </html>
- `
- app.post('/create', async function(req, res){
- // console.log('serving request: ', req);
- const author = req.body.author_name;
- const name = req.body.biscuit_name;
- const quantity = req.body.biscuit_quantity;
- const biscuit_info = {
- 'author': author,
- 'name': name,
- 'quantity': quantity };
- const name_key = author + "|" + name;
- const token_key = TOKEN_PREFIX + name_key;
- let [listing, token_info] = await Promise.all([keyv.get(LISTING_KEY), keyv.get(token_key)]);
- if(!token_info){
- if(!listing) listing = [];
- listing.push(biscuit_info);
- await Promise.all([keyv.set(LISTING_KEY, listing), keyv.set(token_key, biscuit_info)]);
- res.send(mustache.render(create_result_html, biscuit_info));
- }
- else{
- res.send('token already exists.');
- }
- })
- listing_html = `<!DOCTYPE html>
- <html>
- <header>
- ${css_style}
- </header>
- <body>
- <table>
- <tr><th> Author </th> <th> Name </th> <th> Quantity </th></tr>
- {{#listing}}
- <tr><td>{{author}}</td>
- <td>{{name}}</td>
- <td>{{quantity}}</td></tr>
- {{/listing}}
- </table>
- </body>
- </html>
- `
- app.get('/list', async function(req, res){
- //token_listing = [{'name': 'currency', 'quantity': '1000000'}];
- const token_list = await keyv.get(LISTING_KEY);
- const page_data = {'listing': token_list}
- console.log('listing', token_list);
- res.send(mustache.render(listing_html, page_data));
- })
- const server = app.listen(8089, function(){
- const host = server.address().address;
- const port = server.address().port;
- console.log('Cookie service provider listening at %s:%s', host, port);
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement