make it work again!

This commit is contained in:
Alexey 2020-01-29 19:59:37 +03:00
parent 44dbc3abbe
commit 86b84773ee
11 changed files with 150 additions and 69 deletions

1
.eslintignore Normal file
View File

@ -0,0 +1 @@
lib/*

1
.prettierignore Normal file
View File

@ -0,0 +1 @@
lib/*

View File

@ -72,7 +72,7 @@ module.exports = {
/*
** Plugins to load before mounting the App
*/
plugins: [],
plugins: [{ src: '~plugins/phase2', ssr: false }],
/*
** Nuxt.js dev-modules
*/
@ -107,7 +107,12 @@ module.exports = {
/*
** You can extend webpack config here
*/
extend(config, ctx) {},
extend(config, ctx) {
config.module.rules.push({
test: /\.js$/,
loader: require.resolve('@open-wc/webpack-import-meta-loader')
})
},
html: {
minify: {
collapseWhitespace: true, // as @dario30186 mentioned

View File

@ -13,6 +13,7 @@
},
"dependencies": {
"@nuxtjs/axios": "^5.3.6",
"@open-wc/webpack-import-meta-loader": "^0.4.1",
"async-mutex": "^0.1.4",
"aws-sdk": "^2.610.0",
"body-parser": "^1.19.0",
@ -23,6 +24,7 @@
"express-fileupload": "^1.1.6",
"express-session": "^1.17.0",
"morgan": "^1.9.1",
"multer": "^1.4.2",
"mysql2": "^2.1.0",
"nuxt": "^2.0.0",
"nuxt-buefy": "^0.3.2",

View File

@ -2,18 +2,21 @@
<div class="has-text-centered">
<div class="title is-size-1">Hello, Anonymous</div>
<div class="buttons is-centered">
<b-button :disable="isContributeBtnDisabled" type="is-primary" outlined
<b-button
:disable="isContributeBtnDisabled"
@click="makeContribution"
type="is-primary"
outlined
>Make the contribution</b-button
>
<b-button type="is-primary" outlined>Sign In</b-button>
</div>
{{ status }}
</div>
</template>
<script>
/* eslint-disable no-console */
import init, { contribute } from '@/lib/phase2/phase2.js'
const initPromise = init()
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
@ -28,15 +31,15 @@ export default {
async makeContribution({ retry = 0 } = {}) {
try {
this.isContributeBtnDisabled = true
await initPromise
this.status = 'Downloading last contribution'
let data = await fetch('challenge')
let data = await fetch('api/challenge')
data = new Uint8Array(await data.arrayBuffer())
this.status = 'Generating random contribution'
await timeout(100) // allow UI to update before freezing in wasm
console.log('Source params', data)
const contribute = await this.$contribute()
const result = contribute(data)
console.log('Updated params', result)
@ -45,7 +48,7 @@ export default {
formData.append('response', new Blob([result], { type: 'application/octet-stream' }))
formData.append('name', 'William') // TODO put real name here
formData.append('company', 'Microsoft')
const resp = await fetch('response', {
const resp = await fetch('api/response', {
method: 'POST',
body: formData
})

10
plugins/phase2.js Normal file
View File

@ -0,0 +1,10 @@
import init, { contribute } from '@/lib/phase2/phase2.js'
export default ({ store, isHMR, app }, inject) => {
inject('contribute', main)
}
async function main() {
const initPromise = init()
await initPromise
return contribute
}

View File

@ -1,5 +1,5 @@
const aws = require('aws-sdk')
const s3 = new aws.S3()
// const aws = require('aws-sdk')
// const s3 = new aws.S3()
const fs = require('fs').promises
const path = require('path')
const util = require('util')
@ -8,8 +8,10 @@ const express = require('express')
const router = express.Router()
const { Mutex } = require('async-mutex')
const mutex = new Mutex()
const Contribution = require('../models/contributions.model.js')
const oauth = require('oauth')
const multer = require('multer')
const Contribution = require('../models/contributions.model.js')
const upload = multer({ dest: '/tmp/tornado' })
const consumer = new oauth.OAuth(
'https://twitter.com/oauth/request_token',
@ -18,53 +20,54 @@ const consumer = new oauth.OAuth(
process.env.TWITTER_CONSUMER_SECRET,
'1.0A',
process.env.TWITTER_CALLBACK_URL,
'HMAC-SHA1')
'HMAC-SHA1'
)
async function uploadToS3(response) {
const currentContributionIndex = await Contribution.currentContributionIndex()
return await s3.upload({
Bucket: process.env.AWS_S3_BUCKET,
Key: `response_${currentContributionIndex}`,
ACL: 'public-read',
Body: response,
}).promise()
}
// async function uploadToS3(response) {
// const currentContributionIndex = await Contribution.currentContributionIndex()
// return await s3
// .upload({
// Bucket: process.env.AWS_S3_BUCKET,
// Key: `response_${currentContributionIndex}`,
// ACL: 'public-read',
// Body: response
// })
// .promise()
// }
async function verifyResponse() {
async function verifyResponse({ filename }) {
console.log('Running verifier')
const { stdout, stderr } = await exec(
'../bin/verify_contribution circuit.json current.params /tmp/new.params', {
cwd: './snark_files/',
env: { 'RUST_BACKTRACE': 1 }
`../bin/verify_contribution circuit.json current.params /tmp/tornado/${filename}`,
{
cwd: './server/snark_files/',
env: { RUST_BACKTRACE: 1 }
}
)
console.log(stdout)
console.error(stderr)
}
router.get('/', (req, res) => {
let userData
consumer.get(
'https://api.twitter.com/1.1/account/verify_credentials.json',
req.session.oauthAccessToken,
req.session.oauthAccessTokenSecret,
function (error, data,) {
if (error) {
console.log('error', error)
userData = { name: 'Anonymous' }
res.render('pages/index', { userData })
// res.send("Error getting twitter screen name : " + util.inspect(error), 500);
} else {
userData = JSON.parse(data)
req.session.twitterScreenName = userData.screen_name
res.render('pages/index', { userData })
}
})
})
// router.get('/dashboard') --> index
// router.get('/makeContribution')
// router.get('/autotizeContribution?token=asdfas')
// router.get('/', (req, res) => {
// let userData
// consumer.get(
// 'https://api.twitter.com/1.1/account/verify_credentials.json',
// req.session.oauthAccessToken,
// req.session.oauthAccessTokenSecret,
// function(error, data) {
// if (error) {
// console.log('error', error)
// userData = { name: 'Anonymous' }
// res.render('pages/index', { userData })
// // res.send("Error getting twitter screen name : " + util.inspect(error), 500);
// } else {
// userData = JSON.parse(data)
// req.session.twitterScreenName = userData.screen_name
// res.render('pages/index', { userData })
// }
// }
// )
// })
router.get('/challenge', (req, res) => {
res.sendFile('./snark_files/current.params', { root: path.join(__dirname, '../') })
@ -75,8 +78,8 @@ router.get('/contributions', async (req, res) => {
res.json(contributions).send()
})
router.post('/response', async (req, res) => {
if (!req.files.response) {
router.post('/response', upload.single('response'), async (req, res) => {
if (!req.file) {
res.status(400).send('Missing response file')
return
}
@ -85,11 +88,12 @@ router.post('/response', async (req, res) => {
const currentContributionIndex = await Contribution.currentContributionIndex()
try {
console.log(`Started processing contribution ${currentContributionIndex}`)
await fs.writeFile('/tmp/new.params', req.files.response.data)
await verifyResponse()
// await fs.writeFile('/tmp/new.params', req.file.response.data)
await verifyResponse({ filename: req.file.filename })
} catch (e) {
console.error('Error', e)
res.status(422).send(e.toString())
return
}
try {
@ -101,7 +105,7 @@ router.post('/response', async (req, res) => {
// )
console.log('Committing changes')
await fs.rename('/tmp/new.params', './snark_files/current.params')
await fs.rename(`/tmp/tornado/${req.file.filename}`, './server/snark_files/current.params')
await Contribution.insertContributionInfo(
req.body ? req.body.name || null : null,
req.body ? req.body.company || null : null

View File

@ -5,7 +5,6 @@ const express = require('express')
const bodyParser = require('body-parser')
const morgan = require('morgan')
const session = require('express-session')
const fileUpload = require('express-fileupload')
const { Nuxt, Builder } = require('nuxt')
const config = require('../nuxt.config.js')
const sessionsController = require('./controllers/sessions')
@ -26,20 +25,15 @@ async function start() {
} else {
await nuxt.ready()
}
app.use('/api', sessionsController)
app.use('/api', contributionController)
// Give nuxt middleware to express
app.use(nuxt.render)
app.use(fileUpload({}))
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
const accessLogStream = fs.createWriteStream(
path.join(__dirname, 'access.log'),
{
flags: 'a'
}
)
const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), {
flags: 'a'
})
app.use(morgan('combined', { stream: accessLogStream }))
app.use(
@ -53,8 +47,9 @@ async function start() {
res.locals.session = req.session
next()
})
app.use('/', sessionsController)
app.use('/', contributionController)
// Give nuxt middleware to express
app.use(nuxt.render)
app.listen(port, host, () => {
console.log(`Server is running on port ${port}.`)

Binary file not shown.

Binary file not shown.

View File

@ -1065,6 +1065,11 @@
mustache "^2.3.0"
stack-trace "0.0.10"
"@open-wc/webpack-import-meta-loader@^0.4.1":
version "0.4.1"
resolved "https://registry.yarnpkg.com/@open-wc/webpack-import-meta-loader/-/webpack-import-meta-loader-0.4.1.tgz#d4d165dd998ecfab6ab3dcd66456eadf62348af2"
integrity sha512-aP0jeEsBjnmNzhaCh8WHFL5cXzKd38a/1QU+IHwN49/xjwAUMqK7J12fTxaIZMdZ+Poe7bSz4+3zGKOcxbyM/w==
"@types/color-name@^1.1.1":
version "1.1.1"
resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0"
@ -1495,6 +1500,11 @@ anymatch@~3.1.1:
normalize-path "^3.0.0"
picomatch "^2.0.4"
append-field@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/append-field/-/append-field-1.0.0.tgz#1e3440e915f0b1203d23748e78edd7b9b5b43e56"
integrity sha1-HjRA6RXwsSA9I3SOeO3XubW0PlY=
aproba@^1.0.3, aproba@^1.1.1:
version "1.2.0"
resolved "https://registry.yarnpkg.com/aproba/-/aproba-1.2.0.tgz#6802e6264efd18c790a1b0d517f0f2627bf2c94a"
@ -2005,6 +2015,14 @@ bulma@0.7.5:
resolved "https://registry.yarnpkg.com/bulma/-/bulma-0.7.5.tgz#35066c37f82c088b68f94450be758fc00a967208"
integrity sha512-cX98TIn0I6sKba/DhW0FBjtaDpxTelU166pf7ICXpCCuplHWyu6C9LYZmL5PEsnePIeJaiorsTEzzNk3Tsm1hw==
busboy@^0.2.11:
version "0.2.14"
resolved "https://registry.yarnpkg.com/busboy/-/busboy-0.2.14.tgz#6c2a622efcf47c57bbbe1e2a9c37ad36c7925453"
integrity sha1-bCpiLvz0fFe7vh4qnDetNseSVFM=
dependencies:
dicer "0.2.5"
readable-stream "1.1.x"
busboy@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/busboy/-/busboy-0.3.1.tgz#170899274c5bf38aae27d5c62b71268cd585fd1b"
@ -2472,7 +2490,7 @@ concat-map@0.0.1:
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
concat-stream@^1.5.0:
concat-stream@^1.5.0, concat-stream@^1.5.2:
version "1.6.2"
resolved "https://registry.yarnpkg.com/concat-stream/-/concat-stream-1.6.2.tgz#904bdf194cd3122fc675c77fc4ac3d4ff0fd1a34"
integrity sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==
@ -3079,6 +3097,14 @@ detect-indent@^5.0.0:
resolved "https://registry.yarnpkg.com/detect-indent/-/detect-indent-5.0.0.tgz#3871cc0a6a002e8c3e5b3cf7f336264675f06b9d"
integrity sha1-OHHMCmoALow+Wzz38zYmRnXwa50=
dicer@0.2.5:
version "0.2.5"
resolved "https://registry.yarnpkg.com/dicer/-/dicer-0.2.5.tgz#5996c086bb33218c812c090bddc09cd12facb70f"
integrity sha1-WZbAhrszIYyBLAkL3cCc0S+stw8=
dependencies:
readable-stream "1.1.x"
streamsearch "0.1.2"
dicer@0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/dicer/-/dicer-0.3.0.tgz#eacd98b3bfbf92e8ab5c2fdb71aaac44bb06b872"
@ -5044,6 +5070,11 @@ is-wsl@^1.1.0:
resolved "https://registry.yarnpkg.com/is-wsl/-/is-wsl-1.1.0.tgz#1f16e4aa22b04d1336b66188a66af3c600c3a66d"
integrity sha1-HxbkqiKwTRM2tmGIpmrzxgDDpm0=
isarray@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-0.0.1.tgz#8a18acfca9a8f4177e09abfc6038939b05d1eedf"
integrity sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=
isarray@1.0.0, isarray@^1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"
@ -5760,6 +5791,20 @@ ms@^2.1.1:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009"
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
multer@^1.4.2:
version "1.4.2"
resolved "https://registry.yarnpkg.com/multer/-/multer-1.4.2.tgz#2f1f4d12dbaeeba74cb37e623f234bf4d3d2057a"
integrity sha512-xY8pX7V+ybyUpbYMxtjM9KAiD9ixtg5/JkeKUTD6xilfDv0vzzOFcCp4Ljb1UU3tSOM3VTZtKo63OmzOrGi3Cg==
dependencies:
append-field "^1.0.0"
busboy "^0.2.11"
concat-stream "^1.5.2"
mkdirp "^0.5.1"
object-assign "^4.1.1"
on-finished "^2.3.0"
type-is "^1.6.4"
xtend "^4.0.0"
mustache@^2.3.0:
version "2.3.2"
resolved "https://registry.yarnpkg.com/mustache/-/mustache-2.3.2.tgz#a6d4d9c3f91d13359ab889a812954f9230a3d0c5"
@ -7460,6 +7505,16 @@ read-pkg@^2.0.0:
string_decoder "~1.1.1"
util-deprecate "~1.0.1"
readable-stream@1.1.x:
version "1.1.14"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-1.1.14.tgz#7cf4c54ef648e3813084c636dd2079e166c081d9"
integrity sha1-fPTFTvZI44EwhMY23SB54WbAgdk=
dependencies:
core-util-is "~1.0.0"
inherits "~2.0.1"
isarray "0.0.1"
string_decoder "~0.10.x"
readable-stream@^3.1.1:
version "3.5.0"
resolved "https://registry.yarnpkg.com/readable-stream/-/readable-stream-3.5.0.tgz#465d70e6d1087f6162d079cd0b5db7fbebfd1606"
@ -8336,6 +8391,11 @@ string_decoder@^1.0.0, string_decoder@^1.1.1:
dependencies:
safe-buffer "~5.2.0"
string_decoder@~0.10.x:
version "0.10.31"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-0.10.31.tgz#62e203bc41766c6c28c9fc84301dab1c5310fa94"
integrity sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=
string_decoder@~1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.1.1.tgz#9cf1611ba62685d7030ae9e4ba34149c3af03fc8"
@ -8728,7 +8788,7 @@ type-fest@^0.8.1:
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.8.1.tgz#09e249ebde851d3b1e48d27c105444667f17b83d"
integrity sha512-4dbzIzqvjtgiM5rw1k5rEHtBANKmdudhGyBEajN01fEyhaAIhsoKNy6y7+IN93IfpFtwY9iqi7kD+xwKhQsNJA==
type-is@~1.6.17, type-is@~1.6.18:
type-is@^1.6.4, type-is@~1.6.17, type-is@~1.6.18:
version "1.6.18"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.18.tgz#4e552cd05df09467dcbc4ef739de89f2cf37c131"
integrity sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==