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..74ebc548a 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 @@ -301,7 +301,7 @@ class _AmountAddressStepState extends State { barcode = 'Error'; }); } else { - final address = result; + final address = removeBlockchainPrefix(result); final uri = Uri.tryParse(address.trim()); setState(() { diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index bafbb18d5..d52a096f0 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -865,6 +865,27 @@ Future scanQr(BuildContext context) async { ); } +/// Removes the blockchain prefix from [address] +/// +/// Returns [address] if either the prefix or address are empty. +/// +/// Example usage: +/// ```dart +/// removeBlockchainPrefix(eth:0x123) == '0x123' +/// ``` +String removeBlockchainPrefix(String address) { + if (address.contains(':')) { + final List parts = address.split(':'); + final bool hasPrefix = parts.length == 2 && parts[0].isNotEmpty; + final bool hasAddress = parts.length == 2 && parts[1].isNotEmpty; + if (hasPrefix && hasAddress) { + return parts[1]; + } + } + + return address; +} + /// Function to generate password based on some criteria /// /// Adapted from code at https://blog.albertobonacina.com/password-generator-with-dart. diff --git a/test/utils/utils_test.dart b/test/utils/utils_test.dart new file mode 100644 index 000000000..5fb45f94b --- /dev/null +++ b/test/utils/utils_test.dart @@ -0,0 +1,36 @@ +import 'package:flutter_test/flutter_test.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 = removeBlockchainPrefix(address); + expect(result, '0x1234567890abcdef'); + }); + + test('removes blackcoin prefix from address', () { + const String address = 'blackcoin:B8Q9aZ4Mz1ZwWYaknJdZ8t3Z6z9F3Fz1Zz'; + final String result = removeBlockchainPrefix(address); + expect(result, 'B8Q9aZ4Mz1ZwWYaknJdZ8t3Z6z9F3Fz1Zz'); + }); + + test('returns the same address if no prefix is present', () { + const String address = '0x1234567890abcdef'; + final String result = removeBlockchainPrefix(address); + expect(result, '0x1234567890abcdef'); + }); + + test('returns the address after the prefix', () { + const String address = ':0x1234567890abcdef'; + final String result = removeBlockchainPrefix(address); + expect(result, ':0x1234567890abcdef'); + }); + + test('returns the same address if no prefix is present', () { + const String address = '0x1234567890abcdef:'; + final String result = removeBlockchainPrefix(address); + expect(result, '0x1234567890abcdef:'); + }); + }); +}