Skip to content

Commit

Permalink
Add passphrase support for Eth, Polygon, and Tron (#1719)
Browse files Browse the repository at this point in the history
* Add passphrase support for Eth, Polygon, and Tron

* move passphrase to advanced settings even for restore
  • Loading branch information
OmarHatem28 authored Oct 4, 2024
1 parent fc14bf4 commit 4b4d8a4
Show file tree
Hide file tree
Showing 29 changed files with 249 additions and 154 deletions.
2 changes: 1 addition & 1 deletion cw_bitcoin_cash/lib/src/bitcoin_cash_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ abstract class BitcoinCashWalletBase extends ElectrumWallet with Store {
unspentCoinsInfo: unspentCoinsInfo,
initialAddresses: initialAddresses,
initialBalance: initialBalance,
seedBytes: await MnemonicBip39.toSeed(mnemonic, passphrase: passphrase),
seedBytes: MnemonicBip39.toSeed(mnemonic, passphrase: passphrase),
encryptionFileUtils: encryptionFileUtils,
initialRegularAddressIndex: initialRegularAddressIndex,
initialChangeAddressIndex: initialChangeAddressIndex,
Expand Down
2 changes: 1 addition & 1 deletion cw_bitcoin_cash/lib/src/bitcoin_cash_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class BitcoinCashWalletService extends WalletService<
final strength = credentials.seedPhraseLength == 24 ? 256 : 128;

final wallet = await BitcoinCashWalletBase.create(
mnemonic: credentials.mnemonic ?? await MnemonicBip39.generate(strength: strength),
mnemonic: credentials.mnemonic ?? MnemonicBip39.generate(strength: strength),
password: credentials.password!,
walletInfo: credentials.walletInfo!,
unspentCoinsInfo: unspentCoinsInfoSource,
Expand Down
5 changes: 4 additions & 1 deletion cw_ethereum/lib/ethereum_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class EthereumWallet extends EVMChainWallet {
super.initialBalance,
super.privateKey,
required super.encryptionFileUtils,
super.passphrase,
}) : super(nativeCurrency: CryptoCurrency.eth);

@override
Expand Down Expand Up @@ -150,8 +151,9 @@ class EthereumWallet extends EVMChainWallet {
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,
Expand All @@ -166,6 +168,7 @@ class EthereumWallet extends EVMChainWallet {
password: password,
mnemonic: keysData.mnemonic,
privateKey: keysData.privateKey,
passphrase: keysData.passphrase,
initialBalance: balance,
client: EthereumClient(),
encryptionFileUtils: encryptionFileUtils,
Expand Down
2 changes: 2 additions & 0 deletions cw_ethereum/lib/ethereum_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class EthereumWalletService extends EVMChainWalletService<EthereumWallet> {
walletInfo: credentials.walletInfo!,
mnemonic: mnemonic,
password: credentials.password!,
passphrase: credentials.passphrase,
client: client,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
Expand Down Expand Up @@ -144,6 +145,7 @@ class EthereumWalletService extends EVMChainWalletService<EthereumWallet> {
password: credentials.password!,
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo!,
passphrase: credentials.passphrase,
client: client,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
Expand Down
16 changes: 13 additions & 3 deletions cw_evm/lib/evm_chain_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ abstract class EVMChainWalletBase
required String password,
EVMChainERC20Balance? initialBalance,
required this.encryptionFileUtils,
this.passphrase,
}) : syncStatus = const NotConnectedSyncStatus(),
_password = password,
_mnemonic = mnemonic,
Expand Down Expand Up @@ -178,6 +179,7 @@ abstract class EVMChainWalletBase
mnemonic: _mnemonic,
privateKey: _hexPrivateKey,
password: _password,
passphrase: passphrase,
);
walletAddresses.address = _evmChainPrivateKey.address.hexEip55;
}
Expand Down Expand Up @@ -545,6 +547,7 @@ abstract class EVMChainWalletBase
'mnemonic': _mnemonic,
'private_key': privateKey,
'balance': balance[currency]!.toJSON(),
'passphrase': passphrase,
});

