Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
finalHash.appendBN(new BN(0));
} else if (B.x.fromRed().eq(new BN(0)) && B.y.fromRed().eq(new BN(0))) {
errors.push(errorTypes.BAD_BLINDING_FACTOR);
finalHash.append(B);
} else {
finalHash.append(B);
}
return {
kBar,
B,
};
});
const recoveredChallenge = finalHash.keccak(groupReduction);
const finalChallenge = `0x${padLeft(recoveredChallenge.toString(16), 64)}`;
// Check if the recovered challenge, matches the original challenge. If so, proof construction is validated
if (finalChallenge !== challenge) {
errors.push(errorTypes.CHALLENGE_RESPONSE_FAIL);
}
const valid = errors.length === 0;
return {
valid,
errors,
};
};
} else if (B.x.fromRed().eq(new BN(0)) && B.y.fromRed().eq(new BN(0))) {
errors.push(errorTypes.BAD_BLINDING_FACTOR);
finalHash.append(B);
} else {
finalHash.append(B);
}
kBarArray.push(kBar);
return {
kBar,
B,
};
});
const recoveredChallenge = finalHash.keccak(groupReduction);
const finalChallenge = `0x${padLeft(recoveredChallenge.toString(16), 64)}`;
// Check if the recovered challenge, matches the original challenge. If so, proof construction is validated
if (finalChallenge !== challengeHex) {
errors.push(errorTypes.CHALLENGE_RESPONSE_FAIL);
}
const valid = errors.length === 0;
return {
valid,
errors,
};
};
const expectedOutput = `0x${outputCoder
.encodeProofOutputs([
{
inputNotes: [inputNotes[0]],
outputNotes: [outputNotes[0]],
publicOwner,
publicValue,
challenge,
},
{
inputNotes: [outputNotes[1]],
outputNotes: [inputNotes[1]],
publicOwner,
publicValue,
challenge: `0x${padLeft(sha3(challenge).slice(2), 64)}`,
},
])
.slice(0x42)}`;
return { proofData, expectedOutput };
};
let encoded;
let metaDataSize;
if (encodeMetaData) {
encoded = Array(8).fill();
encoded[7] = note.metaData.slice(2);
metaDataSize = parseInt(note.metaData.slice(2).length / 2, 10);
} else {
encoded = Array(7).fill();
metaDataSize = 0;
}
const noteDataLength = (0x20 * 2 + metaDataSize).toString(16);
const noteLength = (0x20 * 4 + 0x20 * 2 + metaDataSize).toString(16);
encoded[0] = padLeft(noteLength, 64);
encoded[1] = padLeft('1', 64);
encoded[2] = padLeft(note.owner.slice(2), 64);
encoded[3] = padLeft(note.noteHash.slice(2), 64);
encoded[4] = padLeft(noteDataLength, 64);
encoded[5] = padLeft(bn128.compress(note.gamma.x.fromRed(), note.gamma.y.fromRed()).toString(16), 64);
encoded[6] = padLeft(bn128.compress(note.sigma.x.fromRed(), note.sigma.y.fromRed()).toString(16), 64);
return encoded.join('');
};
exportNote() {
const publicKey = this.getPublic();
const viewingKey = this.getView();
let k = '0x';
let a = '0x';
if (BN.isBN(this.k)) {
k = padLeft(this.k.fromRed().toString(16), 64);
}
if (BN.isBN(this.a)) {
a = padLeft(this.a.fromRed().toString(16), 64);
}
return {
publicKey,
viewingKey,
k,
a,
noteHash: this.noteHash,
};
}
function encodeProofData(proofData) {
const { length } = proofData;
const noteString = proofData.map(notes => encodeNote(notes));
const data = [padLeft(Number(length).toString(16), 64), ...noteString].join('');
return {
data,
length: Number(data.length / 2),
};
}
const generateCommitment = async (k) => {
const a = padLeft(new BN(crypto.randomBytes(32), 16).umod(bn128.curve.n).toString(16), 64);
const kHex = padLeft(toHex(Number(k).toString(10)).slice(2), 8);
const ephemeral = secp256k1.ec.keyFromPrivate(crypto.randomBytes(32));
const viewingKey = `0x${a}${kHex}${padLeft(ephemeral.getPublic(true, 'hex'), 66)}`;
return note.fromViewKey(viewingKey);
};
noteCoder.encodeNotePublicKey = ({ gamma, sigma, ephemeral }) => {
const gammaEnc = gamma.encode('hex', true);
const sigmaEnc = sigma.encode('hex', true);
const ephemeralEnc = ephemeral.encode('hex', true);
return `0x${padLeft(gammaEnc, 66)}${padLeft(sigmaEnc, 66)}${padLeft(ephemeralEnc, 66)}`;
};
const { offsets } = encodedParameters.reduce((acc, encodedParameter) => {
acc.offsets.push(padLeft(acc.offset.toString(16), 64));
acc.offset += (encodedParameter.length) / 2;
return acc;
}, {
offset: (Object.keys(config).length + 1) * 32,