| 1 | // NOTE: This code was generated by Hive, but since Hive is on the way out, this |
| 2 | // code no longer uses hive generators, so when we get rid of hive we also will |
| 3 | // be able to get rid of |
| 4 | |
| 5 | import 'dart:async'; |
| 6 | |
| 7 | import 'package:cw_core/utils/print_verbose.dart'; |
| 8 | import 'package:cw_core/wallet_info.dart' as newWi; |
| 9 | import 'package:cw_core/address_info.dart'; |
| 10 | import 'package:cw_core/hive_type_ids.dart'; |
| 11 | import 'package:cw_core/wallet_type.dart'; |
| 12 | import 'package:hive/hive.dart'; |
| 13 | |
| 14 | part 'wallet_info_legacy.part.dart'; |
| 15 | |
| 16 | // // @HiveType(typeId: DERIVATION_TYPE_TYPE_ID) |
| 17 | // enum DerivationType { |
| 18 | // // @HiveField(0) |
| 19 | // unknown, |
| 20 | // // @HiveField(1) |
| 21 | // def, // default is a reserved word |
| 22 | // // @HiveField(2) |
| 23 | // nano, |
| 24 | // // @HiveField(3) |
| 25 | // bip39, |
| 26 | // // @HiveField(4) |
| 27 | // electrum, |
| 28 | // } |
| 29 | |
| 30 | // // @HiveType(typeId: HARDWARE_WALLET_TYPE_TYPE_ID) |
| 31 | // enum HardwareWalletType { |
| 32 | // // @HiveField(0) |
| 33 | // ledger, |
| 34 | // } |
| 35 | |
| 36 | // @HiveType(typeId: DerivationInfo.typeId) |
| 37 | class DerivationInfo extends HiveObject { |
| 38 | DerivationInfo({ |
| 39 | this.derivationType, |
| 40 | this.derivationPath, |
| 41 | this.balance = "", |
| 42 | this.address = "", |
| 43 | this.transactionsCount = 0, |
| 44 | this.scriptType, |
| 45 | this.description, |
| 46 | }); |
| 47 | |
| 48 | static const typeId = DERIVATION_INFO_TYPE_ID; |
| 49 | |
| 50 | // @HiveField(0, defaultValue: '') |
| 51 | String address; |
| 52 | |
| 53 | // @HiveField(1, defaultValue: '') |
| 54 | String balance; |
| 55 | |
| 56 | // @HiveField(2, defaultValue: 0) |
| 57 | int transactionsCount; |
| 58 | |
| 59 | // @HiveField(3) |
| 60 | newWi.DerivationType? derivationType; |
| 61 | |
| 62 | // @HiveField(4) |
| 63 | String? derivationPath; |
| 64 | |
| 65 | // @HiveField(5) |
| 66 | final String? scriptType; |
| 67 | |
| 68 | // @HiveField(6) |
| 69 | final String? description; |
| 70 | } |
| 71 | |
| 72 | // @HiveType(typeId: WalletInfo.typeId) |
| 73 | class WalletInfo extends HiveObject { |
| 74 | WalletInfo( |
| 75 | this.id, |
| 76 | this.name, |
| 77 | this.type, |
| 78 | this.isRecovery, |
| 79 | this.restoreHeight, |
| 80 | this.timestamp, |
| 81 | this.dirPath, |
| 82 | this.path, |
| 83 | this.address, |
| 84 | this.yatEid, |
| 85 | this.yatLastUsedAddressRaw, |
| 86 | this.showIntroCakePayCard, |
| 87 | this.derivationInfo, |
| 88 | this.hardwareWalletType, |
| 89 | this.parentAddress, |
| 90 | this.hashedWalletIdentifier, |
| 91 | this.isNonSeedWallet, |
| 92 | ) : _yatLastUsedAddressController = StreamController<String>.broadcast(); |
| 93 | |
| 94 | factory WalletInfo.external({ |
| 95 | required String id, |
| 96 | required String name, |
| 97 | required WalletType type, |
| 98 | required bool isRecovery, |
| 99 | required int restoreHeight, |
| 100 | required DateTime date, |
| 101 | required String dirPath, |
| 102 | required String path, |
| 103 | required String address, |
| 104 | bool? showIntroCakePayCard, |
| 105 | String yatEid = '', |
| 106 | String yatLastUsedAddressRaw = '', |
| 107 | DerivationInfo? derivationInfo, |
| 108 | newWi.HardwareWalletType? hardwareWalletType, |
| 109 | String? parentAddress, |
| 110 | String? hashedWalletIdentifier, |
| 111 | bool? isNonSeedWallet, |
| 112 | }) { |
| 113 | return WalletInfo( |
| 114 | id, |
| 115 | name, |
| 116 | type, |
| 117 | isRecovery, |
| 118 | restoreHeight, |
| 119 | date.millisecondsSinceEpoch, |
| 120 | dirPath, |
| 121 | path, |
| 122 | address, |
| 123 | yatEid, |
| 124 | yatLastUsedAddressRaw, |
| 125 | showIntroCakePayCard, |
| 126 | derivationInfo, |
| 127 | hardwareWalletType, |
| 128 | parentAddress, |
| 129 | hashedWalletIdentifier, |
| 130 | isNonSeedWallet ?? false, |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | static const typeId = WALLET_INFO_TYPE_ID; |
| 135 | static const boxName = 'WalletInfo'; |
| 136 | |
| 137 | // @HiveField(0, defaultValue: '') |
| 138 | String id; |
| 139 | |
| 140 | // @HiveField(1, defaultValue: '') |
| 141 | String name; |
| 142 | |
| 143 | // @HiveField(2) |
| 144 | WalletType type; |
| 145 | |
| 146 | // @HiveField(3, defaultValue: false) |
| 147 | bool isRecovery; |
| 148 | |
| 149 | // @HiveField(4, defaultValue: 0) |
| 150 | int restoreHeight; |
| 151 | |
| 152 | // @HiveField(5, defaultValue: 0) |
| 153 | int timestamp; |
| 154 | |
| 155 | // @HiveField(6, defaultValue: '') |
| 156 | String dirPath; |
| 157 | |
| 158 | // @HiveField(7, defaultValue: '') |
| 159 | String path; |
| 160 | |
| 161 | // @HiveField(8, defaultValue: '') |
| 162 | String address; |
| 163 | |
| 164 | // @HiveField(10) |
| 165 | Map<String, String>? addresses; |
| 166 | |
| 167 | // @HiveField(11) |
| 168 | String? yatEid; |
| 169 | |
| 170 | // @HiveField(12) |
| 171 | String? yatLastUsedAddressRaw; |
| 172 | |
| 173 | // @HiveField(13) |
| 174 | bool? showIntroCakePayCard; |
| 175 | |
| 176 | // @HiveField(14) |
| 177 | Map<int, List<AddressInfo>>? addressInfos; |
| 178 | |
| 179 | // @HiveField(15) |
| 180 | List<String>? usedAddresses; |
| 181 | |
| 182 | @deprecated |
| 183 | // @HiveField(16) |
| 184 | newWi.DerivationType? derivationType; // no longer used |
| 185 | |
| 186 | @deprecated |
| 187 | // @HiveField(17) |
| 188 | String? derivationPath; // no longer used |
| 189 | |
| 190 | // @HiveField(18) |
| 191 | String? addressPageType; |
| 192 | |
| 193 | // @HiveField(19) |
| 194 | String? network; |
| 195 | |
| 196 | // @HiveField(20) |
| 197 | DerivationInfo? derivationInfo; |
| 198 | |
| 199 | // @HiveField(21) |
| 200 | newWi.HardwareWalletType? hardwareWalletType; |
| 201 | |
| 202 | // @HiveField(22) |
| 203 | String? parentAddress; |
| 204 | |
| 205 | // @HiveField(23) |
| 206 | List<String>? hiddenAddresses; |
| 207 | |
| 208 | // @HiveField(24) |
| 209 | List<String>? manualAddresses; |
| 210 | |
| 211 | // @HiveField(25) |
| 212 | String? hashedWalletIdentifier; |
| 213 | |
| 214 | // @HiveField(26, defaultValue: false) |
| 215 | bool isNonSeedWallet; |
| 216 | |
| 217 | String get yatLastUsedAddress => yatLastUsedAddressRaw ?? ''; |
| 218 | |
| 219 | set yatLastUsedAddress(String address) { |
| 220 | yatLastUsedAddressRaw = address; |
| 221 | _yatLastUsedAddressController.add(address); |
| 222 | } |
| 223 | |
| 224 | String get yatEmojiId => yatEid ?? ''; |
| 225 | |
| 226 | bool get isShowIntroCakePayCard { |
| 227 | if (showIntroCakePayCard == null) { |
| 228 | return type != WalletType.haven; |
| 229 | } |
| 230 | return showIntroCakePayCard!; |
| 231 | } |
| 232 | |
| 233 | bool get isHardwareWallet => hardwareWalletType != null; |
| 234 | |
| 235 | DateTime get date => DateTime.fromMillisecondsSinceEpoch(timestamp); |
| 236 | |
| 237 | Stream<String> get yatLastUsedAddressStream => _yatLastUsedAddressController.stream; |
| 238 | |
| 239 | StreamController<String> _yatLastUsedAddressController; |
| 240 | |
| 241 | Future<void> updateRestoreHeight(int height) async { |
| 242 | restoreHeight = height; |
| 243 | await save(); |
| 244 | } |
| 245 | |
| 246 | Future<void> migrateToSqlite() async { |
| 247 | final di = newWi.DerivationInfo( |
| 248 | id: 0, |
| 249 | derivationType: |
| 250 | derivationInfo?.derivationType ?? derivationType ?? newWi.DerivationType.unknown, |
| 251 | derivationPath: derivationInfo?.derivationPath ?? derivationPath ?? '', |
| 252 | ); |
| 253 | final derivationInfoId = await di.save(); |
| 254 | final walletInfo = newWi.WalletInfo( |
| 255 | 0, |
| 256 | id, |
| 257 | name, |
| 258 | type, |
| 259 | isRecovery, |
| 260 | restoreHeight, |
| 261 | timestamp, |
| 262 | dirPath, |
| 263 | path, |
| 264 | address, |
| 265 | yatEid, |
| 266 | yatLastUsedAddressRaw, |
| 267 | showIntroCakePayCard, |
| 268 | derivationInfoId, |
| 269 | hardwareWalletType, |
| 270 | parentAddress, |
| 271 | hashedWalletIdentifier, |
| 272 | isNonSeedWallet, |
| 273 | 0, |
| 274 | addressPageType, |
| 275 | false, |
| 276 | true, |
| 277 | null) |
| 278 | ..network = network; |
| 279 | final wiId = await walletInfo.save(); |
| 280 | for (final address in usedAddresses ?? <String>[]) { |
| 281 | await newWi.WalletInfoAddress.insert(wiId, newWi.WalletInfoAddressType.used, address); |
| 282 | } |
| 283 | for (final address in hiddenAddresses ?? <String>[]) { |
| 284 | await newWi.WalletInfoAddress.insert(wiId, newWi.WalletInfoAddressType.hidden, address); |
| 285 | } |
| 286 | for (final address in manualAddresses ?? <String>[]) { |
| 287 | await newWi.WalletInfoAddress.insert(wiId, newWi.WalletInfoAddressType.manual, address); |
| 288 | } |
| 289 | for (int i = 0; i < (addressInfos?.length ?? 0); i++) { |
| 290 | for (final address in addressInfos![i] ?? <AddressInfo>[]) { |
| 291 | await newWi.WalletInfoAddressInfo.insert( |
| 292 | walletInfoId: wiId, |
| 293 | mapKey: i, |
| 294 | accountIndex: address.accountIndex ?? 0, |
| 295 | address: address.address, |
| 296 | label: address.label, |
| 297 | ); |
| 298 | } |
| 299 | } |
| 300 | await walletInfo.setAddresses(addresses ?? <String, String>{}); |
| 301 | } |
| 302 | |
| 303 | static Future<void> migrateAllToSqlite(final Box<WalletInfo> box) async { |
| 304 | printV('Migrating WalletInfo to SQLite: start'); |
| 305 | final sw = Stopwatch()..start(); |
| 306 | final list = box.values.toList(); |
| 307 | for (final wi in list) { |
| 308 | try { |
| 309 | await wi.migrateToSqlite(); |
| 310 | await wi.delete(); |
| 311 | } catch (e) { |
| 312 | printV('Error migrating WalletInfo ${wi.id}: $e'); |
| 313 | } |
| 314 | } |
| 315 | printV('Migrating WalletInfo to SQLite: end (${sw.elapsedMilliseconds}ms)'); |
| 316 | } |
| 317 | } |