mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-01-11 13:24:51 +00:00
change rep working
This commit is contained in:
parent
18e8142258
commit
6688682096
3 changed files with 73 additions and 2 deletions
|
@ -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 {
|
Future<String> requestWork(String hash) async {
|
||||||
return http
|
return http
|
||||||
.post(
|
.post(
|
||||||
|
|
|
@ -270,6 +270,8 @@ abstract class NanoWalletBase
|
||||||
@override
|
@override
|
||||||
String get seed => _mnemonic;
|
String get seed => _mnemonic;
|
||||||
|
|
||||||
|
String get representative => _representativeAddress ?? "";
|
||||||
|
|
||||||
@action
|
@action
|
||||||
@override
|
@override
|
||||||
Future<void> startSync() async {
|
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();
|
Future<void>? updateBalance() async => await _updateBalance();
|
||||||
|
|
||||||
void _onNewTransaction(FilterEvent event) {
|
void _onNewTransaction(FilterEvent event) {
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
import 'package:cake_wallet/core/address_validator.dart';
|
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/alert_with_two_actions.dart';
|
||||||
import 'package:cake_wallet/src/widgets/base_text_form_field.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:cake_wallet/utils/show_pop_up.dart';
|
||||||
import 'package:cw_core/crypto_currency.dart';
|
import 'package:cw_core/crypto_currency.dart';
|
||||||
|
import 'package:cw_nano/nano_wallet.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_mobx/flutter_mobx.dart';
|
import 'package:flutter_mobx/flutter_mobx.dart';
|
||||||
import 'package:cake_wallet/generated/i18n.dart';
|
import 'package:cake_wallet/generated/i18n.dart';
|
||||||
|
@ -105,8 +108,17 @@ class NanoChangeRepPage extends BasePage {
|
||||||
false;
|
false;
|
||||||
|
|
||||||
if (confirmed) {
|
if (confirmed) {
|
||||||
// _wallet.
|
try {
|
||||||
Navigator.of(context).pop();
|
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,
|
text: S.of(context).change,
|
||||||
|
|
Loading…
Reference in a new issue