Skip to content

Commit

Permalink
feat: ✨ Add encrypt and decrypt methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Chralu committed Dec 18, 2024
1 parent 24f9eae commit 09a9164
Show file tree
Hide file tree
Showing 12 changed files with 794 additions and 46 deletions.
10 changes: 10 additions & 0 deletions lib/infrastructure/rpc/awc_json_rpc_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import 'dart:convert';

import 'package:aewallet/domain/rpc/subscription.dart';
import 'package:aewallet/infrastructure/rpc/add_service/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/decrypt_payloads/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/dto/rpc_command_handler.dart';
import 'package:aewallet/infrastructure/rpc/encrypt_payloads/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/get_accounts/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/get_current_account/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/get_endpoint/command_handler.dart';
Expand Down Expand Up @@ -92,6 +94,14 @@ class AWCJsonRPCServer {
..registerMethod(
'signPayloads',
(params) => _handle(RPCSignPayloadsCommandHandler(), params),
)
..registerMethod(
'encryptPayloads',
(params) => _handle(RPCEncryptPayloadsCommandHandler(), params),
)
..registerMethod(
'decryptPayloads',
(params) => _handle(RPCDecryptPayloadsCommandHandler(), params),
);
}

Expand Down
42 changes: 42 additions & 0 deletions lib/infrastructure/rpc/decrypt_payloads/command_handler.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:aewallet/domain/rpc/commands/command.dart';
import 'package:aewallet/infrastructure/rpc/dto/request_origin.dart';
import 'package:aewallet/infrastructure/rpc/dto/rpc_command_handler.dart';
import 'package:archethic_wallet_client/archethic_wallet_client.dart' as awc;

class RPCDecryptPayloadsCommandHandler extends RPCCommandHandler<
awc.DecryptPayloadRequest, awc.DecryptPayloadsResult> {
RPCDecryptPayloadsCommandHandler() : super();

@override
RPCCommand<awc.DecryptPayloadRequest> commandToModel(
awc.Request dto,
) {
final rpcDecryptPayloadCommandDataList = <awc.DecryptPayloadRequestData>[];
final payloads = dto.payload['payloads'];
for (final Map<String, dynamic> payload in payloads) {
final payloadDecoded = payload['payload'] ?? '';
final isHexa = payload['isHexa'] ?? false;
final rpcDecryptTransactionCommandData = awc.DecryptPayloadRequestData(
payload: payloadDecoded,
isHexa: isHexa,
);
rpcDecryptPayloadCommandDataList.add(rpcDecryptTransactionCommandData);
}

return RPCCommand(
origin: dto.origin.toModel,
data: awc.DecryptPayloadRequest(
serviceName: dto.payload['serviceName'],
pathSuffix: dto.payload['pathSuffix'],
description: dto.payload['description'] ?? {},
payloads: rpcDecryptPayloadCommandDataList,
),
);
}

@override
Map<String, dynamic> resultFromModel(
awc.DecryptPayloadsResult model,
) =>
model.toJson();
}
20 changes: 20 additions & 0 deletions lib/infrastructure/rpc/deeplink_server.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'dart:async';

import 'package:aewallet/infrastructure/rpc/add_service/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/decrypt_payloads/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/dto/rpc_command_handler.dart';
import 'package:aewallet/infrastructure/rpc/encrypt_payloads/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/get_accounts/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/get_current_account/command_handler.dart';
import 'package:aewallet/infrastructure/rpc/get_endpoint/command_handler.dart';
Expand Down Expand Up @@ -126,6 +128,24 @@ class ArchethicDeeplinkRPCServer extends DeeplinkRpcRequestReceiver {
),
),
);
registerHandler(
DeeplinkRpcRequestHandler(
route: const DeeplinkRpcRoute('encrypt_payload'),
handle: (request) => _handle(
RPCEncryptPayloadsCommandHandler(),
request,
),
),
);
registerHandler(
DeeplinkRpcRequestHandler(
route: const DeeplinkRpcRoute('decrypt_payload'),
handle: (request) => _handle(
RPCDecryptPayloadsCommandHandler(),
request,
),
),
);
}

