change rep working

This commit is contained in:
fosse 2023-08-04 09:33:48 -04:00
parent 18e8142258
commit 6688682096
3 changed files with 73 additions and 2 deletions

View file

@ -72,6 +72,48 @@ class NanoClient {
}
}
Future<String> changeRep({
required String privateKey,
required String repAddress,
required String ourAddress,
}) async {
try {
final accountInfo = await getAccountInfo(ourAddress);
// construct the change block:
Map<String, String> 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<String> requestWork(String hash) async {
return http
.post(

View file

@ -270,6 +270,8 @@ abstract class NanoWalletBase
@override
String get seed => _mnemonic;
String get representative => _representativeAddress ?? "";
@action
@override
Future<void> startSync() async {
@ -350,6 +352,21 @@ abstract class NanoWalletBase
}
}
Future<void> 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<void>? updateBalance() async => await _updateBalance();
void _onNewTransaction(FilterEvent event) {

View file

@ -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<AppStore>().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,