CW-319-Wallet-Seed-keys-URI-QR-code (#831)

* wallet QR code on Wallet/Seed screen

* fix restore height for new wallet

* fix height parameter

* fix currenHeight and HeightByDate for haven

* update configure.dart

* fix coments

* minor fix
This commit is contained in:
Serhii 2023-03-15 17:30:06 +02:00 committed by GitHub
parent f458e5b349
commit fc2627df38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 393 additions and 238 deletions

View file

@ -1,4 +1,6 @@
import 'package:intl/intl.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
// FIXME: Hardcoded values; Works only for monero
@ -93,7 +95,7 @@ int getMoneroHeigthByDate({required DateTime date}) {
int height = 0;
try {
if ((dates[raw] == null)||(dates[raw] == lastHeight)) {
if ((dates[raw] == null) || (dates[raw] == lastHeight)) {
startHeight = dates.values.toList()[dates.length - 2];
endHeight = dates.values.toList()[dates.length - 1];
final heightPerDay = (endHeight - startHeight) / 31;
@ -118,3 +120,86 @@ int getMoneroHeigthByDate({required DateTime date}) {
return height;
}
const havenDates = {
"2023-03": 1309180,
"2023-01": 1266810,
"2022-12": 1244510,
"2022-11": 1222970,
"2022-10": 1200700,
"2022-09": 1179140,
"2022-08": 1156870,
"2022-07": 1134600,
"2022-06": 1113030,
"2022-05": 1090800,
"2022-04": 1069250,
"2022-03": 1047000,
"2022-02": 1026960,
"2022-01": 1004700,
"2021-12": 982400,
"2021-11": 961000,
"2021-10": 938600,
"2021-09": 917000,
"2021-08": 894800,
"2021-07": 886000,
"2021-06": 867300,
"2021-05": 845000,
"2021-04": 823500,
"2021-03": 801500,
"2021-02": 781000,
"2021-01": 759000,
"2020-12": 736500,
"2020-11": 715000,
"2020-10": 693000,
"2020-09": 671000,
"2020-08": 649000,
"2020-07": 626600,
"2020-06": 605000,
"2020-05": 582700,
"2020-04": 561100,
"2020-03": 539000,
"2020-02": 518000,
"2020-01": 496000,
"2019-12": 473400,
"2019-11": 451900,
"2019-10": 429600,
"2019-09": 408000,
"2019-08": 385700,
"2019-07": 363800,
"2019-06": 342200,
"2019-05": 320000,
"2019-04": 298400,
"2019-03": 276000,
"2019-02": 256000,
"2019-01": 233700,
"2018-12": 211400,
"2018-11": 189800,
"2018-10": 167500,
"2018-09": 145900,
"2018-08": 123700,
"2018-07": 101400,
"2018-06": 80000,
"2018-05": 57550,
"2018-04": 32000,
"2018-03": 8500
};
DateTime formatMapKey(String key) => dateFormat.parse(key);
int getHavenHeightByDate({required DateTime date}) {
String closestKey =
havenDates.keys.firstWhere((key) => formatMapKey(key).isBefore(date), orElse: () => '');
return havenDates[closestKey] ?? 0;
}
Future<int> getHavenCurrentHeight() async {
final response = await http.get(Uri.parse('https://explorer.havenprotocol.org/api/networkinfo'));
if (response.statusCode == 200) {
final info = jsonDecode(response.body);
return info['data']['height'] as int;
} else {
throw Exception('Failed to load current blockchain height');
}
}

View file

@ -2,14 +2,14 @@ part of 'haven.dart';
class CWHavenAccountList extends HavenAccountList {
CWHavenAccountList(this._wallet);
final Object _wallet;
@override
@computed
ObservableList<Account> get accounts {
final havenWallet = _wallet as HavenWallet;
final accounts = havenWallet.walletAddresses.accountList
.accounts
final accounts = havenWallet.walletAddresses.accountList.accounts
.map((acc) => Account(id: acc.id, label: acc.label))
.toList();
return ObservableList<Account>.of(accounts);
@ -43,29 +43,25 @@ class CWHavenAccountList extends HavenAccountList {
}
@override
Future<void> setLabelAccount(Object wallet, {required int accountIndex, required String label}) async {
Future<void> setLabelAccount(Object wallet,
{required int accountIndex, required String label}) async {
final havenWallet = wallet as HavenWallet;
await havenWallet.walletAddresses.accountList
.setLabelAccount(
accountIndex: accountIndex,
label: label);
.setLabelAccount(accountIndex: accountIndex, label: label);
}
}
class CWHavenSubaddressList extends MoneroSubaddressList {
CWHavenSubaddressList(this._wallet);
final Object _wallet;
@override
@computed
ObservableList<Subaddress> get subaddresses {
final havenWallet = _wallet as HavenWallet;
final subAddresses = havenWallet.walletAddresses.subaddressList
.subaddresses
.map((sub) => Subaddress(
id: sub.id,
address: sub.address,
label: sub.label))
final subAddresses = havenWallet.walletAddresses.subaddressList.subaddresses
.map((sub) => Subaddress(id: sub.id, address: sub.address, label: sub.label))
.toList();
return ObservableList<Subaddress>.of(subAddresses);
}
@ -85,20 +81,18 @@ class CWHavenSubaddressList extends MoneroSubaddressList {
@override
List<Subaddress> getAll(Object wallet) {
final havenWallet = wallet as HavenWallet;
return havenWallet.walletAddresses
.subaddressList
return havenWallet.walletAddresses.subaddressList
.getAll()
.map((sub) => Subaddress(id: sub.id, label: sub.label, address: sub.address))
.toList();
}
@override
Future<void> addSubaddress(Object wallet, {required int accountIndex, required String label}) async {
Future<void> addSubaddress(Object wallet,
{required int accountIndex, required String label}) async {
final havenWallet = wallet as HavenWallet;
await havenWallet.walletAddresses.subaddressList
.addSubaddress(
accountIndex: accountIndex,
label: label);
.addSubaddress(accountIndex: accountIndex, label: label);
}
@override
@ -106,15 +100,13 @@ class CWHavenSubaddressList extends MoneroSubaddressList {
{required int accountIndex, required int addressIndex, required String label}) async {
final havenWallet = wallet as HavenWallet;
await havenWallet.walletAddresses.subaddressList
.setLabelSubaddress(
accountIndex: accountIndex,
addressIndex: addressIndex,
label: label);
.setLabelSubaddress(accountIndex: accountIndex, addressIndex: addressIndex, label: label);
}
}
class CWHavenWalletDetails extends HavenWalletDetails {
CWHavenWalletDetails(this._wallet);
final Object _wallet;
@computed
@ -160,9 +152,10 @@ class CWHaven extends Haven {
}
@override
int getHeigthByDate({required DateTime date}) {
return getMoneroHeigthByDate(date: date);
}
int getHeightByDate({required DateTime date}) => getHavenHeightByDate(date: date);
@override
Future<int> getCurrentHeight() => getHavenCurrentHeight();
@override
TransactionPriority getDefaultTransactionPriority() {
@ -208,8 +201,8 @@ class CWHaven extends Haven {
}
@override
WalletCredentials createHavenRestoreWalletFromKeysCredentials({
required String name,
WalletCredentials createHavenRestoreWalletFromKeysCredentials(
{required String name,
required String spendKey,
required String viewKey,
required String address,
@ -227,27 +220,19 @@ class CWHaven extends Haven {
}
@override
WalletCredentials createHavenRestoreWalletFromSeedCredentials({
required String name,
WalletCredentials createHavenRestoreWalletFromSeedCredentials(
{required String name,
required String password,
required int height,
required String mnemonic}) {
return HavenRestoreWalletFromSeedCredentials(
name: name,
password: password,
height: height,
mnemonic: mnemonic);
name: name, password: password, height: height, mnemonic: mnemonic);
}
@override
WalletCredentials createHavenNewWalletCredentials({
required String name,
required String language,
String? password}) {
return HavenNewWalletCredentials(
name: name,
password: password,
language: language);
WalletCredentials createHavenNewWalletCredentials(
{required String name, required String language, String? password}) {
return HavenNewWalletCredentials(name: name, password: password, language: language);
}
@override
@ -258,16 +243,18 @@ class CWHaven extends Haven {
'privateSpendKey': keys.privateSpendKey,
'privateViewKey': keys.privateViewKey,
'publicSpendKey': keys.publicSpendKey,
'publicViewKey': keys.publicViewKey};
'publicViewKey': keys.publicViewKey
};
}
@override
Object createHavenTransactionCreationCredentials({
required List<Output> outputs,
Object createHavenTransactionCreationCredentials(
{required List<Output> outputs,
required TransactionPriority priority,
required String assetType}) {
return HavenTransactionCreationCredentials(
outputs: outputs.map((out) => OutputInfo(
outputs: outputs
.map((out) => OutputInfo(
fiatAmount: out.fiatAmount,
cryptoAmount: out.cryptoAmount,
address: out.address,
@ -339,8 +326,6 @@ class CWHaven extends Haven {
}
@override
List<AssetRate> getAssetRate()
=> getRate()
.map((rate) => AssetRate(rate.getAssetType(), rate.getRate()))
.toList();
List<AssetRate> getAssetRate() =>
getRate().map((rate) => AssetRate(rate.getAssetType(), rate.getRate())).toList();
}

View file

@ -1,6 +1,7 @@
import 'package:auto_size_text/auto_size_text.dart';
import 'package:cake_wallet/src/widgets/section_divider.dart';
import 'package:cake_wallet/utils/show_bar.dart';
import 'package:device_display_brightness/device_display_brightness.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
@ -9,6 +10,7 @@ import 'package:cake_wallet/generated/i18n.dart';
import 'package:cake_wallet/src/screens/base_page.dart';
import 'package:cake_wallet/src/widgets/list_row.dart';
import 'package:cake_wallet/view_model/wallet_keys_view_model.dart';
import 'package:cake_wallet/routes.dart';
class WalletKeysPage extends BasePage {
WalletKeysPage(this.walletKeysViewModel);
@ -18,6 +20,32 @@ class WalletKeysPage extends BasePage {
final WalletKeysViewModel walletKeysViewModel;
@override
Widget trailing(BuildContext context) => IconButton(
onPressed: () async {
// Get the current brightness:
final double brightness = await DeviceDisplayBrightness.getBrightness();
// ignore: unawaited_futures
DeviceDisplayBrightness.setBrightness(1.0);
await Navigator.pushNamed(
context,
Routes.fullscreenQR,
arguments: {
'qrData': (await walletKeysViewModel.url).toString(),
'isLight': true,
},
);
// ignore: unawaited_futures
DeviceDisplayBrightness.setBrightness(brightness);
},
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
icon: Image.asset(
'assets/images/qr_code_icon.png',
));
@override
Widget body(BuildContext context) {
return Column(

View file

@ -5,6 +5,7 @@ import 'package:cw_core/wallet_base.dart';
import 'package:cake_wallet/src/screens/transaction_details/standart_list_item.dart';
import 'package:cake_wallet/monero/monero.dart';
import 'package:cake_wallet/haven/haven.dart';
import 'package:cw_monero/api/wallet.dart' as monero_wallet;
part 'wallet_keys_view_model.g.dart';
@ -15,10 +16,11 @@ abstract class WalletKeysViewModelBase with Store {
: title = wallet.type == WalletType.bitcoin || wallet.type == WalletType.litecoin
? S.current.wallet_seed
: S.current.wallet_keys,
_wallet = wallet,
_restoreHeight = wallet.walletInfo.restoreHeight,
items = ObservableList<StandartListItem>() {
if (wallet.type == WalletType.monero) {
final keys = monero!.getKeys(wallet);
items.addAll([
if (keys['publicSpendKey'] != null)
StandartListItem(title: S.current.spend_key_public, value: keys['publicSpendKey']!),
@ -34,7 +36,6 @@ abstract class WalletKeysViewModelBase with Store {
if (wallet.type == WalletType.haven) {
final keys = haven!.getKeys(wallet);
items.addAll([
if (keys['publicSpendKey'] != null)
StandartListItem(title: S.current.spend_key_public, value: keys['publicSpendKey']!),
@ -58,4 +59,59 @@ abstract class WalletKeysViewModelBase with Store {
final ObservableList<StandartListItem> items;
final String title;
final WalletBase _wallet;
final int _restoreHeight;
Future<int?> currentHeight() async {
if (_wallet.type == WalletType.haven) {
return await haven!.getCurrentHeight();
}
if (_wallet.type == WalletType.monero) {
return monero_wallet.getCurrentHeight();
}
return null;
}
String get _path {
switch (_wallet.type) {
case WalletType.monero:
return 'monero_wallet:';
case WalletType.bitcoin:
return 'bitcoin_wallet:';
case WalletType.litecoin:
return 'litecoin_wallet:';
case WalletType.haven:
return 'haven_wallet:';
default:
throw Exception('Unexpected wallet type: ${_wallet.toString()}');
}
}
Future<String?> get restoreHeight async {
if (_restoreHeight != 0) {
return _restoreHeight.toString();
}
final _currentHeight = await currentHeight();
if (_currentHeight == null) {
return null;
}
return ((_currentHeight / 1000).floor() * 1000).toString();
}
Future<Map<String, String>> get _queryParams async {
final restoreHeightResult = await restoreHeight;
return {
'seed': _wallet.seed,
if (restoreHeightResult != null) ...{'height': restoreHeightResult}
};
}
Future<Uri> get url async {
return Uri(
path: _path,
queryParameters: await _queryParams,
);
}
}

View file

@ -402,7 +402,8 @@ abstract class Haven {
String getTransactionAddress(Object wallet, int accountIndex, int addressIndex);
int getHeigthByDate({required DateTime date});
int getHeightByDate({required DateTime date});
Future<int> getCurrentHeight();
TransactionPriority getDefaultTransactionPriority();
TransactionPriority deserializeMoneroTransactionPriority({required int raw});
List<TransactionPriority> getTransactionPriorities();