request historical rates from coingecko in whole utc days
What changed, and why it matters
This commit changes how Sparrow Wallet asks CoinGecko for historical Bitcoin exchange rates. Previously it requested rates using raw timestamps, which could include partial days and a slightly wrong one-year cutoff. Now it rounds the request to whole UTC days and clamps the end date to the current time. This is a correctness/quality fix for price data used in wallet history and reporting; it is not a direct security vulnerability patch.
Treat as a routine data-quality fix. Reviewers may optionally verify that the new day-boundary math correctly handles leap years and edge cases around the 365-day cutoff, and that downstream consumers of getHistoricalExchangeRates handle empty or shortened result sets gracefully.
Security signals we found
Fixes timezone/date-boundary handling in external API requests
Reduces risk of requesting future or out-of-range timestamps
No input validation, output parsing, or cryptographic changes observed
Evidence from the diff
ExchangeSource.java’s CoinGecko historical-rate request logic was refactored. The old code converted start/end Date objects to epoch seconds and enforced a one-year lookback using Calendar.getInstance() (local timezone, not UTC, and not aligned to day boundaries). The new code computes whole UTC days using TimeUnit.DAYS.toSeconds(1), Math.floorDiv/Math.ceilDiv, and System.currentTimeMillis(), ensuring requests are aligned to UTC day boundaries and bounded by ‘now’. The change reduces API misuse and improves consistency of returned daily rates, but does not address cryptographic, network-trust, or authentication issues.
Changed components
src/main/java/com/sparrowwallet/sparrow/net/ExchangeSource.javaCoinGecko historical exchange-rate fetchingInspect captured patch +8 / −7
### src/main/java/com/sparrowwallet/sparrow/net/ExchangeSource.java
@@ -22,6 +22,7 @@
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.*;
+import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public enum ExchangeSource {
@@ -165,13 +166,13 @@ private CoinGeckoRates getRates() {
@Override
public Map<Date, Double> getHistoricalExchangeRates(Currency currency, Date start, Date end) {
- long startDate = start.getTime() / 1000;
- long endDate = end.getTime() / 1000;
-
- Calendar cal = Calendar.getInstance();
- cal.add(Calendar.YEAR, -1);
- startDate = Math.max(cal.getTimeInMillis() / 1000, startDate);
- endDate = Math.max(cal.getTimeInMillis() / 1000, endDate);
+ //Requested in whole UTC days, the rates being used by day, and starting no earlier than the first whole day within the last 365
+ long daySeconds = TimeUnit.DAYS.toSeconds(1);
+ long now = System.currentTimeMillis() / 1000;
+ long firstDay = Math.max(Math.ceilDiv(now - 365 * daySeconds, daySeconds), Math.floorDiv(start.getTime() / 1000, daySeconds));
+ long lastDay = Math.max(firstDay, Math.ceilDiv(end.getTime() / 1000, daySeconds));
+ long startDate = firstDay * daySeconds;
+ long endDate = Math.min(lastDay * daySeconds, now);
String url = "https://api.coingecko.com/api/v3/coins/bitcoin/market_chart/range?vs_currency=" + currency.getCurrencyCode() + "&from=" + startDate + "&to=" + endDate;
Why this scored 19/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.