Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove prefix from QR code scans #121

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:');
});
});
}
Loading