refactor: import only necessary ethers functions

This commit is contained in:
cedoor
2022-01-21 22:44:25 +01:00
parent 19334431cb
commit 3aaacee524
2 changed files with 50 additions and 35 deletions

View File

@@ -36,8 +36,10 @@
},
"dependencies": {
"@zk-kit/incremental-merkle-tree": "^0.2.0",
"@ethersproject/bytes": "^5.5.0",
"@ethersproject/solidity": "^5.5.0",
"@ethersproject/strings": "^5.5.0",
"circomlibjs": "0.0.8",
"ethers": "^5.4.7",
"ffjavascript": "0.2.38",
"incrementalquintree": "^1.0.7",
"snarkjs": "^0.4.10"

View File

@@ -1,56 +1,69 @@
import { hexlify } from "@ethersproject/bytes"
import { keccak256 } from "@ethersproject/solidity"
import { toUtf8Bytes } from "@ethersproject/strings"
import { IncrementalMerkleTree } from "@zk-kit/incremental-merkle-tree"
import { MerkleProof } from "@zk-kit/types"
import { MerkleProof, StrBigInt } from "@zk-kit/types"
import { poseidon } from "circomlibjs"
import * as ethers from "ethers"
import { ZqField } from "ffjavascript"
import fs from "fs"
export const SNARK_FIELD_SIZE = BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617")
export const Fq = new ZqField(SNARK_FIELD_SIZE)
type IncrementalQuinTree = any
export function genSignalHash(signal: string): bigint {
const converted = hexlify(toUtf8Bytes(signal))
export const poseidonHash = (data: Array<bigint>): bigint => poseidon(data)
export const genSignalHash = (signal: string): bigint => {
const converted = ethers.utils.hexlify(ethers.utils.toUtf8Bytes(signal))
return BigInt(ethers.utils.solidityKeccak256(["bytes"], [converted])) >> BigInt(8)
return BigInt(keccak256(["bytes"], [converted])) >> BigInt(8)
}
export const genExternalNullifier = (plaintext: string): string => {
const _cutOrExpandHexToBytes = (hexStr: string, bytes: number): string => {
const len = bytes * 2
export function genExternalNullifier(plaintext: string): string {
const hashed = keccak256(["string"], [plaintext])
const hexStr = `0x${hashed.slice(8)}`
const len = 32 * 2
const h = hexStr.slice(2, len + 2)
const h = hexStr.slice(2, len + 2)
return `0x${h.padStart(len, "0")}`
}
const hashed = ethers.utils.solidityKeccak256(["string"], [plaintext])
return _cutOrExpandHexToBytes(`0x${hashed.slice(8)}`, 32)
return `0x${h.padStart(len, "0")}`
}
export const createTree = (depth: number, zeroValue: number | BigInt, arity: number): IncrementalQuinTree =>
new IncrementalMerkleTree(poseidonHash, depth, zeroValue, arity)
/**
* Creates merkle proof
* @param depth depth of tree
* @param zeroValue zero value of tree
* @param leavesPerNode number of leaves to derive hash from
* @param leaves leaves to build try from
* @param leaf leaf for which merkle proof should be generated
* @returns merkle proof
* Returns the content of a file as array buffer. It uses `fetch`
* on browsers and fs.readFileSync with node.
* @param filePath The file path.
* @returns The file content as array buffer.
*/
export const generateMerkleProof = (
export async function getFileBuffer(filePath: string): Promise<ArrayBuffer> {
if (typeof window !== "undefined") {
const response = await fetch(filePath)
return response.arrayBuffer()
}
return fs.readFileSync(filePath)
}
/**
* Creates a Merkle proof.
* @param depth The depth of the tree.
* @param zeroValue The zero value of the tree.
* @param arity The number of leaves per node.
* @param leaves The list of the leaves of the tree.
* @param leaf The leaf for which Merkle proof should be created.
* @returns The Merkle proof.
*/
export function generateMerkleProof(
depth: number,
zeroValue: number | BigInt,
zeroValue: StrBigInt,
arity: number,
leaves: Array<bigint | string>,
leaf: bigint | string
): MerkleProof => {
const tree: IncrementalQuinTree = new IncrementalMerkleTree(poseidonHash, depth, zeroValue, arity)
leaves: StrBigInt[],
leaf: StrBigInt
): MerkleProof {
const tree = new IncrementalMerkleTree(poseidon, depth, zeroValue, arity)
const leafIndex = leaves.indexOf(leaf)
if (leafIndex === -1) throw new Error("Leaf does not exists")
if (leafIndex === -1) {
throw new Error("The leaf does not exists")
}
for (const leaf of leaves) {
tree.insert(leaf)