Future<void> _updateBalance() async {
Expand Down Expand Up @@ -574,15 +577,19 @@ abstract class EVMChainWalletBase
}
}

Future<EthPrivateKey> getPrivateKey(
{String? mnemonic, String? privateKey, required String password}) async {
Future<EthPrivateKey> getPrivateKey({
String? mnemonic,
String? privateKey,
required String password,
String? passphrase,
}) async {
assert(mnemonic != null || privateKey != null);

if (privateKey != null) {
return EthPrivateKey.fromHex(privateKey);
}

final seed = bip39.mnemonicToSeed(mnemonic!);
final seed = bip39.mnemonicToSeed(mnemonic!, passphrase: passphrase ?? '');

final root = bip32.BIP32.fromSeed(seed);

Expand Down Expand Up @@ -716,4 +723,7 @@ abstract class EVMChainWalletBase

@override
String get password => _password;

@override
final String? passphrase;
}
25 changes: 11 additions & 14 deletions cw_evm/lib/evm_chain_wallet_creation_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@ import 'package:cw_core/wallet_info.dart';

class EVMChainNewWalletCredentials extends WalletCredentials {
EVMChainNewWalletCredentials({
required String name,
WalletInfo? walletInfo,
String? password,
String? parentAddress,
required super.name,
super.walletInfo,
super.password,
super.parentAddress,
this.mnemonic,
}) : super(
name: name,
walletInfo: walletInfo,
password: password,
parentAddress: parentAddress,
);
super.passphrase,
});

final String? mnemonic;
}

class EVMChainRestoreWalletFromSeedCredentials extends WalletCredentials {
EVMChainRestoreWalletFromSeedCredentials({
required String name,
required String password,
required super.name,
required super.password,
required this.mnemonic,
WalletInfo? walletInfo,
}) : super(name: name, password: password, walletInfo: walletInfo);
super.walletInfo,
super.passphrase,
});

final String mnemonic;
}
Expand Down
4 changes: 4 additions & 0 deletions cw_nano/lib/nano_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ abstract class NanoWalletBase
required String password,
NanoBalance? initialBalance,
required EncryptionFileUtils encryptionFileUtils,
this.passphrase,
}) : syncStatus = NotConnectedSyncStatus(),
_password = password,
_mnemonic = mnemonic,
Expand Down Expand Up @@ -548,4 +549,7 @@ abstract class NanoWalletBase
}
return await NanoSignatures.verifyMessage(message, signature, address);
}

@override
final String? passphrase;
}
6 changes: 5 additions & 1 deletion cw_nano/lib/nano_wallet_creation_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ class NanoNewWalletCredentials extends WalletCredentials {
DerivationType? derivationType,
this.mnemonic,
String? parentAddress,
String? passphrase,
}) : super(
name: name,
password: password,
walletInfo: walletInfo,
parentAddress: parentAddress,
passphrase: passphrase,
);

final String? mnemonic;
}

Expand All @@ -25,10 +27,12 @@ class NanoRestoreWalletFromSeedCredentials extends WalletCredentials {
required this.mnemonic,
String? password,
required DerivationType derivationType,
String? passphrase,
}) : super(
name: name,
password: password,
derivationInfo: DerivationInfo(derivationType: derivationType),
passphrase: passphrase,
);

final String mnemonic;
Expand Down
5 changes: 4 additions & 1 deletion cw_polygon/lib/polygon_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PolygonWallet extends EVMChainWallet {
super.privateKey,
required super.client,
required super.encryptionFileUtils,
super.passphrase,
}) : super(nativeCurrency: CryptoCurrency.maticpoly);

