From fc031111a7a9ea142ee47175d1fa7c103d730b30 Mon Sep 17 00:00:00 2001 From: Jack Date: Fri, 15 Dec 2023 12:17:10 -0900 Subject: [PATCH] feat: fix validateOutputAmount API for BigNumber fix: preserve backward compatibility --- src/outputs.ts | 10 +++++++--- src/utils.ts | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/outputs.ts b/src/outputs.ts index 5c79d4e..f07353a 100644 --- a/src/outputs.ts +++ b/src/outputs.ts @@ -61,7 +61,7 @@ export function validateOutput(network, output, inputsTotalSats?) { /** * Lowest acceptable output amount in Satoshis. */ -const DUST_LIMIT_SATS = new BigNumber(546); +const DUST_LIMIT_SATS = "546"; /** * Validate the given output amount (in Satoshis). @@ -74,11 +74,15 @@ const DUST_LIMIT_SATS = new BigNumber(546); * * - Cannot exceed the total input amount (this check is only run if * `inputsTotalSats` is passed. + * + * TODO: minSats accepting a BigNumber is only to maintain some backward + * compatibility. Ideally, the arg would not expose this lib's dependencies to + * the caller. It should be made to only accept number or string. */ export function validateOutputAmount( amountSats, - maxSats?, - minSats = DUST_LIMIT_SATS + maxSats?: number | string, + minSats: number | string | BigNumber = DUST_LIMIT_SATS ) { let a, its; try { diff --git a/src/utils.ts b/src/utils.ts index c80fb01..dd0bf22 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -74,6 +74,8 @@ export function satoshisToBitcoins(satoshis: string | number) { * - Rounds down output value to the nearest Satoshi. */ export function bitcoinsToSatoshis(btc: string | number) { + const btcAmount = new BigNumber(btc); + return new BigNumber(btc) .shiftedBy(8) .integerValue(BigNumber.ROUND_DOWN)