mirror of
https://github.com/cake-tech/cake_wallet.git
synced 2025-06-28 12:29:51 +00:00
Cw 565 sign messages (#1378)
* version bump to 3.13.9, auth working on mac * bump flutter version in workflow file * workflow fix * test fix * downgrade flutter version * test fix * test fix * update gradle version * start working on ui for message signing * updates * sign working for a few wallet types * updates & verification for electrum currencies * nano support * sign/verify working on eth, bitcoin broken * update translations * Implement Verify Message for Monero * save [skip ci] * pub key extraction working * fixes for electrum signing * verify working for solana! * electrum still not working :( [skip ci] * electrum messages working! * fixes for updated dart version, localization file updates * remove accidental inclusion * missed some unimplemented throws * Update res/values/strings_de.arb Co-authored-by: Konstantin Ullrich <konstantinullrich12@gmail.com> * Apply suggestions from code review Co-authored-by: Konstantin Ullrich <konstantinullrich12@gmail.com> * review suggestions and updates [skip ci] * [skip ci] add polygon * [skip ci] merge mac-auth/update version * fix litecoin * bio auth mac fix * remove comment and change duration from 2 to 0 * cherry pick previous changes * litecoin fixes, sign form fixes, use new walletAddressPicker * support accounts * verify messages working for monero * working sign and verify messages for nano * electrum signing working [skip ci] * additional nano fixes * update translations * attempt to decode signatures with base64 * workaround for secure storage bug on mac * bump version to 3.19.5 (because breez will need this version anyways) * some code cleanup * some changess didn't get saved * just documenting the issue [skip ci] * undo accidental removal + minor code cleanup * merge conflicts * merge fixes [skip ci] * add tron support * [wip] fixing * remove duplicate references to electrum path for maintainability * fixes * minor fix * fixes * undo debug comment * update migration for all electrum based wallets * hotfixes * copy over the rest of the fixes * minor code cleanup [skip ci] * updates * electrum signing workinggit statusgit statusgit statusgit status! * copy same fixes for litecoin * litecoin fixes * add v to litecoin signatures * fix dependencies * fix bitcoin_base version * merge fix * dep override * fix conflicts with main * trial fix for android build * fixes * fix * dep fix, should build * fix signing for bitcoin cash * [skip ci] minor code cleanup * [skip ci] minor code cleanup 2 * forgot wonero, various other fixes * more fixes * fix solana (untested) --------- Co-authored-by: Konstantin Ullrich <konstantinullrich12@gmail.com> Co-authored-by: Omar Hatem <omarh.ismail1@gmail.com>
This commit is contained in:
parent
eef319658a
commit
83ef61e928
65 changed files with 1479 additions and 271 deletions
|
@ -2,11 +2,11 @@ import 'dart:async';
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:cw_core/nano_account_info_response.dart';
|
||||
import 'package:cw_nano/nano_block_info_response.dart';
|
||||
import 'package:cw_core/n2_node.dart';
|
||||
import 'package:cw_nano/nano_balance.dart';
|
||||
import 'package:cw_nano/nano_transaction_model.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:nanodart/nanodart.dart';
|
||||
import 'package:cw_core/node.dart';
|
||||
import 'package:nanoutil/nanoutil.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
@ -111,6 +111,27 @@ class NanoClient {
|
|||
}
|
||||
}
|
||||
|
||||
Future<BlockContentsResponse?> getBlockContents(String block) async {
|
||||
try {
|
||||
final response = await http.post(
|
||||
_node!.uri,
|
||||
headers: CAKE_HEADERS,
|
||||
body: jsonEncode(
|
||||
{
|
||||
"action": "block_info",
|
||||
"json_block": "true",
|
||||
"hash": block,
|
||||
},
|
||||
),
|
||||
);
|
||||
final data = await jsonDecode(response.body);
|
||||
return BlockContentsResponse.fromJson(data["contents"] as Map<String, dynamic>);
|
||||
} catch (e) {
|
||||
print("error while getting block info $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> changeRep({
|
||||
required String privateKey,
|
||||
required String repAddress,
|
||||
|
@ -135,8 +156,8 @@ class NanoClient {
|
|||
};
|
||||
|
||||
// sign the change block:
|
||||
final String hash = NanoBlocks.computeStateHash(
|
||||
NanoAccountType.NANO,
|
||||
final String hash = NanoSignatures.computeStateHash(
|
||||
NanoBasedCurrency.NANO,
|
||||
changeBlock["account"]!,
|
||||
changeBlock["previous"]!,
|
||||
changeBlock["representative"]!,
|
||||
|
@ -248,7 +269,7 @@ class NanoClient {
|
|||
}
|
||||
final String representative = infoResponse.representative;
|
||||
// link = destination address:
|
||||
final String link = NanoAccounts.extractPublicKey(destinationAddress);
|
||||
final String link = NanoDerivations.addressToPublicKey(destinationAddress);
|
||||
final String linkAsAccount = destinationAddress;
|
||||
|
||||
// construct the send block:
|
||||
|
@ -262,8 +283,8 @@ class NanoClient {
|
|||
};
|
||||
|
||||
// sign the send block:
|
||||
final String hash = NanoBlocks.computeStateHash(
|
||||
NanoAccountType.NANO,
|
||||
final String hash = NanoSignatures.computeStateHash(
|
||||
NanoBasedCurrency.NANO,
|
||||
sendBlock["account"]!,
|
||||
sendBlock["previous"]!,
|
||||
sendBlock["representative"]!,
|
||||
|
@ -285,7 +306,6 @@ class NanoClient {
|
|||
|
||||
Future<void> receiveBlock({
|
||||
required String blockHash,
|
||||
required String source,
|
||||
required String amountRaw,
|
||||
required String destinationAddress,
|
||||
required String privateKey,
|
||||
|
@ -310,15 +330,56 @@ class NanoClient {
|
|||
representative = infoData.representative;
|
||||
}
|
||||
|
||||
if ((BigInt.tryParse(amountRaw) ?? BigInt.zero) <= BigInt.zero) {
|
||||
throw Exception("amountRaw must be greater than zero");
|
||||
}
|
||||
|
||||
BlockContentsResponse? frontierContents;
|
||||
|
||||
if (!openBlock) {
|
||||
// get the block info of the frontier block:
|
||||
frontierContents = await getBlockContents(frontier);
|
||||
|
||||
if (frontierContents == null) {
|
||||
throw Exception("error while getting frontier block info");
|
||||
}
|
||||
|
||||
final String frontierHash = NanoSignatures.computeStateHash(
|
||||
NanoBasedCurrency.NANO,
|
||||
frontierContents.account,
|
||||
frontierContents.previous,
|
||||
frontierContents.representative,
|
||||
BigInt.parse(frontierContents.balance),
|
||||
frontierContents.link,
|
||||
);
|
||||
|
||||
bool valid = await NanoSignatures.verify(
|
||||
frontierHash,
|
||||
frontierContents.signature,
|
||||
destinationAddress,
|
||||
);
|
||||
|
||||
if (!valid) {
|
||||
throw Exception(
|
||||
"Frontier block signature is invalid! Potentially malicious block detected!");
|
||||
}
|
||||
}
|
||||
|
||||
// first get the account balance:
|
||||
final BigInt currentBalance = (await getBalance(destinationAddress)).currentBalance;
|
||||
late BigInt currentBalance;
|
||||
if (!openBlock) {
|
||||
currentBalance = BigInt.parse(frontierContents!.balance);
|
||||
} else {
|
||||
currentBalance = BigInt.zero;
|
||||
}
|
||||
final BigInt txAmount = BigInt.parse(amountRaw);
|
||||
final BigInt balanceAfterTx = currentBalance + txAmount;
|
||||
|
||||
// link = send block hash:
|
||||
final String link = blockHash;
|
||||
// this "linkAsAccount" is meaningless:
|
||||
final String linkAsAccount = NanoAccounts.createAccount(NanoAccountType.NANO, blockHash);
|
||||
final String linkAsAccount =
|
||||
NanoDerivations.publicKeyToAddress(blockHash, currency: NanoBasedCurrency.NANO);
|
||||
|
||||
// construct the receive block:
|
||||
Map<String, String> receiveBlock = {
|
||||
|
@ -332,8 +393,8 @@ class NanoClient {
|
|||
};
|
||||
|
||||
// sign the receive block:
|
||||
final String hash = NanoBlocks.computeStateHash(
|
||||
NanoAccountType.NANO,
|
||||
final String hash = NanoSignatures.computeStateHash(
|
||||
NanoBasedCurrency.NANO,
|
||||
receiveBlock["account"]!,
|
||||
receiveBlock["previous"]!,
|
||||
receiveBlock["representative"]!,
|
||||
|
@ -345,7 +406,7 @@ class NanoClient {
|
|||
// get PoW for the receive block:
|
||||
String? work;
|
||||
if (openBlock) {
|
||||
work = await requestWork(NanoAccounts.extractPublicKey(destinationAddress));
|
||||
work = await requestWork(NanoDerivations.addressToPublicKey(destinationAddress));
|
||||
} else {
|
||||
work = await requestWork(frontier);
|
||||
}
|
||||
|
@ -409,10 +470,8 @@ class NanoClient {
|
|||
for (final blockHash in blocks.keys) {
|
||||
final block = blocks[blockHash];
|
||||
final String amountRaw = block["amount"] as String;
|
||||
final String source = block["source"] as String;
|
||||
await receiveBlock(
|
||||
blockHash: blockHash,
|
||||
source: source,
|
||||
amountRaw: amountRaw,
|
||||
privateKey: privateKey,
|
||||
destinationAddress: destinationAddress,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue