diff --git a/lib/screens/portfolio/coin_detail/steps_withdraw.dart/amount_address_step/amount_address_step.dart b/lib/screens/portfolio/coin_detail/steps_withdraw.dart/amount_address_step/amount_address_step.dart index 445d9f38e..91a843274 100644 --- a/lib/screens/portfolio/coin_detail/steps_withdraw.dart/amount_address_step/amount_address_step.dart +++ b/lib/screens/portfolio/coin_detail/steps_withdraw.dart/amount_address_step/amount_address_step.dart @@ -188,6 +188,10 @@ class _AmountAddressStepState extends State { widget.addressController.text = address; } + set qrAmount(String amount) { + widget.amountController.text = amount; + } + void showWrongCoinDialog(PaymentUriInfo uriInfo) { dialogBloc.dialog = showDialog( context: context, @@ -301,8 +305,9 @@ class _AmountAddressStepState extends State { barcode = 'Error'; }); } else { - final address = result; - final uri = Uri.tryParse(address.trim()); + final String address = getAddressFromUri(result.trim()); + final String amount = getParameterValue(result.trim(), 'amount'); + final Uri uri = Uri.tryParse(result.trim()); setState(() { final PaymentUriInfo uriInfo = PaymentUriInfo.fromUri(uri); @@ -310,6 +315,7 @@ class _AmountAddressStepState extends State { handlePaymentData(uriInfo); } else { handleQrAdress(address); + qrAmount = amount ?? ''; } }); } diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index bafbb18d5..4fbe145c8 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -711,6 +711,35 @@ class PaymentUriInfo { final String amount; } +/// Returns the value of a [parameter] from the [address]. +/// Returns null if the [parameter] is not found. +/// [address] is expected to be a blockchain address or URL with parameters, +/// e.g. '?amount=123' +/// +/// Example usage: +/// ```dart +/// getParameterValue('litecoin:NPZcaiggQTy6uxL?amount=0.2', 'amount') == '0.2' +/// ``` +String getParameterValue(String address, String parameter) { + final RegExp regExp = RegExp('(?<=$parameter=)(.*?)(?=&|\$)'); + final RegExpMatch match = regExp.firstMatch(address); + return match?.group(0); +} + +/// Returns the address from the [uri]. +/// Returns the [uri] if no address is found. +/// [uri] is expected to be a blockchain address or URL with parameters, +/// e.g. '?amount=123' +/// Example usage: +/// ```dart +/// getAddressFromUri('litecoin:NPZcaiggQTy6uxL?amount=0.2') == 'NPZcaiggQTy6uxL' +/// ``` +String getAddressFromUri(String uri) { + final RegExp regExp = RegExp(r'(?<=:)(.*?)(?=\?|$)'); + final RegExpMatch match = regExp.firstMatch(uri); + return match?.group(0) ?? uri.split(':').last; +} + void showUriDetailsDialog( BuildContext context, PaymentUriInfo uriInfo, Function callbackIfAccepted) { if (uriInfo == null) return; diff --git a/test/utils/utils_test.dart b/test/utils/utils_test.dart new file mode 100644 index 000000000..796a786d8 --- /dev/null +++ b/test/utils/utils_test.dart @@ -0,0 +1,50 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:komodo_dex/model/coin.dart'; +import 'package:komodo_dex/utils/utils.dart'; + +void main() { + group('removeBlockchainPrefix', () { + test('removes blockchain prefix from address', () { + const String address = 'eth:0x1234567890abcdef'; + final String result = getAddressFromUri(address); + expect(result, '0x1234567890abcdef'); + }); + + test('removes blackcoin prefix from address', () { + const String address = 'blackcoin:B8Q9aZ4Mz1ZwWYaknJdZ8t3Z6z9F3Fz1Zz'; + final String result = getAddressFromUri(address); + expect(result, 'B8Q9aZ4Mz1ZwWYaknJdZ8t3Z6z9F3Fz1Zz'); + }); + + test('returns the same address if no prefix is present', () { + const String address = '0x1234567890abcdef'; + final String result = getAddressFromUri(address); + expect(result, '0x1234567890abcdef'); + }); + }); + + group('getParameterValue', () { + test('returns the parameter value from the address', () { + const String address = + 'eth:0x1234567890abcdef?param1=value1¶m2=value2'; + const String parameter = 'param1'; + final String result = getParameterValue(address, parameter); + expect(result, 'value1'); + }); + + test('returns null if the parameter is not present in the address', () { + const String address = + 'eth:0x1234567890abcdef?param1=value1¶m2=value2'; + const String parameter = 'param3'; + final String result = getParameterValue(address, parameter); + expect(result, isNull); + }); + + test('returns null if the address does not contain any parameters', () { + const String address = 'eth:0x1234567890abcdef'; + const String parameter = 'param1'; + final String result = getParameterValue(address, parameter); + expect(result, isNull); + }); + }); +}