Skip to content

Commit

Permalink
Remove prefix from QR code scans
Browse files Browse the repository at this point in the history
  • Loading branch information
takenagain committed Mar 11, 2024
1 parent 571a223 commit 3b44149
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ class _AmountAddressStepState extends State<AmountAddressStep> {
barcode = 'Error';
});
} else {
final address = result;
final address = removeBlockchainPrefix(result);
final uri = Uri.tryParse(address.trim());

setState(() {
Expand Down
21 changes: 21 additions & 0 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,27 @@ Future<String> 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<String> 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.
Expand Down
36 changes: 36 additions & 0 deletions test/utils/utils_test.dart
Original file line number Diff line number Diff line change
@@ -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:');
});
});
}

0 comments on commit 3b44149

Please sign in to comment.