Move recover and wallet balances into abstract class

This commit is contained in:
likho 2023-09-26 17:36:21 +02:00
parent abd7111528
commit 098a69eded
2 changed files with 97 additions and 13 deletions

View file

@ -37,8 +37,8 @@ class Epiccash extends Bip39Currency {
return LibEpiccash.getMnemonic(); return LibEpiccash.getMnemonic();
} }
Future<String> initializeNew(({String config, String mnemonic, String password, String name})? data) { Future<void> createNewWallet(({String config, String mnemonic, String password, String name})? data) async {
return LibEpiccash.initializeNewWallet( await LibEpiccash.initializeNewWallet(
config: data!.config, config: data!.config,
mnemonic: data.mnemonic, mnemonic: data.mnemonic,
password: data.password, password: data.password,

View file

@ -27,13 +27,18 @@ abstract class LibEpiccash {
} }
/// ///
/// Fetch the mnemonic of (some? current?) wallet /// Fetch the mnemonic For a new wallet (Only used in the example app)
/// ///
// TODO: ensure the above documentation comment is correct // TODO: ensure the above documentation comment is correct
// TODO: ensure this will always return the mnemonic. If not, this function should throw an exception // TODO: ensure this will always return the mnemonic. If not, this function should throw an exception
// TODO: probably remove this as we don't use it in stack wallet. We store the mnemonic separately // TODO: probably remove this as we don't use it in stack wallet. We store the mnemonic separately
static String getMnemonic() { static String getMnemonic() {
try {
return lib_epiccash.walletMnemonic(); return lib_epiccash.walletMnemonic();
} catch (e) {
throw Exception(e.toString());
}
} }
// Private function wrapper for compute // Private function wrapper for compute
@ -55,17 +60,18 @@ abstract class LibEpiccash {
} }
/// ///
/// Create and or initialize a new epiccash wallet. /// Create a new epiccash wallet.
/// ///
// TODO: Complete/modify the documentation comment above // TODO: Complete/modify the documentation comment above
// TODO: Should return a void future. On error this function should throw and exception // TODO: Should return a void future. On error this function should throw and exception
static Future<String> initializeNewWallet({ static Future<void> initializeNewWallet({
required String config, required String config,
required String mnemonic, required String mnemonic,
required String password, required String password,
required String name, required String name,
}) async { }) async {
final String result = await compute( try {
await compute(
_initializeWalletWrapper, _initializeWalletWrapper,
( (
config: config, config: config,
@ -74,7 +80,85 @@ abstract class LibEpiccash {
name: name, name: name,
), ),
); );
} catch (e) {
throw("Error creating new wallet : ${e.toString()}");
}
}
return result; ///
/// Private function wrapper for wallet balances
///
static Future<String> _walletBalancesWrapper(
({
String wallet,
int refreshFromNode,
int minimumConfirmations
}) data,) async {
return lib_epiccash.getWalletInfo(
data.wallet,
data.refreshFromNode,
data.minimumConfirmations
);
}
///
/// Get balance information for the currently open wallet
///
static Future<String> getWalletBalances({
required String wallet,
required int refreshFromNode,
required int minimumConfirmations
}) async {
try {
String balances = await compute(_walletBalancesWrapper, (
wallet: wallet,
refreshFromNode: refreshFromNode,
minimumConfirmations: minimumConfirmations,
));
return balances;
} catch (e) {
throw("Error getting wallet info : ${e.toString()}");
} }
} }
///
/// Private function wrapper for recover wallet function
///
static Future<String> _recoverWalletWrapper(
({
String config,
String password,
String mnemonic,
String name,
}) data,) async {
return lib_epiccash.recoverWallet(
data.config,
data.password,
data.mnemonic,
data.name,
);
}
///
/// Recover an Epic wallet using a mnemonic
///
static Future<void> recoverWallet({
required String config,
required String password,
required String mnemonic,
required String name
}) async {
try {
await compute(_recoverWalletWrapper, (
config: config,
password: password,
mnemonic: mnemonic,
name: name,
));
} catch (e) {
throw("Error recovering wallet : ${e.toString()}");
}
}
}