From 6688682096f0291714f711d45c617a3b7dcc82e7 Mon Sep 17 00:00:00 2001 From: fosse Date: Fri, 4 Aug 2023 09:33:48 -0400 Subject: [PATCH] change rep working --- cw_nano/lib/nano_client.dart | 42 +++++++++++++++++++ cw_nano/lib/nano_wallet.dart | 17 ++++++++ .../screens/nano/nano_change_rep_page.dart | 16 ++++++- 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/cw_nano/lib/nano_client.dart b/cw_nano/lib/nano_client.dart index 52abcd1b2..b6d8399a9 100644 --- a/cw_nano/lib/nano_client.dart +++ b/cw_nano/lib/nano_client.dart @@ -72,6 +72,48 @@ class NanoClient { } } + Future changeRep({ + required String privateKey, + required String repAddress, + required String ourAddress, + }) async { + try { + final accountInfo = await getAccountInfo(ourAddress); + + // construct the change block: + Map changeBlock = { + "type": "state", + "account": ourAddress, + "previous": accountInfo["frontier"] as String, + "representative": repAddress, + "balance": accountInfo["balance"] as String, + "link": "0000000000000000000000000000000000000000000000000000000000000000", + "link_as_account": "nano_1111111111111111111111111111111111111111111111111111hifc8npp", + }; + + // sign the change block: + final String hash = NanoBlocks.computeStateHash( + NanoAccountType.NANO, + changeBlock["account"]!, + changeBlock["previous"]!, + changeBlock["representative"]!, + BigInt.parse(changeBlock["balance"]!), + changeBlock["link"]!, + ); + final String signature = NanoSignatures.signBlock(hash, privateKey); + + // get PoW for the send block: + final String work = await requestWork(accountInfo["frontier"] as String); + + changeBlock["signature"] = signature; + changeBlock["work"] = work; + + return await processBlock(changeBlock, "change"); + } catch (e) { + throw Exception("error while changing representative"); + } + } + Future requestWork(String hash) async { return http .post( diff --git a/cw_nano/lib/nano_wallet.dart b/cw_nano/lib/nano_wallet.dart index 978da9268..1403bb484 100644 --- a/cw_nano/lib/nano_wallet.dart +++ b/cw_nano/lib/nano_wallet.dart @@ -269,6 +269,8 @@ abstract class NanoWalletBase @override String get seed => _mnemonic; + + String get representative => _representativeAddress ?? ""; @action @override @@ -350,6 +352,21 @@ abstract class NanoWalletBase } } + Future changeRep(String address) async { + try { + final String hash = await _client.changeRep( + privateKey: _privateKey, + repAddress: address, + ourAddress: _publicAddress, + ); + if (hash.isNotEmpty) { + _representativeAddress = address; + } + } catch (e) { + throw Exception("Failed to change representative address $e"); + } + } + Future? updateBalance() async => await _updateBalance(); void _onNewTransaction(FilterEvent event) { diff --git a/lib/src/screens/nano/nano_change_rep_page.dart b/lib/src/screens/nano/nano_change_rep_page.dart index 0b45f4d93..632bff1e2 100644 --- a/lib/src/screens/nano/nano_change_rep_page.dart +++ b/lib/src/screens/nano/nano_change_rep_page.dart @@ -1,8 +1,11 @@ import 'package:cake_wallet/core/address_validator.dart'; +import 'package:cake_wallet/di.dart'; import 'package:cake_wallet/src/widgets/alert_with_two_actions.dart'; import 'package:cake_wallet/src/widgets/base_text_form_field.dart'; +import 'package:cake_wallet/store/app_store.dart'; import 'package:cake_wallet/utils/show_pop_up.dart'; import 'package:cw_core/crypto_currency.dart'; +import 'package:cw_nano/nano_wallet.dart'; import 'package:flutter/material.dart'; import 'package:flutter_mobx/flutter_mobx.dart'; import 'package:cake_wallet/generated/i18n.dart'; @@ -105,8 +108,17 @@ class NanoChangeRepPage extends BasePage { false; if (confirmed) { - // _wallet. - Navigator.of(context).pop(); + try { + final wallet = getIt.get().wallet!; + if (wallet is NanoWallet) { + await wallet.changeRep(_addressController.text); + } + // TODO: show message saying success: + + Navigator.of(context).pop(); + } catch (e) { + throw e; + } } }, text: S.of(context).change,