@sylix/coworker 2.0.11 → 2.0.14

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.
Files changed (169) hide show
  1. package/dist/commands/slash/config.d.ts.map +1 -1
  2. package/dist/commands/slash/config.js +22 -4
  3. package/dist/commands/slash/config.js.map +1 -1
  4. package/dist/core/CoWorkerAgent.d.ts.map +1 -1
  5. package/dist/core/CoWorkerAgent.js +6 -3
  6. package/dist/core/CoWorkerAgent.js.map +1 -1
  7. package/dist/skills/defaults/accessibility/screen-reader-testing.md +545 -0
  8. package/dist/skills/defaults/accessibility/wcag-audit-patterns.md +555 -0
  9. package/dist/skills/defaults/ai-ml/rag.md +276 -0
  10. package/dist/skills/defaults/backend-development/api-design-principles.md +528 -0
  11. package/dist/skills/defaults/backend-development/api-design.md +285 -0
  12. package/dist/skills/defaults/backend-development/architecture-patterns.md +494 -0
  13. package/dist/skills/defaults/backend-development/async-python.md +237 -0
  14. package/dist/skills/defaults/backend-development/auth-implementation-patterns.md +638 -0
  15. package/dist/skills/defaults/backend-development/bazel-build-optimization.md +387 -0
  16. package/dist/skills/defaults/backend-development/billing-automation/SKILL.md +566 -0
  17. package/dist/skills/defaults/backend-development/code-review-excellence.md +538 -0
  18. package/dist/skills/defaults/backend-development/cqrs-implementation.md +554 -0
  19. package/dist/skills/defaults/backend-development/database-design.md +305 -0
  20. package/dist/skills/defaults/backend-development/debugging-strategies.md +536 -0
  21. package/dist/skills/defaults/backend-development/e2e-testing-patterns.md +544 -0
  22. package/dist/skills/defaults/backend-development/error-handling-patterns.md +641 -0
  23. package/dist/skills/defaults/backend-development/fastapi-templates.md +559 -0
  24. package/dist/skills/defaults/backend-development/fastapi.md +309 -0
  25. package/dist/skills/defaults/backend-development/git-advanced-workflows.md +405 -0
  26. package/dist/skills/defaults/backend-development/microservices-patterns.md +595 -0
  27. package/dist/skills/defaults/backend-development/microservices.md +284 -0
  28. package/dist/skills/defaults/backend-development/monorepo-management.md +623 -0
  29. package/dist/skills/defaults/backend-development/nodejs-backend-patterns.md +1048 -0
  30. package/dist/skills/defaults/backend-development/nx-workspace-patterns.md +457 -0
  31. package/dist/skills/defaults/backend-development/paypal-integration/SKILL.md +478 -0
  32. package/dist/skills/defaults/backend-development/pci-compliance/SKILL.md +480 -0
  33. package/dist/skills/defaults/backend-development/python-anti-patterns.md +349 -0
  34. package/dist/skills/defaults/backend-development/python-background-jobs.md +364 -0
  35. package/dist/skills/defaults/backend-development/python-code-style.md +360 -0
  36. package/dist/skills/defaults/backend-development/python-configuration.md +368 -0
  37. package/dist/skills/defaults/backend-development/python-design-patterns.md +296 -0
  38. package/dist/skills/defaults/backend-development/python-error-handling.md +323 -0
  39. package/dist/skills/defaults/backend-development/python-packaging.md +887 -0
  40. package/dist/skills/defaults/backend-development/python-performance-optimization.md +874 -0
  41. package/dist/skills/defaults/backend-development/python-project-structure.md +252 -0
  42. package/dist/skills/defaults/backend-development/python-resilience.md +376 -0
  43. package/dist/skills/defaults/backend-development/python-resource-management.md +421 -0
  44. package/dist/skills/defaults/backend-development/python-type-safety.md +428 -0
  45. package/dist/skills/defaults/backend-development/sql-optimization-patterns.md +509 -0
  46. package/dist/skills/defaults/backend-development/stripe-integration/SKILL.md +522 -0
  47. package/dist/skills/defaults/backend-development/turborepo-caching.md +376 -0
  48. package/dist/skills/defaults/blockchain/defi-protocol-templates.md +430 -0
  49. package/dist/skills/defaults/blockchain/nft-standards.md +364 -0
  50. package/dist/skills/defaults/blockchain/solidity-security.md +514 -0
  51. package/dist/skills/defaults/blockchain/web3-testing.md +360 -0
  52. package/dist/skills/defaults/business/competitive-landscape/SKILL.md +527 -0
  53. package/dist/skills/defaults/business/market-sizing-analysis/SKILL.md +451 -0
  54. package/dist/skills/defaults/business/startup-financial-modeling/SKILL.md +494 -0
  55. package/dist/skills/defaults/business/startup-metrics-framework/SKILL.md +564 -0
  56. package/dist/skills/defaults/business/team-composition-analysis.md +437 -0
  57. package/dist/skills/defaults/compliance/employment-contract-templates/SKILL.md +527 -0
  58. package/dist/skills/defaults/compliance/gdpr-data-handling/SKILL.md +630 -0
  59. package/dist/skills/defaults/data-engineering/airflow-dag-patterns.md +436 -0
  60. package/dist/skills/defaults/data-engineering/airflow.md +519 -0
  61. package/dist/skills/defaults/data-engineering/data-quality.md +583 -0
  62. package/dist/skills/defaults/data-engineering/dbt-transformation-patterns.md +482 -0
  63. package/dist/skills/defaults/data-engineering/dbt.md +556 -0
  64. package/dist/skills/defaults/data-engineering/ml-pipeline-workflow/SKILL.md +247 -0
  65. package/dist/skills/defaults/data-engineering/spark-optimization.md +348 -0
  66. package/dist/skills/defaults/data-engineering/spark.md +411 -0
  67. package/dist/skills/defaults/database/postgresql.md +202 -0
  68. package/dist/skills/defaults/debugging/systematic-debugging.md +249 -0
  69. package/dist/skills/defaults/devops/architecture-decision-records.md +448 -0
  70. package/dist/skills/defaults/devops/changelog-automation.md +580 -0
  71. package/dist/skills/defaults/devops/cicd.md +314 -0
  72. package/dist/skills/defaults/devops/cloud.md +263 -0
  73. package/dist/skills/defaults/devops/code-review-excellence.md +299 -0
  74. package/dist/skills/defaults/devops/cost-optimization.md +295 -0
  75. package/dist/skills/defaults/devops/deployment-pipeline-design.md +356 -0
  76. package/dist/skills/defaults/devops/docker.md +281 -0
  77. package/dist/skills/defaults/devops/git-workflows.md +205 -0
  78. package/dist/skills/defaults/devops/github-actions.md +311 -0
  79. package/dist/skills/defaults/devops/gitlab-ci-patterns.md +266 -0
  80. package/dist/skills/defaults/devops/hybrid-cloud-networking.md +241 -0
  81. package/dist/skills/defaults/devops/istio-traffic-management.md +327 -0
  82. package/dist/skills/defaults/devops/kubernetes.md +339 -0
  83. package/dist/skills/defaults/devops/linkerd-patterns.md +311 -0
  84. package/dist/skills/defaults/devops/multi-cloud-architecture.md +181 -0
  85. package/dist/skills/defaults/devops/observability.md +243 -0
  86. package/dist/skills/defaults/devops/openapi-spec-generation.md +1024 -0
  87. package/dist/skills/defaults/devops/postmortem-writing.md +396 -0
  88. package/dist/skills/defaults/devops/prometheus-configuration.md +265 -0
  89. package/dist/skills/defaults/devops/secrets-management.md +341 -0
  90. package/dist/skills/defaults/devops/service-mesh-observability.md +385 -0
  91. package/dist/skills/defaults/devops/terraform-module-library.md +244 -0
  92. package/dist/skills/defaults/finance/backtesting-frameworks/SKILL.md +663 -0
  93. package/dist/skills/defaults/finance/risk-metrics-calculation/SKILL.md +557 -0
  94. package/dist/skills/defaults/frontend/accessibility-compliance.md +420 -0
  95. package/dist/skills/defaults/frontend/design-system-patterns.md +337 -0
  96. package/dist/skills/defaults/frontend/interaction-design.md +327 -0
  97. package/dist/skills/defaults/frontend/javascript.md +311 -0
  98. package/dist/skills/defaults/frontend/modern-javascript-patterns.md +927 -0
  99. package/dist/skills/defaults/frontend/react-native-design.md +440 -0
  100. package/dist/skills/defaults/frontend/react.md +345 -0
  101. package/dist/skills/defaults/frontend/responsive-design.md +472 -0
  102. package/dist/skills/defaults/frontend/tailwind-design-system.md +337 -0
  103. package/dist/skills/defaults/frontend/typescript-advanced-types.md +724 -0
  104. package/dist/skills/defaults/frontend/typescript.md +334 -0
  105. package/dist/skills/defaults/frontend/visual-design-foundations.md +326 -0
  106. package/dist/skills/defaults/frontend/web-component-design.md +279 -0
  107. package/dist/skills/defaults/game-development/godot-gdscript-patterns.md +188 -0
  108. package/dist/skills/defaults/game-development/unity-ecs-patterns.md +594 -0
  109. package/dist/skills/defaults/kubernetes/gitops-workflow.md +285 -0
  110. package/dist/skills/defaults/kubernetes/gitops.md +280 -0
  111. package/dist/skills/defaults/kubernetes/helm-chart-scaffolding.md +553 -0
  112. package/dist/skills/defaults/kubernetes/helm.md +343 -0
  113. package/dist/skills/defaults/kubernetes/k8s-manifest-generator.md +501 -0
  114. package/dist/skills/defaults/kubernetes/k8s-security-policies.md +342 -0
  115. package/dist/skills/defaults/kubernetes/manifests.md +330 -0
  116. package/dist/skills/defaults/kubernetes/security.md +337 -0
  117. package/dist/skills/defaults/llm-application/embedding-strategies.md +608 -0
  118. package/dist/skills/defaults/llm-application/hybrid-search-implementation.md +570 -0
  119. package/dist/skills/defaults/llm-application/hybrid-search.md +570 -0
  120. package/dist/skills/defaults/llm-application/langchain-architecture.md +666 -0
  121. package/dist/skills/defaults/llm-application/langchain.md +259 -0
  122. package/dist/skills/defaults/llm-application/llm-evaluation.md +695 -0
  123. package/dist/skills/defaults/llm-application/prompt-engineering-patterns.md +449 -0
  124. package/dist/skills/defaults/llm-application/prompt-engineering.md +219 -0
  125. package/dist/skills/defaults/llm-application/rag-implementation.md +434 -0
  126. package/dist/skills/defaults/llm-application/similarity-search-patterns.md +560 -0
  127. package/dist/skills/defaults/llm-application/similarity-search.md +560 -0
  128. package/dist/skills/defaults/llm-application/vector-index-tuning.md +523 -0
  129. package/dist/skills/defaults/mobile/mobile-android-design.md +440 -0
  130. package/dist/skills/defaults/mobile/mobile-ios-design.md +266 -0
  131. package/dist/skills/defaults/monitoring/distributed-tracing.md +436 -0
  132. package/dist/skills/defaults/monitoring/grafana-dashboards.md +370 -0
  133. package/dist/skills/defaults/monitoring/prometheus-configuration.md +379 -0
  134. package/dist/skills/defaults/monitoring/slo-implementation.md +323 -0
  135. package/dist/skills/defaults/refactoring/code-refactoring.md +349 -0
  136. package/dist/skills/defaults/security/anti-reversing-techniques/SKILL.md +559 -0
  137. package/dist/skills/defaults/security/auditor.md +168 -0
  138. package/dist/skills/defaults/security/binary-analysis-patterns/SKILL.md +438 -0
  139. package/dist/skills/defaults/security/memory-forensics/SKILL.md +483 -0
  140. package/dist/skills/defaults/security/mtls-configuration.md +349 -0
  141. package/dist/skills/defaults/security/protocol-reverse-engineering/SKILL.md +520 -0
  142. package/dist/skills/defaults/security/sast-configuration.md +182 -0
  143. package/dist/skills/defaults/security/security.md +313 -0
  144. package/dist/skills/defaults/security/stride-analysis.md +273 -0
  145. package/dist/skills/defaults/security/threat-mitigation-mapping.md +290 -0
  146. package/dist/skills/defaults/systems/bash-defensive-patterns/SKILL.md +539 -0
  147. package/dist/skills/defaults/systems/bats-testing-patterns/SKILL.md +631 -0
  148. package/dist/skills/defaults/systems/go-concurrency-patterns.md +657 -0
  149. package/dist/skills/defaults/systems/memory-safety-patterns.md +605 -0
  150. package/dist/skills/defaults/systems/rust-async-patterns.md +519 -0
  151. package/dist/skills/defaults/systems/shellcheck-configuration/SKILL.md +456 -0
  152. package/dist/skills/defaults/team-collaboration/multi-reviewer-patterns.md +126 -0
  153. package/dist/skills/defaults/team-collaboration/parallel-feature-development.md +151 -0
  154. package/dist/skills/defaults/testing/javascript-testing-patterns.md +1021 -0
  155. package/dist/skills/defaults/testing/python-testing-patterns.md +351 -0
  156. package/dist/skills/defaults/testing/testing.md +332 -0
  157. package/dist/skills/defaults/workflows/context-driven-development.md +384 -0
  158. package/dist/skills/defaults/workflows/track-management.md +592 -0
  159. package/dist/skills/defaults/workflows/workflow-patterns.md +622 -0
  160. package/dist/skills/index.d.ts +11 -0
  161. package/dist/skills/index.d.ts.map +1 -0
  162. package/dist/skills/index.js +129 -0
  163. package/dist/skills/index.js.map +1 -0
  164. package/dist/utils/character.js +4 -4
  165. package/dist/utils/character.js.map +1 -1
  166. package/dist/utils/inputbar.d.ts.map +1 -1
  167. package/dist/utils/inputbar.js +7 -0
  168. package/dist/utils/inputbar.js.map +1 -1
  169. package/package.json +1 -1
