Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
75 changes: 75 additions & 0 deletions packages/ndk/lib/domain_layer/usecases/cashu/cashu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import '../../repositories/cache_manager.dart';
import '../../repositories/cashu_key_derivation.dart';
import '../../repositories/cashu_repo.dart';
import '../../repositories/wallets_repo.dart';
import 'cashu_backup.dart';
import 'cashu_bdhke.dart';
import 'cashu_cache_decorator.dart';
import 'cashu_keysets.dart';
Expand All @@ -39,6 +40,8 @@ class Cashu {

late final CashuSeed _cashuSeed;

late final CashuBackup _cashuBackup;

final CashuKeyDerivation _cashuKeyDerivation;

Cashu({
Expand All @@ -64,6 +67,12 @@ class Cashu {
_cashuSeed = CashuSeed(
userSeedPhrase: cashuUserSeedphrase,
);
_cashuBackup = CashuBackup(
cacheManagerCashu: _cacheManagerCashu,
cacheManager: _cacheManager,
walletsRepo: _walletsRepo,
cashuSeed: _cashuSeed,
);
if (cashuUserSeedphrase == null) {
Logger.log.w(() =>
'Cashu initialized without user seed phrase, cashu features will not work \nSet the seed phrase using NdkConfig or Cashu.setCashuSeedPhrase()');
Expand Down Expand Up @@ -100,6 +109,72 @@ class Cashu {
return _cashuSeed;
}

/// Export a full backup of the local cashu database (proofs, keysets, mint
/// infos, derivation counters and transactions) as a JSON map.
///
/// The global seed phrase is NOT included by default — it is wallet
/// independent and should be backed up separately. Set [includeSeedPhrase]
/// true only for a single self-contained backup, and then treat the result
/// like a private key. See [CashuBackup].
Future<Map<String, dynamic>> exportBackup({
bool includeSeedPhrase = false,
bool includeTransactions = true,
}) {
return _cashuBackup.exportToMap(
includeSeedPhrase: includeSeedPhrase,
includeTransactions: includeTransactions,
);
}

/// Export a full backup of all local cashu state as a JSON string.
/// See [exportBackup].
Future<String> exportBackupJsonString({
bool includeSeedPhrase = false,
bool includeTransactions = true,
bool pretty = true,
}) {
return _cashuBackup.exportToJsonString(
includeSeedPhrase: includeSeedPhrase,
includeTransactions: includeTransactions,
pretty: pretty,
);
}

/// Restore cashu state from a backup map produced by [exportBackup].
///
/// NOTE: the restored seed phrase is only loaded into memory; persist
/// [CashuBackupRestoreResult.seedPhrase] to secure storage to finish the
/// restore. After restoring you may want to call [restore] to re-sync proofs
/// from each mint. See [CashuBackup].
Future<CashuBackupRestoreResult> importBackup(
Map<String, dynamic> json, {
bool restoreSeedPhrase = true,
bool restoreTransactions = true,
}) async {
final result = await _cashuBackup.importFromMap(
json,
restoreSeedPhrase: restoreSeedPhrase,
restoreTransactions: restoreTransactions,
);
await _updateBalances();
return result;
}

/// Restore cashu state from a backup JSON string. See [importBackup].
Future<CashuBackupRestoreResult> importBackupJsonString(
String jsonString, {
bool restoreSeedPhrase = true,
bool restoreTransactions = true,
}) async {
final result = await _cashuBackup.importFromJsonString(
jsonString,
restoreSeedPhrase: restoreSeedPhrase,
restoreTransactions: restoreTransactions,
);
await _updateBalances();
return result;
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

/// Restores proofs from a mint using the wallet's seed phrase.
///
/// This implements NUT-09 (Restore) using NUT-13 (Deterministic Secrets).
Expand Down
Loading
Loading