classic-ui/store/snark.js

111 lines
2.9 KiB
JavaScript
Raw Normal View History

2022-04-22 06:05:56 +03:00
/* eslint-disable no-console */
import zlib from 'zlib'
2022-04-22 06:05:56 +03:00
import axios from 'axios'
import Web3 from 'web3'
2022-04-22 06:05:56 +03:00
import { detectMob } from '@/utils'
import networkConfig from '@/networkConfig'
const { APP_ENS_NAME } = process.env
const groth16 = require('@tornado/websnark/src/groth16')
2022-04-22 06:05:56 +03:00
function buildGroth16() {
const isMobile = detectMob()
const wasmMemory = isMobile ? 1000 : 2000
return groth16({ wasmInitialMemory: wasmMemory })
}
function getEns() {
2022-10-31 00:51:20 +03:00
const { url } = Object.values(networkConfig.netId1.rpcUrls)[0]
const provider = new Web3(url)
2022-10-31 00:51:20 +03:00
return provider.eth.ens
2022-04-22 06:05:56 +03:00
}
async function getTornadoKeys(getProgress) {
try {
const keys = await Promise.all([
download({ name: 'tornado.json.gz', contentType: 'string' }),
download({ name: 'tornadoProvingKey.bin.gz', contentType: 'arraybuffer', getProgress })
2022-04-22 06:05:56 +03:00
])
return { circuit: JSON.parse(keys[0]), provingKey: keys[1].buffer }
2022-04-22 06:05:56 +03:00
} catch (err) {
2022-10-31 00:51:20 +03:00
console.log('ERR', err)
2022-04-22 06:05:56 +03:00
throw err
}
}
async function getIPFSIdFromENS(ensName) {
try {
2022-10-31 00:51:20 +03:00
const ensInterface = getEns()
const { decoded } = await ensInterface.getContenthash(ensName)
2022-04-22 06:05:56 +03:00
2022-10-31 00:51:20 +03:00
return decoded
2022-04-22 06:05:56 +03:00
} catch (err) {
throw new Error(err)
}
}
async function fetchFile({ url, name, getProgress, id, retryAttempt = 0 }) {
try {
const response = await axios.get(`${url}/${name}`, {
responseType: 'blob',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
timeout: 10_000,
2022-04-22 06:05:56 +03:00
onDownloadProgress: (progressEvent) => {
if (typeof getProgress === 'function') {
const progress = Math.round((progressEvent.loaded * 100) / 9626311)
getProgress(progress)
}
}
})
return response
} catch (err) {
if (!id) {
id = await getIPFSIdFromENS(APP_ENS_NAME)
}
const knownResources = [
url,
`https://ipfs.io/ipfs/${id}`,
`https://dweb.link/ipfs/${id}`,
`https://gateway.pinata.cloud/ipfs/${id}`
]
if (retryAttempt < knownResources.length) {
const fallbackUrl = knownResources[retryAttempt]
retryAttempt++
const response = await fetchFile({ name, getProgress, retryAttempt, id, url: fallbackUrl })
return response
}
throw err
}
}
/**
* Function to download
* @param {*} name filename
* @param {'base64'|'string'|'binarystring'|'text'|'blob'|'uint8array'|'arraybuffer'|'array'|'nodebuffer'} contentType type of the content.
* @param getProgress function
*/
async function download({ name, contentType, getProgress, eventName = 'events' }) {
try {
// eslint-disable-next-line no-undef
const prefix = __webpack_public_path__.slice(0, -7)
const response = await fetchFile({ getProgress, url: prefix, name })
const buffer = Buffer.from(await response.data.arrayBuffer())
const content = zlib.inflateSync(buffer)
2022-04-22 06:05:56 +03:00
return content
} catch (err) {
throw err
}
}
export { getTornadoKeys, buildGroth16, download }