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

feat: ✨ Improve security : encrypt the secret with aeWallet #237

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
81 changes: 74 additions & 7 deletions lib/domain/usecases/bridge_evm_to_ae.usecase.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:aebridge/util/faucet_util.dart';
import 'package:archethic_dapp_framework_flutter/archethic_dapp_framework_flutter.dart'
as aedappfm;
import 'package:archethic_lib_dart/archethic_lib_dart.dart';
import 'package:archethic_wallet_client/archethic_wallet_client.dart' as awc;
import 'package:crypto/crypto.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_gen/gen_l10n/localizations.dart';
Expand All @@ -38,16 +39,82 @@ class BridgeEVMToArchethicUseCase
final session = ref.read(sessionNotifierProvider);
await bridgeNotifier.setCurrentStep(0);

Uint8List? secret;
if (recoverySecret != null) {
secret = Uint8List.fromList(recoverySecret);
} else {
secret = generateRandomSecret();
await bridgeNotifier.setSecret(secret.toList());
late Uint8List secret;
try {
final dappClient = await aedappfm.sl.getAsync<awc.ArchethicDAppClient>();
final walletTo = session.walletTo;
if (recoverySecret != null) {
final encryptedSecret = Uint8List.fromList(recoverySecret);

final decryptedPayloadsResult = await dappClient.decryptPayloads(
awc.DecryptPayloadRequest(
serviceName:
Uri.encodeFull('archethic-wallet-${walletTo!.nameAccount}'),
payloads: [
awc.DecryptPayloadRequestData(
payload: uint8ListToHex(encryptedSecret),
isHexa: true,
),
],
description: {
'en': localizations.aeDecryptSecret,
},
),
);
await decryptedPayloadsResult.when(
success: (decryptedPayloads) {
secret = hexToUint8List(
decryptedPayloads.decryptedPayloads[0].decryptedPayload,
);
},
failure: (failure) async {
await bridgeNotifier
.setFailure(const aedappfm.Failure.connectivityArchethic());
await bridgeNotifier.setTransferInProgress(false);
throw Exception();
},
);
} else {
secret = generateRandomSecret();

final encryptedPayloadsResult = await dappClient.encryptPayloads(
awc.EncryptPayloadRequest(
serviceName:
Uri.encodeFull('archethic-wallet-${walletTo!.nameAccount}'),
payloads: [
awc.EncryptPayloadRequestData(
payload: uint8ListToHex(secret),
isHexa: true,
),
],
),
);
await encryptedPayloadsResult.when(
success: (encryptedPayload) async {
await bridgeNotifier.setSecret(
hexToUint8List(
encryptedPayload.encryptedPayloads[0].encryptedPayload,
),
);
},
failure: (failure) async {
await bridgeNotifier
.setFailure(const aedappfm.Failure.connectivityArchethic());
await bridgeNotifier.setTransferInProgress(false);
throw Exception();
},
);
}
} catch (e) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure this try/catch is useful : error cases are already handled by result.failures.

await bridgeNotifier.setFailure(
aedappfm.Failure.other(cause: '$e'),
);
await bridgeNotifier.setTransferInProgress(false);
throw Exception();
}

final secretHash = sha256.convert(
secret,
secret.toList(),
);

String? htlcEVMAddress;
Expand Down
1 change: 1 addition & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
"aeSignTxDesc3": "This action reveals the secret required to securely unlock funds on both chains, ensuring the finality and symmetry of the bridge.",
"aeSignTxDesc4": "This action allows funds to be sent to the smart contract, enabling its execution and ensuring the secure processing of the bridge",
"aeSignTxDesc5": "This action allows you to execute the refund of your funds.",
"aeDecryptSecret": "This action allows to decrypt the secret required to secure the bridge between the 2 blockchains.",
"aeBridgeProvidedBy": "Bridge provided by",
"trustWalletWarning": "Trust Wallet users, please check that the \"Authorize eth_sign\" option is ticked in your application's security menu before processing."
}
Loading