dev
dart 161 lines 4.76 KB
Raw
1 import 'dart:io';
2
3 import 'package:bip39/bip39.dart' as bip39;
4 import 'package:cw_core/balance.dart';
5 import 'package:cw_core/encryption_file_utils.dart';
6 import 'package:cw_core/pathForWallet.dart';
7 import 'package:cw_core/transaction_history.dart';
8 import 'package:cw_core/transaction_info.dart';
9 import 'package:cw_core/tron_token.dart';
10 import 'package:cw_core/wallet_base.dart';
11 import 'package:cw_core/wallet_info.dart';
12 import 'package:cw_core/wallet_service.dart';
13 import 'package:cw_core/wallet_type.dart';
14 import 'package:cw_tron/tron_client.dart';
15 import 'package:cw_tron/tron_exception.dart';
16 import 'package:cw_tron/tron_wallet.dart';
17 import 'package:cw_tron/tron_wallet_creation_credentials.dart';
18
19 class TronWalletService extends WalletService<
20 TronNewWalletCredentials,
21 TronRestoreWalletFromSeedCredentials,
22 TronRestoreWalletFromPrivateKey,
23 TronNewWalletCredentials> {
24 TronWalletService({required this.client, required this.isDirect});
25
26 late TronClient client;
27
28 final bool isDirect;
29
30 @override
31 WalletType getType() => WalletType.tron;
32
33 @override
34 Future<TronWallet> create(TronNewWalletCredentials credentials, {bool? isTestnet}) async {
35 final strength = credentials.seedPhraseLength == 24 ? 256 : 128;
36
37 final mnemonic = credentials.mnemonic ?? bip39.generateMnemonic(strength: strength);
38
39 final wallet = TronWallet(
40 walletInfo: credentials.walletInfo!,
41 derivationInfo: await credentials.walletInfo!.getDerivationInfo(),
42 mnemonic: mnemonic,
43 password: credentials.password!,
44 passphrase: credentials.passphrase,
45 encryptionFileUtils: encryptionFileUtilsFor(isDirect),
46 );
47
48 await wallet.init();
49 await wallet.addInitialTokens();
50 await wallet.save();
51
52 return wallet;
53 }
54
55 @override
56 Future<TronWallet> openWallet(String name, String password) async {
57 final walletInfo = await WalletInfo.get(name, getType());
58 if (walletInfo == null) {
59 throw Exception('Wallet not found');
60 }
61
62 try {
63 final wallet = await TronWalletBase.open(
64 name: name,
65 password: password,
66 walletInfo: walletInfo,
67 encryptionFileUtils: encryptionFileUtilsFor(isDirect),
68 );
69
70 await wallet.init();
71 await wallet.addInitialTokens();
72 await wallet.save();
73 saveBackup(name);
74 return wallet;
75 } catch (_) {
76 await restoreWalletFilesFromBackup(name);
77
78 final wallet = await TronWalletBase.open(
79 name: name,
80 password: password,
81 walletInfo: walletInfo,
82 encryptionFileUtils: encryptionFileUtilsFor(isDirect),
83 );
84
85 await wallet.init();
86 await wallet.addInitialTokens();
87 await wallet.save();
88 return wallet;
89 }
90 }
91
92 @override
93 Future<TronWallet> restoreFromKeys(
94 TronRestoreWalletFromPrivateKey credentials, {
95 bool? isTestnet,
96 }) async {
97 final wallet = TronWallet(
98 password: credentials.password!,
99 privateKey: credentials.privateKey,
100 walletInfo: credentials.walletInfo!,
101 derivationInfo: await credentials.walletInfo!.getDerivationInfo(),
102 encryptionFileUtils: encryptionFileUtilsFor(isDirect),
103 );
104
105 await wallet.init();
106 await wallet.addInitialTokens();
107 await wallet.save();
108
109 return wallet;
110 }
111
112 @override
113 Future<TronWallet> restoreFromSeed(
114 TronRestoreWalletFromSeedCredentials credentials, {
115 bool? isTestnet,
116 }) async {
117 if (!bip39.validateMnemonic(credentials.mnemonic)) {
118 throw TronMnemonicIsIncorrectException();
119 }
120
121 final wallet = TronWallet(
122 password: credentials.password!,
123 mnemonic: credentials.mnemonic,
124 walletInfo: credentials.walletInfo!,
125 derivationInfo: await credentials.walletInfo!.getDerivationInfo(),
126 passphrase: credentials.passphrase,
127 encryptionFileUtils: encryptionFileUtilsFor(isDirect),
128 );
129
130 await wallet.init();
131 await wallet.addInitialTokens();
132 await wallet.save();
133
134 return wallet;
135 }
136
137 @override
138 Future<bool> isWalletExit(String name) async =>
139 File(await pathForWallet(name: name, type: getType())).existsSync();
140
141 @override
142 Future<void> remove(String wallet) async {
143 File(await pathForWalletDir(name: wallet, type: getType())).delete(recursive: true);
144 final walletInfo = await WalletInfo.get(wallet, getType());
145 if (walletInfo == null) {
146 throw Exception('Wallet not found');
147 }
148 await WalletInfo.delete(walletInfo);
149 final nameStillUsed = await WalletInfo.get(wallet, getType()) != null;
150 if (!nameStillUsed) {
151 await TronToken.deleteAllForWallet(wallet);
152 }
153 }
154
155 @override
156 Future<WalletBase<Balance, TransactionHistoryBase<TransactionInfo>, TransactionInfo>>
157 restoreFromHardwareWallet(TronNewWalletCredentials credentials) {
158 // TODO: implement restoreFromHardwareWallet
159 throw UnimplementedError();
160 }
161 }