Files
hash-wallet/lib/utils/payment_request.dart
T
jwinterm 04203654f1 Rename Flutter project + binary: cake_wallet -> hash_wallet
The big one. Touches 760+ files. Locks in the user's decision to break
import-line compatibility with upstream cake-tech/cake_wallet in exchange
for a binary named hash_wallet on every platform.

Changes:
- pubspec_description.yaml: project name cake_wallet -> hash_wallet
- 762 Dart files: every 'package:cake_wallet/...' import rewritten to
  'package:hash_wallet/...' (lib/, cw_*/lib/, tool/, test/, integration_test/)
- linux/CMakeLists.txt: BINARY_NAME cake_wallet -> hash_wallet
- windows/CMakeLists.txt: project + BINARY_NAME (CakeWallet -> HashWallet)
- windows/runner/Runner.rc: company / file / product strings to Hash Wallet
  + Such Software LLC copyright
- macos/Runner.xcodeproj/project_base.pbxproj: cake_wallet_new.app refs
  -> hash_wallet.app (macOS output bundle name)
- com.suchsoftware.HashWallet.yml (Flatpak): all binary path refs
  cake_wallet -> hash_wallet (e.g. /app/hash_wallet/hash_wallet)
- linux/com.suchsoftware.HashWallet.desktop: Exec=hash_wallet
- scripts/android/copy_monero_deps.sh: CW_DIR target path
- configure_hash_wallet.sh: error message typo (old filename)

INTENTIONALLY UNCHANGED:
- cw_* subpackage names (cw_core, cw_bitcoin, cw_wownero, etc.) — they
  remain as the historical 'cake wallet' naming; renaming them would
  cascade through every sub-package's internal structure
- Method channel name 'com.cake_wallet/native_utils' in iOS/macOS/Android
  AppDelegates and MainActivity — it's a string identifier that has to
  match the Dart side exactly. Renaming requires both-sides change; it's
  internal-only and not user-visible.
- ghcr.io/cake-tech/cake_wallet:debian13-... docker image references in
  build scripts — that's Cake Labs' published image we use as the build
  environment, not ours.

User data directory now follows the new binary name:
  ~/.local/share/hash_wallet/  (was ~/.local/share/cake_wallet/)
  ~/.config/hash_wallet/       (was ~/.config/cake_wallet/)
Existing users on the old name will need migration if any exist.
2026-05-16 11:33:25 -04:00

124 lines
4.0 KiB
Dart

import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/lnurl.dart';
import 'package:cw_core/payment_uris.dart';
import 'package:hash_wallet/nano/nano.dart';
class PaymentRequest {
PaymentRequest(this.address, this.amount, this.note, this.scheme, this.pjUri,
{this.callbackUrl, this.callbackMessage, this.contractAddress});
factory PaymentRequest.fromString(String input) {
try {
return PaymentRequest.fromBolt11(input);
} catch (_) {
return PaymentRequest.fromUri(Uri.parse(input));
}
}
factory PaymentRequest.fromBolt11(String invoice) {
final amountRaw = getBolt11Amount(invoice) ?? 0;
final amount = CryptoCurrency.btcln.formatAmount(BigInt.from(amountRaw));
return PaymentRequest(invoice, amount, '', '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;
if (uri != null) {
if (uri.queryParameters['pj'] != null) {
pjUri = uri.toString();
}
address = uri.queryParameters['address'] ?? uri.path;
try {
final lnAmount =
CryptoCurrency.btcln.formatAmount(BigInt.from(getBolt11Amount(uri.path) ?? 0));
if (lnAmount != 0) {
amount = lnAmount;
}
} 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);
address = paymentUri.address;
amount = paymentUri.amount;
contractAddress = paymentUri.contractAddress;
}
}
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,
);
}
final String address;
final String amount;
final String note;
final String scheme;
final String? pjUri;
final String? callbackUrl;
final String? callbackMessage;
final String? contractAddress;
/// 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;
// Try to parse as double - if successful, it's already in usable format
final parsed = double.tryParse(amount.replaceAll(',', '.'));
if (parsed == null) return false;
// Check if the amount contains a decimal point and is a reasonable number,
// it's likely already in usable format rather than raw format
// Raw amounts are typically very large integers without decimal points
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;
}
}