Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const txHash = mimc7.multiHash(
[tx.from[0], tx.from[1], BigInt(tx.detail), tx.updated_pubkey[0], tx.updated_pubkey[1]]
)
const signature = eddsa.signMiMC(alicePrvKey, txHash)
// update Alice account
const newAlice = {
pubkey: tx.updated_pubkey,
detail: BigInt(tx.detail)
}
const newAliceHash = mimc7.multiHash(
[newAlice.pubkey[0], newAlice.pubkey[1], BigInt(newAlice.detail)]
)
// update root
const final_root = mimc7.multiHash([newAliceHash, bobHash])
// console.log('tree_root: ' + tree_root.toString())
// console.log('final_root: ' + final_root.toString())
// console.log('accounts_pubkeys Alice.pubkey[0]: ' + Alice.pubkey[0].toString())
// console.log('accounts_pubkeys Alice.pubkey[1]: ' + Alice.pubkey[1].toString())
// console.log('accounts_detail: ' + final_root.toString())
// console.log('sender_detail: ' + Alice.detail.toString())
// console.log('sender_updated_pubkey[0]: ' + newAlice.pubkey[0].toString())
// console.log('sender_updated_pubkey[1]: ' + newAlice.pubkey[1].toString())
// console.log('sender_updated_detail: ' + newAlice.detail.toString())
// console.log("signature['R8'][0]: " + signature.R8[0].toString())
// console.log("signature['R8'][1]: " + signature.R8[1].toString())
// console.log("signature['S']: " + signature.S.toString())
// console.log('aliceHash: ' + aliceHash.toString())
// console.log('bobHash: ' + bobHash.toString())
// console.log('newAliceHash: ' + newAliceHash.toString())
console.log('Submitted initial message')
// Wait 10 seconds
console.log('Sleeping 10 seconds...')
await sleep(10000)
console.log('Woken up')
// Construct 2nd message
const userSecondMessage = [
...userPosition,
...userPubKey,
1n // New position
]
const userSecondMessageHash = mimc7.multiHash(userSecondMessage)
const secondSignature: MiMicSignature = eddsa.signMiMC(
userPrvKey.toString(),
userSecondMessageHash
)
// Insert signature into tx
const userSecondMessage2 = [
...userSecondMessage,
secondSignature.R8[0],
secondSignature.R8[1],
secondSignature.S
]
const userSecondEncryptedMessage = encrypt(
userSecondMessage2,
detail: 0,
updated_pubkey: Alice.pubkey
}
// Alice sign tx
const txHash = mimc7.multiHash(
[tx.from[0], tx.from[1], BigInt(tx.detail), tx.updated_pubkey[0], tx.updated_pubkey[1]]
)
const signature = eddsa.signMiMC(alicePrvKey, txHash)
// update Alice account
const newAlice = {
pubkey: tx.updated_pubkey,
detail: BigInt(tx.detail)
}
const newAliceHash = mimc7.multiHash(
[newAlice.pubkey[0], newAlice.pubkey[1], BigInt(newAlice.detail)]
)
// update root
const final_root = mimc7.multiHash([newAliceHash, bobHash])
// console.log('tree_root: ' + tree_root.toString())
// console.log('final_root: ' + final_root.toString())
// console.log('accounts_pubkeys Alice.pubkey[0]: ' + Alice.pubkey[0].toString())
// console.log('accounts_pubkeys Alice.pubkey[1]: ' + Alice.pubkey[1].toString())
// console.log('accounts_detail: ' + final_root.toString())
// console.log('sender_detail: ' + Alice.detail.toString())
// console.log('sender_updated_pubkey[0]: ' + newAlice.pubkey[0].toString())
// console.log('sender_updated_pubkey[1]: ' + newAlice.pubkey[1].toString())
// console.log('sender_updated_detail: ' + newAlice.detail.toString())
// console.log("signature['R8'][0]: " + signature.R8[0].toString())
function multiHash(arr) {
// TODO check bigints inside finite field
return mimc7.multiHash(arr);
}
const { Circuit } = require('snarkjs')
const zkSnark = require('snarkjs').original
const { unstringifyBigInts } = require('snarkjs/src/stringifybigint')
const alicePrvKey = Buffer.from('1'.toString().padStart(64, '0'), 'hex')
const alicePubKey = eddsa.prv2pub(alicePrvKey)
const bobPrvKey = Buffer.from('2'.toString().padStart(64, '0'), 'hex')
const bobPubKey = eddsa.prv2pub(bobPrvKey)
// accounts (1 = Yes, 0 = No)
const Alice = {
pubkey: alicePubKey,
detail: 1
}
const aliceHash = mimc7.multiHash(
[Alice.pubkey[0], Alice.pubkey[1], BigInt(Alice.detail)]
)
const Bob = {
pubkey: bobPubKey,
detail: 0
}
const bobHash = mimc7.multiHash(
[Bob.pubkey[0], Bob.pubkey[1], BigInt(Bob.detail)]
)
const tree_root = mimc7.multiHash([aliceHash, bobHash])
// transaction
const tx = {
from: Alice.pubkey,
const main = async () => {
const pk = stringifyBigInts(userPubKey)
// First message (insert new user)
const userPosition: Array = [
...userPubKey,
0n // Action
]
const userInitialMessage = [
...userPosition,
0n, 0n, 0n
]
const userMessageHash = mimc7.multiHash(userInitialMessage)
const signature: MiMicSignature = eddsa.signMiMC(
userPrvKey.toString(),
userMessageHash
)
// Insert signature into tx
const userInitialMessage2 = [
...userInitialMessage,
signature.R8[0],
signature.R8[1],
signature.S
]
const userInitialEncryptedMessage = encrypt(
userInitialMessage2,
// accounts (1 = Yes, 0 = No)
const Alice = {
pubkey: alicePubKey,
detail: 1
}
const aliceHash = mimc7.multiHash(
[Alice.pubkey[0], Alice.pubkey[1], BigInt(Alice.detail)]
)
const Bob = {
pubkey: bobPubKey,
detail: 0
}
const bobHash = mimc7.multiHash(
[Bob.pubkey[0], Bob.pubkey[1], BigInt(Bob.detail)]
)
const tree_root = mimc7.multiHash([aliceHash, bobHash])
// transaction
const tx = {
from: Alice.pubkey,
detail: 0,
updated_pubkey: Alice.pubkey
}
// Alice sign tx
const txHash = mimc7.multiHash(
[tx.from[0], tx.from[1], BigInt(tx.detail), tx.updated_pubkey[0], tx.updated_pubkey[1]]
)
const encrypt = (
msg: Array,
priv: BigInt,
pub: Tuple
): Array => {
// Encrypts a message
const sharedKey = ecdh(priv, pub)
const iv = mimc7.multiHash(msg, BigInt(0))
return [
iv, ...msg.map((e: BigInt, i: Number): BigInt => {
return e + mimc7.hash(sharedKey, iv + bigInt(i))
})
]
}
detail: 1
}
const aliceHash = mimc7.multiHash(
[Alice.pubkey[0], Alice.pubkey[1], BigInt(Alice.detail)]
)
const Bob = {
pubkey: bobPubKey,
detail: 0
}
const bobHash = mimc7.multiHash(
[Bob.pubkey[0], Bob.pubkey[1], BigInt(Bob.detail)]
)
const tree_root = mimc7.multiHash([aliceHash, bobHash])
// transaction
const tx = {
from: Alice.pubkey,
detail: 0,
updated_pubkey: Alice.pubkey
}
// Alice sign tx
const txHash = mimc7.multiHash(
[tx.from[0], tx.from[1], BigInt(tx.detail), tx.updated_pubkey[0], tx.updated_pubkey[1]]
)
const signature = eddsa.signMiMC(alicePrvKey, txHash)
// update Alice account
const newAlice = {