@override
Expand Down Expand Up @@ -128,8 +129,9 @@ class PolygonWallet extends EVMChainWallet {
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,
Expand All @@ -144,6 +146,7 @@ class PolygonWallet extends EVMChainWallet {
password: password,
mnemonic: keysData.mnemonic,
privateKey: keysData.privateKey,
passphrase: keysData.passphrase,
initialBalance: balance,
client: PolygonClient(),
encryptionFileUtils: encryptionFileUtils,
Expand Down
2 changes: 2 additions & 0 deletions cw_polygon/lib/polygon_wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class PolygonWalletService extends EVMChainWalletService<PolygonWallet> {
walletInfo: credentials.walletInfo!,
mnemonic: mnemonic,
password: credentials.password!,
passphrase: credentials.passphrase,
client: client,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
Expand Down Expand Up @@ -125,6 +126,7 @@ class PolygonWalletService extends EVMChainWalletService<PolygonWallet> {
password: credentials.password!,
mnemonic: credentials.mnemonic,
walletInfo: credentials.walletInfo!,
passphrase: credentials.passphrase,
client: client,
encryptionFileUtils: encryptionFileUtilsFor(isDirect),
);
Expand Down
5 changes: 4 additions & 1 deletion cw_solana/lib/solana_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import 'package:solana/base58.dart';
import 'package:solana/metaplex.dart' as metaplex;
import 'package:solana/solana.dart';
import 'package:solana/src/crypto/ed25519_hd_keypair.dart';
import 'package:cryptography/cryptography.dart';

part 'solana_wallet.g.dart';

Expand All @@ -49,6 +48,7 @@ abstract class SolanaWalletBase
required String password,
SolanaBalance? initialBalance,
required this.encryptionFileUtils,
this.passphrase,
}) : syncStatus = const NotConnectedSyncStatus(),
_password = password,
_mnemonic = mnemonic,
Expand Down Expand Up @@ -632,4 +632,7 @@ abstract class SolanaWalletBase

@override
String get password => _password;

@override
final String? passphrase;
}
20 changes: 14 additions & 6 deletions cw_solana/lib/solana_wallet_creation_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,30 @@ class SolanaNewWalletCredentials extends WalletCredentials {
String? password,
String? parentAddress,
this.mnemonic,
String? passphrase,
}) : super(
name: name,
walletInfo: walletInfo,
password: password,
parentAddress: parentAddress,
passphrase: passphrase,
);
final String? mnemonic;
}

class SolanaRestoreWalletFromSeedCredentials extends WalletCredentials {
SolanaRestoreWalletFromSeedCredentials(
{required String name,
required String password,
required this.mnemonic,
WalletInfo? walletInfo})
: super(name: name, password: password, walletInfo: walletInfo);
SolanaRestoreWalletFromSeedCredentials({
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;
}
Expand Down
13 changes: 11 additions & 2 deletions cw_tron/lib/tron_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abstract class TronWalletBase
required String password,
TronBalance? initialBalance,
required this.encryptionFileUtils,
this.passphrase,
}) : syncStatus = const NotConnectedSyncStatus(),
_password = password,
_mnemonic = mnemonic,
Expand Down Expand Up @@ -113,6 +114,7 @@ abstract class TronWalletBase
mnemonic: _mnemonic,
privateKey: _hexPrivateKey,
password: _password,
passphrase: passphrase,
);

_tronPublicKey = _tronPrivateKey.publicKey();
Expand Down Expand Up @@ -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,
Expand All @@ -165,6 +168,7 @@ abstract class TronWalletBase
password: password,
mnemonic: keysData.mnemonic,
privateKey: keysData.privateKey,
passphrase: keysData.passphrase,
initialBalance: balance,
encryptionFileUtils: encryptionFileUtils,
);
Expand All @@ -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);
Expand Down Expand Up @@ -463,6 +468,7 @@ abstract class TronWalletBase
'mnemonic': _mnemonic,
'private_key': privateKey,
'balance': balance[currency]!.toJSON(),
'passphrase': passphrase,
});

Future<void> _updateBalance() async {
Expand Down Expand Up @@ -607,4 +613,7 @@ abstract class TronWalletBase

@override
String get password => _password;

@override
final String? passphrase;
}
Loading

0 comments on commit 4b4d8a4

Please sign in to comment.