| 1 | import 'dart:convert'; |
| 2 | |
| 3 | import 'package:cake_wallet/view_model/restore/restore_mode.dart'; |
| 4 | import 'package:cw_core/utils/zpub.dart'; |
| 5 | import 'package:cw_core/wallet_type.dart'; |
| 6 | |
| 7 | class RestoredWallet { |
| 8 | RestoredWallet( |
| 9 | {required this.restoreMode, |
| 10 | required this.type, |
| 11 | required this.address, |
| 12 | this.name, |
| 13 | this.txId, |
| 14 | this.spendKey, |
| 15 | this.viewKey, |
| 16 | this.scanSecret, |
| 17 | this.spendPubkey, |
| 18 | this.mnemonicSeed, |
| 19 | this.passphrase, |
| 20 | this.txAmount, |
| 21 | this.txDescription, |
| 22 | this.recipientName, |
| 23 | this.height, |
| 24 | this.privateKey}); |
| 25 | |
| 26 | final WalletRestoreMode restoreMode; |
| 27 | final WalletType type; |
| 28 | final String? name; |
| 29 | final String? address; |
| 30 | final String? txId; |
| 31 | final String? spendKey; |
| 32 | final String? viewKey; |
| 33 | final String? scanSecret; |
| 34 | final String? spendPubkey; |
| 35 | final String? mnemonicSeed; |
| 36 | final String? passphrase; |
| 37 | final String? txAmount; |
| 38 | final String? txDescription; |
| 39 | final String? recipientName; |
| 40 | final int? height; |
| 41 | final String? privateKey; |
| 42 | |
| 43 | factory RestoredWallet.fromKey(Map<String, dynamic> json) { |
| 44 | try { |
| 45 | final codeParsed = jsonDecode(json['raw_qr'].toString()); |
| 46 | if (codeParsed["version"] == 0) { |
| 47 | json['address'] = codeParsed["primaryAddress"]; |
| 48 | json['view_key'] = codeParsed["privateViewKey"]; |
| 49 | json['height'] = codeParsed["restoreHeight"].toString(); |
| 50 | json['label'] = codeParsed["label"]; |
| 51 | } |
| 52 | } catch (e) { |
| 53 | // fine, we don't care, it is only for monero anyway |
| 54 | } |
| 55 | if (json['ltub'] != null) { |
| 56 | json['xpub'] = convertLtubToXpub(json['ltub'] as String); |
| 57 | } |
| 58 | if (json['zpub'] != null) { |
| 59 | json['xpub'] = convertZpubToXpub(json['zpub'] as String); |
| 60 | } |
| 61 | if (json['xpub'] != null) { |
| 62 | json['xpub'] = convertAnyToXpub(json['xpub'] as String); |
| 63 | } |
| 64 | json['view_key'] ??= json['xpub']; |
| 65 | final height = json['height'] as String?; |
| 66 | return RestoredWallet( |
| 67 | name: json['label'] as String?, |
| 68 | restoreMode: json['mode'] as WalletRestoreMode, |
| 69 | type: json['type'] as WalletType, |
| 70 | address: json['address'] as String?, |
| 71 | spendKey: json['spend_key'] as String?, |
| 72 | viewKey: json['view_key'] as String?, |
| 73 | scanSecret: json['scan_secret'] as String?, |
| 74 | spendPubkey: json['spend_pubkey'] as String?, |
| 75 | height: height != null ? int.tryParse(height) ?? 0 : 0, |
| 76 | privateKey: json['private_key'] as String?, |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | factory RestoredWallet.fromSeed(Map<String, dynamic> json) { |
| 81 | final height = json['height'] as String?; |
| 82 | final mnemonic_seed = json['mnemonic_seed'] as String?; |
| 83 | final seed = json['seed'] as String? ?? json['hexSeed'] as String?; |
| 84 | final passphrase = json['passphrase'] as String?; |
| 85 | return RestoredWallet( |
| 86 | restoreMode: json['mode'] as WalletRestoreMode, |
| 87 | type: json['type'] as WalletType, |
| 88 | address: json['address'] as String?, |
| 89 | mnemonicSeed: mnemonic_seed ?? seed, |
| 90 | passphrase: passphrase, |
| 91 | height: height != null ? int.parse(height) : 0, |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | factory RestoredWallet.fromTxIds(Map<String, dynamic> json) { |
| 96 | return RestoredWallet( |
| 97 | restoreMode: json['mode'] as WalletRestoreMode, |
| 98 | type: json['type'] as WalletType, |
| 99 | address: json['address'] as String?, |
| 100 | txId: json['tx_payment_id'] as String, |
| 101 | txAmount: json['tx_amount'] as String, |
| 102 | txDescription: json['tx_description'] as String?, |
| 103 | recipientName: json['recipient_name'] as String?, |
| 104 | ); |
| 105 | } |
| 106 | } |