@@ -23,10 +23,9 @@ class BitcoinWalletService extends WalletService<
2323 BitcoinRestoreWalletFromSeedCredentials ,
2424 BitcoinWalletFromKeysCredentials ,
2525 BitcoinRestoreWalletFromHardware > {
26- BitcoinWalletService (this .walletInfoSource, this . unspentCoinsInfoSource,
26+ BitcoinWalletService (this .unspentCoinsInfoSource,
2727 this .payjoinSessionSource, this .isDirect);
2828
29- final Box <WalletInfo > walletInfoSource;
3029 final Box <UnspentCoinsInfo > unspentCoinsInfoSource;
3130 final Box <PayjoinSession > payjoinSessionSource;
3231 final bool isDirect;
@@ -40,7 +39,13 @@ class BitcoinWalletService extends WalletService<
4039 credentials.walletInfo? .network = network.value;
4140
4241 final String mnemonic;
43- switch ( credentials.walletInfo? .derivationInfo? .derivationType) {
42+ final derivationInfo = await credentials.walletInfo! .getDerivationInfo ();
43+ derivationInfo.derivationType = credentials.derivationInfo? .derivationType ?? derivationInfo.derivationType;
44+ derivationInfo.derivationPath = credentials.derivationInfo? .derivationPath ?? derivationInfo.derivationPath;
45+ derivationInfo.description = credentials.derivationInfo? .description ?? derivationInfo.description;
46+ derivationInfo.scriptType = credentials.derivationInfo? .scriptType ?? derivationInfo.scriptType;
47+ await derivationInfo.save ();
48+ switch (derivationInfo.derivationType) {
4449 case DerivationType .bip39:
4550 final strength = credentials.seedPhraseLength == 24 ? 256 : 128 ;
4651
@@ -51,6 +56,7 @@ class BitcoinWalletService extends WalletService<
5156 mnemonic = await generateElectrumMnemonic ();
5257 break ;
5358 }
59+ await derivationInfo.save ();
5460
5561 final wallet = await BitcoinWalletBase .create (
5662 mnemonic: mnemonic,
@@ -75,8 +81,10 @@ class BitcoinWalletService extends WalletService<
7581
7682 @override
7783 Future <BitcoinWallet > openWallet (String name, String password) async {
78- final walletInfo = walletInfoSource.values
79- .firstWhereOrNull ((info) => info.id == WalletBase .idFor (name, getType ()))! ;
84+ final walletInfo = await WalletInfo .get (name, getType ());
85+ if (walletInfo == null ) {
86+ throw Exception ('Wallet not found' );
87+ }
8088 try {
8189 final wallet = await BitcoinWalletBase .open (
8290 password: password,
@@ -107,9 +115,11 @@ class BitcoinWalletService extends WalletService<
107115 @override
108116 Future <void > remove (String wallet) async {
109117 File (await pathForWalletDir (name: wallet, type: getType ())).delete (recursive: true );
110- final walletInfo = walletInfoSource.values
111- .firstWhereOrNull ((info) => info.id == WalletBase .idFor (wallet, getType ()))! ;
112- await walletInfoSource.delete (walletInfo.key);
118+ final walletInfo = await WalletInfo .get (wallet, getType ());
119+ if (walletInfo == null ) {
120+ throw Exception ('Wallet not found' );
121+ }
122+ await WalletInfo .delete (walletInfo);
113123
114124 final unspentCoinsToDelete = unspentCoinsInfoSource.values.where (
115125 (unspentCoin) => unspentCoin.walletId == walletInfo.id).toList ();
@@ -123,8 +133,10 @@ class BitcoinWalletService extends WalletService<
123133
124134 @override
125135 Future <void > rename (String currentName, String password, String newName) async {
126- final currentWalletInfo = walletInfoSource.values
127- .firstWhereOrNull ((info) => info.id == WalletBase .idFor (currentName, getType ()))! ;
136+ final currentWalletInfo = await WalletInfo .get (currentName, getType ());
137+ if (currentWalletInfo == null ) {
138+ throw Exception ('Wallet not found' );
139+ }
128140 final currentWallet = await BitcoinWalletBase .open (
129141 password: password,
130142 name: currentName,
@@ -141,23 +153,26 @@ class BitcoinWalletService extends WalletService<
141153 newWalletInfo.id = WalletBase .idFor (newName, getType ());
142154 newWalletInfo.name = newName;
143155
144- await walletInfoSource. put (currentWalletInfo.key, newWalletInfo );
156+ await newWalletInfo. save ( );
145157 }
146158
147159 @override
148160 Future <BitcoinWallet > restoreFromHardwareWallet (BitcoinRestoreWalletFromHardware credentials,
149161 {bool ? isTestnet}) async {
150162 final network = isTestnet == true ? BitcoinNetwork .testnet : BitcoinNetwork .mainnet;
151163 credentials.walletInfo? .network = network.value;
152- credentials.walletInfo? .derivationInfo? .derivationPath =
164+ final derivationInfo = await credentials.walletInfo! .getDerivationInfo ();
165+ derivationInfo.derivationPath =
153166 credentials.hwAccountData.derivationPath;
154167
155168 final xpub = convertZpubToXpub (credentials.hwAccountData.xpub! );
156169
170+ await credentials.walletInfo! .save ();
157171 final wallet = await BitcoinWallet (
158172 password: credentials.password! ,
159173 xpub: xpub,
160174 walletInfo: credentials.walletInfo! ,
175+ derivationInfo: derivationInfo,
161176 unspentCoinsInfo: unspentCoinsInfoSource,
162177 networkParam: network,
163178 encryptionFileUtils: encryptionFileUtilsFor (isDirect),
@@ -180,6 +195,7 @@ class BitcoinWalletService extends WalletService<
180195 password: credentials.password! ,
181196 xpub: xpub,
182197 walletInfo: credentials.walletInfo! ,
198+ derivationInfo: await credentials.walletInfo! .getDerivationInfo (),
183199 unspentCoinsInfo: unspentCoinsInfoSource,
184200 networkParam: network,
185201 encryptionFileUtils: encryptionFileUtilsFor (isDirect),
0 commit comments