Files
hash-wallet/lib/utils/payment_request.dart
T
jwinterm dd449f2594 Merge upstream Cake Wallet v6.5.0
Catches up from the May 12 fork point (v6.1.2+23) through v6.5.0: 535
upstream commits, including the Monero silent-open fix, Monero on Trezor,
Ledger/Trezor BLE fixes, the new seed and history screens, quick Bitcoin
sync, git-pinned dependencies (pubspec_overrides.yaml), frozen Hive
adapters (*.part.dart), monero_c v0.18.4.6-RC2 and Flutter 3.41.9.

Merged with both sides normalized first (import rename, 100-column
format), which took the conflict count from 614 paths to 322. The
procedure is now scripts/upstream_sync.sh.

Kept from Hash Bags:
- WalletType enum order and Hive IDs. SQLite stores WalletType.index and
  our order differs from upstream's; taking theirs would reread saved
  wallets as other chains. Our frozen adapter keeps the shipped IDs.
- The 1.0.2 legacy PIN and wallet-password decode and self-heal.
- Removed chains (Solana, Tron, Zano, Zcash, Decred, Haven) and the new
  Robinhood chain stay out; Cake Pay and non-Trocador swaps stay out.
- Default nodes (node.wownero.org, sethforprivacy, Blockstream, bysh.me),
  converted to upstream's isDefault node-list key; Wownero is back in the
  node reset list.
- Privacy: address resolvers limited to domain-anchored sources again;
  prices.neroswap.com for fiat; upstream's charts tab, which reads
  prices.cakewallet.com, sits behind FeatureFlag.isChartsEnabled = false.
- Lightning stays behind FeatureFlag.isLightningEnabled, including
  upstream's new post-seed Lightning username step.
- Our icon pipeline, branding and bundle identifiers.

