0.0.11-alpha: more sensible naming

Signed-off-by: T-Hax <>
This commit is contained in:
T-Hax 2023-05-13 19:55:47 +00:00
parent d1d23f252a
commit cd64bc0f63
5 changed files with 71 additions and 63 deletions

View file

@ -6,7 +6,7 @@ import { TornadoInstance, TornadoProxy } from './deth'
// Monorepo
import { RelayerProperties as RelayerDataProperties } from '@tornado/sdk-data'
import { ZKDepositData, InputFor } from '@tornado/sdk-crypto'
import { DepositInfo, InputFor } from '@tornado/sdk-crypto'
// External imports
import { TransactionRequest } from '@ethersproject/abstract-provider'
@ -174,58 +174,59 @@ export class Core extends Synchronizer {
return Contracts.getProxy(String(this.chain.id), this.chain.provider)
}
async buildDepositProof(
async createDepositProof(
instance: TornadoInstance,
relayerProperties: RelayerProperties,
recipientAddress: string,
zkDepositsData: ZKDepositData,
depositInfo: DepositInfo,
options?: Options.Core.BuildDepositProof
): Promise<Array<string>> {
return (
await this.buildDepositProofs(
await this.createDepositProofs(
instance,
relayerProperties,
[recipientAddress],
[zkDepositsData],
[depositInfo],
options
)
)[0]
}
// TODO: Abstract out verification parts of this and provide it as a standalone service in crypto or somewhere else
/**
* @param instance This is the Tornado Instance which will be withdrawn from.
* @param relayerProperties The properties of the relayer that is going to be used for the withdrawals. If the service fee is 0, it is assumed that there is no relayer, but that a manual wallet withdraw is being made. These properties are included in the ZK proof.
* @param recipientAddresses The recipient addresses which should receive the withdrawals, in order.
* @param zkDepositsData These represent the public and private values, reconstructed from the deposit note, generated during the building of deposit transactions, used for building the proof of knowledge statement for withdrawal, for each withdrawal (in this context).
* @param depositInfo These represent the public and private values, reconstructed from the deposit note, generated during the building of deposit transactions, used for building the proof of knowledge statement for withdrawal, for each withdrawal (in this context).
* @param options Numerous options which most importantly allow a user to specify whether he is buying ETH, whether to check proof data validity and finally to modulate the gas prices which will be used to calculate the gas fees paid to the relayer.
* @returns The proofs for which the user should then decide whether to use a relayer (recommended, but decide carefully which one) or use his own wallet (if needed).
*/
async buildDepositProofs(
async createDepositProofs(
instance: TornadoInstance,
relayerProperties: RelayerProperties,
recipientAddresses: Array<string>,
zkDepositsData: Array<ZKDepositData>,
depositInfo: Array<DepositInfo>,
options?: Options.Core.BuildDepositProof
): Promise<Array<Array<string>>> {
this._checkProvider('buildDepositProofs')
this._checkProvider('createDepositProofs')
// Extract commitments and nullifier hashes
const hexCommitments: string[] = []
const hexNullifierHashes: string[] = []
const purchaseAmounts =
options?.ethPurchaseAmounts ?? new Array(zkDepositsData.length).fill(BigNumber.from(0))
options?.ethPurchaseAmounts ?? new Array(depositInfo.length).fill(BigNumber.from(0))
if (zkDepositsData.length !== recipientAddresses.length)
if (depositInfo.length !== recipientAddresses.length)
throw ErrorUtils.getError(
'Core.buildDepositProofs: the number of recipients must equal the length of zkDepositsData.'
'Core.createDepositProofs: the number of recipients must equal the length of depositInfo.'
)
if (zkDepositsData.length !== purchaseAmounts.length)
if (depositInfo.length !== purchaseAmounts.length)
throw ErrorUtils.getError(
'Core.buildDepositProofs: if purchase amounts is specified, it must equal the length of zkDepositsData.'
'Core.createDepositProofs: if purchase amounts is specified, it must equal the length of depositInfo.'
)
zkDepositsData.forEach((deposit) => {
depositInfo.forEach((deposit) => {
hexCommitments.push(deposit.hexCommitment)
hexNullifierHashes.push(deposit.hexNullifierHash)
})
@ -258,7 +259,7 @@ export class Core extends Synchronizer {
// Also add the invalid commitments. We can do leafIndices[i] because the matched one are concatenated
// at the start
for (let i = 0, len = zkDepositsData.length; i < len; i++) {
for (let i = 0, len = depositInfo.length; i < len; i++) {
if (!leafIndices[i]) invalidCommitments.push(hexCommitments[i])
if (checkSpent && checkSpentArray![i]) spentNotes.push(hexNullifierHashes[i])
}
@ -269,7 +270,7 @@ export class Core extends Synchronizer {
if (commitmentsAreInvalid || notesAreSpent)
throw ErrorUtils.getError(
`Core.buildDepositProofs: ` +
`Core.createDepositProofs: ` +
(commitmentsAreInvalid
? `following commitments are invalid:\n\n${invalidCommitments.join('\n')}\n\n`
: '') +
@ -294,7 +295,7 @@ export class Core extends Synchronizer {
// Check whether the root is valid
if (checkKnownRoot && !(await instance.isKnownRoot(root)))
throw ErrorUtils.getError(
'Core.buildDepositProofs: the merkle tree created is not valid, something went wrong with syncing.'
'Core.createDepositProofs: the merkle tree created is not valid, something went wrong with syncing.'
)
// Rest of note invariant arguments
@ -319,7 +320,7 @@ export class Core extends Synchronizer {
if (!tokenPrice && !native)
throw ErrorUtils.getError(
'Core.buildDepositProofs: a token price MUST be supplied if the token withdrawn is not native.'
'Core.createDepositProofs: a token price MUST be supplied if the token withdrawn is not native.'
)
this.emit(
@ -335,13 +336,13 @@ export class Core extends Synchronizer {
)
// Compute proofs
for (let i = 0, len = zkDepositsData.length; i < len; i++) {
for (let i = 0, len = depositInfo.length; i < len; i++) {
inputsForProofs.push({
public: {
root: root,
tree: merkleTree,
leafIndex: leafIndices[i],
hexNullifierHash: zkDepositsData[i].hexNullifierHash,
hexNullifierHash: depositInfo[i].hexNullifierHash,
recipientAddress: recipientAddresses[i],
relayerAddress: relayerProperties.address,
fee: this._calcWithdrawalFee(
@ -358,8 +359,8 @@ export class Core extends Synchronizer {
refund: purchaseAmounts[i] ? bigInt(purchaseAmounts[i].toString()) : bigInt(0)
},
private: {
nullifier: zkDepositsData[i].nullifier,
secret: zkDepositsData[i].secret
nullifier: depositInfo[i].nullifier,
secret: depositInfo[i].secret
}
})
}
@ -447,7 +448,7 @@ export class Core extends Synchronizer {
async loadNotes(
indexes?: Array<number>,
keys?: Partial<Keys.InstanceLookup>
): Promise<Array<ZKDepositData>> {
): Promise<Array<DepositInfo>> {
const rows = await Cache.loadContents<Docs.Note>('DepositNotes')
let docs: Array<Docs.Note | undefined> = []
@ -472,11 +473,11 @@ export class Core extends Synchronizer {
return this.parseNotes(notes)
}
parseNotes(notes: Array<string>): Array<ZKDepositData> {
parseNotes(notes: Array<string>): Array<DepositInfo> {
return notes.map((note) => Primitives.parseNote(note))
}
parseNote(note: string): ZKDepositData {
parseNote(note: string): DepositInfo {
return this.parseNotes([note])[0]
}
@ -573,10 +574,10 @@ export class Core extends Synchronizer {
* @param options Whether or not to populate the transactions (only in the sense of encoding transaction data), and whether to backup notes and invoices. Defaults: `depositsPerInstance = [1], doNotPopulate = false, backup { notes = true, invoices = false }` Deposits per instance are hardcoded to 1, since we're doing a single transaction.
* @returns A promise which resolves to the created transaction.
*/
buildDepositTransaction(instance: TornadoInstance, options?: Options.Core.Deposit): Transactions.Deposit {
createDepositTransaction(instance: TornadoInstance, options?: Options.Core.Deposit): Transactions.Deposit {
let opts: Options.Core.Deposit = options ?? {}
opts.depositsPerInstance = [1]
return this.buildDepositTransactions([instance], opts)[0]
return this.createDepositTransactions([instance], opts)[0]
}
/**
@ -586,11 +587,11 @@ export class Core extends Synchronizer {
* @returns A promise which resolves to the created transactions.
* @todo TODO: Maybe this should be sync and deposit backups should be async somewhere else
*/
buildDepositTransactions(
createDepositTransactions(
instances: Array<TornadoInstance>,
options?: Options.Core.Deposit
): Array<Transactions.Deposit> {
this._checkProvider('buildDepositTransactions')
this._checkProvider('createDepositTransactions')
const depositsPerInstance = options?.depositsPerInstance ?? new Array<number>(instances.length).fill(1)
@ -598,7 +599,7 @@ export class Core extends Synchronizer {
if (depositsPerInstance.length != instances.length)
throw ErrorUtils.getError(
'Core.buildDepositTx: number of deposit amount elements must equal the number of instances!'
'Core.createDepositTx: number of deposit amount elements must equal the number of instances!'
)
const chainId = this.chain.id