mirror of
https://github.com/tornadocash/tornado-nova
synced 2024-02-02 14:53:56 +01:00
33 lines
1021 B
JavaScript
33 lines
1021 B
JavaScript
const { ethers } = require('hardhat')
|
|
const { BigNumber } = ethers
|
|
const { randomBN, poseidonHash } = require('./utils')
|
|
const Keypair = require('./keypair')
|
|
|
|
class Utxo {
|
|
constructor({ amount = 0, keypair = new Keypair(), blinding = randomBN(), index } = {}) {
|
|
this.amount = BigNumber.from(amount)
|
|
this.blinding = BigNumber.from(blinding)
|
|
this.keypair = keypair
|
|
this.index = index
|
|
}
|
|
|
|
getCommitment() {
|
|
if (!this._commitment) {
|
|
this._commitment = poseidonHash([this.amount, this.blinding, this.keypair.pubkey])
|
|
}
|
|
return this._commitment
|
|
}
|
|
|
|
getNullifier() {
|
|
if (!this._nullifier) {
|
|
if (this.amount > 0 && (this.index === undefined || this.keypair.privkey === undefined || this.keypair.privkey === null)) {
|
|
throw new Error('Can not compute nullifier without utxo index or private key')
|
|
}
|
|
this._nullifier = poseidonHash([this.getCommitment(), this.index || 0, this.keypair.privkey || 0])
|
|
}
|
|
return this._nullifier
|
|
}
|
|
}
|
|
|
|
module.exports = Utxo
|