mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-03-25 08:38:45 +00:00
use AddressUtils where appropriate
instead of duplicating logic
This commit is contained in:
parent
b73628d939
commit
19cae77e2b
3 changed files with 74 additions and 61 deletions
lib
pages/send_view
pages_desktop_specific/my_stack_view/wallet_view/sub_widgets
utilities
|
@ -163,25 +163,23 @@ class _SendViewState extends ConsumerState<SendView> {
|
|||
level: LogLevel.Info,
|
||||
);
|
||||
|
||||
final results = AddressUtils.parseUri(qrResult.rawContent);
|
||||
final paymentData = AddressUtils.parsePaymentUri(qrResult.rawContent);
|
||||
|
||||
Logging.instance.log("qrResult parsed: $results", level: LogLevel.Info);
|
||||
|
||||
if (results.isNotEmpty && results["scheme"] == coin.uriScheme) {
|
||||
if (paymentData.coin.uriScheme == coin.uriScheme) {
|
||||
// auto fill address
|
||||
_address = (results["address"] ?? "").trim();
|
||||
_address = paymentData.address.trim();
|
||||
sendToController.text = _address!;
|
||||
|
||||
// autofill notes field
|
||||
if (results["message"] != null) {
|
||||
noteController.text = results["message"]!;
|
||||
} else if (results["label"] != null) {
|
||||
noteController.text = results["label"]!;
|
||||
if (paymentData.message != null) {
|
||||
noteController.text = paymentData.message!;
|
||||
} else if (paymentData.label != null) {
|
||||
noteController.text = paymentData.label!;
|
||||
}
|
||||
|
||||
// autofill amount field
|
||||
if (results["amount"] != null) {
|
||||
final Amount amount = Decimal.parse(results["amount"]!).toAmount(
|
||||
if (paymentData.amount != null) {
|
||||
final Amount amount = Decimal.parse(paymentData.amount!).toAmount(
|
||||
fractionDigits: coin.fractionDigits,
|
||||
);
|
||||
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
|
||||
|
@ -1071,7 +1069,7 @@ class _SendViewState extends ConsumerState<SendView> {
|
|||
_address = _address!.substring(0, _address!.indexOf("\n"));
|
||||
}
|
||||
|
||||
sendToController.text = formatAddress(_address!);
|
||||
sendToController.text = AddressUtils().formatAddress(_address!);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -1402,7 +1400,8 @@ class _SendViewState extends ConsumerState<SendView> {
|
|||
|
||||
if (coin is Epiccash) {
|
||||
// strip http:// and https:// if content contains @
|
||||
content = formatAddress(
|
||||
content = AddressUtils()
|
||||
.formatAddress(
|
||||
content,
|
||||
);
|
||||
}
|
||||
|
@ -2421,22 +2420,3 @@ class _SendViewState extends ConsumerState<SendView> {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
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://") ||
|
||||
epicAddress.startsWith("https://")) &&
|
||||
epicAddress.contains("@")) {
|
||||
epicAddress = epicAddress.replaceAll("http://", "");
|
||||
epicAddress = epicAddress.replaceAll("https://", "");
|
||||
}
|
||||
// strip mailto: prefix
|
||||
if (epicAddress.startsWith("mailto:")) {
|
||||
epicAddress = epicAddress.replaceAll("mailto:", "");
|
||||
}
|
||||
// strip / suffix if the address contains an @ symbol (and is thus an epicbox address)
|
||||
if (epicAddress.endsWith("/") && epicAddress.contains("@")) {
|
||||
epicAddress = epicAddress.substring(0, epicAddress.length - 1);
|
||||
}
|
||||
return epicAddress;
|
||||
}
|
||||
|
|
|
@ -743,13 +743,15 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
|
|||
|
||||
void _processQrCodeData(String qrCodeData) {
|
||||
try {
|
||||
var results = AddressUtils.parseUri(qrCodeData);
|
||||
if (results.isNotEmpty && results["scheme"] == coin.uriScheme) {
|
||||
_address = (results["address"] ?? "").trim();
|
||||
final paymentData = AddressUtils.parsePaymentUri(qrCodeData);
|
||||
if (paymentData.coin.uriScheme == coin.uriScheme) {
|
||||
// Auto fill address.
|
||||
_address = paymentData.address.trim();
|
||||
sendToController.text = _address!;
|
||||
|
||||
if (results["amount"] != null) {
|
||||
final Amount amount = Decimal.parse(results["amount"]!).toAmount(
|
||||
// Amount.
|
||||
if (paymentData.amount != null) {
|
||||
final Amount amount = Decimal.parse(paymentData.amount!).toAmount(
|
||||
fractionDigits: coin.fractionDigits,
|
||||
);
|
||||
cryptoAmountController.text = ref.read(pAmountFormatter(coin)).format(
|
||||
|
@ -759,6 +761,13 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
|
|||
ref.read(pSendAmount.notifier).state = amount;
|
||||
}
|
||||
|
||||
// Note/message.
|
||||
if (paymentData.message != null) {
|
||||
_note = paymentData.message;
|
||||
} else if (paymentData.label != null) {
|
||||
_note = paymentData.label;
|
||||
}
|
||||
|
||||
_setValidAddressProviders(_address);
|
||||
setState(() {
|
||||
_addressToggleFlag = sendToController.text.isNotEmpty;
|
||||
|
@ -809,30 +818,23 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
|
|||
content = content.substring(0, content.indexOf("\n"));
|
||||
}
|
||||
|
||||
// Check if the pasted value is a URI:
|
||||
final results = AddressUtils.parseUri(content);
|
||||
if (results.isNotEmpty) {
|
||||
if (results["scheme"] == coin.uriScheme) {
|
||||
try {
|
||||
final paymentData = AddressUtils.parsePaymentUri(content);
|
||||
if (paymentData.coin.uriScheme == coin.uriScheme) {
|
||||
// auto fill address
|
||||
_address = results["address"] ?? "";
|
||||
if (coin is Bitcoincash || coin is Ecash) {
|
||||
sendToController.text = results["scheme"] != null
|
||||
? results["scheme"]! + ":" + _address!
|
||||
: _address!;
|
||||
} else {
|
||||
sendToController.text = _address!;
|
||||
_address = paymentData.address;
|
||||
sendToController.text = _address!;
|
||||
|
||||
// autofill notes field.
|
||||
if (paymentData.message != null) {
|
||||
_note = paymentData.message;
|
||||
} else if (paymentData.label != null) {
|
||||
_note = paymentData.label;
|
||||
}
|
||||
|
||||
// autofill notes field
|
||||
if (results["message"] != null) {
|
||||
_note = results["message"]!;
|
||||
} else if (results["label"] != null) {
|
||||
_note = results["label"]!;
|
||||
}
|
||||
|
||||
// autofill amount field
|
||||
if (results["amount"] != null) {
|
||||
final amount = Decimal.parse(results["amount"]!).toAmount(
|
||||
// autofill amoutn field
|
||||
if (paymentData.amount != null) {
|
||||
final amount = Decimal.parse(paymentData.amount!).toAmount(
|
||||
fractionDigits: coin.fractionDigits,
|
||||
);
|
||||
cryptoAmountController.text = ref
|
||||
|
@ -846,13 +848,24 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
|
|||
setState(() {
|
||||
_addressToggleFlag = sendToController.text.isNotEmpty;
|
||||
});
|
||||
} else {
|
||||
if (coin is Epiccash) {
|
||||
content = AddressUtils().formatAddress(content);
|
||||
}
|
||||
|
||||
return;
|
||||
sendToController.text = content;
|
||||
_address = content;
|
||||
|
||||
_setValidAddressProviders(_address);
|
||||
setState(() {
|
||||
_addressToggleFlag = sendToController.text.isNotEmpty;
|
||||
});
|
||||
}
|
||||
} else {
|
||||
} catch (e) {
|
||||
// If parsing fails, treat it as a plain address.
|
||||
if (coin is Epiccash) {
|
||||
// strip http:// and https:// if content contains @
|
||||
content = formatAddress(content);
|
||||
content = AddressUtils().formatAddress(content);
|
||||
}
|
||||
|
||||
sendToController.text = content;
|
||||
|
|
|
@ -359,6 +359,26 @@ class AddressUtils {
|
|||
throw UnsupportedError('Unsupported URI scheme: $scheme');
|
||||
}
|
||||
}
|
||||
|
||||
/// Formats an address string to remove any unnecessary prefixes or suffixes.
|
||||
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://") ||
|
||||
epicAddress.startsWith("https://")) &&
|
||||
epicAddress.contains("@")) {
|
||||
epicAddress = epicAddress.replaceAll("http://", "");
|
||||
epicAddress = epicAddress.replaceAll("https://", "");
|
||||
}
|
||||
// strip mailto: prefix
|
||||
if (epicAddress.startsWith("mailto:")) {
|
||||
epicAddress = epicAddress.replaceAll("mailto:", "");
|
||||
}
|
||||
// strip / suffix if the address contains an @ symbol (and is thus an epicbox address)
|
||||
if (epicAddress.endsWith("/") && epicAddress.contains("@")) {
|
||||
epicAddress = epicAddress.substring(0, epicAddress.length - 1);
|
||||
}
|
||||
return epicAddress;
|
||||
}
|
||||
}
|
||||
|
||||
class PaymentUriData {
|
||||
|
|
Loading…
Reference in a new issue