2021-06-08 20:50:34 +02:00
|
|
|
const { ethers } = require('hardhat')
|
|
|
|
const { BigNumber } = ethers
|
|
|
|
const { randomBN, poseidonHash } = require('./utils')
|
2021-06-09 13:19:22 +02:00
|
|
|
const Keypair = require('./kaypair')
|
2021-06-08 20:50:34 +02:00
|
|
|
|
|
|
|
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
|
2021-06-08 20:50:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getCommitment() {
|
|
|
|
if (!this._commitment) {
|
2021-06-09 13:19:22 +02:00
|
|
|
this._commitment = poseidonHash([this.amount, this.blinding, this.keypair.pubkey])
|
2021-06-08 20:50:34 +02:00
|
|
|
}
|
|
|
|
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)) {
|
2021-06-08 20:50:34 +02:00
|
|
|
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])
|
2021-06-08 20:50:34 +02:00
|
|
|
}
|
|
|
|
return this._nullifier
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Utxo
|