Core Principles
This document contains immutable core principles that apply across all domains of the go-crypto-wallet project.
Security
Security is of utmost importance in this financial-related project.
Critical Rules:
- NEVER log private keys or sensitive information
- Encrypt or zero-clear private keys in memory when possible
- Do not pass passwords via command-line arguments; use secure input methods
- Conduct security review when making changes involving sensitive information
- Always verify that changes don't break existing functionality
- Consider the impact on offline wallet operations (keygen, sign)
When to Run Security Scans:
- For security-related changes, run
make go-check-vuln - Review encryption/decryption logic carefully
- Extra caution for private key management and wallet operations
Error Handling
Proper error handling is essential for robustness and debugging.
Rules:
- Wrap errors with
fmt.Errorf+%wto preserve error chains - Use
errors.Is/errors.Asfor error checking - Include context information in error messages
- Never ignore errors (detected by
errchecklinter)
Example:
result, err := service.DoSomething()
if err != nil {
return nil, fmt.Errorf("failed to do something: %w", err)
}Panic Usage
Following the principle of separation of concerns, the project clearly separates the instance construction phase from the instance usage phase. Therefore, panic is only allowed during instance construction.
Where panic is Acceptable:
main.gofiles (application entry points)internal/dipackage (dependency injection container)pkg/dipackage (legacy dependency injection container - for backward compatibility)
Important:
panicshould NOT be used in business logic, service layers, or infrastructure layers- Use proper error handling with error returns in all other packages
- The separation allows for fail-fast behavior during initialization while maintaining robust error handling during runtime
Context Management
Context management is crucial for proper request handling, timeouts, and cancellation.
Rules:
- Add
context.Contextto all API calls - Implement timeouts and cancellation
- Implement graceful shutdown
- Pass context through all layers (use cases, services, repositories)
Logging
Structured logging helps with debugging and monitoring.
Rules:
- Use structured logging (via
pkg/logger/) - Set appropriate log levels
- NEVER log sensitive information (private keys, passwords, etc.)
- Avoid excessive logging in hot paths
Patterns to Avoid
These patterns should be avoided across the entire codebase:
- Using
log.Fatal(except inmain) - Using
panicoutside of instance construction (i.e., outside ofmain.goandpkg/dipackage) - Leaving commented-out code
- Unused imports, variables, or functions
- Ignoring errors (detected by
errcheck) - Using
go build -vdirectly (usemake check-buildinstead) - Using
go tool golangci-lintdirectly (usemake go-lintinstead) - For
unused-receiverlint errors: Remove the receiver entirely instead of renaming it to_. Renaming to_will only cause the same error to appear for other receivers, so convert the method to a function from the start.
Recommended Patterns
These patterns should be used throughout the codebase:
- Abstraction through interfaces
- Dependency injection
- Proper error wrapping with context
- Use of
context.Context - Clear, focused package responsibilities
- Self-contained utility functions
- Comprehensive documentation
- Unit tests for all exported functions
Documentation
Good documentation makes the codebase more maintainable.
Rules:
- Add godoc comments to exported functions and methods
- Add package-level comments
- Include usage examples for complex logic
- Keep documentation up-to-date with code changes
Important Notes
- This is a financial-related project; make changes carefully
- Implement breaking changes incrementally with rollback plans
- Security-related changes must be reviewed
- Always verify that changes don't break existing functionality
- Consider the impact on offline wallet operations (keygen, sign)
See Also
- Architecture Guidelines - Clean Architecture principles and layer guidelines
- Coding Standards - Linting, formatting, and naming conventions
- Workflow Guidelines - Git operations and dependency management