lunalib 1.1.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.
@@ -0,0 +1,172 @@
1
+ import time
2
+ import hashlib
3
+ from typing import Dict, Tuple
4
+
5
+ class TransactionSecurity:
6
+ """Enhanced transaction security system"""
7
+
8
+ def __init__(self):
9
+ self.min_transaction_amount = 0.000001
10
+ self.max_transaction_amount = 100000000
11
+ self.required_fee = 0.00001
12
+ self.rate_limits = {}
13
+ self.blacklisted_addresses = set()
14
+
15
+ def validate_transaction_security(self, transaction: Dict) -> Tuple[bool, str]:
16
+ """Comprehensive transaction security validation"""
17
+ tx_type = transaction.get("type", "").lower()
18
+
19
+ if tx_type == "gtx_genesis":
20
+ return self._validate_genesis_transaction(transaction)
21
+ elif tx_type == "reward":
22
+ return self._validate_reward_transaction(transaction)
23
+ elif tx_type == "transfer":
24
+ return self._validate_transfer_transaction(transaction)
25
+ else:
26
+ return False, f"Unknown transaction type: {tx_type}"
27
+
28
+ def _validate_genesis_transaction(self, transaction: Dict) -> Tuple[bool, str]:
29
+ """Validate GTX Genesis transaction"""
30
+ required_fields = ["bill_serial", "denomination", "mining_difficulty", "hash", "nonce"]
31
+ for field in required_fields:
32
+ if field not in transaction:
33
+ return False, f"Missing GTX field: {field}"
34
+
35
+ # Validate denomination
36
+ denomination = transaction.get("denomination")
37
+ valid_denominations = [1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000]
38
+ if denomination not in valid_denominations:
39
+ return False, f"Invalid denomination: {denomination}"
40
+
41
+ # Validate mining proof
42
+ if not self._validate_mining_proof(transaction):
43
+ return False, "Invalid mining proof"
44
+
45
+ return True, "Valid GTX Genesis transaction"
46
+
47
+ def _validate_reward_transaction(self, transaction: Dict) -> Tuple[bool, str]:
48
+ """Validate reward transaction"""
49
+ required_fields = ["from", "to", "amount", "block_height", "hash"]
50
+ for field in required_fields:
51
+ if field not in transaction:
52
+ return False, f"Missing reward field: {field}"
53
+
54
+ # Only network can create rewards
55
+ if transaction.get("from") != "network":
56
+ return False, "Unauthorized reward creation"
57
+
58
+ return True, "Valid reward transaction"
59
+
60
+ def _validate_transfer_transaction(self, transaction: Dict) -> Tuple[bool, str]:
61
+ """Validate transfer transaction"""
62
+ required_fields = ["from", "to", "amount", "signature", "public_key", "nonce"]
63
+ for field in required_fields:
64
+ if field not in transaction:
65
+ return False, f"Missing field: {field}"
66
+
67
+ # Amount validation
68
+ amount = transaction.get("amount", 0)
69
+ if amount < self.min_transaction_amount:
70
+ return False, f"Amount below minimum: {self.min_transaction_amount}"
71
+ if amount > self.max_transaction_amount:
72
+ return False, f"Amount above maximum: {self.max_transaction_amount}"
73
+
74
+ # Fee validation
75
+ fee = transaction.get("fee", 0)
76
+ if fee < self.required_fee:
77
+ return False, f"Insufficient fee: {fee} (required: {self.required_fee})"
78
+
79
+ # Signature validation
80
+ if not self._validate_signature(transaction):
81
+ return False, "Invalid signature"
82
+
83
+ # Anti-spam checks
84
+ from_address = transaction.get("from", "")
85
+ if not self._check_rate_limit(from_address):
86
+ return False, "Rate limit exceeded"
87
+
88
+ if self._is_blacklisted(from_address):
89
+ return False, "Address is blacklisted"
90
+
91
+ return True, "Valid transfer transaction"
92
+
93
+ def _validate_mining_proof(self, transaction: Dict) -> bool:
94
+ """Validate mining proof-of-work"""
95
+ try:
96
+ difficulty = transaction.get("mining_difficulty", 0)
97
+ bill_hash = transaction.get("hash", "")
98
+
99
+ # Verify difficulty requirement
100
+ target = "0" * difficulty
101
+ return bill_hash.startswith(target)
102
+ except:
103
+ return False
104
+
105
+ def _validate_signature(self, transaction: Dict) -> bool:
106
+ """Validate transaction signature"""
107
+ try:
108
+ signature = transaction.get("signature", "")
109
+ public_key = transaction.get("public_key", "")
110
+
111
+ # Basic format validation
112
+ if len(signature) != 64:
113
+ return False
114
+
115
+ # In production, use proper ECDSA verification
116
+ # For now, simplified check
117
+ return all(c in "0123456789abcdef" for c in signature.lower())
118
+ except:
119
+ return False
120
+
121
+ def _check_rate_limit(self, address: str) -> bool:
122
+ """Check transaction rate limiting"""
123
+ now = time.time()
124
+
125
+ if address not in self.rate_limits:
126
+ self.rate_limits[address] = []
127
+
128
+ # Remove old entries (older than 1 minute)
129
+ self.rate_limits[address] = [t for t in self.rate_limits[address] if now - t < 60]
130
+
131
+ # Check if over limit (10 transactions per minute)
132
+ if len(self.rate_limits[address]) >= 10:
133
+ return False
134
+
135
+ self.rate_limits[address].append(now)
136
+ return True
137
+
138
+ def _is_blacklisted(self, address: str) -> bool:
139
+ """Check if address is blacklisted"""
140
+ return address.lower() in self.blacklisted_addresses
141
+
142
+ def blacklist_address(self, address: str):
143
+ """Add address to blacklist"""
144
+ self.blacklisted_addresses.add(address.lower())
145
+
146
+ def calculate_security_score(self, transaction: Dict) -> int:
147
+ """Calculate security score for transaction"""
148
+ score = 0
149
+
150
+ # Signature strength
151
+ signature = transaction.get("signature", "")
152
+ if len(signature) == 64:
153
+ score += 40
154
+
155
+ # Public key presence
156
+ if transaction.get("public_key"):
157
+ score += 20
158
+
159
+ # Timestamp freshness
160
+ timestamp = transaction.get("timestamp", 0)
161
+ if time.time() - timestamp < 600: # 10 minutes
162
+ score += 20
163
+
164
+ # Nonce uniqueness
165
+ if transaction.get("nonce"):
166
+ score += 10
167
+
168
+ # Additional security features
169
+ if transaction.get("security_hash"):
170
+ score += 10
171
+
172
+ return min(score, 100)