Skip to content

Commit

Permalink
Use wallet abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
KingGorrin committed Nov 21, 2023
1 parent dc95cfd commit 8cdc786
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 63 deletions.
4 changes: 2 additions & 2 deletions lib/blocs/auto_receive_tx_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class AutoReceiveTxWorker extends BaseBloc<WalletNotification> {
(await zenon!.ledger.getAccountBlockByHash(currentHash))!
.toAddress
.toString();
KeyPair keyPair = kKeyStore!.getKeyPair(
WalletAccount account = await kWallet!.getAccount(
kDefaultAddressList.indexOf(toAddress),
);
AccountBlockTemplate transactionParams = AccountBlockTemplate.receive(
Expand All @@ -43,7 +43,7 @@ class AutoReceiveTxWorker extends BaseBloc<WalletNotification> {
await AccountBlockUtils.createAccountBlock(
transactionParams,
'receive transaction',
blockSigningKey: keyPair,
walletAccount: account,
waitForRequiredPlasma: true,
);
pool.removeFirst();
Expand Down
6 changes: 3 additions & 3 deletions lib/blocs/auto_unlock_htlc_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AutoUnlockHtlcWorker extends BaseBloc<WalletNotification> {
}

Future<void> autoUnlock() async {
if (pool.isNotEmpty && !running && kKeyStore != null) {
if (pool.isNotEmpty && !running && kWallet != null) {
running = true;
Hash currentHash = pool.first;
try {
Expand All @@ -38,7 +38,7 @@ class AutoUnlockHtlcWorker extends BaseBloc<WalletNotification> {
if (!kDefaultAddressList.contains(htlc.hashLocked.toString())) {
throw 'Swap address not in default addresses. Please add the address in the addresses list.';
}
KeyPair? keyPair = kKeyStore!.getKeyPair(
WalletAccount? account = await kWallet!.getAccount(
kDefaultAddressList.indexOf(htlc.hashLocked.toString()),
);
AccountBlockTemplate transactionParams = zenon!.embedded.htlc
Expand All @@ -47,7 +47,7 @@ class AutoUnlockHtlcWorker extends BaseBloc<WalletNotification> {
await AccountBlockUtils.createAccountBlock(
transactionParams,
'complete swap',
blockSigningKey: keyPair,
walletAccount: account,
waitForRequiredPlasma: true,
);
_sendSuccessNotification(response, htlc.hashLocked.toString());
Expand Down
2 changes: 1 addition & 1 deletion lib/blocs/hide_widget_status_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HideWidgetStatusBloc extends BaseBloc<bool?> {
try {
addEvent(null);
if (!isHidden) {
await KeyStoreUtils.decryptKeyStoreFile(kKeyStorePath!, password);
await KeyStoreUtils.decryptKeyStoreFile(kWalletId!, password);
}
await _markWidgetAsHidden(widgetTitle, isHidden);
addEvent(isHidden);
Expand Down
2 changes: 1 addition & 1 deletion lib/blocs/key_store_path_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class KeyStorePathBloc extends BaseBloc<String?> {
try {
await KeyStoreUtils.createKeyStore(mnemonic, passphrase);
await InitUtils.initWalletAfterDecryption();
addEvent(kKeyStorePath);
addEvent(kWalletId);
} catch (e, stackTrace) {
addError(e, stackTrace);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/blocs/p2p_swap/htlc_swap/complete_htlc_swap_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class CompleteHtlcSwapBloc extends BaseBloc<HtlcSwap?> {

AccountBlockTemplate transactionParams = zenon!.embedded.htlc.unlock(
Hash.parse(htlcId), FormatUtils.decodeHexString(swap.preimage!));
KeyPair blockSigningKeyPair = kKeyStore!.getKeyPair(
WalletAccount account = await kWallet!.getAccount(
kDefaultAddressList.indexOf(swap.selfAddress.toString()),
);
AccountBlockUtils.createAccountBlock(transactionParams, 'complete swap',
blockSigningKey: blockSigningKeyPair, waitForRequiredPlasma: true)
walletAccount: account, waitForRequiredPlasma: true)
.then(
(response) async {
swap.state = P2pSwapState.completed;
Expand Down
6 changes: 3 additions & 3 deletions lib/blocs/p2p_swap/htlc_swap/join_htlc_swap_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class JoinHtlcSwapBloc extends BaseBloc<HtlcSwap?> {
required P2pSwapChain fromChain,
required P2pSwapChain toChain,
required int counterHtlcExpirationTime,
}) {
}) async {
try {
addEvent(null);
AccountBlockTemplate transactionParams = zenon!.embedded.htlc.create(
Expand All @@ -31,11 +31,11 @@ class JoinHtlcSwapBloc extends BaseBloc<HtlcSwap?> {
initialHtlc.keyMaxSize,
initialHtlc.hashLock,
);
KeyPair blockSigningKeyPair = kKeyStore!.getKeyPair(
WalletAccount account = await kWallet!.getAccount(
kDefaultAddressList.indexOf(initialHtlc.hashLocked.toString()),
);
AccountBlockUtils.createAccountBlock(transactionParams, 'join swap',
blockSigningKey: blockSigningKeyPair, waitForRequiredPlasma: true)
walletAccount: account, waitForRequiredPlasma: true)
.then(
(response) async {
final swap = HtlcSwap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ class ReclaimHtlcSwapFundsBloc extends BaseBloc<AccountBlockTemplate?> {
void reclaimFunds({
required Hash htlcId,
required Address selfAddress,
}) {
}) async {
try {
addEvent(null);
AccountBlockTemplate transactionParams =
zenon!.embedded.htlc.reclaim(htlcId);
KeyPair blockSigningKeyPair = kKeyStore!.getKeyPair(
WalletAccount account = await kWallet!.getAccount(
kDefaultAddressList.indexOf(selfAddress.toString()),
);
AccountBlockUtils.createAccountBlock(
transactionParams, 'reclaim swap funds',
blockSigningKey: blockSigningKeyPair, waitForRequiredPlasma: true)
walletAccount: account, waitForRequiredPlasma: true)
.then(
(response) {
ZenonAddressUtils.refreshBalance();
Expand Down
4 changes: 2 additions & 2 deletions lib/blocs/p2p_swap/htlc_swap/start_htlc_swap_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class StartHtlcSwapBloc extends BaseBloc<HtlcSwap?> {
htlcPreimageMaxLength,
hashLock.getBytes(),
);
KeyPair blockSigningKeyPair = kKeyStore!.getKeyPair(
WalletAccount account = await kWallet!.getAccount(
kDefaultAddressList.indexOf(selfAddress.toString()),
);
AccountBlockUtils.createAccountBlock(transactionParams, 'start swap',
blockSigningKey: blockSigningKeyPair, waitForRequiredPlasma: true)
walletAccount: account, waitForRequiredPlasma: true)
.then(
(response) async {
final swap = HtlcSwap(
Expand Down
6 changes: 3 additions & 3 deletions lib/blocs/transfer/send_payment_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SendPaymentBloc extends BaseBloc<AccountBlockTemplate?> {
List<int>? data,
Token? token,
AccountBlockTemplate? block,
}) {
}) async {
assert(
block == null &&
fromAddress != null &&
Expand All @@ -31,13 +31,13 @@ class SendPaymentBloc extends BaseBloc<AccountBlockTemplate?> {
amount!,
data,
);
KeyPair blockSigningKeyPair = kKeyStore!.getKeyPair(
WalletAccount account = await kWallet!.getAccount(
kDefaultAddressList.indexOf(fromAddress),
);
AccountBlockUtils.createAccountBlock(
accountBlock,
'send transaction',
blockSigningKey: blockSigningKeyPair,
walletAccount: account,
waitForRequiredPlasma: true,
).then(
(response) {
Expand Down
10 changes: 5 additions & 5 deletions lib/screens/change_wallet_password_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ class _ChangeWalletPasswordScreenState
String currentPassword,
String newPassword,
) async {
String mnemonic = kKeyStore!.mnemonic!;
String oldKeyStorePath = kKeyStorePath!;
String mnemonic = (kWallet as KeyStore).mnemonic!;
String oldWalletId = kWalletId!;
await KeyStoreUtils.createKeyStore(
mnemonic,
newPassword,
keyStoreName:
'${await kKeyStore!.getKeyPair(0).address}_${DateTime.now().millisecondsSinceEpoch}',
'${await (await kWallet!.getAccount(0)).getAddress()}_${DateTime.now().millisecondsSinceEpoch}',
);
await FileUtils.deleteFile(oldKeyStorePath);
await FileUtils.deleteFile(oldWalletId);
if (!mounted) return;
Navigator.pop(context);
}
Expand Down Expand Up @@ -219,7 +219,7 @@ class _ChangeWalletPasswordScreenState
? () {
_loadingButtonKey.currentState!.animateForward();
model.decryptKeyStoreFile(
kKeyStorePath!,
kWalletId!,
_currentPasswordController.text,
);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/dump_mnemonic_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class _DumpMnemonicScreenState extends State<DumpMnemonicScreen> {
try {
_continueButtonKey.currentState!.animateForward();
await KeyStoreUtils.decryptKeyStoreFile(
kKeyStorePath!,
kWalletId!,
_passwordController.text,
).then((keyStore) {
setState(() {
Expand Down
6 changes: 3 additions & 3 deletions lib/screens/splash_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class _SplashScreenState extends State<SplashScreen>

void _navigateToNextScreen() {
_controller.stop();
return kKeyStorePath != null
return kWalletId != null
? _checkForDefaultNode()
: Navigator.pushReplacementNamed(
context,
Expand Down Expand Up @@ -111,8 +111,8 @@ class _SplashScreenState extends State<SplashScreen>
);

Future<void> _deleteKeyStoreFile() async {
await FileUtils.deleteFile(kKeyStorePath!);
kKeyStorePath = null;
await FileUtils.deleteFile(kWalletId!);
kWalletId = null;
}

void _checkForDefaultNode() => sharedPrefsService!.get(
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/account_block_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AccountBlockUtils {
static Future<AccountBlockTemplate> createAccountBlock(
AccountBlockTemplate transactionParams,
String purposeOfGeneratingPlasma, {
KeyPair? blockSigningKey,
WalletAccount? walletAccount,
bool waitForRequiredPlasma = false,
}) async {
SyncInfo syncInfo = await zenon!.stats.syncInfo();
Expand All @@ -26,7 +26,7 @@ class AccountBlockUtils {
(syncInfo.targetHeight - syncInfo.currentHeight) < 20))
: true;
if (nodeIsSynced) {
Address address = (await blockSigningKey?.getAddress() ??
Address address = (await walletAccount?.getAddress() ??
await zenon!.defaultKeyPair!.getAddress());
try {
// Wait until the lock is unused.
Expand All @@ -45,7 +45,7 @@ class AccountBlockUtils {

bool needPlasma = await zenon!.requiresPoW(
transactionParams,
blockSigningKey: blockSigningKey,
blockSigningKey: walletAccount,
);

if (needPlasma) {
Expand All @@ -55,7 +55,7 @@ class AccountBlockUtils {
}
final AccountBlockTemplate response = await zenon!.send(
transactionParams,
currentKeyPair: blockSigningKey,
currentKeyPair: walletAccount,
generatingPowCallback: _addEventToPowGeneratingStatusBloc,
waitForRequiredPlasma: waitForRequiredPlasma,
);
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/address_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import 'package:zenon_syrius_wallet_flutter/utils/node_utils.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

class SetAddressArguments {
final KeyStore? keystore;
final Wallet? wallet;
final SendPort port;

SetAddressArguments(this.keystore, this.port);
SetAddressArguments(this.wallet, this.port);
}

class ZenonAddressUtils {
Expand All @@ -31,7 +31,7 @@ class ZenonAddressUtils {
int addrListLength = kDefaultAddressList.length;
for (int i = 0; i < numAddr; i++) {
int addrListCounter = addrListLength + i;
Address? address = await kKeyStore!.getKeyPair(addrListCounter).address;
Address? address = await (await kWallet!.getAccount(addrListCounter)).getAddress();
listAddr.add(address);
Box addressesBox = Hive.box(kAddressesBox);
await addressesBox.add(listAddr.elementAt(i).toString());
Expand Down Expand Up @@ -65,7 +65,7 @@ class ZenonAddressUtils {
List<Future<String>>.generate(
kNumOfInitialAddresses,
(index) async =>
(await args.keystore!.getKeyPair(index).address).toString()),
(await (await args.wallet!.getAccount(index)).getAddress()).toString()),
))) {
args.port.send(element);
}
Expand All @@ -82,10 +82,10 @@ class ZenonAddressUtils {
kSelectedAddress = sharedPrefsService!.get(kDefaultAddressKey);
}

static Future<void> setAddresses(KeyStore? keyStore) async {
static Future<void> setAddresses(Wallet? wallet) async {
final port = ReceivePort();
Box addressesBox = await Hive.openBox(kAddressesBox);
final args = SetAddressArguments(keyStore, port.sendPort);
final args = SetAddressArguments(wallet, port.sendPort);
if (addressesBox.isEmpty) {
Isolate.spawn<SetAddressArguments>(
setAddressesFunction,
Expand Down
10 changes: 2 additions & 8 deletions lib/utils/global.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:zenon_syrius_wallet_flutter/model/model.dart';
import 'package:zenon_syrius_wallet_flutter/utils/constants.dart';
Expand All @@ -9,7 +7,7 @@ import 'package:znn_sdk_dart/znn_sdk_dart.dart';
ValueNotifier<String?> kLastWalletConnectUriNotifier = ValueNotifier(null);
String? kCurrentNode;
String? kSelectedAddress;
String? kKeyStorePath;
String? kWalletId;
String? kLocalIpAddress;

int? kAutoLockWalletMinutes;
Expand All @@ -20,11 +18,7 @@ double? kAutoEraseWalletLimit;

bool kWalletInitCompleted = false;

KeyStore? kKeyStore;

KeyStoreManager kKeyStoreManager = KeyStoreManager(
walletPath: Directory(kKeyStorePath!),
);
Wallet? kWallet;

List<String> kDbNodes = [];
List<String?> kDefaultAddressList = [];
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/init_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,20 @@ class InitUtils {
);

static Future<void> initWalletAfterDecryption() async {
await ZenonAddressUtils.setAddresses(kKeyStore);
await ZenonAddressUtils.setAddresses(kWallet);
await ZenonAddressUtils.setAddressLabels();
await ZenonAddressUtils.setDefaultAddress();
zenon!.defaultKeyPair = kKeyStore!.getKeyPair(
zenon!.defaultKeyPair = await kWallet!.getAccount(
kDefaultAddressList.indexOf(kSelectedAddress),
);
await _openFavoriteTokensBox();
await _openNotificationsBox();
await _openRecipientBox();
await NodeUtils.initWebSocketClient();
await _setWalletVersion();
final baseAddress = await kKeyStore!.getKeyPair(0).address;
final baseAddress = await (await kWallet!.getAccount(0)).getAddress();
await htlcSwapsService!.openBoxes(
baseAddress.toString(), kKeyStore!.getKeyPair(0).getPrivateKey()!);
baseAddress.toString(), (await kWallet!.getAccount(0) as KeyPair).getPrivateKey()!);
sl<HtlcSwapsHandler>().start();
kWalletInitCompleted = true;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/utils/keystore_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class KeyStoreUtils {
KeyStore keyStore,
String keyStorePath,
) {
kKeyStorePath = keyStorePath;
kKeyStore = keyStore;
kWalletId = keyStorePath;
kWallet = keyStore;
}

static Future<void> createKeyStore(
Expand Down Expand Up @@ -53,21 +53,21 @@ class KeyStoreUtils {
}

static Future<void> setKeyStorePath() async {
if (kKeyStorePath == null) {
if (kWalletId == null) {
Box keyStoreBox = await Hive.openBox(kKeyStoreBox);
if (keyStoreBox.isEmpty) {
// Here we check if the key store path is saved in another place
// and we copy that value, if it exists
String? keyStorePath = sharedPrefsService!.get(kEntropyFilePathKey);
if (keyStorePath != null) {
keyStoreBox.add(keyStorePath);
kKeyStorePath = keyStoreBox.values.first;
kWalletId = keyStoreBox.values.first;
}
} else {
kKeyStorePath = keyStoreBox.values.first;
kWalletId = keyStoreBox.values.first;
}
} else {
_saveKeyStorePath(kKeyStorePath);
_saveKeyStorePath(kWalletId);
}
}
}
2 changes: 1 addition & 1 deletion lib/utils/node_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class NodeUtils {
(_kHeight == 0 || result['height'] >= _kHeight + 1)) {
_kHeight = result['height'];
if (sl<AutoReceiveTxWorker>().pool.isNotEmpty &&
kKeyStore != null) {
kWallet != null) {
sl<AutoReceiveTxWorker>().autoReceive();
}
}
Expand Down
Loading

0 comments on commit 8cdc786

Please sign in to comment.