Skip to content

Commit

Permalink
Use wallet name to identify ledger device (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
KingGorrin committed Mar 13, 2024
1 parent 3c848d5 commit ba8e838
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
10 changes: 4 additions & 6 deletions lib/blocs/ledger_wallet_file_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ 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 {
Future<void> getLedgerWalletPath(String walletId, String password,
String? walletName) async {
try {
await WalletUtils.createLedgerWalletFile(
walletId, password);
await WalletUtils.createLedgerWalletFile(walletId, password,
walletName: walletName);
await InitUtils.initWalletAfterDecryption(
Crypto.digest(utf8.encode(password)));
addEvent(kWalletFile as LedgerWalletFile);
Expand Down
11 changes: 7 additions & 4 deletions lib/screens/onboarding/create_ledger_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ import 'package:zenon_syrius_wallet_flutter/screens/screens.dart';
import 'package:zenon_syrius_wallet_flutter/utils/utils.dart';
import 'package:zenon_syrius_wallet_flutter/utils/wallet_file.dart';
import 'package:zenon_syrius_wallet_flutter/widgets/widgets.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

class CreateLedgerWalletScreen extends StatefulWidget {
final String walletId;
final WalletDefinition deviceInfo;
final String password;
final int progressBarNumLevels;

const CreateLedgerWalletScreen(
this.walletId,
this.deviceInfo,
this.password, {
this.progressBarNumLevels = 4,
Key? key,
}) : super(key: key);

@override
State<CreateLedgerWalletScreen> createState() => _CreateLedgerWalletScreenState();
State<CreateLedgerWalletScreen> createState() =>
_CreateLedgerWalletScreenState();
}

class _CreateLedgerWalletScreenState extends State<CreateLedgerWalletScreen> {
Expand All @@ -29,8 +31,9 @@ class _CreateLedgerWalletScreenState extends State<CreateLedgerWalletScreen> {
super.initState();
_ledgerWalletFileBloc = LedgerWalletFileBloc()
..getLedgerWalletPath(
widget.walletId,
widget.deviceInfo.walletId,
widget.password,
widget.deviceInfo.walletName,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ class _HardwareWalletDeviceChoiceScreenState
? () {
NavigationUtils.push(
context,
HardwareWalletPasswordScreen(_selectedDevice!.walletId),
HardwareWalletPasswordScreen(_selectedDevice!),
);
}
: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import 'package:flutter/material.dart';
import 'package:zenon_syrius_wallet_flutter/screens/onboarding/create_ledger_screen.dart';
import 'package:zenon_syrius_wallet_flutter/utils/utils.dart';
import 'package:zenon_syrius_wallet_flutter/widgets/widgets.dart';
import 'package:znn_sdk_dart/znn_sdk_dart.dart';

class HardwareWalletPasswordScreen extends StatefulWidget {
final String walletId;
final WalletDefinition deviceInfo;

const HardwareWalletPasswordScreen(this.walletId, {Key? key}) : super(key: key);
const HardwareWalletPasswordScreen(this.deviceInfo, {Key? key}) : super(key: key);

@override
State<HardwareWalletPasswordScreen> createState() =>
Expand Down Expand Up @@ -115,7 +116,7 @@ class _HardwareWalletPasswordScreenState extends State<HardwareWalletPasswordScr
NavigationUtils.push(
context,
CreateLedgerWalletScreen(
widget.walletId,
widget.deviceInfo,
_passwordController.text,
),
);
Expand Down
23 changes: 12 additions & 11 deletions lib/utils/wallet_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,37 +133,38 @@ class KeyStoreWalletFile extends WalletFile {

class LedgerWalletFile extends WalletFile {
final Mutex _lock = Mutex();
final String _walletId;
final String _walletName;
LedgerWallet? _wallet;

static final LedgerWalletManager ledgerWalletManager = LedgerWalletManager();

static Future<LedgerWallet> _connect(String walletId) async {
static Future<LedgerWallet> _connect(String walletIdOrName) async {
for (var walletDefinition
in await ledgerWalletManager.getWalletDefinitions()) {
if (walletDefinition.walletId == walletId) {
if (walletDefinition.walletId == walletIdOrName ||
walletDefinition.walletName == walletIdOrName) {
return await ledgerWalletManager.getWallet(walletDefinition)
as LedgerWallet;
}
}
throw const LedgerError.connectionError(
origMessage:
'Cannot find the hardware device, please connect the device on which the wallet is initialized');
'Cannot find the hardware device, please connect/unlock the device on which the wallet is initialized');
}

static Future<LedgerWalletFile> create(String walletId, String password,
{String? name}) async {
{String? walletName}) async {
LedgerWallet wallet = await _connect(walletId);
try {
final baseAddress = (await (await wallet.getAccount()).getAddress());
name ??= baseAddress.toString();
final walletPath = path.join(znnDefaultWalletDirectory.path, name);
await WalletFile.write(walletPath, password, utf8.encode(walletId),
walletName ??= baseAddress.toString();
final walletPath = path.join(znnDefaultWalletDirectory.path, walletName);
await WalletFile.write(walletPath, password, utf8.encode(walletName),
metadata: {
baseAddressKey: baseAddress.toString(),
walletTypeKey: ledgerWalletType
});
return LedgerWalletFile._internal(walletPath, walletId);
return LedgerWalletFile._internal(walletPath, walletName);
} finally {
await wallet.disconnect();
}
Expand All @@ -181,7 +182,7 @@ class LedgerWalletFile extends WalletFile {
return LedgerWalletFile._internal(walletPath, utf8.decode(decrypted));
}

LedgerWalletFile._internal(super._path, this._walletId);
LedgerWalletFile._internal(super._path, this._walletName);

@override
String get walletType => ledgerWalletType;
Expand All @@ -196,7 +197,7 @@ class LedgerWalletFile extends WalletFile {
Future<Wallet> open() async {
await _lock.acquire();
try {
_wallet = await _connect(_walletId);
_wallet = await _connect(_walletName);
return _wallet!;
} catch (_) {
_lock.release();
Expand Down
5 changes: 3 additions & 2 deletions lib/utils/wallet_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ class WalletUtils {
static Future<void> createLedgerWalletFile(
String walletId,
String password, {
String? name,
String? walletName,
}) async {
kWalletFile = await LedgerWalletFile.create(walletId, password, name: name);
kWalletFile = await LedgerWalletFile.create(walletId, password,
walletName: walletName);
kWalletPath = kWalletFile!.walletPath;
await _storeWalletPath(kWalletFile!.walletPath);
}
Expand Down

0 comments on commit ba8e838

Please sign in to comment.