How to use the @zilliqa-js/util.BN function in @zilliqa-js/util

To help you get started, we’ve selected a few @zilliqa-js/util 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 Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-account / src / transaction.ts View on Github external
private async getBlockNumber(): Promise {
    try {
      const res: RPCResponse = await this.provider.send(
        RPCMethod.GetLatestTxBlock,
      );
      if (res.error === undefined && res.result.header.BlockNum) {
        // if blockNumber is too high, we use BN to be safer
        return new BN(res.result.header.BlockNum);
      } else {
        throw new Error('Can not get latest BlockNumber');
      }
    } catch (error) {
      throw error;
    }
  }
github zilpay / zil-pay / extension / controllers / services / blockchain / zilliqa.js View on Github external
nonce,
      version   // Netowrk version. type Number.
    } = txData;

    if (!version) {
      version = await this.version();
    }
    if (isNaN(nonce)) {
      nonce = balance.nonce;
    }
    if (currentNonce > balance.nonce) {
      nonce = currentNonce;
    }

    amount = new BN(amount);
    gasPrice = new BN(gasPrice);
    gasLimit = Long.fromNumber(gasLimit);

    nonce++;

    return this.transactions.new({
      nonce,
      gasPrice,
      amount,
      gasLimit,
      version,
      toAddr,
      pubKey,
      code,
      data
    });
  }
github zilpay / zil-pay / extension / controllers / services / blockchain / zilliqa.js View on Github external
toAddr,   // Recipient address. type String.
      nonce,
      version   // Netowrk version. type Number.
    } = txData;

    if (!version) {
      version = await this.version();
    }
    if (isNaN(nonce)) {
      nonce = balance.nonce;
    }
    if (currentNonce > balance.nonce) {
      nonce = currentNonce;
    }

    amount = new BN(amount);
    gasPrice = new BN(gasPrice);
    gasLimit = Long.fromNumber(gasLimit);

    nonce++;

    return this.transactions.new({
      nonce,
      gasPrice,
      amount,
      gasLimit,
      version,
      toAddr,
      pubKey,
      code,
      data
    });
github Zilliqa / nucleus-wallet / src / contexts / zil-context / index.tsx View on Github external
private getParams = async (toAddr, amountInZil) => {
    const response = await zilliqa.blockchain.getMinimumGasPrice();
    const gasPrice: string = response.result || '';

    const amountInQa = units.toQa(amountInZil, units.Units.Zil);
    return {
      toAddr,
      version,
      amount: amountInQa,
      gasPrice: new BN(gasPrice.toString()),
      gasLimit: Long.fromNumber(1)
    };
  };
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-crypto / src / schnorr.ts View on Github external
export const sign = (
  msg: Buffer,
  privKey: Buffer,
  pubKey: Buffer,
): Signature => {
  const prv = new BN(privKey);
  const drbg = getDRBG(msg);
  const len = curve.n.byteLength();

  let sig;
  while (!sig) {
    const k = new BN(drbg.generate(len));
    sig = trySign(msg, k, prv, pubKey);
  }

  return sig;
};
github zilpay / zil-pay / src / views / Popup.vue View on Github external
isMaxAmount() {
      try {
        const amountBN = new BN(this.CONFIRM_TX.amount);
        const balanceBN = new BN(this.account.balance);
        const feeBN = new BN(toZIL(this.fee));
        const txAmountBN = feeBN.add(amountBN);
        const isInsufficientFunds = balanceBN.lt(txAmountBN);

        if (isInsufficientFunds) {
          return ERRORCODE[1];
        }
      } catch(err) {
        return ERRORCODE[3];
      }

      return null;
    },
    txParams() {
github zilpay / zil-pay / src / views / Send.vue View on Github external
maxAmount() {
       if (+this.account.balance == 0) {
        return 0;
      }

      const fullBalance = new BN(this.account.balance);
      const feeBN = new BN(toZIL(this.fee));
      const amount = fullBalance.sub(feeBN);

      return fromZil(amount, false);
    }
  },
github zilpay / zil-pay / packages / background / services / blockchain / zilliqa.js View on Github external
} = txData

    if (!version) {
      version = await this.version()
    }

    if (isNaN(nonce)) {
      nonce = balance.nonce
    }

    if (currentNonce > balance.nonce) {
      nonce = currentNonce
    }

    amount = new BN(amount)
    gasPrice = new BN(gasPrice)
    gasLimit = Long.fromNumber(gasLimit)

    nonce++

    return this.transactions.new({
      nonce,
      gasPrice,
      amount,
      gasLimit,
      version,
      toAddr,
      pubKey,
      code,
      data
    })
  }
github Zilliqa / Zilliqa-JavaScript-Library / packages / zilliqa-js-crypto / src / util.ts View on Github external
throw new Error(`${address} is not a valid base 16 address`);
  }

  address = address.toLowerCase().replace('0x', '');
  const hash = hashjs
    .sha256()
    .update(address, 'hex')
    .digest('hex');
  const v = new BN(hash, 'hex', 'be');
  let ret = '0x';

  for (let i = 0; i < address.length; i++) {
    if ('0123456789'.indexOf(address[i]) !== -1) {
      ret += address[i];
    } else {
      ret += v.and(new BN(2).pow(new BN(255 - 6 * i))).gte(new BN(1))
        ? address[i].toUpperCase()
        : address[i].toLowerCase();
    }
  }

  return ret;
};