-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
63 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,4 +140,4 @@ export const exportedForTesting = { | |
generatePushdataPrefixedPaymentId, | ||
stringToHex, | ||
getDataPushdata, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,53 @@ | ||
import { CryptoCurrency } from "./types"; | ||
|
||
const DEFAULT = 3; | ||
const MAX = 4; | ||
const DEFAULT = 3 | ||
const MAX = 4 | ||
|
||
export const getNSatoshis = ( | ||
amount: number, | ||
randomSatoshis: boolean | number, | ||
satsPrecision: number, | ||
): number => { | ||
const amountDigits = amount | ||
.toFixed(satsPrecision) | ||
.toString() | ||
.replace('.', '').length; | ||
|
||
export const getNSatoshis = (amount: number, randomSatoshis: boolean | number, satsPrecision: number): number => { | ||
const amountDigits = amount.toFixed(satsPrecision).toString().replace('.', '').length; | ||
if (randomSatoshis === true) { | ||
return Math.min(DEFAULT, amountDigits); | ||
return Math.min(DEFAULT, amountDigits) | ||
} else if (randomSatoshis === false) { | ||
throw new Error('Trying to randomize satoshis when not allowed.'); | ||
throw new Error("Trying to randomize satoshis when not allowed.") | ||
} | ||
if (randomSatoshis > MAX) { | ||
randomSatoshis = MAX; | ||
randomSatoshis = MAX | ||
} | ||
return Math.min(randomSatoshis, amountDigits); | ||
}; | ||
return Math.min(randomSatoshis, amountDigits) | ||
} | ||
|
||
export const randomizeSatoshis = (amount: number, addressType: CryptoCurrency, randomSatoshis: boolean | number): number => { | ||
if (amount === 0) { | ||
return 0; | ||
} | ||
let nSatoshis: number; | ||
let random: number; | ||
let randomizedAmount: number; | ||
let ret: number; | ||
let nSatoshis: number | ||
let random: number | ||
let randomizedAmount: number | ||
let ret: number | ||
switch (addressType) { | ||
case 'BCH': | ||
nSatoshis = getNSatoshis(amount, randomSatoshis, 8); | ||
random = Math.floor(Math.random() * 10 ** nSatoshis) * 1e-8; | ||
nSatoshis = getNSatoshis(amount, randomSatoshis, 8) | ||
random = Math.floor(Math.random() * 10 ** nSatoshis) * 1e-8 | ||
|
||
randomizedAmount = | ||
Math.max(0, +amount.toFixed(nSatoshis)) + // zero out the least-significant digits | ||
random; | ||
random | ||
ret = +randomizedAmount.toFixed(8); | ||
break; | ||
break | ||
case 'XEC': | ||
nSatoshis = getNSatoshis(amount, randomSatoshis, 2); | ||
random = Math.floor(Math.random() * 10 ** nSatoshis) * 1e-2; | ||
nSatoshis = getNSatoshis(amount, randomSatoshis, 2) | ||
random = Math.floor(Math.random() * 10 ** nSatoshis) * 1e-2 | ||
|
||
const multiplier = 10 ** (nSatoshis - 2); | ||
randomizedAmount = | ||
Math.max(0, +(Math.floor(amount / multiplier) * multiplier)) + // zero out the least-significant digits | ||
random; | ||
const multiplier = 10 ** (nSatoshis - 2) | ||
randomizedAmount = Math.max(0, +(Math.floor(amount / multiplier) * multiplier)) + // zero out the least-significant digits | ||
random | ||
ret = +randomizedAmount.toFixed(2); | ||
break; | ||
break | ||
default: | ||
throw new Error(`Invalid currency: ${addressType}`); | ||
throw new Error(`Invalid currency: ${addressType}`) | ||
} | ||
return ret; | ||
return ret | ||
}; | ||
|
||
export default randomizeSatoshis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters