init
This commit is contained in:
commit
44f31f8b9f
402 changed files with 47865 additions and 0 deletions
47
utils/adapters.js
Normal file
47
utils/adapters.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import { eventsType } from '@/constants'
|
||||
|
||||
export function formatEvents(events, type) {
|
||||
if (type === eventsType.DEPOSIT) {
|
||||
return events.map(({ blockNumber, transactionHash, returnValues }) => {
|
||||
const { commitment, leafIndex, timestamp } = returnValues
|
||||
return {
|
||||
blockNumber,
|
||||
transactionHash,
|
||||
commitment,
|
||||
leafIndex: Number(leafIndex),
|
||||
timestamp
|
||||
}
|
||||
})
|
||||
} else {
|
||||
return events.map(({ blockNumber, transactionHash, returnValues }) => {
|
||||
const { nullifierHash, to, fee } = returnValues
|
||||
return {
|
||||
blockNumber,
|
||||
transactionHash,
|
||||
nullifierHash,
|
||||
to,
|
||||
fee
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export function formatEvent(event, type) {
|
||||
if (type === eventsType.DEPOSIT) {
|
||||
return {
|
||||
timestamp: event.timestamp,
|
||||
commitment: event.commitment,
|
||||
leafIndex: Number(event.index ? event.index : event.leafIndex),
|
||||
blockNumber: event.blockNumber,
|
||||
transactionHash: event.transactionHash
|
||||
}
|
||||
} else {
|
||||
return {
|
||||
to: event.to,
|
||||
fee: event.fee,
|
||||
nullifierHash: event.nullifier ? event.nullifier : event.nullifierHash,
|
||||
blockNumber: Number(event.blockNumber),
|
||||
transactionHash: event.transactionHash
|
||||
}
|
||||
}
|
||||
}
|
||||
101
utils/crypto.js
Normal file
101
utils/crypto.js
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
import crypto from 'crypto'
|
||||
import { BN, toBN } from 'web3-utils'
|
||||
|
||||
import { pedersen } from '@/services'
|
||||
|
||||
const CUT_LENGTH = 31
|
||||
|
||||
export function parseNote(note) {
|
||||
const [, currency, amount, netId, hexNote] = note.split('-')
|
||||
|
||||
return {
|
||||
...parseHexNote(hexNote),
|
||||
netId,
|
||||
amount,
|
||||
currency
|
||||
}
|
||||
}
|
||||
|
||||
export function parseHexNote(hexNote) {
|
||||
const buffNote = Buffer.from(hexNote.slice(2), 'hex')
|
||||
|
||||
const commitment = buffPedersenHash(buffNote)
|
||||
|
||||
const nullifierBuff = buffNote.slice(0, CUT_LENGTH)
|
||||
const nullifierHash = BigInt(buffPedersenHash(nullifierBuff))
|
||||
const nullifier = BigInt(leInt2Buff(buffNote.slice(0, CUT_LENGTH)))
|
||||
|
||||
const secret = BigInt(leInt2Buff(buffNote.slice(CUT_LENGTH, CUT_LENGTH * 2)))
|
||||
|
||||
return {
|
||||
secret,
|
||||
nullifier,
|
||||
commitment,
|
||||
nullifierBuff,
|
||||
nullifierHash,
|
||||
commitmentHex: toFixedHex(commitment),
|
||||
nullifierHex: toFixedHex(nullifierHash)
|
||||
}
|
||||
}
|
||||
|
||||
export function leInt2Buff(value) {
|
||||
return new BN(value, 16, 'le')
|
||||
}
|
||||
|
||||
export function randomBN(nbytes = 31) {
|
||||
return toBN(leInt2Buff(crypto.randomBytes(nbytes)).toString())
|
||||
}
|
||||
|
||||
export function buffPedersenHash(buffer) {
|
||||
const [hash] = pedersen.unpackPoint(buffer)
|
||||
return pedersen.toStringBuffer(hash)
|
||||
}
|
||||
|
||||
export function toFixedHex(value, length = 32) {
|
||||
const isBuffer = value instanceof Buffer
|
||||
|
||||
const str = isBuffer ? value.toString('hex') : BigInt(value).toString(16)
|
||||
return '0x' + str.padStart(length * 2, '0')
|
||||
}
|
||||
|
||||
export const isEmptyArray = (arr) => !Array.isArray(arr) || !arr.length
|
||||
|
||||
export function packEncryptedMessage(encryptedMessage) {
|
||||
const nonceBuf = Buffer.from(encryptedMessage.nonce, 'base64')
|
||||
const ephemPublicKeyBuf = Buffer.from(encryptedMessage.ephemPublicKey, 'base64')
|
||||
const ciphertextBuf = Buffer.from(encryptedMessage.ciphertext, 'base64')
|
||||
const messageBuff = Buffer.concat([
|
||||
Buffer.alloc(24 - nonceBuf.length),
|
||||
nonceBuf,
|
||||
Buffer.alloc(32 - ephemPublicKeyBuf.length),
|
||||
ephemPublicKeyBuf,
|
||||
ciphertextBuf
|
||||
])
|
||||
return '0x' + messageBuff.toString('hex')
|
||||
}
|
||||
|
||||
export function unpackEncryptedMessage(encryptedMessage) {
|
||||
if (encryptedMessage.slice(0, 2) === '0x') {
|
||||
encryptedMessage = encryptedMessage.slice(2)
|
||||
}
|
||||
const messageBuff = Buffer.from(encryptedMessage, 'hex')
|
||||
const nonceBuf = messageBuff.slice(0, 24)
|
||||
const ephemPublicKeyBuf = messageBuff.slice(24, 56)
|
||||
const ciphertextBuf = messageBuff.slice(56)
|
||||
return {
|
||||
version: 'x25519-xsalsa20-poly1305',
|
||||
nonce: nonceBuf.toString('base64'),
|
||||
ephemPublicKey: ephemPublicKeyBuf.toString('base64'),
|
||||
ciphertext: ciphertextBuf.toString('base64')
|
||||
}
|
||||
}
|
||||
|
||||
export function checkCommitments(events = []) {
|
||||
events.forEach(({ leafIndex }, i) => {
|
||||
// TODO reload events, need for if infura provider missing events
|
||||
if (leafIndex !== i) {
|
||||
console.error(`Missing deposit event for deposit #${i}`)
|
||||
throw new Error(window.$nuxt.$t('failedToFetchAllDepositEvents'))
|
||||
}
|
||||
})
|
||||
}
|
||||
17
utils/debounce.js
Normal file
17
utils/debounce.js
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
export const _debounce = (func, waitFor) => {
|
||||
let timeout = null
|
||||
|
||||
const debounceFunction = (...args) => {
|
||||
if (timeout) {
|
||||
clearTimeout(timeout)
|
||||
timeout = null
|
||||
}
|
||||
timeout = setTimeout(() => {
|
||||
return func(...args)
|
||||
}, waitFor)
|
||||
}
|
||||
|
||||
return debounceFunction
|
||||
}
|
||||
|
||||
export const debounce = _debounce((func, args) => func(args), 400)
|
||||
37
utils/index.js
Normal file
37
utils/index.js
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import FileSaver from 'file-saver'
|
||||
|
||||
export * from './crypto'
|
||||
export * from './debounce'
|
||||
export * from './adapters'
|
||||
export * from './storeUtils'
|
||||
export * from './stringUtils'
|
||||
export * from './numberUtils'
|
||||
export * from './instanceUtils'
|
||||
|
||||
export function sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
export function detectMob() {
|
||||
if (
|
||||
navigator.userAgent.match(/Android/i) ||
|
||||
navigator.userAgent.match(/webOS/i) ||
|
||||
navigator.userAgent.match(/iPhone/i) ||
|
||||
navigator.userAgent.match(/iPad/i) ||
|
||||
navigator.userAgent.match(/iPod/i) ||
|
||||
navigator.userAgent.match(/BlackBerry/i) ||
|
||||
navigator.userAgent.match(/Windows Phone/i)
|
||||
) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
export function saveAsFile(data, name) {
|
||||
if (detectMob()) {
|
||||
return
|
||||
}
|
||||
|
||||
FileSaver.saveAs(data, name)
|
||||
}
|
||||
16
utils/instanceUtils.js
Normal file
16
utils/instanceUtils.js
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import networkConfig from '@/networkConfig'
|
||||
|
||||
export function getInstanceByAddress({ netId, address }) {
|
||||
const { tokens } = networkConfig[`netId${netId}`]
|
||||
|
||||
for (const [currency, { instanceAddress }] of Object.entries(tokens)) {
|
||||
for (const [amount, instance] of Object.entries(instanceAddress)) {
|
||||
if (instance === address) {
|
||||
return {
|
||||
amount,
|
||||
currency
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
utils/numberUtils.js
Normal file
6
utils/numberUtils.js
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { BigNumber as BN } from 'bignumber.js'
|
||||
|
||||
// return the number of decimal places of the value
|
||||
export function decimalPlaces(value) {
|
||||
return new BN(value).dp()
|
||||
}
|
||||
24
utils/storeUtils.js
Normal file
24
utils/storeUtils.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import { cloneDeep } from 'lodash'
|
||||
import { CHAIN_IDS } from '@/constants'
|
||||
|
||||
const netIdWrapper = (initialState) => (acc, netId) => ({
|
||||
...acc,
|
||||
[netId]: Object.assign({}, cloneDeep(initialState))
|
||||
})
|
||||
|
||||
export function createChainIdState(initialState) {
|
||||
return CHAIN_IDS.reduce(netIdWrapper(initialState), {})
|
||||
}
|
||||
|
||||
export function isStorageAvailable(type) {
|
||||
try {
|
||||
const test = '__test__'
|
||||
const storage = window[type]
|
||||
|
||||
storage.setItem(test, test)
|
||||
storage.removeItem(test)
|
||||
return true
|
||||
} catch {
|
||||
return false
|
||||
}
|
||||
}
|
||||
22
utils/stringUtils.js
Normal file
22
utils/stringUtils.js
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
export function capitalizeFirstLetter(string) {
|
||||
return string.charAt(0).toUpperCase() + string.slice(1)
|
||||
}
|
||||
|
||||
export const hashRender = (hash, size = 4, separator = '...') => {
|
||||
return hash.slice(0, size) + separator + hash.slice(-size)
|
||||
}
|
||||
|
||||
export const sliceAddress = (address) => {
|
||||
return '0x' + hashRender(address.slice(2))
|
||||
}
|
||||
|
||||
const semVerRegex = /^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
|
||||
|
||||
export const parseSemanticVersion = (version) => {
|
||||
const { groups } = semVerRegex.exec(version)
|
||||
return groups
|
||||
}
|
||||
|
||||
export const isWalletRejection = (err) => {
|
||||
return /cance(l)+ed|denied|rejected/im.test(err.message)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue