A security-first, structured knowledge base for CashScript smart contract development on Bitcoin Cash (BCH).
This repository powers NexMCP — a deterministic AI pipeline that generates, validates, and audits CashScript contracts using structural rules, anti-pattern detection, and secure design templates.
To eliminate insecure smart contract generation by:
- Encoding authoritative security rules
- Defining explicit anti-patterns
- Providing canonical secure patterns
- Supplying production-ready contract templates
- Enabling deterministic AI-assisted contract generation
This is not a documentation mirror. It is a security-enforced knowledge architecture.
NexKB/
├── BCH_Knowledge_Base-main/
│ ├── concepts/
│ ├── language/
│ ├── sdk/
│ ├── examples/
│ ├── cashtokens/
│ ├── best-practices/
│ ├── reference/
│ └── faq/
├── anti_pattern/ # Vulnerable contract examples (negative patterns)
├── patterns/ # Canonical secure implementations
├── templates/ # Complete production-ready contracts
├── common_mistakes/ # Common error patterns
├── specs/ # Generation & enforcement specifications
├── docs/ # Security architecture documentation
├── KB_REQUIREMENTS.md # Knowledge base requirements specification
├── security_rules.json # Structural enforcement database
├── LICENSE # GNU Affero General Public License v3.0
└── README.md # Project overview
NexKB enforces a Negative Type System for CashScript:
Explicit vulnerable examples that define what must never be generated:
- Unvalidated input position (
unvalidated_position.cash) - Missing tokenCategory validation (
missing_token_validation.cash) - Fee assumption violations (
fee_assumption_violation.cash) - Mint authority leakage (
minting_authority_leak.cash) - Time boundary errors (
time_validation_error.cash) - Division by zero (
common_mistakes/division_by_zero.cash) - Covenant continuation failures (
vulnerable_covenant.cash)
These power static enforcement in NexMCP.
Canonical security primitives:
- Covenant continuation validation (
patterns/covenant_validation.cash) - Cross-contract authorization (
patterns/cross_contract_auth.cash) - Input validation (
patterns/input_validation.cash) - Minting control (
patterns/minting_control.cash) - Output count bounding (
patterns/output_count_limit.cash) - Sidecar enforcement (
patterns/sidecar_attach.cash) - Position validation (
patterns/position_validation.cash)
These are deterministic structural templates.
Production-grade implementations:
- English Auction (
templates/auction_english.cash) - Dutch Auction (
templates/auction_dutch.cash) - Escrow (2-of-3) (
templates/escrow_2of3.cash) - Crowdfunding (refundable) (
templates/crowdfunding_refundable.cash) - Linear Vesting (
templates/vesting_linear.cash)
All templates:
- BCH-only unless specified
- Explicit token validation
- Explicit output bounding
- No hidden assumptions
- Audit-ready comments
This repository includes a structured copy of:
BCH_Knowledge_Base - Created and maintained by: 👉 toorik2
This corpus contains conceptual and reference documentation used for contextual retrieval.
BCH_Knowledge_Base-main/
├── concepts/
├── language/
├── sdk/
├── examples/
├── cashtokens/
├── best-practices/
├── reference/
└── faq/
The FAQ dataset included in this repository originates from toorik2's BCH Knowledge Base.
It contains:
| Channel | FAQs | Categories |
|---|---|---|
| Electron Cash | 327 | 11 |
| BCH Devs & Builders | 305 | 11 |
| BCHN | 222 | 9 |
| CashScript | 120 | 13 |
| CashToken Devs | 81 | 9 |
| BCH Compilers | 34 | 5 |
Total: 1,089 Q&As
These are used as contextual retrieval material.
NexKB feeds a 3-phase deterministic pipeline:
Phase 1 – Skeleton Generator: Structural contract layout
Phase 2 – Logic Implementation: Controlled logic insertion using secure patterns
Phase 3 – Structural Gate: Rejects:
- EVM hallucinations
- Token misuse
- Output leaks
- Mint authority exposure
- Division-by-zero risks
NexMCP performs deep semantic validation using NexKB's anti-pattern system.
Step 1 – AST Parsing
- Parse contract into structured representation
- Extract functions, require statements, outputs, token usage, arithmetic ops
Step 2 – Structural Rule Validation Validate:
activeInputIndexusagelockingBytecodeenforcementtx.outputs.lengthboundstokenCategoryenforcement- Explicit value validation
- Division/modulo safety
Step 3 – Anti-Pattern Detection Cross-check against NexKB anti-pattern registry:
- Implicit output ordering
- Fee assumptions
- Missing token validation
- Mint authority leakage
- Unvalidated covenant continuation
- Division-by-zero risk
- Time validation errors
- Input position vulnerabilities
Step 4 – Violation Report Produces:
- Line-level violation reference
- Anti-pattern category
- Severity level
- Deterministic explanation
- Suggested secure pattern reference
Audit does not regenerate code. It produces a structured, explainable security report.
If violations are detected, NexMCP activates the Repair Agent.
Repair Flow
-
Violation Mapping
- Map detected issue → secure pattern in NexKB
- Identify minimal code delta required
-
Patch Construction
- Insert missing
require()guards - Add
tokenCategoryenforcement - Enforce output bounds
- Fix time comparison operators
- Add divisor validation before arithmetic
- Repair covenant continuation constraints
- Insert missing
-
Structural Re-Validation
- Re-run Structural Gate
- Ensure no new violations introduced
- Ensure logic remains intact
-
Return Corrected Contract
- Only valid contract is returned
- User never sees failed intermediate attempts
- Deterministic security constraints preserved
NexMCP enforces three distinct layers:
| Layer | Purpose |
|---|---|
| Generation | Build structurally valid contract |
| Audit | Detect semantic vulnerabilities |
| Repair | Deterministically fix violations |
This separation ensures:
- Clean UX (no failed drafts shown)
- No silent logic mutation during generation
- Explicit user-triggered security analysis
- Transparent and explainable fixes
This knowledge base is continuously evolving with:
- New security patterns and anti-patterns
- Additional contract templates
- Enhanced documentation
- Community-contributed content
- Improved AI integration
We welcome contributions that enhance the security and completeness of this knowledge base:
- Security Research: New vulnerability patterns and fixes
- Contract Templates: Production-tested implementations
- Documentation: Guides, tutorials, and references
- Community FAQs: Technical Q&As from BCH development
GNU Affero General Public License v3.0 (AGPL-3.0)
NexOps
toorik2 - https://github.com/toorik2
Extracted FAQ data originates from BCH developer Telegram communities and public documentation.
NexKB is:
- A structured security knowledge system
- An enforcement architecture
- A deterministic generation framework
It is not a documentation mirror nor an official CashScript project.