@@ -0,0 +1,514 @@
1
+ ---
2
+ name: solidity-security
3
+ description: Master smart contract security best practices to prevent common vulnerabilities and implement secure Solidity patterns. Use when writing smart contracts, auditing existing contracts, or implementing security measures for blockchain applications.
4
+ ---
5
+
6
+ # Solidity Security
7
+
8
+ Master smart contract security best practices, vulnerability prevention, and secure Solidity development patterns.
9
+
10
+ ## When to Use This Skill
11
+
12
+ - Writing secure smart contracts
13
+ - Auditing existing contracts for vulnerabilities
14
+ - Implementing secure DeFi protocols
15
+ - Preventing reentrancy, overflow, and access control issues
16
+ - Optimizing gas usage while maintaining security
17
+ - Preparing contracts for professional audits
18
+ - Understanding common attack vectors
19
+
20
+ ## Critical Vulnerabilities
21
+
22
+ ### 1. Reentrancy
23
+
24
+ Attacker calls back into your contract before state is updated.
25
+
26
+ **Vulnerable Code:**
27
+
28
+ ```solidity
29
+ // VULNERABLE TO REENTRANCY
30
+ contract VulnerableBank {
31
+ mapping(address => uint256) public balances;
32
+
33
+ function withdraw() public {
34
+ uint256 amount = balances[msg.sender];
35
+
36
+ // DANGER: External call before state update
37
+ (bool success, ) = msg.sender.call{value: amount}("");
38
+ require(success);
39
+
40
+ balances[msg.sender] = 0; // Too late!
41
+ }
42
+ }
43
+ ```
44
+
45
+ **Secure Pattern (Checks-Effects-Interactions):**
46
+
47
+ ```solidity
48
+ contract SecureBank {
49
+ mapping(address => uint256) public balances;
50
+
51
+ function withdraw() public {
52
+ uint256 amount = balances[msg.sender];
53
+ require(amount > 0, "Insufficient balance");
54
+
55
+ // EFFECTS: Update state BEFORE external call
56
+ balances[msg.sender] = 0;
57
+
58
+ // INTERACTIONS: External call last
59
+ (bool success, ) = msg.sender.call{value: amount}("");
60
+ require(success, "Transfer failed");
61
+ }
62
+ }
63
+ ```
64
+
65
+ **Alternative: ReentrancyGuard**
66
+
67
+ ```solidity
68
+ import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
69
+
70
+ contract SecureBank is ReentrancyGuard {
71
+ mapping(address => uint256) public balances;
72
+
73
+ function withdraw() public nonReentrant {
74
+ uint256 amount = balances[msg.sender];
75
+ require(amount > 0, "Insufficient balance");
76
+
77
+ balances[msg.sender] = 0;
78
+
79
+ (bool success, ) = msg.sender.call{value: amount}("");
80
+ require(success, "Transfer failed");
81
+ }
82
+ }
83
+ ```
84
+
85
+ ### 2. Integer Overflow/Underflow
86
+
87
+ **Vulnerable Code (Solidity < 0.8.0):**
88
+
89
+ ```solidity
90
+ // VULNERABLE
91
+ contract VulnerableToken {
92
+ mapping(address => uint256) public balances;
93
+
94
+ function transfer(address to, uint256 amount) public {
95
+ // No overflow check - can wrap around
96
+ balances[msg.sender] -= amount; // Can underflow!
97
+ balances[to] += amount; // Can overflow!
98
+ }
99
+ }
100
+ ```
101
+
102
+ **Secure Pattern (Solidity >= 0.8.0):**
103
+
104
+ ```solidity
105
+ // Solidity 0.8+ has built-in overflow/underflow checks
106
+ contract SecureToken {
107
+ mapping(address => uint256) public balances;
108
+
109
+ function transfer(address to, uint256 amount) public {
110
+ // Automatically reverts on overflow/underflow
111
+ balances[msg.sender] -= amount;
112
+ balances[to] += amount;
113
+ }
114
+ }
115
+ ```
116
+
117
+ **For Solidity < 0.8.0, use SafeMath:**
118
+
119
+ ```solidity
120
+ import "@openzeppelin/contracts/utils/math/SafeMath.sol";
121
+
122
+ contract SecureToken {
123
+ using SafeMath for uint256;
124
+ mapping(address => uint256) public balances;
125
+
126
+ function transfer(address to, uint256 amount) public {
127
+ balances[msg.sender] = balances[msg.sender].sub(amount);
128
+ balances[to] = balances[to].add(amount);
129
+ }
130
+ }
131
+ ```
132
+
133
+ ### 3. Access Control
134
+
135
+ **Vulnerable Code:**
136
+
137
+ ```solidity
138
+ // VULNERABLE: Anyone can call critical functions
139
+ contract VulnerableContract {
140
+ address public owner;
141
+
142
+ function withdraw(uint256 amount) public {
143
+ // No access control!
144
+ payable(msg.sender).transfer(amount);
145
+ }
146
+ }
147
+ ```
148
+
149
+ **Secure Pattern:**
150
+
151
+ ```solidity
152
+ import "@openzeppelin/contracts/access/Ownable.sol";
153
+
154
+ contract SecureContract is Ownable {
155
+ function withdraw(uint256 amount) public onlyOwner {
156
+ payable(owner()).transfer(amount);
157
+ }
158
+ }
159
+
160
+ // Or implement custom role-based access
161
+ contract RoleBasedContract {
162
+ mapping(address => bool) public admins;
163
+
164
+ modifier onlyAdmin() {
165
+ require(admins[msg.sender], "Not an admin");
166
+ _;
167
+ }
168
+
169
+ function criticalFunction() public onlyAdmin {
170
+ // Protected function
171
+ }
172
+ }
173
+ ```
174
+
175
+ ### 4. Front-Running
176
+
177
+ **Vulnerable:**
178
+
179
+ ```solidity
180
+ // VULNERABLE TO FRONT-RUNNING
181
+ contract VulnerableDEX {
182
+ function swap(uint256 amount, uint256 minOutput) public {
183
+ // Attacker sees this in mempool and front-runs
184
+ uint256 output = calculateOutput(amount);
185
+ require(output >= minOutput, "Slippage too high");
186
+ // Perform swap
187
+ }
188
+ }
189
+ ```
190
+
191
+ **Mitigation:**
192
+
193
+ ```solidity
194
+ contract SecureDEX {
195
+ mapping(bytes32 => bool) public usedCommitments;
196
+
197
+ // Step 1: Commit to trade
198
+ function commitTrade(bytes32 commitment) public {
199
+ usedCommitments[commitment] = true;
200
+ }
201
+
202
+ // Step 2: Reveal trade (next block)
203
+ function revealTrade(
204
+ uint256 amount,
205
+ uint256 minOutput,
206
+ bytes32 secret
207
+ ) public {
208
+ bytes32 commitment = keccak256(abi.encodePacked(
209
+ msg.sender, amount, minOutput, secret
210
+ ));
211
+ require(usedCommitments[commitment], "Invalid commitment");
212
+ // Perform swap
213
+ }
214
+ }
215
+ ```
216
+
217
+ ## Security Best Practices
218
+
219
+ ### Checks-Effects-Interactions Pattern
220
+
221
+ ```solidity
222
+ contract SecurePattern {
223
+ mapping(address => uint256) public balances;
224
+
225
+ function withdraw(uint256 amount) public {
226
+ // 1. CHECKS: Validate conditions
227
+ require(amount <= balances[msg.sender], "Insufficient balance");
228
+ require(amount > 0, "Amount must be positive");
229
+
230
+ // 2. EFFECTS: Update state
231
+ balances[msg.sender] -= amount;
232
+
233
+ // 3. INTERACTIONS: External calls last
234
+ (bool success, ) = msg.sender.call{value: amount}("");
235
+ require(success, "Transfer failed");
236
+ }
237
+ }
238
+ ```
239
+
240
+ ### Pull Over Push Pattern
241
+
242
+ ```solidity
243
+ // Prefer this (pull)
244
+ contract SecurePayment {
245
+ mapping(address => uint256) public pendingWithdrawals;
246
+
247
+ function recordPayment(address recipient, uint256 amount) internal {
248
+ pendingWithdrawals[recipient] += amount;
249
+ }
250
+
251
+ function withdraw() public {
252
+ uint256 amount = pendingWithdrawals[msg.sender];
253
+ require(amount > 0, "Nothing to withdraw");
254
+
255
+ pendingWithdrawals[msg.sender] = 0;
256
+ payable(msg.sender).transfer(amount);
257
+ }
258
+ }
259
+
260
+ // Over this (push)
261
+ contract RiskyPayment {
262
+ function distributePayments(address[] memory recipients, uint256[] memory amounts) public {
263
+ for (uint i = 0; i < recipients.length; i++) {
264
+ // If any transfer fails, entire batch fails
265
+ payable(recipients[i]).transfer(amounts[i]);
266
+ }
267
+ }
268
+ }
269
+ ```
270
+
271
+ ### Input Validation
272
+
273
+ ```solidity
274
+ contract SecureContract {
275
+ function transfer(address to, uint256 amount) public {
276
+ // Validate inputs
277
+ require(to != address(0), "Invalid recipient");
278
+ require(to != address(this), "Cannot send to contract");
279
+ require(amount > 0, "Amount must be positive");
280
+ require(amount <= balances[msg.sender], "Insufficient balance");
281
+
282
+ // Proceed with transfer
283
+ balances[msg.sender] -= amount;
284
+ balances[to] += amount;
285
+ }
286
+ }
287
+ ```
288
+
289
+ ### Emergency Stop (Circuit Breaker)
290
+
291
+ ```solidity
292
+ import "@openzeppelin/contracts/security/Pausable.sol";
293
+
294
+ contract EmergencyStop is Pausable, Ownable {
295
+ function criticalFunction() public whenNotPaused {
296
+ // Function logic
297
+ }
298
+
299
+ function emergencyStop() public onlyOwner {
300
+ _pause();
301
+ }
302
+
303
+ function resume() public onlyOwner {
304
+ _unpause();
305
+ }
306
+ }
307
+ ```
308
+
309
+ ## Gas Optimization
310
+
311
+ ### Use `uint256` Instead of Smaller Types
312
+
313
+ ```solidity
314
+ // More gas efficient
315
+ contract GasEfficient {
316
+ uint256 public value; // Optimal
317
+
318
+ function set(uint256 _value) public {
319
+ value = _value;
320
+ }
321
+ }
322
+
323
+ // Less efficient
324
+ contract GasInefficient {
325
+ uint8 public value; // Still uses 256-bit slot
326
+
327
+ function set(uint8 _value) public {
328
+ value = _value; // Extra gas for type conversion
329
+ }
330
+ }
331
+ ```
332
+
333
+ ### Pack Storage Variables
334
+
335
+ ```solidity
336
+ // Gas efficient (3 variables in 1 slot)
337
+ contract PackedStorage {
338
+ uint128 public a; // Slot 0
339
+ uint64 public b; // Slot 0
340
+ uint64 public c; // Slot 0
341
+ uint256 public d; // Slot 1
342
+ }
343
+
344
+ // Gas inefficient (each variable in separate slot)
345
+ contract UnpackedStorage {
346
+ uint256 public a; // Slot 0
347
+ uint256 public b; // Slot 1
348
+ uint256 public c; // Slot 2
349
+ uint256 public d; // Slot 3
350
+ }
351
+ ```
352
+
353
+ ### Use `calldata` Instead of `memory` for Function Arguments
354
+
355
+ ```solidity
356
+ contract GasOptimized {
357
+ // More gas efficient
358
+ function processData(uint256[] calldata data) public pure returns (uint256) {
359
+ return data[0];
360
+ }
361
+
362
+ // Less efficient
363
+ function processDataMemory(uint256[] memory data) public pure returns (uint256) {
364
+ return data[0];
365
+ }
366
+ }
367
+ ```
368
+
369
+ ### Use Events for Data Storage (When Appropriate)
370
+
371
+ ```solidity
372
+ contract EventStorage {
373
+ // Emitting events is cheaper than storage
374
+ event DataStored(address indexed user, uint256 indexed id, bytes data);
375
+
376
+ function storeData(uint256 id, bytes calldata data) public {
377
+ emit DataStored(msg.sender, id, data);
378
+ // Don't store in contract storage unless needed
379
+ }
380
+ }
381
+ ```
382
+
383
+ ## Common Vulnerabilities Checklist
384
+
385
+ ```solidity
386
+ // Security Checklist Contract
387
+ contract SecurityChecklist {
388
+ /**
389
+ * [ ] Reentrancy protection (ReentrancyGuard or CEI pattern)
390
+ * [ ] Integer overflow/underflow (Solidity 0.8+ or SafeMath)
391
+ * [ ] Access control (Ownable, roles, modifiers)
392
+ * [ ] Input validation (require statements)
393
+ * [ ] Front-running mitigation (commit-reveal if applicable)
394
+ * [ ] Gas optimization (packed storage, calldata)
395
+ * [ ] Emergency stop mechanism (Pausable)
396
+ * [ ] Pull over push pattern for payments
397
+ * [ ] No delegatecall to untrusted contracts
398
+ * [ ] No tx.origin for authentication (use msg.sender)
399
+ * [ ] Proper event emission
400
+ * [ ] External calls at end of function
401
+ * [ ] Check return values of external calls
402
+ * [ ] No hardcoded addresses
403
+ * [ ] Upgrade mechanism (if proxy pattern)
404
+ */
405
+ }
406
+ ```
407
+
408
+ ## Testing for Security
409
+
410
+ ```javascript
411
+ // Hardhat test example
412
+ const { expect } = require("chai");
413
+ const { ethers } = require("hardhat");
414
+
415
+ describe("Security Tests", function () {
416
+ it("Should prevent reentrancy attack", async function () {
417
+ const [attacker] = await ethers.getSigners();
418
+
419
+ const VictimBank = await ethers.getContractFactory("SecureBank");
420
+ const bank = await VictimBank.deploy();
421
+
422
+ const Attacker = await ethers.getContractFactory("ReentrancyAttacker");
423
+ const attackerContract = await Attacker.deploy(bank.address);
424
+
425
+ // Deposit funds
426
+ await bank.deposit({ value: ethers.utils.parseEther("10") });
427
+
428
+ // Attempt reentrancy attack
429
+ await expect(
430
+ attackerContract.attack({ value: ethers.utils.parseEther("1") }),
431
+ ).to.be.revertedWith("ReentrancyGuard: reentrant call");
432
+ });
433
+
434
+ it("Should prevent integer overflow", async function () {
435
+ const Token = await ethers.getContractFactory("SecureToken");
436
+ const token = await Token.deploy();
437
+
438
+ // Attempt overflow
439
+ await expect(token.transfer(attacker.address, ethers.constants.MaxUint256))
440
+ .to.be.reverted;
441
+ });
442
+
443
+ it("Should enforce access control", async function () {
444
+ const [owner, attacker] = await ethers.getSigners();
445
+
446
+ const Contract = await ethers.getContractFactory("SecureContract");
447
+ const contract = await Contract.deploy();
448
+
449
+ // Attempt unauthorized withdrawal
450
+ await expect(contract.connect(attacker).withdraw(100)).to.be.revertedWith(
451
+ "Ownable: caller is not the owner",
452
+ );
453
+ });
454
+ });
455
+ ```
456
+
457
+ ## Audit Preparation
458
+
459
+ ```solidity
460
+ contract WellDocumentedContract {
461
+ /**
462
+ * @title Well Documented Contract
463
+ * @dev Example of proper documentation for audits
464
+ * @notice This contract handles user deposits and withdrawals
465
+ */
466
+
467
+ /// @notice Mapping of user balances
468
+ mapping(address => uint256) public balances;
469
+
470
+ /**
471
+ * @dev Deposits ETH into the contract
472
+ * @notice Anyone can deposit funds
473
+ */
474
+ function deposit() public payable {
475
+ require(msg.value > 0, "Must send ETH");
476
+ balances[msg.sender] += msg.value;
477
+ }
478
+
479
+ /**
480
+ * @dev Withdraws user's balance
481
+ * @notice Follows CEI pattern to prevent reentrancy
482
+ * @param amount Amount to withdraw in wei
483
+ */
484
+ function withdraw(uint256 amount) public {
485
+ // CHECKS
486
+ require(amount <= balances[msg.sender], "Insufficient balance");
487
+
488
+ // EFFECTS
489
+ balances[msg.sender] -= amount;
490
+
491
+ // INTERACTIONS
492
+ (bool success, ) = msg.sender.call{value: amount}("");
493
+ require(success, "Transfer failed");
494
+ }
495
+ }
496
+ ```
497
+
498
+ ## Tools for Security Analysis
499
+
500
+ - **Slither**: Static analysis tool
501
+ - **Mythril**: Security analysis tool
502
+ - **Echidna**: Fuzzing tool
503
+ - **Manticore**: Symbolic execution
504
+ - **Securify**: Automated security scanner
505
+
506
+ ## Common Pitfalls
507
+
508
+ 1. **Using `tx.origin` for Authentication**: Use `msg.sender` instead
509
+ 2. **Unchecked External Calls**: Always check return values
510
+ 3. **Delegatecall to Untrusted Contracts**: Can hijack your contract
511
+ 4. **Floating Pragma**: Pin to specific Solidity version
512
+ 5. **Missing Events**: Emit events for state changes
513
+ 6. **Excessive Gas in Loops**: Can hit block gas limit
514
+ 7. **No Upgrade Path**: Consider proxy patterns if upgrades needed