Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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;
}
}
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
});
}
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
});
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)
};
};
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;
};
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() {
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);
}
},
} = 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
})
}
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;
};