Skip to content

Commit

Permalink
Merge pull request #935 from cypherstack/uri
Browse files Browse the repository at this point in the history
Filter unrecognized from extra URI parameters IAW BIP21
  • Loading branch information
julian-CStack authored Aug 27, 2024
2 parents 319080c + 2745598 commit dbc5fb7
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 78 deletions.
2 changes: 1 addition & 1 deletion lib/pages/receive_view/addresses/address_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class _AddressCardState extends ConsumerState<AddressCard> {
key: _qrKey,
child: QR(
data: AddressUtils.buildUriString(
widget.coin,
widget.coin.uriScheme,
address.value,
{},
),
Expand Down
4 changes: 2 additions & 2 deletions lib/pages/receive_view/addresses/address_details_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class _AddressDetailsViewState extends ConsumerState<AddressDetailsView> {
key: _qrKey,
child: QR(
data: AddressUtils.buildUriString(
ref.watch(pWalletCoin(widget.walletId)),
ref.watch(pWalletCoin(widget.walletId)).uriScheme,
address.value,
{},
),
Expand Down Expand Up @@ -289,7 +289,7 @@ class _AddressDetailsViewState extends ConsumerState<AddressDetailsView> {
key: _qrKey,
child: QR(
data: AddressUtils.buildUriString(
coin,
coin.uriScheme,
address.value,
{},
),
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/receive_view/addresses/address_qr_popup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class _AddressQrPopupState extends State<AddressQrPopup> {
key: _qrKey,
child: QR(
data: AddressUtils.buildUriString(
widget.coin,
widget.coin.uriScheme,
widget.addressString,
{},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class _GenerateUriQrCodeViewState extends State<GenerateUriQrCodeView> {
}

final uriString = AddressUtils.buildUriString(
widget.coin,
widget.coin.uriScheme,
receivingAddress,
queryParams,
);
Expand Down Expand Up @@ -263,7 +263,7 @@ class _GenerateUriQrCodeViewState extends State<GenerateUriQrCodeView> {
}

_uriString = AddressUtils.buildUriString(
widget.coin,
widget.coin.uriScheme,
receivingAddress,
{},
);
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/receive_view/receive_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ class _ReceiveViewState extends ConsumerState<ReceiveView> {
children: [
QR(
data: AddressUtils.buildUriString(
coin,
coin.uriScheme,
address,
{},
),
Expand Down
44 changes: 12 additions & 32 deletions lib/pages/send_view/send_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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!);
}
});
}
Expand Down Expand Up @@ -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,
);
}
Expand Down Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class _DesktopReceiveState extends ConsumerState<DesktopReceive> {
Center(
child: QR(
data: AddressUtils.buildUriString(
coin,
coin.uriScheme,
address,
{},
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,13 +730,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(
Expand All @@ -746,6 +748,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;
Expand Down Expand Up @@ -796,18 +805,65 @@ class _DesktopSendState extends ConsumerState<DesktopSend> {
content = content.substring(0, content.indexOf("\n"));
}

if (coin is Epiccash) {
// strip http:// and https:// if content contains @
content = formatAddress(content);
}
try {
final paymentData = AddressUtils.parsePaymentUri(content);
if (paymentData.coin.uriScheme == coin.uriScheme) {
// auto fill 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;
}

sendToController.text = content;
_address = content;
// autofill amoutn field
if (paymentData.amount != null) {
final amount = Decimal.parse(paymentData.amount!).toAmount(
fractionDigits: coin.fractionDigits,
);
cryptoAmountController.text = ref
.read(pAmountFormatter(coin))
.format(amount, withUnitName: false);
ref.read(pSendAmount.notifier).state = amount;
}

_setValidAddressProviders(_address);
setState(() {
_addressToggleFlag = sendToController.text.isNotEmpty;
});
// Trigger validation after pasting.
_setValidAddressProviders(_address);
setState(() {
_addressToggleFlag = sendToController.text.isNotEmpty;
});
} else {
if (coin is Epiccash) {
content = AddressUtils().formatAddress(content);
}

sendToController.text = content;
_address = content;

_setValidAddressProviders(_address);
setState(() {
_addressToggleFlag = sendToController.text.isNotEmpty;
});
}
} catch (e) {
// If parsing fails, treat it as a plain address.
if (coin is Epiccash) {
// strip http:// and https:// if content contains @
content = AddressUtils().formatAddress(content);
}

sendToController.text = content;
_address = content;

// Trigger validation after pasting.
_setValidAddressProviders(_address);
setState(() {
_addressToggleFlag = sendToController.text.isNotEmpty;
});
}
}
}

Expand Down
Loading

0 comments on commit dbc5fb7

Please sign in to comment.