Skip to content

Commit

Permalink
Add validation for keys
Browse files Browse the repository at this point in the history
  • Loading branch information
kackogut committed Sep 19, 2023
1 parent d5c7a61 commit 83a49c9
Show file tree
Hide file tree
Showing 12 changed files with 294 additions and 26 deletions.
104 changes: 104 additions & 0 deletions .idea/libraries/Dart_Packages.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/data/base/text_value.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import '../error/input_error.dart';

class TextValue {
TextValue({
required this.text,
this.text = "",
this.error,
});

Expand Down
10 changes: 10 additions & 0 deletions lib/data/error/input_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,13 @@ class RequiredAmount extends InputError {
@override
String text = "Field is required";
}

class KeyNotBase58Encoded extends InputError {
@override
String text = "Invalid value, must be base58 encoded";
}

class KeyLengthInvalid extends InputError {
@override
String text = "Key value must be between 32 and 44 characters";
}
7 changes: 5 additions & 2 deletions lib/di/blocs_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:sol_pay_gen/domain/generate_transfer_request_qr_use_case.dart';
import 'package:sol_pay_gen/feature/qr/bloc/qr_generator_cubit.dart';
import 'package:sol_pay_gen/validator/keys_validator.dart';
import 'package:sol_pay_gen/validator/number_validator.dart';

import '../data/transfer/transfer_request_repository.dart';
Expand All @@ -11,8 +12,10 @@ MultiBlocProvider getBlocProviders({required StatelessWidget child}) {
return MultiBlocProvider(
providers: [
BlocProvider<ParametersInputCubit>(
create: (BuildContext context) =>
ParametersInputCubit(context.read<NumberValidator>()),
create: (BuildContext context) => ParametersInputCubit(
context.read<NumberValidator>(),
context.read<KeysValidator>(),
),
),
BlocProvider<QrGeneratorCubit>(
create: (BuildContext context) => QrGeneratorCubit(
Expand Down
8 changes: 6 additions & 2 deletions lib/di/repositories_providers.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:sol_pay_gen/data/transfer/transfer_request_repository.dart';
import 'package:sol_pay_gen/validator/keys_validator.dart';
import 'package:sol_pay_gen/validator/number_validator.dart';

MultiRepositoryProvider getRepositoryProviders({
Expand All @@ -8,10 +9,13 @@ MultiRepositoryProvider getRepositoryProviders({
return MultiRepositoryProvider(
providers: [
RepositoryProvider<TransferRequestRepository>(
create: (context) => DefaultTransferRequestRepository(),
create: (_) => DefaultTransferRequestRepository(),
),
RepositoryProvider<NumberValidator>(
create: (context) => DefaultNumberValidator(),
create: (_) => DefaultNumberValidator(),
),
RepositoryProvider<KeysValidator>(
create: (_) => DefaultKeysValidator(),
)
],
child: blocProviders,
Expand Down
38 changes: 30 additions & 8 deletions lib/feature/input/bloc/parameters_input_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:sol_pay_gen/data/base/text_value.dart';
import 'package:sol_pay_gen/feature/input/bloc/parameters_input_state.dart';

import '../../../data/error/input_error.dart';
import '../../../validator/keys_validator.dart';
import '../../../validator/number_validator.dart';

class ParametersInputCubit extends Cubit<ParametersInputState> {
final NumberValidator _numberValidator;
final KeysValidator _keysValidator;

ParametersInputCubit(
this._numberValidator,
this._keysValidator,
) : super(
ParametersInputState(
address: TextValue(text: ""),
amount: TextValue(text: ""),
reference: null,
address: TextValue(),
amount: TextValue(),
reference: TextValue(),
memo: null,
message: null,
label: null,
Expand All @@ -22,9 +26,12 @@ class ParametersInputCubit extends Cubit<ParametersInputState> {
);

void onAddressChange(String address) {
emit(
state.copyWith(address: TextValue(text: address)),
);
emit(state.copyWith(
address: TextValue(
text: address,
error: _validateAddress(address),
),
));
}

void onAmountChange(String amount) {
Expand All @@ -45,7 +52,12 @@ class ParametersInputCubit extends Cubit<ParametersInputState> {
}

void onReferenceChange(String reference) {
emit(state.copyWith(reference: reference));
emit(state.copyWith(
reference: TextValue(
text: reference,
error: reference.isEmpty ? null : _keysValidator.validateKey(reference),
),
));
}

void onSplTokenChange(String token) {
Expand All @@ -57,6 +69,16 @@ class ParametersInputCubit extends Cubit<ParametersInputState> {
}

void onValidate() {
emit(state.validate());
emit(state.copyWith(
address: state.address.copyWith(
error: _validateAddress(state.address.text),
),
));
}

InputError? _validateAddress(String address) {
return address.isEmpty
? RequiredAmount()
: _keysValidator.validateKey(address);
}
}
16 changes: 4 additions & 12 deletions lib/feature/input/bloc/parameters_input_state.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import 'package:equatable/equatable.dart';
import 'package:sol_pay_gen/data/base/text_value.dart';
import 'package:sol_pay_gen/data/error/input_error.dart';

class ParametersInputState extends Equatable {
final TextValue address;
final TextValue amount;
final String? reference;
final TextValue reference;
final String? label;
final String? message;
final String? memo;
Expand Down Expand Up @@ -36,7 +35,7 @@ class ParametersInputState extends Equatable {
TextValue? amount,
String? label,
String? message,
String? reference,
TextValue? reference,
String? memo,
String? splTokenAddress,
}) =>
Expand All @@ -50,13 +49,6 @@ class ParametersInputState extends Equatable {
splTokenAddress: splTokenAddress ?? this.splTokenAddress,
);

ParametersInputState validate() {
return copyWith(
address: address.copyWith(
error: address.text.isEmpty ? RequiredAmount() : null,
),
);
}

bool isValid() => amount.isValid() && address.isValid();
bool isValid() =>
amount.isValid() && address.isValid() && reference.isValid();
}
Loading

0 comments on commit 83a49c9

Please sign in to comment.