mirror of
https://github.com/tornadocash/tornado-nova
synced 2024-02-02 14:53:56 +01:00
keypair
This commit is contained in:
parent
cb2a587540
commit
a19a226277
@ -66,7 +66,7 @@ async function getProof({ inputs, outputs, tree, extAmount, fee, recipient, rela
|
||||
|
||||
// data for 2 transaction inputs
|
||||
inAmount: inputs.map((x) => x.amount),
|
||||
inPrivateKey: inputs.map((x) => x.privkey),
|
||||
inPrivateKey: inputs.map((x) => x.keypair.privkey),
|
||||
inBlinding: inputs.map((x) => x.blinding),
|
||||
inPathIndices: inputMerklePathIndices,
|
||||
inPathElements: inputMerklePathElements,
|
||||
@ -74,7 +74,7 @@ async function getProof({ inputs, outputs, tree, extAmount, fee, recipient, rela
|
||||
// data for 2 transaction outputs
|
||||
outAmount: outputs.map((x) => x.amount),
|
||||
outBlinding: outputs.map((x) => x.blinding),
|
||||
outPubkey: outputs.map((x) => x.pubkey),
|
||||
outPubkey: outputs.map((x) => x.keypair.pubkey),
|
||||
outPathIndices: outputIndex >> Math.log2(outputs.length),
|
||||
outPathElements: outputPath.slice(Math.log2(outputs.length)),
|
||||
}
|
||||
@ -130,7 +130,7 @@ async function transact({ tornadoPool, utxo }) {
|
||||
const inputs = [utxo, new Utxo()]
|
||||
const outputs = [
|
||||
new Utxo({ amount: utxo.amount / 4 }),
|
||||
new Utxo({ amount: (utxo.amount * 3) / 4, privkey: utxo.privkey }),
|
||||
new Utxo({ amount: (utxo.amount * 3) / 4, keypair: utxo.keypair }),
|
||||
]
|
||||
|
||||
const { proof, args } = await getProof({
|
||||
|
31
src/kaypair.js
Normal file
31
src/kaypair.js
Normal file
@ -0,0 +1,31 @@
|
||||
const { ethers } = require('hardhat')
|
||||
const { BigNumber } = ethers
|
||||
const { randomBN, poseidonHash, toFixedHex } = require('./utils')
|
||||
|
||||
class Keypair {
|
||||
constructor(privkey = randomBN()) {
|
||||
this.privkey = privkey
|
||||
this.pubkey = poseidonHash([this.privkey])
|
||||
this.encryptionKey = 0 // todo
|
||||
}
|
||||
|
||||
toString() {
|
||||
return toFixedHex(this.pubkey) + toFixedHex(this.encryptionKey).slice(2)
|
||||
}
|
||||
|
||||
static fromString(str) {
|
||||
if (str.length === 130) {
|
||||
str = str.slice(2)
|
||||
}
|
||||
if (str.length !== 128) {
|
||||
throw new Error('Invalid key length')
|
||||
}
|
||||
return Object.assign(new Keypair(), {
|
||||
privkey: null,
|
||||
pubkey: BigNumber.from('0x' + str.slice(0, 64)),
|
||||
encryptionKey: BigNumber.from('0x' + str.slice(64, 128)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Keypair
|
30
src/utxo.js
30
src/utxo.js
@ -1,43 +1,29 @@
|
||||
const { ethers } = require('hardhat')
|
||||
const { BigNumber } = ethers
|
||||
const { randomBN, poseidonHash } = require('./utils')
|
||||
|
||||
function fromPrivkey(privkey) {
|
||||
return {
|
||||
privkey,
|
||||
pubkey: poseidonHash([privkey]),
|
||||
}
|
||||
}
|
||||
const Keypair = require('./kaypair')
|
||||
|
||||
class Utxo {
|
||||
constructor({ amount, pubkey, privkey, blinding, index } = {}) {
|
||||
if (!pubkey) {
|
||||
if (privkey) {
|
||||
pubkey = fromPrivkey(privkey).pubkey
|
||||
} else {
|
||||
;({ pubkey, privkey } = fromPrivkey(randomBN()))
|
||||
}
|
||||
}
|
||||
this.amount = BigNumber.from(amount || 0)
|
||||
this.blinding = blinding || randomBN()
|
||||
this.pubkey = pubkey
|
||||
this.privkey = privkey
|
||||
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.pubkey])
|
||||
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.privkey === undefined)) {
|
||||
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.privkey || 0])
|
||||
this._nullifier = poseidonHash([this.getCommitment(), this.index || 0, this.keypair.privkey || 0])
|
||||
}
|
||||
return this._nullifier
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user