get hash from webassm

This commit is contained in:
Alexey 2020-04-27 14:11:35 +03:00
parent 9eccbd61c9
commit c88dc70552
5 changed files with 19 additions and 14 deletions

Binary file not shown.

View File

@ -82,6 +82,12 @@ import { mapGetters, mapActions } from 'vuex'
import Cloak from '@/components/Cloak' import Cloak from '@/components/Cloak'
import Form from '@/components/Form' import Form from '@/components/Form'
const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
function buf2hex(buffer) {
// buffer is an ArrayBuffer
return Array.prototype.map
.call(new Uint8Array(buffer), (x) => ('00' + x.toString(16)).slice(-2))
.join('')
}
export default { export default {
components: { components: {
@ -183,12 +189,12 @@ export default {
let msgBuffer = new TextEncoder('utf-8').encode(userInput) let msgBuffer = new TextEncoder('utf-8').encode(userInput)
let hashBuffer = await window.crypto.subtle.digest('SHA-256', msgBuffer) let hashBuffer = await window.crypto.subtle.digest('SHA-256', msgBuffer)
const entropyFromUser = new Uint8Array(hashBuffer) const entropyFromUser = new Uint8Array(hashBuffer)
console.log('entropyFromUser', entropyFromUser.toString()) // console.log('entropyFromUser', entropyFromUser.toString())
msgBuffer = window.crypto.getRandomValues(new Uint8Array(1024)) msgBuffer = window.crypto.getRandomValues(new Uint8Array(1024))
hashBuffer = await window.crypto.subtle.digest('SHA-256', msgBuffer) hashBuffer = await window.crypto.subtle.digest('SHA-256', msgBuffer)
const entropyFromBrowser = new Uint8Array(hashBuffer) const entropyFromBrowser = new Uint8Array(hashBuffer)
console.log('entropyFromBrowser', entropyFromBrowser.toString()) // console.log('entropyFromBrowser', entropyFromBrowser.toString())
// suffle the browser and user random // suffle the browser and user random
const entropy = new Uint8Array(entropyFromBrowser.length) const entropy = new Uint8Array(entropyFromBrowser.length)
@ -196,14 +202,19 @@ export default {
entropy[i] = entropyFromBrowser[i] + entropyFromUser[i] entropy[i] = entropyFromBrowser[i] + entropyFromUser[i]
} }
console.log('entropy', entropy) // console.log('entropy', entropy)
await this.sleep(100) // so browser can render the messages await this.sleep(100) // so browser can render the messages
const result = contribute(data, entropy) const result = contribute(data, entropy)
console.log('Updated params', result) console.log('Updated params', result)
const hash = '0x' + buf2hex(result.slice(0, 64))
const contribution = result.slice(64)
console.log('hash', hash)
console.log('contribution', contribution)
this.$root.$emit('enableLoading', 'Uploading and verifying your contribution') this.$root.$emit('enableLoading', 'Uploading and verifying your contribution')
const formData = new FormData() const formData = new FormData()
formData.append('response', new Blob([result], { type: 'application/octet-stream' })) formData.append('response', new Blob([contribution], { type: 'application/octet-stream' }))
if (this.contributionType !== 'anonymous') { if (this.contributionType !== 'anonymous') {
formData.append('name', this.userName) formData.append('name', this.userName)
formData.append('company', this.userCompany) formData.append('company', this.userCompany)
@ -217,7 +228,7 @@ export default {
this.$store.commit('user/SET_CONTRIBUTION_INDEX', responseData.contributionIndex) this.$store.commit('user/SET_CONTRIBUTION_INDEX', responseData.contributionIndex)
this.status.msg = 'Your contribution is verified and recorded.' this.status.msg = 'Your contribution is verified and recorded.'
this.status.type = 'is-success' this.status.type = 'is-success'
this.contributionHash = responseData.hash this.contributionHash = hash
if (this.contributionType === 'anonymous') { if (this.contributionType === 'anonymous') {
this.authorizeLink = `${window.location.origin}/authorize-contribution?token=${responseData.token}` this.authorizeLink = `${window.location.origin}/authorize-contribution?token=${responseData.token}`
} }

View File

@ -84,12 +84,7 @@ router.post('/response', upload.single('response'), async (req, res) => {
token = crypto.randomBytes(32).toString('hex') token = crypto.randomBytes(32).toString('hex')
} }
const contribution = await fs.readFile(`/tmp/tornado/${req.file.filename}`) await Contribution.create({ name, company, handle, socialType, token })
const blake2Instance = blake2.createHash('blake2b')
blake2Instance.update(contribution)
const hash = '0x' + blake2Instance.digest('hex')
await Contribution.create({ name, company, handle, socialType, token, hash })
console.log('Contribution is correct, uploading to storage') console.log('Contribution is correct, uploading to storage')
if (process.env.DISABLE_S3 !== 'true') { if (process.env.DISABLE_S3 !== 'true') {
@ -103,8 +98,8 @@ router.post('/response', upload.single('response'), async (req, res) => {
`./server/snark_files/response_${contributionIndex}` `./server/snark_files/response_${contributionIndex}`
) )
console.log('Finished. The hash of the contribution is', hash) console.log('Contribution finished.')
res.json({ contributionIndex, token, hash }) res.json({ contributionIndex, token })
} catch (e) { } catch (e) {
console.error('Got error during save', e) console.error('Got error during save', e)
res.status(503).send(e.toString()) res.status(503).send(e.toString())

View File

@ -24,7 +24,6 @@ module.exports = (sequelize, DataTypes) => {
company: DataTypes.STRING, company: DataTypes.STRING,
handle: DataTypes.STRING, handle: DataTypes.STRING,
socialType: DataTypes.STRING, socialType: DataTypes.STRING,
hash: DataTypes.STRING,
attestation: DataTypes.STRING attestation: DataTypes.STRING
}, },
{ {