File size: 6,026 Bytes
edbc049
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
"""
ERC721 Approve Validator

Validate ERC721 NFT approve operation execution.
"""

from typing import Dict, Any


class ERC721ApproveValidator:
    """Validate ERC721 approval operation"""
    
    def __init__(
        self,
        nft_address: str,
        spender_address: str,
        token_id: int
    ):
        self.nft_address = nft_address.lower()
        self.spender_address = spender_address.lower()
        self.token_id = token_id
        
        # approve(address,uint256) function selector
        self.expected_selector = '0x095ea7b3'
        
        # Total 100 points
        self.max_score = 100
    
    def validate(
        self,
        tx: Dict[str, Any],
        receipt: Dict[str, Any],
        state_before: Dict[str, Any],
        state_after: Dict[str, Any]
    ) -> Dict[str, Any]:
        """
        Validate ERC721 approval transaction
        
        Checks:
        1. Transaction executed successfully (30 points)
        2. Approved address correctly set (50 points)
        3. Used function: approve function (correct selector)(20 points)
        """
        checks = []
        total_score = 0
        
        # 1. Validate transaction success (30 points)
        tx_status = receipt.get('status', 0)
        if tx_status == 1:
            checks.append({
                'name': 'Transaction Success',
                'passed': True,
                'message': 'Transaction executed successfully',
                'score': 30
            })
            total_score += 30
        else:
            checks.append({
                'name': 'Transaction Success',
                'passed': False,
                'message': f'Transaction failed with status: {tx_status}',
                'score': 30
            })
            # If transaction failed, return directly
            return {
                'passed': False,
                'score': 0,
                'max_score': self.max_score,
                'checks': checks,
                'details': {
                    'nft_address': self.nft_address,
                    'token_id': self.token_id,
                    'spender_address': self.spender_address,
                    'transaction_status': tx_status
                }
            }
        
        # 2. Validate approved address (40 points)
        approved_before = state_before.get('nft_approved', '').lower() if state_before.get('nft_approved') else None
        approved_after = state_after.get('nft_approved', '').lower() if state_after.get('nft_approved') else None
        
        if approved_after and approved_after == self.spender_address:
            checks.append({
                'name': 'Approval Check',
                'passed': True,
                'message': f'NFT #{self.token_id} correctly approved for {self.spender_address}',
                'score': 50,
                'details': {
                    'approved_before': approved_before,
                    'approved_after': approved_after,
                    'expected': self.spender_address
                }
            })
            total_score += 50
        else:
            checks.append({
                'name': 'Approval Check',
                'passed': False,
                'message': f'Approval not set correctly. Expected: {self.spender_address}, Got: {approved_after}',
                'score': 50,
                'details': {
                    'approved_before': approved_before,
                    'approved_after': approved_after,
                    'expected': self.spender_address
                }
            })
        
        # 3. Validate Used function: correct function selector (20 points)
        tx_data = tx.get('data', '') or tx.get('input', '')
        
        if isinstance(tx_data, bytes):
            tx_data = tx_data.hex()
        if isinstance(tx_data, str) and tx_data.startswith('0x'):
            tx_data = tx_data[2:]
        
        # Extract function selector (First 4 bytes = 8 hex chars)
        if len(tx_data) >= 8:
            actual_selector = '0x' + tx_data[:8]
            
            if actual_selector.lower() == self.expected_selector.lower():
                checks.append({
                    'name': 'Function Selector',
                    'passed': True,
                    'message': f'Correct approve selector: {actual_selector}',
                    'score': 20,
                    'details': {
                        'expected': self.expected_selector,
                        'actual': actual_selector
                    }
                })
                total_score += 20
            else:
                checks.append({
                    'name': 'Function Selector',
                    'passed': False,
                    'message': f'Incorrect function selector. Expected approve ({self.expected_selector}), got {actual_selector}',
                    'score': 20,
                    'details': {
                        'expected': self.expected_selector,
                        'actual': actual_selector
                    }
                })
        else:
            checks.append({
                'name': 'Function Selector',
                'passed': False,
                'message': 'Transaction data too short or missing',
                'score': 20
            })
        
        # Aggregate results
        all_passed = all(check['passed'] for check in checks)
        
        return {
            'passed': all_passed,
            'score': total_score,
            'max_score': self.max_score,
            'checks': checks,
            'details': {
                'nft_address': self.nft_address,
                'token_id': self.token_id,
                'spender_address': self.spender_address,
                'approved_before': approved_before,
                'approved_after': approved_after,
                'expected_selector': self.expected_selector,
                'actual_selector': actual_selector if len(tx_data) >= 8 else 'N/A'
            }
        }