stack_wallet/lib/pages/paynym/paynym_claim_view.dart

309 lines
11 KiB
Dart
Raw Normal View History

2023-05-26 21:21:16 +00:00
/*
* This file is part of Stack Wallet.
*
* Copyright (c) 2023 Cypher Stack
* All Rights Reserved.
* The code is distributed under GPLv3 license, see LICENSE file for details.
* Generated by Cypher Stack on 2023-05-26
*
*/
2022-12-20 21:05:11 +00:00
import 'dart:async';
2022-12-20 20:48:31 +00:00
import 'package:flutter/material.dart';
2022-12-20 23:00:03 +00:00
import 'package:flutter_riverpod/flutter_riverpod.dart';
2023-01-04 16:08:05 +00:00
import 'package:flutter_svg/flutter_svg.dart';
2023-04-25 21:06:12 +00:00
import 'package:stackwallet/models/paynym/paynym_account.dart';
2022-12-20 23:00:03 +00:00
import 'package:stackwallet/pages/paynym/dialogs/claiming_paynym_dialog.dart';
2022-12-21 23:02:14 +00:00
import 'package:stackwallet/pages/paynym/paynym_home_view.dart';
import 'package:stackwallet/pages/wallet_view/wallet_view.dart';
2022-12-20 23:00:03 +00:00
import 'package:stackwallet/providers/global/paynym_api_provider.dart';
import 'package:stackwallet/providers/global/wallets_provider.dart';
import 'package:stackwallet/providers/wallet/my_paynym_account_state_provider.dart';
import 'package:stackwallet/services/mixins/paynym_wallet_interface.dart';
import 'package:stackwallet/themes/stack_colors.dart';
2022-12-20 20:48:31 +00:00
import 'package:stackwallet/utilities/assets.dart';
import 'package:stackwallet/utilities/text_styles.dart';
import 'package:stackwallet/utilities/util.dart';
2023-01-04 16:08:05 +00:00
import 'package:stackwallet/widgets/conditional_parent.dart';
2022-12-20 20:48:31 +00:00
import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart';
2023-01-04 16:08:05 +00:00
import 'package:stackwallet/widgets/desktop/desktop_app_bar.dart';
2022-12-20 20:48:31 +00:00
import 'package:stackwallet/widgets/desktop/desktop_scaffold.dart';
import 'package:stackwallet/widgets/desktop/primary_button.dart';
2022-12-20 23:00:03 +00:00
class PaynymClaimView extends ConsumerStatefulWidget {
const PaynymClaimView({
Key? key,
required this.walletId,
}) : super(key: key);
2022-12-20 21:05:11 +00:00
2022-12-20 23:00:03 +00:00
final String walletId;
2022-12-20 20:48:31 +00:00
static const String routeName = "/claimPaynym";
@override
2022-12-20 23:00:03 +00:00
ConsumerState<PaynymClaimView> createState() => _PaynymClaimViewState();
2022-12-20 20:48:31 +00:00
}
2022-12-20 23:00:03 +00:00
class _PaynymClaimViewState extends ConsumerState<PaynymClaimView> {
2023-04-25 21:06:12 +00:00
Future<bool> _addSegwitCode(PaynymAccount myAccount) async {
final manager = ref.read(pWallets).getManager(widget.walletId);
2023-04-25 21:06:12 +00:00
// get wallet to access paynym calls
final wallet = manager.wallet as PaynymWalletInterface;
final token = await ref
.read(paynymAPIProvider)
.token(myAccount.nonSegwitPaymentCode.code);
2023-04-25 21:06:12 +00:00
final signature = await wallet.signStringWithNotificationKey(token.value!);
final pCodeSegwit = await wallet.getPaymentCode(isSegwit: true);
final addResult = await ref.read(paynymAPIProvider).add(
token.value!,
signature,
myAccount.nymID,
pCodeSegwit.toString(),
);
return addResult.value ?? false;
}
2022-12-20 20:48:31 +00:00
@override
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");
final isDesktop = Util.isDesktop;
return MasterScaffold(
isDesktop: isDesktop,
2023-01-04 16:08:05 +00:00
appBar: isDesktop
? DesktopAppBar(
isCompactHeight: true,
background: Theme.of(context).extension<StackColors>()!.popupBG,
leading: Row(
children: [
2023-01-04 16:57:14 +00:00
Padding(
padding: const EdgeInsets.only(
left: 24,
right: 20,
),
child: AppBarIconButton(
size: 32,
color: Theme.of(context)
.extension<StackColors>()!
.textFieldDefaultBG,
shadows: const [],
icon: SvgPicture.asset(
Assets.svg.arrowLeft,
width: 18,
height: 18,
color: Theme.of(context)
.extension<StackColors>()!
.topNavIconPrimary,
),
onPressed: Navigator.of(context).pop,
),
2023-01-04 16:08:05 +00:00
),
SvgPicture.asset(
Assets.svg.user,
width: 42,
height: 42,
color: Theme.of(context).extension<StackColors>()!.textDark,
),
const SizedBox(
width: 10,
),
Text(
"PayNym",
style: STextStyles.desktopH3(context),
)
],
),
)
: AppBar(
leading: const AppBarBackButton(),
titleSpacing: 0,
title: Text(
"PayNym",
style: STextStyles.navBarTitle(context),
overflow: TextOverflow.ellipsis,
),
),
body: ConditionalParent(
condition: !isDesktop,
builder: (child) => SafeArea(
child: Padding(
padding: const EdgeInsets.all(16),
child: child,
),
2022-12-20 20:48:31 +00:00
),
2023-01-04 16:08:05 +00:00
child: ConditionalParent(
condition: isDesktop,
builder: (child) => SizedBox(
width: 328,
child: child,
),
2022-12-20 20:48:31 +00:00
child: Column(
children: [
const Spacer(
flex: 1,
),
2023-05-19 20:11:01 +00:00
SvgPicture.asset(
Assets.svg.unclaimedPaynym,
2022-12-20 20:48:31 +00:00
width: MediaQuery.of(context).size.width / 2,
),
const SizedBox(
height: 20,
),
Text(
"You do not have a PayNym yet.\nClaim yours now!",
2023-01-04 16:08:05 +00:00
style: isDesktop
? STextStyles.desktopSubtitleH2(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
)
: STextStyles.baseXS(context).copyWith(
color: Theme.of(context)
.extension<StackColors>()!
.textSubtitle1,
),
2022-12-20 20:48:31 +00:00
textAlign: TextAlign.center,
),
2023-01-04 16:08:05 +00:00
if (isDesktop)
const SizedBox(
height: 30,
),
if (!isDesktop)
const Spacer(
flex: 2,
),
2022-12-20 20:48:31 +00:00
PrimaryButton(
label: "Claim",
2022-12-20 21:05:11 +00:00
onPressed: () async {
bool shouldCancel = false;
unawaited(
showDialog<bool?>(
context: context,
2022-12-21 19:46:50 +00:00
barrierDismissible: false,
2022-12-20 21:05:11 +00:00
builder: (context) => const ClaimingPaynymDialog(),
).then((value) => shouldCancel = value == true),
);
final manager =
ref.read(pWallets).getManager(widget.walletId);
// get wallet to access paynym calls
final wallet = manager.wallet as PaynymWalletInterface;
2022-12-21 16:17:53 +00:00
if (shouldCancel) return;
2022-12-21 16:17:53 +00:00
// get payment code
2023-04-25 21:06:12 +00:00
final pCode = await wallet.getPaymentCode(isSegwit: false);
2022-12-20 23:00:03 +00:00
if (shouldCancel) return;
2022-12-21 16:17:53 +00:00
// attempt to create new entry in paynym.is db
final created = await ref
2022-12-20 23:00:03 +00:00
.read(paynymAPIProvider)
.create(pCode.toString());
2022-12-21 23:02:14 +00:00
debugPrint("created:$created");
if (shouldCancel) return;
2022-12-21 23:02:14 +00:00
if (created.value!.claimed) {
2022-12-21 16:17:53 +00:00
// payment code already claimed
debugPrint("pcode already claimed!!");
2023-04-25 21:06:12 +00:00
final account =
await ref.read(paynymAPIProvider).nym(pCode.toString());
if (!account.value!.segwit) {
for (int i = 0; i < 100; i++) {
final result = await _addSegwitCode(account.value!);
if (result == true) {
break;
}
}
}
2022-12-21 23:02:14 +00:00
if (mounted) {
2023-01-04 16:08:05 +00:00
if (isDesktop) {
Navigator.of(context, rootNavigator: true).pop();
Navigator.of(context).pop();
} else {
Navigator.of(context).popUntil(
ModalRoute.withName(
WalletView.routeName,
),
);
}
2022-12-21 23:02:14 +00:00
}
2022-12-21 16:17:53 +00:00
return;
}
if (shouldCancel) return;
2022-12-21 23:02:14 +00:00
final token =
await ref.read(paynymAPIProvider).token(pCode.toString());
2022-12-21 16:17:53 +00:00
if (shouldCancel) return;
2022-12-21 16:17:53 +00:00
// sign token with notification private key
2022-12-21 23:02:14 +00:00
final signature =
await wallet.signStringWithNotificationKey(token.value!);
2022-12-21 16:17:53 +00:00
if (shouldCancel) return;
2022-12-21 16:17:53 +00:00
// claim paynym account
final claim = await ref
.read(paynymAPIProvider)
.claim(token.value!, signature);
2022-12-21 16:17:53 +00:00
if (shouldCancel) return;
if (claim.value?.claimed == pCode.toString()) {
2022-12-21 23:02:14 +00:00
final account =
await ref.read(paynymAPIProvider).nym(pCode.toString());
2023-04-25 21:06:12 +00:00
if (!account.value!.segwit) {
for (int i = 0; i < 100; i++) {
final result = await _addSegwitCode(account.value!);
if (result == true) {
break;
}
}
}
2022-12-21 23:02:14 +00:00
ref.read(myPaynymAccountStateProvider.state).state =
account.value!;
2022-12-21 23:02:14 +00:00
if (mounted) {
2023-01-04 16:08:05 +00:00
if (isDesktop) {
Navigator.of(context, rootNavigator: true).pop();
Navigator.of(context).pop();
} else {
Navigator.of(context).popUntil(
ModalRoute.withName(
WalletView.routeName,
),
);
}
2022-12-21 23:02:14 +00:00
await Navigator.of(context).pushNamed(
PaynymHomeView.routeName,
arguments: widget.walletId,
);
}
} else if (mounted && !shouldCancel) {
2023-01-04 16:08:05 +00:00
Navigator.of(context, rootNavigator: isDesktop).pop();
2022-12-20 21:05:11 +00:00
}
2022-12-20 20:48:31 +00:00
},
),
2023-01-04 16:08:05 +00:00
if (isDesktop)
const Spacer(
flex: 2,
),
2022-12-20 20:48:31 +00:00
],
),
),
),
);
}
}