Skip to content

Commit

Permalink
Ledger Wallet support
Browse files Browse the repository at this point in the history
  • Loading branch information
KingGorrin committed Jan 18, 2024
1 parent 267950f commit 23d9b01
Show file tree
Hide file tree
Showing 51 changed files with 1,083 additions and 282 deletions.
8 changes: 4 additions & 4 deletions lib/blocs/auto_receive_tx_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:zenon_syrius_wallet_flutter/main.dart';
import 'package:zenon_syrius_wallet_flutter/model/model.dart';
import 'package:zenon_syrius_wallet_flutter/utils/account_block_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/address_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/extensions.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

Expand All @@ -33,17 +34,16 @@ class AutoReceiveTxWorker extends BaseBloc<WalletNotification> {
(await zenon!.ledger.getAccountBlockByHash(currentHash))!
.toAddress
.toString();
KeyPair keyPair = kKeyStore!.getKeyPair(
kDefaultAddressList.indexOf(toAddress),
);
WalletAccount walletAccount =
await kWalletFile!.account(kDefaultAddressList.indexOf(toAddress));
AccountBlockTemplate transactionParams = AccountBlockTemplate.receive(
currentHash,
);
AccountBlockTemplate response =
await AccountBlockUtils.createAccountBlock(
transactionParams,
'receive transaction',
blockSigningKey: keyPair,
walletAccount: walletAccount,
waitForRequiredPlasma: true,
);
pool.removeFirst();
Expand Down
10 changes: 5 additions & 5 deletions lib/blocs/auto_unlock_htlc_worker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:zenon_syrius_wallet_flutter/model/database/notification_type.dar
import 'package:zenon_syrius_wallet_flutter/model/database/wallet_notification.dart';
import 'package:zenon_syrius_wallet_flutter/utils/account_block_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/address_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/extensions.dart';
import 'package:zenon_syrius_wallet_flutter/utils/format_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';
Expand All @@ -25,7 +26,7 @@ class AutoUnlockHtlcWorker extends BaseBloc<WalletNotification> {
}

Future<void> autoUnlock() async {
if (pool.isNotEmpty && !running && kKeyStore != null) {
if (pool.isNotEmpty && !running && kWalletFile != null) {
running = true;
Hash currentHash = pool.first;
try {
Expand All @@ -38,16 +39,15 @@ 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(
kDefaultAddressList.indexOf(htlc.hashLocked.toString()),
);
WalletAccount walletAccount = await kWalletFile!.account(
kDefaultAddressList.indexOf(htlc.hashLocked.toString()));
AccountBlockTemplate transactionParams = zenon!.embedded.htlc
.unlock(htlc.id, FormatUtils.decodeHexString(swap.preimage!));
AccountBlockTemplate response =
await AccountBlockUtils.createAccountBlock(
transactionParams,
'complete swap',
blockSigningKey: keyPair,
walletAccount: walletAccount,
waitForRequiredPlasma: true,
);
_sendSuccessNotification(response, htlc.hashLocked.toString());
Expand Down
4 changes: 2 additions & 2 deletions lib/blocs/blocs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ export 'auto_receive_tx_worker.dart';
export 'base_bloc.dart';
export 'base_bloc_for_reloading_indicator.dart';
export 'base_bloc_with_refresh_mixin.dart';
export 'decrypt_key_store_bloc.dart';
export 'decrypt_wallet_file_bloc.dart';
export 'hide_widget_status_bloc.dart';
export 'infinite_scroll_bloc.dart';
export 'key_store_path_bloc.dart';
export 'key_store_file_bloc.dart';
export 'lock_bloc.dart';
export 'node_sync_status_bloc.dart';
export 'notifications_bloc.dart';
Expand Down
16 changes: 0 additions & 16 deletions lib/blocs/decrypt_key_store_bloc.dart

This file was deleted.

15 changes: 15 additions & 0 deletions lib/blocs/decrypt_wallet_file_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart';
import 'package:zenon_syrius_wallet_flutter/utils/wallet_file.dart';
import 'package:zenon_syrius_wallet_flutter/utils/wallet_utils.dart';

class DecryptWalletFileBloc extends BaseBloc<WalletFile?> {
Future<void> decryptWalletFile(String type, String path, String password) async {
try {
addEvent(null);
final walletFile = await WalletUtils.decryptWalletFile(type, path, password);
addEvent(walletFile);
} catch (e, stackTrace) {
addError(e, stackTrace);
}
}
}
5 changes: 3 additions & 2 deletions lib/blocs/hide_widget_status_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart';
import 'package:zenon_syrius_wallet_flutter/main.dart';
import 'package:zenon_syrius_wallet_flutter/utils/constants.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:zenon_syrius_wallet_flutter/utils/keystore_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/wallet_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/widget_utils.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

Expand All @@ -15,7 +15,8 @@ class HideWidgetStatusBloc extends BaseBloc<bool?> {
try {
addEvent(null);
if (!isHidden) {
await KeyStoreUtils.decryptKeyStoreFile(kKeyStorePath!, password);
await WalletUtils.decryptWalletFile(
kWalletType!, kWalletPath!, password);
}
await _markWidgetAsHidden(widgetTitle, isHidden);
addEvent(isHidden);
Expand Down
25 changes: 25 additions & 0 deletions lib/blocs/key_store_file_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:async';
import 'dart:convert';

import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:zenon_syrius_wallet_flutter/utils/wallet_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/init_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/wallet_file.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

class KeyStoreFileBloc extends BaseBloc<KeyStoreWalletFile?> {
Future<void> getKeyStorePath(
String mnemonic,
String password,
) async {
try {
await WalletUtils.createKeyStoreWalletFile(mnemonic, password);
await InitUtils.initWalletAfterDecryption(
Crypto.digest(utf8.encode(password)));
addEvent(kWalletFile as KeyStoreWalletFile);
} catch (e, stackTrace) {
addError(e, stackTrace);
}
}
}
21 changes: 0 additions & 21 deletions lib/blocs/key_store_path_bloc.dart

This file was deleted.

26 changes: 26 additions & 0 deletions lib/blocs/ledger_wallet_file_bloc.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'dart:async';
import 'dart:convert';

import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:zenon_syrius_wallet_flutter/utils/wallet_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/init_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/wallet_file.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

class LedgerWalletFileBloc extends BaseBloc<LedgerWalletFile?> {
Future<void> getLedgerWalletPath(
String walletId,
String password,
) async {
try {
await WalletUtils.createLedgerWalletFile(
walletId, password);
await InitUtils.initWalletAfterDecryption(
Crypto.digest(utf8.encode(password)));
addEvent(kWalletFile as LedgerWalletFile);
} catch (e, stackTrace) {
addError(e, stackTrace);
}
}
}
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 walletAccount = await kWalletFile!.account(
kDefaultAddressList.indexOf(swap.selfAddress.toString()),
);
AccountBlockUtils.createAccountBlock(transactionParams, 'complete swap',
blockSigningKey: blockSigningKeyPair, waitForRequiredPlasma: true)
walletAccount: walletAccount, waitForRequiredPlasma: true)
.then(
(response) async {
swap.state = P2pSwapState.completed;
Expand Down
7 changes: 4 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 @@ -5,6 +5,7 @@ import 'package:zenon_syrius_wallet_flutter/model/p2p_swap/p2p_swap.dart';
import 'package:zenon_syrius_wallet_flutter/utils/account_block_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/address_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/date_time_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/extensions.dart';
import 'package:zenon_syrius_wallet_flutter/utils/format_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';
Expand All @@ -19,7 +20,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 +32,11 @@ class JoinHtlcSwapBloc extends BaseBloc<HtlcSwap?> {
initialHtlc.keyMaxSize,
initialHtlc.hashLock,
);
KeyPair blockSigningKeyPair = kKeyStore!.getKeyPair(
WalletAccount walletAccount = await kWalletFile!.account(
kDefaultAddressList.indexOf(initialHtlc.hashLocked.toString()),
);
AccountBlockUtils.createAccountBlock(transactionParams, 'join swap',
blockSigningKey: blockSigningKeyPair, waitForRequiredPlasma: true)
walletAccount: walletAccount, waitForRequiredPlasma: true)
.then(
(response) async {
final swap = HtlcSwap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ import 'package:zenon_syrius_wallet_flutter/blocs/base_bloc.dart';
import 'package:zenon_syrius_wallet_flutter/main.dart';
import 'package:zenon_syrius_wallet_flutter/utils/account_block_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/address_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/extensions.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

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 walletAccount = await kWalletFile!.account(
kDefaultAddressList.indexOf(selfAddress.toString()),
);
AccountBlockUtils.createAccountBlock(
transactionParams, 'reclaim swap funds',
blockSigningKey: blockSigningKeyPair, waitForRequiredPlasma: true)
walletAccount: walletAccount, waitForRequiredPlasma: true)
.then(
(response) {
ZenonAddressUtils.refreshBalance();
Expand Down
5 changes: 3 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 @@ -7,6 +7,7 @@ import 'package:zenon_syrius_wallet_flutter/model/p2p_swap/p2p_swap.dart';
import 'package:zenon_syrius_wallet_flutter/utils/account_block_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/address_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/date_time_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/extensions.dart';
import 'package:zenon_syrius_wallet_flutter/utils/format_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';
Expand Down Expand Up @@ -37,11 +38,11 @@ class StartHtlcSwapBloc extends BaseBloc<HtlcSwap?> {
htlcPreimageMaxLength,
hashLock.getBytes(),
);
KeyPair blockSigningKeyPair = kKeyStore!.getKeyPair(
WalletAccount walletAccount = await kWalletFile!.account(
kDefaultAddressList.indexOf(selfAddress.toString()),
);
AccountBlockUtils.createAccountBlock(transactionParams, 'start swap',
blockSigningKey: blockSigningKeyPair, waitForRequiredPlasma: true)
walletAccount: walletAccount, waitForRequiredPlasma: true)
.then(
(response) async {
final swap = HtlcSwap(
Expand Down
7 changes: 4 additions & 3 deletions lib/blocs/transfer/send_payment_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:zenon_syrius_wallet_flutter/blocs/blocs.dart';
import 'package:zenon_syrius_wallet_flutter/utils/account_block_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/address_utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/extensions.dart';
import 'package:zenon_syrius_wallet_flutter/utils/global.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

Expand All @@ -13,7 +14,7 @@ class SendPaymentBloc extends BaseBloc<AccountBlockTemplate?> {
List<int>? data,
Token? token,
AccountBlockTemplate? block,
}) {
}) async {
assert(
block == null &&
fromAddress != null &&
Expand All @@ -31,13 +32,13 @@ class SendPaymentBloc extends BaseBloc<AccountBlockTemplate?> {
amount!,
data,
);
KeyPair blockSigningKeyPair = kKeyStore!.getKeyPair(
WalletAccount walletAccount = await kWalletFile!.account(
kDefaultAddressList.indexOf(fromAddress),
);
AccountBlockUtils.createAccountBlock(
accountBlock,
'send transaction',
blockSigningKey: blockSigningKeyPair,
walletAccount: walletAccount,
waitForRequiredPlasma: true,
).then(
(response) {
Expand Down
3 changes: 3 additions & 0 deletions lib/model/database/notification_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@ enum NotificationType {

@HiveField(14)
delete,

@HiveField(15)
confirm,
}
5 changes: 5 additions & 0 deletions lib/model/database/notification_type.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/model/database/wallet_notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class WalletNotification extends HiveObject {
return _getCircledIcon(Icons.link);
case NotificationType.delete:
return _getCircledIcon(Icons.delete_forever);
case NotificationType.confirm:
return _getCircledIcon(Icons.remove_red_eye,
iconColor: AppColors.alertNotification);
default:
return _getCircledIcon(MaterialCommunityIcons.arrow_top_right);
}
Expand Down
Loading

0 comments on commit 23d9b01

Please sign in to comment.