Twelve new upstream widget tests are skipped: test and code are unchanged
from v6.5.0 apart from renames (not rerun on a pristine upstream checkout;
upstream's top_bar_test does not compile against its own TopBar).
flutter analyze lib: 0 errors; flutter test: 345 passed, 12 skipped;
flutter build bundle compiles. Built with Flutter 3.41.9.
2026-10-24 23:49:29 -04:00

212 lines
6.1 KiB
Dart

import "package:hash_wallet/nano/nano.dart";
import "package:cw_core/amount/amount_sanitizer.dart";
import "package:cw_core/amount/money.dart";
import "package:cw_core/crypto_currency.dart";
import "package:cw_core/format_fixed.dart";
import "package:cw_core/lnurl.dart";
import "package:cw_core/payment_uris.dart";
import "package:cw_core/utils/print_verbose.dart";
class PaymentRequest {
PaymentRequest(
this.address,
this.amount,
this.note,
this.scheme,
this.pjUri, {
this.callbackUrl,
this.callbackMessage,
this.contractAddress,
this.chainId,
this.rawTokenAmount,
});
factory PaymentRequest.fromString(String input) {
try {
return PaymentRequest.fromBolt11(input);
} catch (_) {
try {
return PaymentRequest.fromUri(Uri.parse(input));
} catch (_) {
return PaymentRequest(input, "", "", "", null);
}
}
}
factory PaymentRequest.fromBolt11(String invoice) {
final amount = getBolt11Amount(invoice) ?? Money.zero(CryptoCurrency.btcln);
return PaymentRequest(invoice, amount.toString(), "", "lightning", null);
}
factory PaymentRequest.fromUri(Uri? uri) {
var address = "";
var amount = "";
var note = "";
var scheme = "";
String? walletType;
String? callbackUrl;
String? callbackMessage;
String? pjUri;
String? contractAddress;
int? chainId;
String? rawTokenAmount;
if (uri != null) {
if (uri.queryParameters["pj"] != null) {
pjUri = uri.toString();
}
address = uri.queryParameters["address"] ?? uri.path;
try {
final lnAmount = getBolt11Amount(uri.path) ?? Money.zero(CryptoCurrency.btcln);
if (lnAmount != 0) {
amount = lnAmount.toString();
}
} catch (_) {}
if (amount.isEmpty) {
amount = uri.queryParameters["tx_amount"] ?? uri.queryParameters["amount"] ?? "";
}
note = uri.queryParameters["tx_description"] ?? uri.queryParameters["message"] ?? "";
scheme = uri.scheme;
callbackUrl = uri.queryParameters["callback"];
callbackMessage = uri.queryParameters["callbackMessage"];
walletType = uri.queryParameters["type"];
if (scheme == "ethereum") {
final paymentUri = ERC681URI.fromUri(uri);
final hasExplicitChainId = uri.path.contains("@");
address = paymentUri.address;
amount = paymentUri.amount;
contractAddress = paymentUri.contractAddress;
chainId = hasExplicitChainId ? paymentUri.chainId : null;
rawTokenAmount = paymentUri.rawTokenAmount;
} else if (scheme == "tron") {
final token = uri.queryParameters["token"];
if (token != null && token.isNotEmpty) {
contractAddress = token;
}
} else if (scheme == "solana") {
final splToken = uri.queryParameters["spl-token"];
if (splToken != null && splToken.isNotEmpty) {
contractAddress = splToken;
}
}
}
if (scheme == "nano-gpt") {
scheme = walletType ?? "nano";
}
if (nano != null) {
if (amount.isNotEmpty) {
if (!_isAlreadyUsableAmount(amount)) {
if (address.contains("nano")) {
amount = nanoUtil!.getRawAsUsableString(amount, nanoUtil!.rawPerNano);
} else if (address.contains("ban")) {
amount = nanoUtil!.getRawAsUsableString(amount, nanoUtil!.rawPerBanano);
}
}
}
}
return PaymentRequest(
address,
amount,
note,
scheme,
pjUri,
callbackUrl: callbackUrl,
callbackMessage: callbackMessage,
contractAddress: contractAddress,
chainId: chainId,
rawTokenAmount: rawTokenAmount,
);
}
PaymentRequest copyWith({
String? address,
String? amount,
String? note,
String? scheme,
String? pjUri,
String? callbackUrl,
String? callbackMessage,
String? contractAddress,
int? chainId,
String? rawTokenAmount,
}) =>
PaymentRequest(
address ?? this.address,
amount ?? this.amount,
note ?? this.note,
scheme ?? this.scheme,
pjUri ?? this.pjUri,
callbackUrl: callbackUrl ?? this.callbackUrl,
callbackMessage: callbackMessage ?? this.callbackMessage,
contractAddress: contractAddress ?? this.contractAddress,
chainId: chainId ?? this.chainId,
rawTokenAmount: rawTokenAmount ?? this.rawTokenAmount,
);
final String address;
final String amount;
final String note;
final String scheme;
final String? pjUri;
final String? callbackUrl;
final String? callbackMessage;
final String? contractAddress;
final int? chainId;
final String? rawTokenAmount;
String? resolveTokenAmount(CryptoCurrency token) {
if (amount.isNotEmpty) {
return amount;
}
if (rawTokenAmount == null || rawTokenAmount!.isEmpty) {
return null;
}
final raw = BigInt.tryParse(rawTokenAmount!);
if (raw == null) {
return null;
}
// 1e9 whole tokens is beyond any real payment, so a larger result means the QR
// came from an old app version that encoded with 18 decimals regardless of token.
if (token.decimals != 18 && raw > BigInt.from(10).pow(token.decimals + 9)) {
printV("resolveTokenAmount: reading $rawTokenAmount with legacy 18 decimals");
return formatFixed(raw, 18);
}
return formatFixed(raw, token.decimals);
}
/// Checks if the amount string is already in a usable format (e.g., "123.45") and doesn't need to be converted from raw format.
///
/// This was causing an error for us when we scan Nano QRs with amounts in them, the amounts are already in usable format so when the parsing was done, it returns 0 wrongly.
static bool _isAlreadyUsableAmount(String amount) {
if (amount.isEmpty) {
return false;
}
final parsed = double.tryParse(amount.sanitized());
if (parsed == null) {
return false;
}
if (amount.contains(".") && parsed > 0 && parsed < 1000000000) {
return true;
}
// If it's a small integer (less than 1 billion), it's likely already usable
if (parsed == parsed.toInt() && parsed < 1000000000) {
return true;
}
return false;
}
}