457 lines
14 KiB
TypeScript
457 lines
14 KiB
TypeScript
// Local types
|
|
import { TornadoInstance } from 'types/deth'
|
|
import * as Types from 'types/sdk/data'
|
|
import { RelayerProperties } from 'types/sdk/data'
|
|
import { Options } from 'types/sdk/main'
|
|
|
|
// Local logic
|
|
import { NumberUtils, ErrorUtils } from 'lib/utils'
|
|
import { AsyncUtils } from 'lib/utils'
|
|
|
|
// Big modules
|
|
import { BigNumber } from 'ethers'
|
|
import { existsSync, mkdirSync } from 'fs'
|
|
import { opendir, readFile, rm } from 'fs/promises'
|
|
import { createInterface } from 'readline'
|
|
|
|
// PouchDB
|
|
import PouchDB from 'pouchdb'
|
|
import * as PouchDBAdapterMemory from 'pouchdb-adapter-memory'
|
|
|
|
// @ts-ignore
|
|
import { toIndexableString } from 'pouchdb-collate'
|
|
import { timeStamp } from 'console'
|
|
|
|
// Register plugins
|
|
PouchDB.plugin(PouchDBAdapterMemory)
|
|
|
|
export namespace Files {
|
|
export type PathGetter = (relative: string) => string
|
|
|
|
export const getModulesPath = (relative: string): string => __dirname + '/../../node_modules/' + relative
|
|
export const getResourcePath = (relative: string): string => __dirname + '/../resources/' + relative
|
|
export const getCachePath = (relative: string): string => __dirname + '/../../cache/' + relative
|
|
|
|
export const cacheDirExists = (): boolean => existsSync(getCachePath(''))
|
|
export const makeCacheDir = (): void => mkdirSync(getCachePath(''))
|
|
|
|
export const loadRaw = (relative: string): Promise<Buffer> => readFile(getResourcePath(relative))
|
|
|
|
export async function wipeCache(prompt: boolean = true): Promise<void> {
|
|
const dir = await opendir(getCachePath(''))
|
|
const toRemove: string[] = []
|
|
const userInput = createInterface({ input: process.stdin, output: process.stdout })
|
|
for await (const entry of dir) {
|
|
if (entry.name.match('(Deposit.*)|(Withdrawal.*)|(Note.*)|(Invoice.*)'))
|
|
toRemove.push(getCachePath(entry.name))
|
|
}
|
|
if (toRemove.length != 0) {
|
|
if (prompt) {
|
|
const promptString = `\nCache wipe requested, following would be wiped:\n\n${toRemove.join(
|
|
'\n'
|
|
)}\n\nContinue? (y/n): `
|
|
function wipeCachePrompt(prompt: string, resolve: any): void {
|
|
userInput.question(prompt, (answer) => {
|
|
if (answer == 'y') {
|
|
userInput.close()
|
|
resolve(true)
|
|
} else if (answer == 'n') {
|
|
userInput.close()
|
|
resolve(false)
|
|
} else wipeCachePrompt('', resolve)
|
|
})
|
|
}
|
|
const answer = await new Promise((resolve) => wipeCachePrompt(promptString, resolve))
|
|
if (answer)
|
|
await Promise.all(toRemove.map((entry) => rm(entry, { recursive: true, force: true }))).catch(
|
|
(err) => {
|
|
throw ErrorUtils.ensureError(err)
|
|
}
|
|
)
|
|
} else {
|
|
await Promise.all(toRemove.map((entry) => rm(entry, { recursive: true, force: true }))).catch(
|
|
(err) => {
|
|
throw ErrorUtils.ensureError(err)
|
|
}
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export namespace Json {
|
|
const cachedJsonData = new Map<string, any>()
|
|
|
|
// reading
|
|
export async function load(
|
|
relativePath: string,
|
|
encoding: BufferEncoding = 'utf8',
|
|
pathGetter: Files.PathGetter = Files.getResourcePath
|
|
): Promise<any> {
|
|
if (cachedJsonData.has(relativePath)) return cachedJsonData.get(relativePath)
|
|
else {
|
|
const obj = JSON.parse(await readFile(pathGetter(relativePath), encoding))
|
|
cachedJsonData.set(relativePath, obj)
|
|
return obj
|
|
}
|
|
}
|
|
|
|
export function toMap<V>(jsonData: any): Map<string, V> {
|
|
return new Map<string, V>(Object.entries(jsonData))
|
|
}
|
|
|
|
export async function loadMap<V>(
|
|
relativePath: string,
|
|
encoding: BufferEncoding = 'utf8'
|
|
): Promise<Map<string, V>> {
|
|
return toMap<V>(await load(relativePath, encoding))
|
|
}
|
|
|
|
export function getError(...values: any[]): Error {
|
|
return ErrorUtils.getError(`there is no such entry for the key-value path [${values.join('][')}]`)
|
|
}
|
|
|
|
export function throwError(...values: any[]): void {
|
|
throw getError(...values)
|
|
}
|
|
|
|
export function getValue(jsonObj: any, keys: any[]): any {
|
|
for (let i = 0; i < keys.length; i++) {
|
|
jsonObj = jsonObj[keys[i]] ?? throwError(...keys.slice(0, i + 1))
|
|
}
|
|
return jsonObj
|
|
}
|
|
}
|
|
|
|
// TODO: Decide whether to also cache the data instead of just loading it for the function call
|
|
|
|
export namespace OnchainData {
|
|
export async function getClassicInstanceData(
|
|
network: string,
|
|
token: string,
|
|
denomination: string
|
|
): Promise<Types.Json.ClassicInstance> {
|
|
const instanceData = Json.getValue(await Json.load('onchain/instances.json'), [network, token])
|
|
return {
|
|
network: +network,
|
|
symbol: token.toUpperCase(),
|
|
decimals: Json.getValue(instanceData, ['decimals']),
|
|
denomination: +denomination,
|
|
deployBlock: Json.getValue(instanceData, ['deployedBlockNumber', denomination]),
|
|
address: Json.getValue(instanceData, ['instanceAddress', denomination]),
|
|
anonymityMiningEnabled: Json.getValue(instanceData, ['miningEnabled'])
|
|
}
|
|
}
|
|
|
|
export async function getInstanceAttrSet<T>(
|
|
key: string,
|
|
paths: Array<{
|
|
network: string
|
|
token: string
|
|
denomination: string
|
|
}>
|
|
): Promise<Array<T>> {
|
|
const obj = await Json.load('onchain/quickLookup.json')
|
|
return await Promise.all(
|
|
paths.map((path) => Json.getValue(obj, [key, `${path.network}${path.token}${path.denomination}`]))
|
|
)
|
|
}
|
|
|
|
// TODO: Add field key for classic or nova
|
|
|
|
export async function getInstanceAddresses(
|
|
paths: Array<{
|
|
network: string
|
|
token: string
|
|
denomination: string
|
|
}>
|
|
): Promise<Array<string>> {
|
|
return getInstanceAttrSet<string>('instanceAddresses', paths)
|
|
}
|
|
|
|
export async function getInstanceAddress(
|
|
network: string,
|
|
token: string,
|
|
denomination: string
|
|
): Promise<string> {
|
|
return (await getInstanceAddresses([{ network: network, token: token, denomination: denomination }]))[0]
|
|
}
|
|
|
|
export async function getInstanceDeployBlockNums(
|
|
paths: Array<{
|
|
network: string
|
|
token: string
|
|
denomination: string
|
|
}>
|
|
): Promise<Array<number>> {
|
|
return getInstanceAttrSet<number>('deployedBlockNumber', paths)
|
|
}
|
|
|
|
export async function getInstanceDeployBlockNum(
|
|
network: string,
|
|
token: string,
|
|
denomination: string
|
|
): Promise<number> {
|
|
return (
|
|
await getInstanceDeployBlockNums([{ network: network, token: token, denomination: denomination }])
|
|
)[0]
|
|
}
|
|
|
|
export async function getProxyAddress(network: string): Promise<string> {
|
|
return Json.getValue(await Json.load('onchain/infrastructure.json'), [network, 'proxy'])
|
|
}
|
|
|
|
export async function getMulticallAddress(network: string): Promise<string> {
|
|
return Json.getValue(await Json.load('onchain/infrastructure.json'), [network, 'multicall'])
|
|
}
|
|
|
|
export async function getMulticall3Address(network: string): Promise<string> {
|
|
return Json.getValue(await Json.load('onchain/infrastructure.json'), [network, 'multicall3'])
|
|
}
|
|
|
|
export async function getTokenData(network: string, token: string): Promise<Types.Json.TokenData> {
|
|
const data = Json.getValue(await Json.load('onchain/tokens.json'), [network, token])
|
|
return {
|
|
network: +network,
|
|
decimals: +data['decimals'],
|
|
address: data['address']
|
|
}
|
|
}
|
|
}
|
|
|
|
export namespace OffchainData {
|
|
export async function getUncensoredRpcURL(network: string, name: string = ''): Promise<string> {
|
|
const rpcs = Json.toMap<string>(
|
|
Json.getValue(await Json.load('offchain/infrastructure.json'), ['jrpc-uncensored', network])
|
|
)
|
|
|
|
if (name.length !== 0) {
|
|
return rpcs.get(name)!
|
|
}
|
|
|
|
let keys = rpcs.keys()
|
|
let randCount = NumberUtils.getRandomFromRange(0, rpcs.size - 1)
|
|
for (let i = 0; i < randCount; i++) keys.next()
|
|
|
|
return rpcs.get(keys.next().value)!
|
|
}
|
|
|
|
export async function getClassicSubgraphURL(network: string): Promise<string> {
|
|
return Json.getValue(await Json.load('offchain/infrastructure.json'), ['subgraph', network])
|
|
}
|
|
}
|
|
|
|
export namespace Constants {
|
|
export const MERKLE_TREE_HEIGHT = 20
|
|
}
|
|
|
|
export namespace Docs {
|
|
// TODO: Probably find some easier way to lookup below docs for the end user...
|
|
|
|
export class Base {
|
|
_id: string
|
|
_rev?: string
|
|
|
|
constructor(id: string) {
|
|
this._id = id
|
|
}
|
|
}
|
|
|
|
export class Deposit extends Base {
|
|
blockNumber: number
|
|
leafIndex: number
|
|
commitment: string
|
|
transactionHash: string
|
|
timestamp: string
|
|
|
|
constructor(obj: any) {
|
|
const blockNumber = obj['blockNumber']
|
|
const transactionHash = obj['transactionHash']
|
|
const commitment = obj['args']['commitment']
|
|
const leafIndex = obj['args']['leafIndex']
|
|
const timestamp = obj['args']['timestamp']
|
|
|
|
// To preserve order because we will need it later
|
|
super(toIndexableString([blockNumber, leafIndex, commitment]))
|
|
|
|
this.commitment = commitment
|
|
this.blockNumber = blockNumber
|
|
this.leafIndex = leafIndex
|
|
this.transactionHash = transactionHash
|
|
this.timestamp = timestamp
|
|
}
|
|
}
|
|
|
|
export class Withdrawal extends Base {
|
|
blockNumber: number
|
|
to: string
|
|
nullifierHash: string
|
|
transactionHash: string
|
|
fee: string
|
|
|
|
constructor(obj: any) {
|
|
const blockNumber = obj['blockNumber']
|
|
const transactionHash = obj['transactionHash']
|
|
const to = obj['args']['to']
|
|
const nullifierHash = obj['args']['nullifierHash']
|
|
const fee = (obj['args']['fee'] as BigNumber).toString()
|
|
|
|
super(toIndexableString([blockNumber, to, nullifierHash]))
|
|
|
|
this.blockNumber = blockNumber
|
|
this.to = to
|
|
this.nullifierHash = nullifierHash
|
|
this.transactionHash = transactionHash
|
|
this.fee = fee
|
|
}
|
|
}
|
|
|
|
export class Note extends Base {
|
|
pathstring: string
|
|
note: string
|
|
|
|
constructor(index: number, pathstring: string, note: string) {
|
|
super(toIndexableString([index, pathstring]))
|
|
this.pathstring = pathstring
|
|
this.note = note
|
|
}
|
|
}
|
|
|
|
export class Invoice extends Base {
|
|
pathstring: string
|
|
invoice: string
|
|
|
|
constructor(index: number, pathstring: string, invoice: string) {
|
|
super(toIndexableString([index, pathstring]))
|
|
this.pathstring = pathstring
|
|
this.invoice = invoice
|
|
}
|
|
}
|
|
|
|
export class Relayer extends Base {
|
|
address: string
|
|
version: string
|
|
serviceFeePercent: number
|
|
miningFeePercent: number
|
|
status: string
|
|
chainId: number
|
|
|
|
constructor(url: string, properties: RelayerProperties) {
|
|
super(toIndexableString([url]))
|
|
this.address = properties.address
|
|
this.version = properties.version
|
|
this.serviceFeePercent = properties.serviceFeePercent
|
|
this.miningFeePercent = properties.miningFeePercent
|
|
this.status = properties.status
|
|
this.chainId = properties.chainId
|
|
}
|
|
}
|
|
}
|
|
|
|
export namespace Cache {
|
|
export class Base<T extends Docs.Base> {
|
|
db: PouchDB.Database<T>
|
|
|
|
constructor(name: string, options?: Options.Cache.Database) {
|
|
if (options?.persistent === false && options?.adapter !== 'memory' && options?.adapter !== null)
|
|
throw ErrorUtils.getError('if not persistent, cache must use memory adapter.')
|
|
|
|
if (options?.adapter === 'memory' && options?.persistent === true)
|
|
throw ErrorUtils.getError("can't specify memory adapter if persistent.")
|
|
|
|
const dbAdapter = options?.adapter ?? (options?.persistent === false ? 'memory' : 'leveldb')
|
|
|
|
if (!Files.cacheDirExists()) Files.makeCacheDir()
|
|
this.db = new PouchDB<T>(Files.getCachePath(name), { adapter: dbAdapter })
|
|
}
|
|
|
|
async get(keys: Array<any>): Promise<T> {
|
|
return await this.db.get(toIndexableString(keys)).catch((err) => {
|
|
throw ErrorUtils.ensureError(err)
|
|
})
|
|
}
|
|
|
|
async close(): Promise<void> {
|
|
await this.db.close()
|
|
}
|
|
|
|
async clear(): Promise<void> {
|
|
await this.db.destroy()
|
|
}
|
|
}
|
|
|
|
export abstract class Syncable<T extends Docs.Base> extends Base<T> {
|
|
sync: AsyncUtils.Sync
|
|
|
|
constructor(name: string, options?: { db?: Options.Cache.Database; sync?: Options.Cache.Sync }) {
|
|
super(name, options?.db)
|
|
this.sync = new AsyncUtils.Sync(options?.sync)
|
|
}
|
|
|
|
abstract getCallbacks(...args: Array<any>): Array<AsyncUtils.Callback>
|
|
|
|
async close(): Promise<void> {
|
|
if (this.sync.pooler!.pending)
|
|
throw ErrorUtils.getError("can't clear while pooler still has pending promises.")
|
|
await super.close()
|
|
}
|
|
|
|
async clear(): Promise<void> {
|
|
if (this.sync.pooler!.pending)
|
|
throw ErrorUtils.getError("can't clear while pooler still has pending promises.")
|
|
await super.clear()
|
|
}
|
|
}
|
|
|
|
export class Deposit extends Syncable<Docs.Deposit> {
|
|
getCallbacks(instance: TornadoInstance): Array<AsyncUtils.Callback> {
|
|
return [
|
|
(fromBlock: number, toBlock: number) => {
|
|
return instance.queryFilter(instance.filters.Deposit(null, null, null), fromBlock, toBlock)
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
export class Withdrawal extends Syncable<Docs.Withdrawal> {
|
|
getCallbacks(instance: TornadoInstance): Array<AsyncUtils.Callback> {
|
|
return [
|
|
(fromBlock: number, toBlock: number) => {
|
|
return instance.queryFilter(instance.filters.Withdrawal(null, null, null, null), fromBlock, toBlock)
|
|
}
|
|
]
|
|
}
|
|
}
|
|
|
|
type DocsArray<T extends Docs.Base> = Array<{
|
|
doc?: T
|
|
id: string
|
|
key: string
|
|
value: {
|
|
rev: string
|
|
deleted?: boolean
|
|
}
|
|
}>
|
|
|
|
export async function loadContents<T extends Docs.Base>(
|
|
nameOfContent: string,
|
|
full: boolean = true,
|
|
emptyError: Error = ErrorUtils.getError(
|
|
`Core.loadCacheContents: there is no cache entry for ${nameOfContent}`
|
|
)
|
|
): Promise<DocsArray<T>> {
|
|
const cache = new Cache.Base<T>(Files.getCachePath(nameOfContent))
|
|
|
|
const docs = await cache.db.allDocs({ include_docs: full }).catch((err) => {
|
|
throw ErrorUtils.ensureError(err)
|
|
})
|
|
|
|
if (docs.total_rows === 0) throw emptyError
|
|
|
|
return docs.rows as DocsArray<T>
|
|
}
|
|
}
|
|
|
|
// Namespace exports
|
|
export { Types }
|