Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions lib/pages/send_view/send_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class _SendViewState extends ConsumerState<SendView> with SendFeeForm {
_clearRecipientForms();

// auto fill address
_address = paymentData.address.trim();
_address = _stripRelayPrefixes(paymentData.address.trim());

// autofill notes field
if (paymentData.message != null) {
Expand Down Expand Up @@ -401,6 +401,31 @@ class _SendViewState extends ConsumerState<SendView> with SendFeeForm {
}
}

/// [address] without the prefixes an Epicbox address can be scanned or
/// pasted with. Other coins' addresses come back unchanged.
String _stripRelayPrefixes(String address) => coin is Epiccash
? AddressUtils().formatEpicCashAddress(address)
: address;

/// Strips the prefixes an Epicbox address can be pasted with. Only
/// rewrites the field when that changes the text, so typing stays intact.
String _formatRelayAddress(String text) {
if (coin is! Epiccash) return text;
String address = text.trim();
if (address.contains("\n")) {
address = address.substring(0, address.indexOf("\n"));
}
final formatted = _stripRelayPrefixes(address);
if (formatted != address) {
sendToController.value = TextEditingValue(
text: formatted,
selection: TextSelection.collapsed(offset: formatted.length),
);
return formatted;
}
return text;
}

Future<void> _checkSparkNameAndOrSetAddress(
String content, {
bool setController = true,
Expand Down Expand Up @@ -520,7 +545,9 @@ class _SendViewState extends ConsumerState<SendView> with SendFeeForm {
_applyUri(paymentData);
} else {
_setOpReturnData(null);
_address = qrResult.rawContent!.split("\n").first.trim();
_address = _stripRelayPrefixes(
qrResult.rawContent!.split("\n").first.trim(),
);
sendToController.text = _address ?? "";

_setValidAddressProviders(_address);
Expand Down Expand Up @@ -1548,24 +1575,6 @@ class _SendViewState extends ConsumerState<SendView> with SendFeeForm {
}
});

// add listener for epic cash to strip http:// and https:// prefixes if the address also ocntains an @ symbol (indicating an epicbox address)
if (coin is Epiccash) {
sendToController.addListener(() {
_address = sendToController.text.trim();

if (_address != null && _address!.isNotEmpty) {
_address = _address!.trim();
if (_address!.contains("\n")) {
_address = _address!.substring(0, _address!.indexOf("\n"));
}

sendToController.text = AddressUtils().formatEpicCashAddress(
_address!,
);
}
});
}

Decimal? price;
if (ref.watch(prefsChangeNotifierProvider.select((s) => s.externalCalls))) {
price = ref.watch(
Expand Down Expand Up @@ -1836,6 +1845,7 @@ class _SendViewState extends ConsumerState<SendView> with SendFeeForm {
selectAll: false,
),
onChanged: (newValue) async {
newValue = _formatRelayAddress(newValue);
final trimmed = newValue.trim();

if (AddressUtils.mayBePaymentUri(
Expand All @@ -1853,8 +1863,12 @@ class _SendViewState extends ConsumerState<SendView> with SendFeeForm {
_applyUri(parsed);
} else {
_setOpReturnData(null);
// The text is already in the field;
// writing it back would move the
// cursor on every keystroke.
await _checkSparkNameAndOrSetAddress(
newValue,
setController: false,
);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1081,7 +1081,10 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
_applyUri(paymentData);
} else {
_setOpReturnData(null);
_address = qrCodeData.split("\n").first.trim();
_address = stripRelayPrefixes(
coin,
qrCodeData.split("\n").first.trim(),
);
sendToController.text = _address ?? "";

_setValidAddressProviders(_address);
Expand Down Expand Up @@ -1296,7 +1299,7 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
_clearRecipientForms();

// auto fill address
_address = paymentData.address;
_address = stripRelayPrefixes(coin, paymentData.address.trim());
sendToController.text = _address!;

// autofill notes field.
Expand Down Expand Up @@ -1347,6 +1350,26 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
}
}

/// Strips the prefixes an Epicbox or MWCMQS address can be pasted with.
/// Only rewrites the field when that changes the text, so typing stays
/// intact.
String _formatRelayAddress(String text) {
if (coin is! Epiccash && coin is! Mimblewimblecoin) return text;
String address = text.trim();
if (address.contains("\n")) {
address = address.substring(0, address.indexOf("\n"));
}
final formatted = stripRelayPrefixes(coin, address);
if (formatted != address) {
sendToController.value = TextEditingValue(
text: formatted,
selection: TextSelection.collapsed(offset: formatted.length),
);
return formatted;
}
return text;
}

Future<void> _checkSparkNameAndOrSetAddress(
String content, {
bool setController = true,
Expand Down Expand Up @@ -1678,39 +1701,8 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
final previewEnabled = ref.watch(pPreviewTxButtonEnabled(coin));
final needsEthFee = coin is Ethereum && isCustomFee;

// add listener for epic cash to strip http:// and https:// prefixes if the address also ocntains an @ symbol (indicating an epicbox address)
if (coin is Epiccash) {
sendToController.addListener(() {
_address = sendToController.text;

if (_address != null && _address!.isNotEmpty) {
_address = _address!.trim();
if (_address!.contains("\n")) {
_address = _address!.substring(0, _address!.indexOf("\n"));
}

sendToController.text = formatAddress(_address!);
}
});
}

final balType = ref.watch(publicPrivateBalanceStateProvider);

if (coin is Mimblewimblecoin) {
sendToController.addListener(() {
_address = sendToController.text;

if (_address != null && _address!.isNotEmpty) {
_address = _address!.trim();
if (_address!.contains("\n")) {
_address = _address!.substring(0, _address!.indexOf("\n"));
}

sendToController.text = formatAddressMwc(_address!);
}
});
}

final isMwebEnabled = ref.watch(
pWalletInfo(walletId).select((s) => s.isMwebEnabled),
);
Expand Down Expand Up @@ -2162,6 +2154,7 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
selectAll: false,
),
onChanged: (newValue) async {
newValue = _formatRelayAddress(newValue);
final trimmed = newValue;

if (AddressUtils.mayBePaymentUri(trimmed, _address ?? "")) {
Expand All @@ -2175,7 +2168,12 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
_applyUri(parsed);
} else {
_setOpReturnData(null);
await _checkSparkNameAndOrSetAddress(newValue);
// The text is already in the field; writing it back
// would move the cursor on every keystroke.
await _checkSparkNameAndOrSetAddress(
newValue,
setController: false,
);
}
} else {
_setOpReturnData(null);
Expand Down Expand Up @@ -2589,6 +2587,15 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
}
}

/// [address] without the prefixes an Epicbox or MWCMQS address can be scanned
/// or pasted with. Other coins' addresses come back unchanged.
String stripRelayPrefixes(CryptoCurrency coin, String address) =>
coin is Epiccash
? formatAddress(address)
: coin is Mimblewimblecoin
? formatAddressMwc(address)
: address;

String formatAddress(String epicAddress) {
// strip http:// or https:// prefixes if the address contains an @ symbol (and is thus an epicbox address)
if ((epicAddress.startsWith("http://") ||
Expand Down
8 changes: 8 additions & 0 deletions lib/utilities/address_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,14 @@ class AddressUtils {
return null;
}

// A web URL is a send address for coins that send over HTTP (Epic Cash's
// and Grin's foreign API), never a payment URI. Without this, typing the
// "http:" of such an address parsed as a URI with an empty address and
// cleared the field.
if (scheme == "http" || scheme == "https") {
return null;
}

try {
final parsedUri = _parseUri(uri);
final Map<String, String> parsedData = parsedUri.params;
Expand Down
19 changes: 19 additions & 0 deletions test/address_utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,23 @@ void main() {
);
});
});

test("web URLs are send addresses, not payment URIs", () {
for (final input in [
"http:",
"http://",
"http://127.0.0.1:3415",
"https://wallet.example.com/v2/foreign",
"HTTPS://wallet.example.com",
]) {
expect(AddressUtils.parsePaymentUri(input), isNull, reason: input);
}
// Ordinary payment URIs still parse.
expect(
AddressUtils.parsePaymentUri(
"bitcoin:1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa?amount=0.1",
)?.address,
"1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
);
});
}
Loading
Loading