Skip to content

Commit

Permalink
BCH calculations numeric precision fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Sramko committed Jul 13, 2020
1 parent 9b99a78 commit 9ba6e71
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "dist/src/index.js",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
8 changes: 4 additions & 4 deletions src/offchain/bcash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ export const prepareBitcoinCashSignedOffchainTransaction =
});

const lastVin = data.find(d => d.vIn === '-1') as WithdrawalResponseData;
tx.addOutput(address, Number(new BigNumber(amount).multipliedBy(100000000).toFixed(8, BigNumber.ROUND_FLOOR)));
tx.addOutput(address, Number(new BigNumber(amount).multipliedBy(100000000).toFixed(0, BigNumber.ROUND_FLOOR)));
if (mnemonic) {
const {xpub} = generateBchWallet(testnet, mnemonic);
tx.addOutput(generateAddressFromXPub(Currency.BCH, testnet, xpub, 0), Number(new BigNumber(lastVin.amount).multipliedBy(100000000).toFixed(8, BigNumber.ROUND_FLOOR)));
tx.addOutput(generateAddressFromXPub(Currency.BCH, testnet, xpub, 0), Number(new BigNumber(lastVin.amount).multipliedBy(100000000).toFixed(0, BigNumber.ROUND_FLOOR)));
} else if (keyPair && changeAddress) {
tx.addOutput(changeAddress, Number(new BigNumber(lastVin.amount).multipliedBy(100000000).toFixed(8, BigNumber.ROUND_FLOOR)));
tx.addOutput(changeAddress, Number(new BigNumber(lastVin.amount).multipliedBy(100000000).toFixed(0, BigNumber.ROUND_FLOOR)));
} else {
throw new Error('Impossible to prepare transaction. Either mnemonic or keyPair and attr must be present.');
}
Expand All @@ -78,7 +78,7 @@ export const prepareBitcoinCashSignedOffchainTransaction =
if (input.vIn === '-1') {
continue;
}
const value = Number(data[i].amount) * 100000000;
const value = Number(new BigNumber(data[i].amount).multipliedBy(100000000).toFixed(0, BigNumber.ROUND_FLOOR));
if (mnemonic) {
const derivationKey = input.address && input.address.derivationKey ? input.address.derivationKey : 0;
const privateKey = await generatePrivateKeyFromMnemonic(Currency.BCH, testnet, mnemonic, derivationKey);
Expand Down
4 changes: 2 additions & 2 deletions src/transaction/bcash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export const prepareBitcoinCashSignedTransaction = async (testnet: boolean, body
for (const [i, item] of fromUTXO.entries()) {
transactionBuilder.addInput(item.txHash, item.index);
privateKeysToSign.push(item.privateKey);
amountToSign.push(Number(new BigNumber(item.value).multipliedBy(100000000).toFixed(8, BigNumber.ROUND_FLOOR)));
amountToSign.push(Number(new BigNumber(item.value).multipliedBy(100000000).toFixed(0, BigNumber.ROUND_FLOOR)));
}
for (const item of to) {
transactionBuilder.addOutput(item.address, Number(new BigNumber(item.value).multipliedBy(100000000).toFixed(8, BigNumber.ROUND_FLOOR)));
transactionBuilder.addOutput(item.address, Number(new BigNumber(item.value).multipliedBy(100000000).toFixed(0, BigNumber.ROUND_FLOOR)));
}

for (let i = 0; i < privateKeysToSign.length; i++) {
Expand Down

0 comments on commit 9ba6e71

Please sign in to comment.