How to use the web3-utils.padLeft function in web3-utils

To help you get started, we’ve selected a few web3-utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github AztecProtocol / AZTEC / packages / aztec.js / src / proof / dividendComputation / verifier.js View on Github external
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,
    };
};
github AztecProtocol / AZTEC / packages / aztec.js / src / proof / bilateralSwap / verifier.js View on Github external
} 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,
    };
};
github AztecProtocol / AZTEC / packages / aztec.js / src / proof / bilateralSwap / index.js View on Github external
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 };
};
github AztecProtocol / AZTEC / packages / aztec.js / src / encoder / outputCoder.js View on Github external
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('');
};
github AztecProtocol / AZTEC / packages / aztec.js / src / note / index.js View on Github external
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,
        };
    }
github AztecProtocol / AZTEC / packages / aztec.js / src / abiEncoder / joinSplit.js View on Github external
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),
    };
}
github AztecProtocol / AZTEC / packages / aztec.js / src / proof / joinSplit / helpers.js View on Github external
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);
};
github AztecProtocol / AZTEC / packages / aztec.js / src / encoder / noteCoder.js View on Github external
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)}`;
};
github AztecProtocol / AZTEC / packages / aztec.js / src / abiEncoder / encoderFactory.js View on Github external
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,