Fix polyseed encryption

This commit is contained in:
Konstantin Ullrich 2024-08-05 11:54:26 +02:00
parent d3c079f1cf
commit bbece29c8e
No known key found for this signature in database
GPG key ID: E9562A013280F5DB
2 changed files with 34 additions and 19 deletions

View file

@ -1,5 +1,7 @@
import 'dart:ffi'; import 'dart:ffi';
import 'dart:io'; import 'dart:io';
import 'package:cw_core/get_height_by_date.dart';
import 'package:cw_core/monero_wallet_utils.dart'; import 'package:cw_core/monero_wallet_utils.dart';
import 'package:cw_core/pathForWallet.dart'; import 'package:cw_core/pathForWallet.dart';
import 'package:cw_core/unspent_coins_info.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_info.dart';
import 'package:cw_core/wallet_service.dart'; import 'package:cw_core/wallet_service.dart';
import 'package:cw_core/wallet_type.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/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' as monero_wallet_manager;
import 'package:cw_monero/api/wallet_manager.dart'; import 'package:cw_monero/api/wallet_manager.dart';
import 'package:cw_monero/monero_wallet.dart'; import 'package:cw_monero/monero_wallet.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:polyseed/polyseed.dart';
import 'package:monero/monero.dart' as monero; import 'package:monero/monero.dart' as monero;
import 'package:polyseed/polyseed.dart';
class MoneroNewWalletCredentials extends WalletCredentials { class MoneroNewWalletCredentials extends WalletCredentials {
MoneroNewWalletCredentials( MoneroNewWalletCredentials(
@ -29,7 +30,11 @@ class MoneroNewWalletCredentials extends WalletCredentials {
class MoneroRestoreWalletFromSeedCredentials extends WalletCredentials { class MoneroRestoreWalletFromSeedCredentials extends WalletCredentials {
MoneroRestoreWalletFromSeedCredentials( 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); : super(name: name, password: password, height: height);
final String mnemonic; final String mnemonic;
@ -58,8 +63,11 @@ class MoneroRestoreWalletFromKeysCredentials extends WalletCredentials {
final String spendKey; final String spendKey;
} }
class MoneroWalletService extends WalletService<MoneroNewWalletCredentials, class MoneroWalletService extends WalletService<
MoneroRestoreWalletFromSeedCredentials, MoneroRestoreWalletFromKeysCredentials, MoneroNewWalletCredentials> { MoneroNewWalletCredentials,
MoneroRestoreWalletFromSeedCredentials,
MoneroRestoreWalletFromKeysCredentials,
MoneroNewWalletCredentials> {
MoneroWalletService(this.walletInfoSource, this.unspentCoinsInfoSource); MoneroWalletService(this.walletInfoSource, this.unspentCoinsInfoSource);
final Box<WalletInfo> walletInfoSource; final Box<WalletInfo> walletInfoSource;
@ -79,14 +87,15 @@ class MoneroWalletService extends WalletService<MoneroNewWalletCredentials,
if (credentials.isPolyseed) { if (credentials.isPolyseed) {
final polyseed = Polyseed.create(); final polyseed = Polyseed.create();
final lang = PolyseedLang.getByEnglishName(credentials.language); final lang = PolyseedLang.getByEnglishName(credentials.language);
polyseed.crypt(credentials.passphrase??'');
if (credentials.passphrase != null) polyseed.crypt(credentials.passphrase!);
final heightOverride = final heightOverride =
getMoneroHeigthByDate(date: DateTime.now().subtract(Duration(days: 2))); getMoneroHeigthByDate(date: DateTime.now().subtract(Duration(days: 2)));
return _restoreFromPolyseed( return _restoreFromPolyseed(
path, credentials.password!, polyseed, credentials.walletInfo!, lang, path, credentials.password!, polyseed, credentials.walletInfo!, lang,
overrideHeight: heightOverride); overrideHeight: heightOverride, passphrase: credentials.passphrase);
} }
await monero_wallet_manager.createWallet( await monero_wallet_manager.createWallet(
@ -185,11 +194,8 @@ class MoneroWalletService extends WalletService<MoneroNewWalletCredentials,
final wmaddr = wmPtr.address; final wmaddr = wmPtr.address;
final waddr = openedWalletsByPath["$path/$wallet"]!.address; final waddr = openedWalletsByPath["$path/$wallet"]!.address;
// await Isolate.run(() { // await Isolate.run(() {
monero.WalletManager_closeWallet( monero.WalletManager_closeWallet(
Pointer.fromAddress(wmaddr), Pointer.fromAddress(wmaddr), Pointer.fromAddress(waddr), false);
Pointer.fromAddress(waddr),
false
);
// }); // });
openedWalletsByPath.remove("$path/$wallet"); openedWalletsByPath.remove("$path/$wallet");
print("wallet closed"); print("wallet closed");
@ -250,7 +256,8 @@ class MoneroWalletService extends WalletService<MoneroNewWalletCredentials,
@override @override
Future<MoneroWallet> restoreFromHardwareWallet(MoneroNewWalletCredentials credentials) { Future<MoneroWallet> 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 @override
@ -290,7 +297,8 @@ class MoneroWalletService extends WalletService<MoneroNewWalletCredentials,
final polyseed = Polyseed.decode(credentials.mnemonic, lang, polyseedCoin); final polyseed = Polyseed.decode(credentials.mnemonic, lang, polyseedCoin);
return _restoreFromPolyseed( return _restoreFromPolyseed(
path, credentials.password!, polyseed, credentials.walletInfo!, lang); path, credentials.password!, polyseed, credentials.walletInfo!, lang,
passphrase: credentials.passphrase);
} catch (e) { } catch (e) {
// TODO: Implement Exception for wallet list service. // TODO: Implement Exception for wallet list service.
print('MoneroWalletsManager Error: $e'); print('MoneroWalletsManager Error: $e');
@ -300,7 +308,11 @@ class MoneroWalletService extends WalletService<MoneroNewWalletCredentials,
Future<MoneroWallet> _restoreFromPolyseed( Future<MoneroWallet> _restoreFromPolyseed(
String path, String password, Polyseed polyseed, WalletInfo walletInfo, PolyseedLang lang, 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 ?? final height = overrideHeight ??
getMoneroHeigthByDate(date: DateTime.fromMillisecondsSinceEpoch(polyseed.birthday * 1000)); getMoneroHeigthByDate(date: DateTime.fromMillisecondsSinceEpoch(polyseed.birthday * 1000));
final spendKey = polyseed.generateKey(coin, 32).toHexString(); final spendKey = polyseed.generateKey(coin, 32).toHexString();

View file

@ -82,14 +82,15 @@ class WowneroWalletService extends WalletService<
if (credentials.isPolyseed) { if (credentials.isPolyseed) {
final polyseed = Polyseed.create(); final polyseed = Polyseed.create();
final lang = PolyseedLang.getByEnglishName(credentials.language); final lang = PolyseedLang.getByEnglishName(credentials.language);
polyseed.crypt(credentials.passphrase??'');
if (credentials.passphrase != null) polyseed.crypt(credentials.passphrase!);
final heightOverride = final heightOverride =
getWowneroHeightByDate(date: DateTime.now().subtract(Duration(days: 2))); getWowneroHeightByDate(date: DateTime.now().subtract(Duration(days: 2)));
return _restoreFromPolyseed( return _restoreFromPolyseed(
path, credentials.password!, polyseed, credentials.walletInfo!, lang, path, credentials.password!, polyseed, credentials.walletInfo!, lang,
overrideHeight: heightOverride); overrideHeight: heightOverride, passphrase: credentials.passphrase);
} }
await wownero_wallet_manager.createWallet( await wownero_wallet_manager.createWallet(
@ -291,7 +292,7 @@ class WowneroWalletService extends WalletService<
final polyseed = Polyseed.decode(credentials.mnemonic, lang, polyseedCoin); final polyseed = Polyseed.decode(credentials.mnemonic, lang, polyseedCoin);
return _restoreFromPolyseed( return _restoreFromPolyseed(
path, credentials.password!, polyseed, credentials.walletInfo!, lang); path, credentials.password!, polyseed, credentials.walletInfo!, lang, passphrase: credentials.passphrase);
} catch (e) { } catch (e) {
// TODO: Implement Exception for wallet list service. // TODO: Implement Exception for wallet list service.
print('WowneroWalletsManager Error: $e'); print('WowneroWalletsManager Error: $e');
@ -301,7 +302,9 @@ class WowneroWalletService extends WalletService<
Future<WowneroWallet> _restoreFromPolyseed( Future<WowneroWallet> _restoreFromPolyseed(
String path, String password, Polyseed polyseed, WalletInfo walletInfo, PolyseedLang lang, 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 ?? final height = overrideHeight ??
getWowneroHeightByDate(date: DateTime.fromMillisecondsSinceEpoch(polyseed.birthday * 1000)); getWowneroHeightByDate(date: DateTime.fromMillisecondsSinceEpoch(polyseed.birthday * 1000));
final spendKey = polyseed.generateKey(coin, 32).toHexString(); final spendKey = polyseed.generateKey(coin, 32).toHexString();