Skip to content

Commit 665cb8b

Browse files
authored
Merge pull request #32 from cake-tech/cyjan-review-fixes
fix: review fixes
2 parents 6873168 + f6fd190 commit 665cb8b

28 files changed

Lines changed: 247 additions & 79 deletions

lib/coins/abstract/wallet_info.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ abstract class CoinWalletInfo {
1313

1414
Coin get coin;
1515

16-
void openUI(final BuildContext context);
16+
Future<void> openUI(final BuildContext context);
1717

1818
Future<bool> checkWalletPassword(final String password);
1919

lib/coins/bitcoin/coin.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import 'package:cupcake/coins/bitcoin/wallet_info.dart';
1515
import 'package:cupcake/l10n/app_localizations.dart';
1616
import 'package:cupcake/utils/encryption/default.dart';
1717
import 'package:cupcake/utils/filesystem.dart';
18+
import 'package:cupcake/utils/zpub.dart';
1819
import 'package:flutter/material.dart';
1920
import 'package:path/path.dart' as p;
2021

@@ -52,7 +53,7 @@ class Bitcoin implements Coin {
5253
}
5354

5455
// Prevent user from slipping outside allowed directory
55-
final String walletPath = p.join(baseDir.path, walletName);
56+
final String walletPath = p.normalize(p.join(baseDir.path, walletName));
5657
if (!walletPath.startsWith(baseDir.path)) {
5758
throw Exception(Coin.L.error_illegal_wallet_name(walletName));
5859
}
@@ -186,6 +187,8 @@ class Bitcoin implements Coin {
186187
.asString();
187188
xpub = xpub.substring(xpub.indexOf("]") + 1, xpub.lastIndexOf("/"));
188189

190+
final zpub = convertXpubToZpub(xpub);
191+
189192
unawaited(() async {
190193
final start = Stopwatch()..start();
191194
print("generating addresses");
@@ -202,7 +205,7 @@ class Bitcoin implements Coin {
202205
return BDKWalletWrapper(
203206
wallets: wallets,
204207
mnemonic: mnemonic,
205-
xpub: xpub,
208+
zpub: zpub,
206209
);
207210
}
208211

lib/coins/bitcoin/wallet.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class BDKWalletWrapper {
2323
BDKWalletWrapper({
2424
required final List<Wallet> wallets,
2525
required this.mnemonic,
26-
required this.xpub,
26+
required this.zpub,
2727
}) : w = wallets;
2828
final List<Wallet> w;
2929
final String mnemonic;
3030
String get currentAddress {
3131
return w[0].getAddress(addressIndex: AddressIndex.peek(index: 0)).address.asString();
3232
}
3333

34-
final String xpub;
34+
final String zpub;
3535

3636
Future<bool> sign({required final PartiallySignedTransaction psbt}) async {
3737
bool ret = false;
@@ -197,8 +197,8 @@ class BitcoinWallet implements CoinWallet {
197197
),
198198
WalletSeedDetail(
199199
type: WalletSeedDetailType.text,
200-
name: "xPub",
201-
value: wallet.xpub,
200+
name: "zPub",
201+
value: wallet.zpub,
202202
),
203203
WalletSeedDetail(
204204
type: WalletSeedDetailType.qr,
@@ -211,7 +211,7 @@ class BitcoinWallet implements CoinWallet {
211211
Uri get publicUri => Uri(
212212
scheme: "bitcoin",
213213
queryParameters: {
214-
"xpub": wallet.xpub,
214+
"zpub": wallet.zpub,
215215
// "path": wallet.derivationPath,
216216
"label": p.basename(walletName),
217217
},

lib/coins/bitcoin/wallet_info.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ class BitcoinWalletInfo extends CoinWalletInfo {
4444
Coins get type => coin.type;
4545

4646
@override
47-
void openUI(final BuildContext context) {
48-
OpenWallet(coinWalletInfo: this, enableBiometric: false).push(context);
47+
Future<void> openUI(final BuildContext context) {
48+
return OpenWallet(coinWalletInfo: this, enableBiometric: false).push(context);
4949
}
5050

5151
@override
@@ -74,14 +74,13 @@ class BitcoinWalletInfo extends CoinWalletInfo {
7474
if (File(p.join(basePath, newName)).existsSync()) {
7575
throw Exception(Coin.L.error_wallet_name_already_exists);
7676
}
77-
File(walletName).copySync(p.join(basePath, newName));
77+
File("$walletName.keys").copySync(p.join(basePath, "$newName.keys"));
7878
// Copy and delete later, if anything throws below we end up with copied walled,
7979
// instead of nuking the wallet
80-
File(walletName).deleteSync();
8180
File("$walletName.keys").deleteSync();
8281
_walletName = newName;
8382
}
8483

8584
@override
86-
bool exists() => File(walletName).existsSync();
85+
bool exists() => File("$walletName.keys").existsSync();
8786
}

lib/coins/monero/coin.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Monero implements Coin {
7979
}
8080

8181
// Prevent user from slipping outside allowed directory
82-
final String walletPath = p.join(baseDir.path, walletName);
82+
final String walletPath = p.normalize(p.join(baseDir.path, walletName));
8383
if (!walletPath.startsWith(baseDir.path)) {
8484
throw Exception(Coin.L.error_illegal_wallet_name(walletName));
8585
}

lib/coins/monero/wallet.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@ class MoneroWallet implements CoinWallet {
4343
int _accountIndex = 0;
4444
@override
4545
int getAccountsCount() => wallet.numSubaddressAccounts();
46+
4647
@override
4748
void setAccount(final int accountIndex) {
48-
if (_accountIndex < getAccountsCount()) {
49+
if (accountIndex >= getAccountsCount()) {
4950
throw Exception(Coin.L.error_account_index_higher_than_count);
5051
}
5152
_accountIndex = accountIndex;

lib/coins/monero/wallet_info.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class MoneroWalletInfo extends CoinWalletInfo {
3939
Coins get type => coin.type;
4040

4141
@override
42-
void openUI(final BuildContext context) {
43-
OpenWallet(coinWalletInfo: this, enableBiometric: false).push(context);
42+
Future<void> openUI(final BuildContext context) {
43+
return OpenWallet(coinWalletInfo: this, enableBiometric: false).push(context);
4444
}
4545

4646
@override
@@ -83,6 +83,7 @@ class MoneroWalletInfo extends CoinWalletInfo {
8383
}
8484
File(walletName).copySync(p.join(basePath, newName));
8585
File("$walletName.keys").copySync(p.join(basePath, "$newName.keys"));
86+
8687
// Copy and delete later, if anything throws below we end up with copied walled,
8788
// instead of nuking the wallet
8889
File(walletName).deleteSync();

lib/utils/zpub.dart

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import 'dart:typed_data';
2+
3+
import 'package:blockchain_utils/blockchain_utils.dart';
4+
5+
String convertXpubToZpub(final String xpub) {
6+
try {
7+
final decoded = Base58Decoder.checkDecode(xpub);
8+
9+
if (decoded.length < 4) {
10+
throw ArgumentError('Invalid extended public key length');
11+
}
12+
13+
final versionBytes = decoded.sublist(0, 4);
14+
final xpubVersionBytes = [0x04, 0x88, 0xb2, 0x1e]; // xpub mainnet version
15+
final tpubVersionBytes = [0x04, 0x35, 0x87, 0xcf]; // tpub testnet version
16+
17+
final bool isXpub = listEquals(versionBytes, xpubVersionBytes);
18+
final bool isTpub = listEquals(versionBytes, tpubVersionBytes);
19+
20+
if (!isXpub && !isTpub) {
21+
return xpub;
22+
}
23+
24+
final zpubVersionBytes = isXpub
25+
? [0x04, 0xb2, 0x47, 0x46]
26+
: // zpub mainnet
27+
[0x04, 0x5f, 0x1c, 0xf6]; // vpub testnet
28+
29+
final newExtendedKey = Uint8List.fromList([
30+
...zpubVersionBytes,
31+
...decoded.sublist(4),
32+
]);
33+
34+
return Base58Encoder.checkEncode(newExtendedKey);
35+
} catch (e) {
36+
throw ArgumentError('Failed to convert xpub to zpub: $e');
37+
}
38+
}
39+
40+
bool listEquals<T>(final List<T> a, final List<T> b) {
41+
if (a.length != b.length) return false;
42+
for (var i = 0; i < a.length; i++) {
43+
if (a[i] != b[i]) return false;
44+
}
45+
return true;
46+
}

lib/view_model/create_wallet_view_model.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ abstract class CreateWalletViewModelBase extends ViewModel with Store {
245245
await callThrowable(
246246
() async {
247247
await _createWallet();
248+
await walletPassword.clear();
249+
await walletPasswordInitial.clear();
248250
},
249251
L.create_wallet,
250252
);

lib/view_model/initial_setup_view_model.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ class InitialSetupViewModel extends ViewModel {
88
@override
99
bool get hasBackground => true;
1010

11-
void showTos() {
12-
TosPage().push(c!);
13-
}
11+
Future<void> showTos() => TosPage().push(c!);
1412
}

0 commit comments

Comments
 (0)