Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crypto_plugins/flutter_libmwc
112 changes: 71 additions & 41 deletions lib/wallets/wallet/impl/mimblewimblecoin_wallet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class MimblewimblecoinWallet extends Bip39Wallet {
: super(Mimblewimblecoin(network));

final syncMutex = Mutex();
final _walletOpenMutex = Mutex();
NodeModel? _mimblewimblecoinNode;
Timer? timer;

Expand Down Expand Up @@ -95,24 +96,29 @@ class MimblewimblecoinWallet extends Bip39Wallet {
}

Future<String> _ensureWalletOpen() async {
final existing = await secureStorageInterface.read(
key: '${walletId}_wallet',
);
if (existing != null && existing.isNotEmpty) return existing;
return await _walletOpenMutex.protect(() async {
final existing = await secureStorageInterface.read(
key: '${walletId}_wallet',
);
if (existing != null && existing.isNotEmpty) return existing;

final config = await _getRealConfig();
final password = await secureStorageInterface.read(
key: '${walletId}_password',
);
if (password == null) {
throw Exception('Wallet password not found');
}
final opened = await libMwc.openWallet(config: config, password: password);
await secureStorageInterface.write(
key: '${walletId}_wallet',
value: opened,
);
return opened;
final config = await _getRealConfig();
final password = await secureStorageInterface.read(
key: '${walletId}_password',
);
if (password == null) {
throw Exception('Wallet password not found');
}
final opened = await libMwc.openWallet(
config: config,
password: password,
);
await secureStorageInterface.write(
key: '${walletId}_wallet',
value: opened,
);
return opened;
});
}

/// Returns an empty String on success, error message on failure.
Expand Down Expand Up @@ -576,6 +582,8 @@ class MimblewimblecoinWallet extends Bip39Wallet {
final String nodeApiAddress = uri.toString();
final walletDir = await _currentWalletDirPath();

await _ensureApiSecret(walletDir, nodeApiAddress);

final Map<String, dynamic> config = {};
config["wallet_dir"] = walletDir;
config["check_node_api_http_addr"] = nodeApiAddress;
Expand All @@ -585,6 +593,21 @@ class MimblewimblecoinWallet extends Bip39Wallet {
return stringConfig;
}

/// Write the node API secret to .api_secret in the wallet directory so that
/// the Rust HTTPNodeClient can authenticate to the MWC node.
Future<void> _ensureApiSecret(String walletDir, String nodeUrl) async {
const defaultNodeHost = 'mwc713.mwc.mw';
const defaultNodeSecret = '11ne3EAUtOXVKwhxm84U';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably unify this secret so the MWC node test file doesn't have to reproduce it (or vice versa). A cleaner approach may be to add an optional auth field/param to the NodeModelAdapter, but I didn't want to get into all that right now.


final file = File('$walletDir/.api_secret');
if (nodeUrl.contains(defaultNodeHost)) {
await Directory(walletDir).create(recursive: true);
await file.writeAsString(defaultNodeSecret);
} else if (await file.exists()) {
await file.delete();
}
}

Future<String> _currentWalletDirPath() async {
final Directory appDir = await StackFileSystem.applicationRootDirectory();

Expand Down Expand Up @@ -894,14 +917,17 @@ class MimblewimblecoinWallet extends Bip39Wallet {
);

//Open wallet
encodedWallet = await libMwc.openWallet(
config: stringConfig,
password: password,
);
await secureStorageInterface.write(
key: '${walletId}_wallet',
value: encodedWallet,
);
encodedWallet = await _walletOpenMutex.protect(() async {
final opened = await libMwc.openWallet(
config: stringConfig,
password: password,
);
await secureStorageInterface.write(
key: '${walletId}_wallet',
value: opened,
);
return opened;
});
//Store MwcMqs address info
await _generateAndStoreReceivingAddressForIndex(0);

Expand Down Expand Up @@ -935,14 +961,16 @@ class MimblewimblecoinWallet extends Bip39Wallet {
key: '${walletId}_password',
);

final walletOpen = await libMwc.openWallet(
config: config,
password: password!,
);
await secureStorageInterface.write(
key: '${walletId}_wallet',
value: walletOpen,
);
await _walletOpenMutex.protect(() async {
final walletOpen = await libMwc.openWallet(
config: config,
password: password!,
);
await secureStorageInterface.write(
key: '${walletId}_wallet',
value: walletOpen,
);
});

await updateNode();
} catch (e, s) {
Expand Down Expand Up @@ -1144,14 +1172,16 @@ class MimblewimblecoinWallet extends Bip39Wallet {
);

//Open Wallet
final walletOpen = await libMwc.openWallet(
config: stringConfig,
password: password,
);
await secureStorageInterface.write(
key: '${walletId}_wallet',
value: walletOpen,
);
await _walletOpenMutex.protect(() async {
final walletOpen = await libMwc.openWallet(
config: stringConfig,
password: password,
);
await secureStorageInterface.write(
key: '${walletId}_wallet',
value: walletOpen,
);
});

await _generateAndStoreReceivingAddressForIndex(
mimblewimblecoinData.receivingIndex,
Expand Down
Loading