ethers/lib/utils#zeroPad TypeScript Examples

The following examples show how to use ethers/lib/utils#zeroPad. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: miner.ts    From pownft-miner with Apache License 2.0 6 votes vote down vote up
export async function attempt(lastHash: string, address: string, nonce: BigNumber, difficulty: BigNumber, addressHashArray: Uint8Array){

    // can we do this smarter, don't like allocating a new array
    addressHashArray.set(zeroPad(nonce.toTwos(256) as any, 32), addressHashArray.length - 32);

    const hashFast = "0x" + await keccak(addressHashArray, 256);

    // sanity check, uncomment this to use the original hash, verify they are equal.  this is about 4-5x slower
    // in my test setup.
    // const hashSlow = solidityKeccak256(["address","bytes32","uint256"], [address, lastHash, nonce]);
    // if (hashSlow !== hashFast) {
    //     console.log(`Hashes do not match ${hashSlow}  ${hashFast}`);
    //     throw Error('oh no!');
    // }

    return BigNumber.from(hashFast).lt(difficulty);
}
Example #2
Source File: hashToField.ts    From hubble-contracts with MIT License 5 votes vote down vote up
export function expandMsg(
    domain: Uint8Array,
    msg: Uint8Array,
    outLen: number
): Uint8Array {
    if (domain.length > 32) {
        throw new Error("bad domain size");
    }

    const out: Uint8Array = new Uint8Array(outLen);

    const len0 = 64 + msg.length + 2 + 1 + domain.length + 1;
    const in0: Uint8Array = new Uint8Array(len0);
    // zero pad
    let off = 64;
    // msg
    in0.set(msg, off);
    off += msg.length;
    // l_i_b_str
    in0.set([(outLen >> 8) & 0xff, outLen & 0xff], off);
    off += 2;
    // I2OSP(0, 1)
    in0.set([0], off);
    off += 1;
    // DST_prime
    in0.set(domain, off);
    off += domain.length;
    in0.set([domain.length], off);

    const b0 = sha256(in0);

    const len1 = 32 + 1 + domain.length + 1;
    const in1: Uint8Array = new Uint8Array(len1);
    // b0
    in1.set(arrayify(b0), 0);
    off = 32;
    // I2OSP(1, 1)
    in1.set([1], off);
    off += 1;
    // DST_prime
    in1.set(domain, off);
    off += domain.length;
    in1.set([domain.length], off);

    const b1 = sha256(in1);

    // b_i = H(strxor(b_0, b_(i - 1)) || I2OSP(i, 1) || DST_prime);
    const ell = Math.floor((outLen + 32 - 1) / 32);
    let bi = b1;

    for (let i = 1; i < ell; i++) {
        const ini: Uint8Array = new Uint8Array(32 + 1 + domain.length + 1);
        const nb0 = zeroPad(arrayify(b0), 32);
        const nbi = zeroPad(arrayify(bi), 32);
        const tmp = new Uint8Array(32);
        for (let i = 0; i < 32; i++) {
            tmp[i] = nb0[i] ^ nbi[i];
        }

        ini.set(tmp, 0);
        let off = 32;
        ini.set([1 + i], off);
        off += 1;
        ini.set(domain, off);
        off += domain.length;
        ini.set([domain.length], off);

        out.set(arrayify(bi), 32 * (i - 1));
        bi = sha256(ini);
    }

    out.set(arrayify(bi), 32 * (ell - 1));
    return out;
}