From bbece29c8eb21e5f106fa151f08f9ae2952cdc9b Mon Sep 17 00:00:00 2001 From: Konstantin Ullrich Date: Mon, 5 Aug 2024 11:54:26 +0200 Subject: [PATCH] Fix polyseed encryption --- cw_monero/lib/monero_wallet_service.dart | 42 ++++++++++++++-------- cw_wownero/lib/wownero_wallet_service.dart | 11 +++--- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/cw_monero/lib/monero_wallet_service.dart b/cw_monero/lib/monero_wallet_service.dart index ad8e2200e..09a805992 100644 --- a/cw_monero/lib/monero_wallet_service.dart +++ b/cw_monero/lib/monero_wallet_service.dart @@ -1,5 +1,7 @@ import 'dart:ffi'; import 'dart:io'; + +import 'package:cw_core/get_height_by_date.dart'; import 'package:cw_core/monero_wallet_utils.dart'; import 'package:cw_core/pathForWallet.dart'; import 'package:cw_core/unspent_coins_info.dart'; @@ -8,15 +10,14 @@ import 'package:cw_core/wallet_credentials.dart'; import 'package:cw_core/wallet_info.dart'; import 'package:cw_core/wallet_service.dart'; import 'package:cw_core/wallet_type.dart'; -import 'package:cw_core/get_height_by_date.dart'; import 'package:cw_monero/api/exceptions/wallet_opening_exception.dart'; import 'package:cw_monero/api/wallet_manager.dart' as monero_wallet_manager; import 'package:cw_monero/api/wallet_manager.dart'; import 'package:cw_monero/monero_wallet.dart'; import 'package:flutter/widgets.dart'; import 'package:hive/hive.dart'; -import 'package:polyseed/polyseed.dart'; import 'package:monero/monero.dart' as monero; +import 'package:polyseed/polyseed.dart'; class MoneroNewWalletCredentials extends WalletCredentials { MoneroNewWalletCredentials( @@ -29,7 +30,11 @@ class MoneroNewWalletCredentials extends WalletCredentials { class MoneroRestoreWalletFromSeedCredentials extends WalletCredentials { MoneroRestoreWalletFromSeedCredentials( - {required String name, required this.mnemonic, required this.passphrase, int height = 0, String? password}) + {required String name, + required this.mnemonic, + required this.passphrase, + int height = 0, + String? password}) : super(name: name, password: password, height: height); final String mnemonic; @@ -58,8 +63,11 @@ class MoneroRestoreWalletFromKeysCredentials extends WalletCredentials { final String spendKey; } -class MoneroWalletService extends WalletService { +class MoneroWalletService extends WalletService< + MoneroNewWalletCredentials, + MoneroRestoreWalletFromSeedCredentials, + MoneroRestoreWalletFromKeysCredentials, + MoneroNewWalletCredentials> { MoneroWalletService(this.walletInfoSource, this.unspentCoinsInfoSource); final Box walletInfoSource; @@ -79,14 +87,15 @@ class MoneroWalletService extends WalletService restoreFromHardwareWallet(MoneroNewWalletCredentials credentials) { - throw UnimplementedError("Restoring a Monero wallet from a hardware wallet is not yet supported!"); + throw UnimplementedError( + "Restoring a Monero wallet from a hardware wallet is not yet supported!"); } @override @@ -290,7 +297,8 @@ class MoneroWalletService extends WalletService _restoreFromPolyseed( String path, String password, Polyseed polyseed, WalletInfo walletInfo, PolyseedLang lang, - {PolyseedCoin coin = PolyseedCoin.POLYSEED_MONERO, int? overrideHeight}) async { + {PolyseedCoin coin = PolyseedCoin.POLYSEED_MONERO, + int? overrideHeight, + String? passphrase}) async { + if (polyseed.isEncrypted) polyseed.crypt(passphrase ?? ''); + final height = overrideHeight ?? getMoneroHeigthByDate(date: DateTime.fromMillisecondsSinceEpoch(polyseed.birthday * 1000)); final spendKey = polyseed.generateKey(coin, 32).toHexString(); diff --git a/cw_wownero/lib/wownero_wallet_service.dart b/cw_wownero/lib/wownero_wallet_service.dart index 7a99a4900..83ffe0aa6 100644 --- a/cw_wownero/lib/wownero_wallet_service.dart +++ b/cw_wownero/lib/wownero_wallet_service.dart @@ -82,14 +82,15 @@ class WowneroWalletService extends WalletService< if (credentials.isPolyseed) { final polyseed = Polyseed.create(); final lang = PolyseedLang.getByEnglishName(credentials.language); - polyseed.crypt(credentials.passphrase??''); + + if (credentials.passphrase != null) polyseed.crypt(credentials.passphrase!); final heightOverride = getWowneroHeightByDate(date: DateTime.now().subtract(Duration(days: 2))); return _restoreFromPolyseed( path, credentials.password!, polyseed, credentials.walletInfo!, lang, - overrideHeight: heightOverride); + overrideHeight: heightOverride, passphrase: credentials.passphrase); } await wownero_wallet_manager.createWallet( @@ -291,7 +292,7 @@ class WowneroWalletService extends WalletService< final polyseed = Polyseed.decode(credentials.mnemonic, lang, polyseedCoin); return _restoreFromPolyseed( - path, credentials.password!, polyseed, credentials.walletInfo!, lang); + path, credentials.password!, polyseed, credentials.walletInfo!, lang, passphrase: credentials.passphrase); } catch (e) { // TODO: Implement Exception for wallet list service. print('WowneroWalletsManager Error: $e'); @@ -301,7 +302,9 @@ class WowneroWalletService extends WalletService< Future _restoreFromPolyseed( String path, String password, Polyseed polyseed, WalletInfo walletInfo, PolyseedLang lang, - {PolyseedCoin coin = PolyseedCoin.POLYSEED_WOWNERO, int? overrideHeight}) async { + {PolyseedCoin coin = PolyseedCoin.POLYSEED_WOWNERO, int? overrideHeight, String? passphrase}) async { + if (polyseed.isEncrypted) polyseed.crypt(passphrase ?? ''); + final height = overrideHeight ?? getWowneroHeightByDate(date: DateTime.fromMillisecondsSinceEpoch(polyseed.birthday * 1000)); final spendKey = polyseed.generateKey(coin, 32).toHexString();