Chat sends move 0 value, so the network fee is structurally load-bearing (it's the only thing that forces a real shielded input; 0-value + 0-fee builds a degenerate, unrelayable tx). Three gaps addressed: 1. Fee floor — broadcastChatMemos now uses max(getDefaultFee(), kChatMinFeeDrgx), so a 0 / too-low global default-fee setting can't silently break chat. 2. Real delivery status — the echo was marked Sent on SUBMIT regardless of the on-chain outcome (the z_sendmany callback was empty), so failures were invisible and the Retry affordance never fired for async failures. Add a third ChatDelivery::Sending state (appended so persisted 0=Sent stays valid); record the echo in-memory as Sending, and resolve it to Sent/Failed from the z_sendmany completion callback — persisting only the final status (so a restart never shows a stuck spinner; a stray persisted Sending loads as Sent). A subtle "sending…" label shows while in flight. 3. Pay-from-funded + pre-check — z_sendmany spends from one z-address, and the identity reply address may be unfunded while funds sit elsewhere. chatPayFromZaddr picks a spendable z-address that can cover the fee (preferring the identity address); the memo still advertises the identity address as reply-to, so paying from a different note is transport-transparent. If nothing can cover the fee, a clear "need a small shielded balance" toast replaces the cryptic failure. Full node only for the callback path; lite resolves optimistically on queue. 8-language strings + CJK subset (+1 glyph 賄). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
124 lines
5.0 KiB
C++
124 lines
5.0 KiB
C++
// DragonX Wallet - HushChat service (implementation).
|
|
|
|
#include "chat_service.h"
|
|
|
|
#include "chat_database.h"
|
|
#include "chat_identity.h" // chatIdentityPublicKeyHex
|
|
|
|
#include <utility>
|
|
|
|
namespace dragonx::chat {
|
|
|
|
ChatService::~ChatService() {
|
|
clearIdentity();
|
|
}
|
|
|
|
void ChatService::setIdentity(const ChatKeyPair& keys) {
|
|
identity_ = keys;
|
|
has_identity_ = true;
|
|
}
|
|
|
|
void ChatService::clearIdentity() {
|
|
wipeChatKeyPair(identity_);
|
|
has_identity_ = false;
|
|
}
|
|
|
|
int ChatService::ingest(const std::vector<HushChatTransactionMetadata>& metadata,
|
|
const std::unordered_map<std::string, std::int64_t>& txTimestamps,
|
|
std::int64_t fallbackTimestamp,
|
|
std::vector<std::string>* newIncomingCids) {
|
|
if (!has_identity_) return 0;
|
|
|
|
int added = 0;
|
|
for (const auto& meta : metadata) {
|
|
ChatMessage message;
|
|
message.direction = ChatDirection::Incoming;
|
|
message.txid = meta.txid;
|
|
message.conversation_id = meta.conversation_id;
|
|
message.peer_zaddr = meta.reply_zaddr;
|
|
message.peer_public_key_hex = meta.sender_public_key_hex;
|
|
const auto timeIt = txTimestamps.find(meta.txid);
|
|
message.timestamp = timeIt != txTimestamps.end() ? timeIt->second : fallbackTimestamp;
|
|
message.payload_position = meta.payload_position;
|
|
|
|
if (meta.type == HushChatHeaderType::ContactRequest) {
|
|
message.kind = ChatMessageKind::ContactRequest;
|
|
message.body = meta.payload_memo; // plaintext request text
|
|
} else {
|
|
message.kind = ChatMessageKind::Message;
|
|
std::string plaintext;
|
|
const ChatCryptoStatus status = decryptIncoming(
|
|
identity_, meta.sender_public_key_hex, meta.secretstream_header_hex,
|
|
meta.payload_memo, plaintext);
|
|
if (status != ChatCryptoStatus::Ok) continue; // drop undecryptable silently
|
|
message.body = std::move(plaintext);
|
|
}
|
|
|
|
// In-memory store dedups (txid+position); only persist the genuinely new ones. On the next
|
|
// session loadFromDatabase() repopulates the store, so re-scanning the chain re-ingests but
|
|
// the store dedup prevents a duplicate write.
|
|
if (store_.append(message)) {
|
|
if (db_) db_->append(message);
|
|
++added;
|
|
// Every ingested message is incoming — report its cid so the caller can notify without
|
|
// relying on a seen-watermark delta (which block-time vs wall-clock skew can swallow).
|
|
if (newIncomingCids) newIncomingCids->push_back(message.conversation_id);
|
|
}
|
|
}
|
|
return added;
|
|
}
|
|
|
|
void ChatService::loadFromDatabase() {
|
|
if (!db_) return;
|
|
for (const auto& message : db_->load()) {
|
|
store_.append(message);
|
|
}
|
|
}
|
|
|
|
std::string ChatService::identityPublicKeyHex() const {
|
|
if (!has_identity_) return {};
|
|
return chatIdentityPublicKeyHex(identity_);
|
|
}
|
|
|
|
ChatComposeStatus ChatService::composeMessage(const std::string& myReplyZaddr,
|
|
const std::string& peerPublicKeyHex,
|
|
const std::string& peerZaddr,
|
|
const std::string& conversationId,
|
|
const std::string& plaintext,
|
|
OutgoingChatMemos& out) const {
|
|
if (!has_identity_) return ChatComposeStatus::MissingField;
|
|
return buildOutgoingMessage(identity_, chatIdentityPublicKeyHex(identity_), myReplyZaddr,
|
|
peerPublicKeyHex, peerZaddr, conversationId, plaintext, out);
|
|
}
|
|
|
|
ChatComposeStatus ChatService::composeContactRequest(const std::string& myReplyZaddr,
|
|
const std::string& peerZaddr,
|
|
const std::string& conversationId,
|
|
const std::string& requestText,
|
|
OutgoingChatMemos& out) const {
|
|
if (!has_identity_) return ChatComposeStatus::MissingField;
|
|
return buildOutgoingContactRequest(chatIdentityPublicKeyHex(identity_), myReplyZaddr,
|
|
peerZaddr, conversationId, requestText, out);
|
|
}
|
|
|
|
bool ChatService::recordOutgoing(const ChatMessage& message) {
|
|
if (store_.append(message)) {
|
|
if (db_) db_->append(message);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
bool ChatService::recordOutgoingPending(const ChatMessage& message) {
|
|
// In-memory only — deliberately NOT persisted yet, so "Sending" never survives to a restart; the
|
|
// row is written by resolveOutgoing() once the broadcast resolves to Sent/Failed.
|
|
return store_.append(message);
|
|
}
|
|
|
|
void ChatService::resolveOutgoing(const std::string& txid, ChatDelivery delivery) {
|
|
const ChatMessage* updated = store_.updateDelivery(txid, delivery);
|
|
if (updated && db_) db_->append(*updated); // first persist, carrying the final status
|
|
}
|
|
|
|
} // namespace dragonx::chat
|