static Future<Map<String, dynamic>> _handle(
Expand Down
41 changes: 41 additions & 0 deletions lib/infrastructure/rpc/encrypt_payloads/command_handler.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:aewallet/domain/rpc/commands/command.dart';
import 'package:aewallet/infrastructure/rpc/dto/request_origin.dart';
import 'package:aewallet/infrastructure/rpc/dto/rpc_command_handler.dart';
import 'package:archethic_wallet_client/archethic_wallet_client.dart' as awc;

class RPCEncryptPayloadsCommandHandler extends RPCCommandHandler<
awc.EncryptPayloadRequest, awc.EncryptPayloadsResult> {
RPCEncryptPayloadsCommandHandler() : super();

@override
RPCCommand<awc.EncryptPayloadRequest> commandToModel(
awc.Request dto,
) {
final rpcEncryptPayloadCommandDataList = <awc.EncryptPayloadRequestData>[];
final payloads = dto.payload['payloads'];
for (final Map<String, dynamic> payload in payloads) {
final payloadDecoded = payload['payload'] ?? '';
final isHexa = payload['isHexa'] ?? false;
final rpcEncryptTransactionCommandData = awc.EncryptPayloadRequestData(
payload: payloadDecoded,
isHexa: isHexa,
);
rpcEncryptPayloadCommandDataList.add(rpcEncryptTransactionCommandData);
}

return RPCCommand(
origin: dto.origin.toModel,
data: awc.EncryptPayloadRequest(
serviceName: dto.payload['serviceName'],
pathSuffix: dto.payload['pathSuffix'],
payloads: rpcEncryptPayloadCommandDataList,
),
);
}

@override
Map<String, dynamic> resultFromModel(
awc.EncryptPayloadsResult model,
) =>
model.toJson();
}
2 changes: 2 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@
"signXTransactionsCommandReceivedNotification": "The %1 application wants you to sign %2 transactions.\nWould you like to confirm this action with the %3 account?\n\nOnce signed, transaction processing fees are estimated at",
"sign1PayloadCommandReceivedNotification": "The %1 application wants you to sign 1 payload.\nWould you like to confirm this action with the %2 account?\n\nThis action does not require any fees.",
"signXPayloadsCommandReceivedNotification": "The %1 application wants you to sign %2 payloads.\nWould you like to confirm this action with the %3 account?\n\nThis action does not require any fees.",
"decrypt1PayloadCommandReceivedNotification": "The %1 application wants you to decrypt 1 payload.\nWould you like to confirm this action with the %2 account?\n\nThis action does not require any fees.",
"decryptXPayloadsCommandReceivedNotification": "The %1 application wants you to decrypt %2 payloads.\nWould you like to confirm this action with the %3 account?\n\nThis action does not require any fees.",
"payload": "Payload",
"hexadecimalFormat": "Hexadecimal Format",
"searchField": "Search...",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:aewallet/domain/rpc/commands/command.dart';
import 'package:archethic_wallet_client/archethic_wallet_client.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';

part 'provider.freezed.dart';

@freezed
class DecryptPayloadsConfirmationFormState
with _$DecryptPayloadsConfirmationFormState {
const factory DecryptPayloadsConfirmationFormState({
required RPCCommand<DecryptPayloadRequest> decryptTransactionCommand,
}) = _DecryptPayloadsConfirmationFormState;
const DecryptPayloadsConfirmationFormState._();
}

class DecryptPayloadsConfirmationFormNotifiers
extends AutoDisposeFamilyAsyncNotifier<DecryptPayloadsConfirmationFormState,
RPCCommand<DecryptPayloadRequest>> {
@override
Future<DecryptPayloadsConfirmationFormState> build(
RPCCommand<DecryptPayloadRequest> arg,
) async {
return DecryptPayloadsConfirmationFormState(
decryptTransactionCommand: arg,
);
}
}

class DecryptPayloadsConfirmationProviders {
static final form = AsyncNotifierProvider.autoDispose.family<
DecryptPayloadsConfirmationFormNotifiers,
DecryptPayloadsConfirmationFormState,
RPCCommand<DecryptPayloadRequest>>(
DecryptPayloadsConfirmationFormNotifiers.new,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark

part of 'provider.dart';

// **************************************************************************
// FreezedGenerator
// **************************************************************************

T _$identity<T>(T value) => value;

final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');

/// @nodoc
mixin _$DecryptPayloadsConfirmationFormState {
RPCCommand<DecryptPayloadRequest> get decryptTransactionCommand =>
throw _privateConstructorUsedError;

/// Create a copy of DecryptPayloadsConfirmationFormState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$DecryptPayloadsConfirmationFormStateCopyWith<
DecryptPayloadsConfirmationFormState>
get copyWith => throw _privateConstructorUsedError;
}

/// @nodoc
abstract class $DecryptPayloadsConfirmationFormStateCopyWith<$Res> {
factory $DecryptPayloadsConfirmationFormStateCopyWith(
DecryptPayloadsConfirmationFormState value,
$Res Function(DecryptPayloadsConfirmationFormState) then) =
_$DecryptPayloadsConfirmationFormStateCopyWithImpl<$Res,
DecryptPayloadsConfirmationFormState>;
@useResult
$Res call({RPCCommand<DecryptPayloadRequest> decryptTransactionCommand});

$RPCCommandCopyWith<DecryptPayloadRequest, $Res>
get decryptTransactionCommand;
}

/// @nodoc
class _$DecryptPayloadsConfirmationFormStateCopyWithImpl<$Res,
$Val extends DecryptPayloadsConfirmationFormState>
implements $DecryptPayloadsConfirmationFormStateCopyWith<$Res> {
_$DecryptPayloadsConfirmationFormStateCopyWithImpl(this._value, this._then);

// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;

/// Create a copy of DecryptPayloadsConfirmationFormState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? decryptTransactionCommand = null,
}) {
return _then(_value.copyWith(
decryptTransactionCommand: null == decryptTransactionCommand
? _value.decryptTransactionCommand
: decryptTransactionCommand // ignore: cast_nullable_to_non_nullable
as RPCCommand<DecryptPayloadRequest>,
) as $Val);
}

/// Create a copy of DecryptPayloadsConfirmationFormState
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$RPCCommandCopyWith<DecryptPayloadRequest, $Res>
get decryptTransactionCommand {
return $RPCCommandCopyWith<DecryptPayloadRequest, $Res>(
_value.decryptTransactionCommand, (value) {
return _then(_value.copyWith(decryptTransactionCommand: value) as $Val);
});
}
}

/// @nodoc
abstract class _$$DecryptPayloadsConfirmationFormStateImplCopyWith<$Res>
implements $DecryptPayloadsConfirmationFormStateCopyWith<$Res> {
factory _$$DecryptPayloadsConfirmationFormStateImplCopyWith(
_$DecryptPayloadsConfirmationFormStateImpl value,
$Res Function(_$DecryptPayloadsConfirmationFormStateImpl) then) =
__$$DecryptPayloadsConfirmationFormStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({RPCCommand<DecryptPayloadRequest> decryptTransactionCommand});

@override
$RPCCommandCopyWith<DecryptPayloadRequest, $Res>
get decryptTransactionCommand;
}

/// @nodoc
class __$$DecryptPayloadsConfirmationFormStateImplCopyWithImpl<$Res>
extends _$DecryptPayloadsConfirmationFormStateCopyWithImpl<$Res,
_$DecryptPayloadsConfirmationFormStateImpl>
implements _$$DecryptPayloadsConfirmationFormStateImplCopyWith<$Res> {
__$$DecryptPayloadsConfirmationFormStateImplCopyWithImpl(
_$DecryptPayloadsConfirmationFormStateImpl _value,
$Res Function(_$DecryptPayloadsConfirmationFormStateImpl) _then)
: super(_value, _then);

/// Create a copy of DecryptPayloadsConfirmationFormState
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
$Res call({
Object? decryptTransactionCommand = null,
}) {
return _then(_$DecryptPayloadsConfirmationFormStateImpl(
decryptTransactionCommand: null == decryptTransactionCommand
? _value.decryptTransactionCommand
: decryptTransactionCommand // ignore: cast_nullable_to_non_nullable
as RPCCommand<DecryptPayloadRequest>,
));
}
}

/// @nodoc
class _$DecryptPayloadsConfirmationFormStateImpl
extends _DecryptPayloadsConfirmationFormState {
const _$DecryptPayloadsConfirmationFormStateImpl(
{required this.decryptTransactionCommand})
: super._();

@override
final RPCCommand<DecryptPayloadRequest> decryptTransactionCommand;

@override
String toString() {
return 'DecryptPayloadsConfirmationFormState(decryptTransactionCommand: $decryptTransactionCommand)';
}

@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$DecryptPayloadsConfirmationFormStateImpl &&
(identical(other.decryptTransactionCommand,
decryptTransactionCommand) ||
other.decryptTransactionCommand == decryptTransactionCommand));
}

@override
int get hashCode => Object.hash(runtimeType, decryptTransactionCommand);

/// Create a copy of DecryptPayloadsConfirmationFormState
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$DecryptPayloadsConfirmationFormStateImplCopyWith<
_$DecryptPayloadsConfirmationFormStateImpl>
get copyWith => __$$DecryptPayloadsConfirmationFormStateImplCopyWithImpl<
_$DecryptPayloadsConfirmationFormStateImpl>(this, _$identity);
}

abstract class _DecryptPayloadsConfirmationFormState
extends DecryptPayloadsConfirmationFormState {
const factory _DecryptPayloadsConfirmationFormState(
{required final RPCCommand<DecryptPayloadRequest>
decryptTransactionCommand}) =
_$DecryptPayloadsConfirmationFormStateImpl;
const _DecryptPayloadsConfirmationFormState._() : super._();

@override
RPCCommand<DecryptPayloadRequest> get decryptTransactionCommand;

/// Create a copy of DecryptPayloadsConfirmationFormState
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$DecryptPayloadsConfirmationFormStateImplCopyWith<
_$DecryptPayloadsConfirmationFormStateImpl>
get copyWith => throw _privateConstructorUsedError;
}
Loading

0 comments on commit 09a9164

Please sign in to comment.