Add passphrase support for Eth, Polygon, and Tron (#1719)

* Add passphrase support for Eth, Polygon, and Tron

* move passphrase to advanced settings even for restore
This commit is contained in:
Omar Hatem 2024-10-04 20:01:46 +03:00 committed by GitHub
parent fc14bf4e2b
commit 4b4d8a4840
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 249 additions and 154 deletions

View file

@ -47,6 +47,7 @@ abstract class TronWalletBase
required String password,
TronBalance? initialBalance,
required this.encryptionFileUtils,
this.passphrase,
}) : syncStatus = const NotConnectedSyncStatus(),
_password = password,
_mnemonic = mnemonic,
@ -113,6 +114,7 @@ abstract class TronWalletBase
mnemonic: _mnemonic,
privateKey: _hexPrivateKey,
password: _password,
passphrase: passphrase,
);
_tronPublicKey = _tronPrivateKey.publicKey();
@ -149,8 +151,9 @@ abstract class TronWalletBase
if (!hasKeysFile) {
final mnemonic = data!['mnemonic'] as String?;
final privateKey = data['private_key'] as String?;
final passphrase = data['passphrase'] as String?;
keysData = WalletKeysData(mnemonic: mnemonic, privateKey: privateKey);
keysData = WalletKeysData(mnemonic: mnemonic, privateKey: privateKey, passphrase: passphrase);
} else {
keysData = await WalletKeysFile.readKeysFile(
name,
@ -165,6 +168,7 @@ abstract class TronWalletBase
password: password,
mnemonic: keysData.mnemonic,
privateKey: keysData.privateKey,
passphrase: keysData.passphrase,
initialBalance: balance,
encryptionFileUtils: encryptionFileUtils,
);
@ -190,12 +194,13 @@ abstract class TronWalletBase
String? mnemonic,
String? privateKey,
required String password,
String? passphrase,
}) async {
assert(mnemonic != null || privateKey != null);
if (privateKey != null) return TronPrivateKey(privateKey);
final seed = bip39.mnemonicToSeed(mnemonic!);
final seed = bip39.mnemonicToSeed(mnemonic!, passphrase: passphrase ?? '');
// Derive a TRON private key from the seed
final bip44 = Bip44.fromSeed(seed, Bip44Coins.tron);
@ -463,6 +468,7 @@ abstract class TronWalletBase
'mnemonic': _mnemonic,
'private_key': privateKey,
'balance': balance[currency]!.toJSON(),
'passphrase': passphrase,
});
Future<void> _updateBalance() async {
@ -607,4 +613,7 @@ abstract class TronWalletBase
@override
String get password => _password;
@override
final String? passphrase;
}

View file

@ -8,23 +8,31 @@ class TronNewWalletCredentials extends WalletCredentials {
String? password,
this.mnemonic,
String? parentAddress,
String? passphrase,
}) : super(
name: name,
walletInfo: walletInfo,
password: password,
parentAddress: parentAddress,
passphrase: passphrase,
);
final String? mnemonic;
}
class TronRestoreWalletFromSeedCredentials extends WalletCredentials {
TronRestoreWalletFromSeedCredentials(
{required String name,
required String password,
required this.mnemonic,
WalletInfo? walletInfo})
: super(name: name, password: password, walletInfo: walletInfo);
TronRestoreWalletFromSeedCredentials({
required String name,
required String password,
required this.mnemonic,
WalletInfo? walletInfo,
String? passphrase,
}) : super(
name: name,
password: password,
walletInfo: walletInfo,
passphrase: passphrase,
);
final String mnemonic;
}

View file

@ -33,10 +33,7 @@ class TronWalletService extends WalletService<
WalletType getType() => WalletType.tron;
@override
Future<TronWallet> create(
TronNewWalletCredentials credentials, {
bool? isTestnet,
}) async {
Future<TronWallet> create(TronNewWalletCredentials credentials, {bool? isTestnet}) async {
final strength = credentials.seedPhraseLength == 24 ? 256 : 128;
final mnemonic = credentials.mnemonic ?? bip39.generateMnemonic(strength: strength);
@ -45,6 +42,7 @@ class TronWalletService extends WalletService<
walletInfo: credentials.walletInfo!,
mnemonic: mnemonic,
password: credentials.password!,
passphrase: credentials.passphrase,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
@ -120,6 +118,7 @@ class TronWalletService extends WalletService<
password: credentials.password!,
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo!,
passphrase: credentials.passphrase,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);