tornado-nova/src/utxo.js

33 lines
1021 B
JavaScript
Raw Normal View History

const { ethers } = require('hardhat')
const { BigNumber } = ethers
const { randomBN, poseidonHash } = require('./utils')
2021-06-09 13:19:22 +02:00
const Keypair = require('./kaypair')
class Utxo {
2021-06-09 13:19:22 +02:00
constructor({ amount = 0, keypair = new Keypair(), blinding = randomBN(), index } = {}) {
this.amount = BigNumber.from(amount)
this.blinding = BigNumber.from(blinding)
this.keypair = keypair
2021-06-09 12:56:33 +02:00
this.index = index
}
getCommitment() {
if (!this._commitment) {
2021-06-09 13:19:22 +02:00
this._commitment = poseidonHash([this.amount, this.blinding, this.keypair.pubkey])
}
return this._commitment
}
getNullifier() {
if (!this._nullifier) {
2021-06-09 13:19:22 +02:00
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')
}
2021-06-09 13:19:22 +02:00
this._nullifier = poseidonHash([this.getCommitment(), this.index || 0, this.keypair.privkey || 0])
}
return this._nullifier
}
}
module.exports = Utxo