lunalib 1.5.0__py3-none-any.whl → 1.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lunalib/core/wallet.py CHANGED
@@ -1010,6 +1010,182 @@ class LunaWallet:
1010
1010
  'crypto_standard': 'SM2_GB/T_32918'
1011
1011
  }
1012
1012
 
1013
+ # ============================================================================
1014
+ # UNIFIED WALLET STATE MANAGER INTEGRATION
1015
+ # ============================================================================
1016
+
1017
+ def sync_with_state_manager(self, blockchain=None, mempool=None) -> Dict:
1018
+ """
1019
+ Sync all registered wallets with the unified WalletStateManager.
1020
+ Scans blockchain once and merges mempool data for all wallets.
1021
+
1022
+ Parameters:
1023
+ blockchain: BlockchainManager instance (required)
1024
+ mempool: MempoolManager instance (required)
1025
+
1026
+ Returns: Dictionary of wallet summaries with balances and transactions
1027
+ """
1028
+ try:
1029
+ if not blockchain or not mempool:
1030
+ print("❌ blockchain and mempool instances required")
1031
+ return {}
1032
+
1033
+ from .wallet_manager import get_wallet_manager
1034
+
1035
+ state_manager = get_wallet_manager()
1036
+
1037
+ # Register all wallets with state manager if not already done
1038
+ addresses = list(self.wallets.keys())
1039
+ state_manager.register_wallets(addresses)
1040
+
1041
+ print(f"🔄 Syncing {len(addresses)} wallets...")
1042
+
1043
+ # Get data from blockchain and mempool (single scan)
1044
+ blockchain_txs = blockchain.scan_transactions_for_addresses(addresses)
1045
+ mempool_txs = mempool.get_pending_transactions_for_addresses(addresses)
1046
+
1047
+ # Sync state manager with the data
1048
+ state_manager.sync_wallets_from_sources(blockchain_txs, mempool_txs)
1049
+
1050
+ # Update LunaWallet balances from state manager
1051
+ balances = state_manager.get_all_balances()
1052
+ for address, balance_data in balances.items():
1053
+ if address in self.wallets:
1054
+ self.wallets[address]['balance'] = balance_data['confirmed_balance']
1055
+ self.wallets[address]['available_balance'] = balance_data['available_balance']
1056
+
1057
+ # Update current wallet
1058
+ if self.current_wallet_address and self.current_wallet_address in balances:
1059
+ balance_data = balances[self.current_wallet_address]
1060
+ self.balance = balance_data['confirmed_balance']
1061
+ self.available_balance = balance_data['available_balance']
1062
+
1063
+ # Return summaries
1064
+ summaries = state_manager.get_all_summaries()
1065
+ print(f"✅ Sync complete - {len(summaries)} wallets updated")
1066
+
1067
+ return summaries
1068
+
1069
+ except Exception as e:
1070
+ print(f"❌ Sync error: {e}")
1071
+ import traceback
1072
+ traceback.print_exc()
1073
+ return {}
1074
+
1075
+ def get_wallet_details(self, address: str = None) -> Optional[Dict]:
1076
+ """
1077
+ Get detailed information for a wallet including balance and transaction summary.
1078
+ If address is None, uses current wallet.
1079
+ """
1080
+ if address is None:
1081
+ address = self.current_wallet_address
1082
+
1083
+ if not address:
1084
+ return None
1085
+
1086
+ try:
1087
+ from .wallet_manager import get_wallet_manager
1088
+ state_manager = get_wallet_manager()
1089
+
1090
+ summary = state_manager.get_wallet_summary(address)
1091
+ if summary:
1092
+ return summary
1093
+
1094
+ # Fallback to basic wallet info
1095
+ if address in self.wallets:
1096
+ wallet_data = self.wallets[address]
1097
+ return {
1098
+ 'address': address,
1099
+ 'label': wallet_data.get('label', 'Wallet'),
1100
+ 'balance': wallet_data.get('balance', 0.0),
1101
+ 'available_balance': wallet_data.get('available_balance', 0.0),
1102
+ 'is_locked': wallet_data.get('is_locked', True),
1103
+ }
1104
+
1105
+ return None
1106
+
1107
+ except Exception as e:
1108
+ print(f"⚠️ Error getting wallet details: {e}")
1109
+ return None
1110
+
1111
+ def get_wallet_transactions(self, address: str = None, tx_type: str = 'all') -> List[Dict]:
1112
+ """
1113
+ Get transactions for a wallet from the state manager.
1114
+
1115
+ tx_type: 'all', 'confirmed', 'pending', 'transfers', 'rewards', 'genesis'
1116
+ """
1117
+ if address is None:
1118
+ address = self.current_wallet_address
1119
+
1120
+ if not address:
1121
+ return []
1122
+
1123
+ try:
1124
+ from .wallet_manager import get_wallet_manager
1125
+ state_manager = get_wallet_manager()
1126
+
1127
+ return state_manager.get_transactions(address, tx_type)
1128
+
1129
+ except Exception as e:
1130
+ print(f"⚠️ Error getting transactions: {e}")
1131
+ return []
1132
+
1133
+ def register_wallet_ui_callback(self, callback: Callable) -> None:
1134
+ """
1135
+ Register a callback to receive real-time wallet balance updates.
1136
+ Callback will be called with: callback(balance_data_dict)
1137
+ """
1138
+ try:
1139
+ from .wallet_manager import get_wallet_manager
1140
+ state_manager = get_wallet_manager()
1141
+ state_manager.on_balance_update(callback)
1142
+ except Exception as e:
1143
+ print(f"⚠️ Error registering callback: {e}")
1144
+
1145
+ def start_continuous_sync(self, blockchain=None, mempool=None, poll_interval: int = 30) -> None:
1146
+ """
1147
+ Start continuous synchronization in background thread.
1148
+ Syncs every poll_interval seconds.
1149
+ """
1150
+ if not blockchain or not mempool:
1151
+ print("❌ blockchain and mempool instances required")
1152
+ return
1153
+
1154
+ try:
1155
+ from .wallet_manager import get_wallet_manager
1156
+ from .wallet_sync_helper import WalletSyncHelper
1157
+
1158
+ state_manager = get_wallet_manager()
1159
+
1160
+ # Register wallets
1161
+ addresses = list(self.wallets.keys())
1162
+ state_manager.register_wallets(addresses)
1163
+
1164
+ def sync_callback(balance_data):
1165
+ """Update LunaWallet when state manager updates"""
1166
+ for address, balance_info in balance_data.items():
1167
+ if address in self.wallets:
1168
+ self.wallets[address]['balance'] = balance_info['confirmed_balance']
1169
+ self.wallets[address]['available_balance'] = balance_info['available_balance']
1170
+
1171
+ if address == self.current_wallet_address:
1172
+ self.balance = balance_info['confirmed_balance']
1173
+ self.available_balance = balance_info['available_balance']
1174
+
1175
+ # Create sync helper
1176
+ sync_helper = WalletSyncHelper(self, blockchain, mempool)
1177
+
1178
+ # Start continuous sync with callback
1179
+ sync_helper.start_continuous_sync(
1180
+ poll_interval,
1181
+ on_balance_update=sync_callback
1182
+ )
1183
+
1184
+ print(f"🔄 Started continuous sync (interval: {poll_interval}s)")
1185
+
1186
+ except Exception as e:
1187
+ print(f"❌ Error starting continuous sync: {e}")
1188
+
1013
1189
  def save_to_file(self, filename=None):
1014
1190
  """Save wallet to file"""
1015
1191
  if not self.data_dir: