A modular, audit-ready Solidity security suite — by @BuildsWithKing.
A modular Solidity security framework for building safer, auditable smart contracts.
KingSecurity is designed around the idea that smart contract security should be modular, auditable, and human-readable. Every module enforces one invariant and can be combined like building blocks.
This repository introduces modular smart contract security primitives such as Kingable, KingPausable, hybrid extensions, KingClaimMistakenETH, KingReentrancyGuard, KingERC20 and KingReentrancyAttacker. Battle-tested with unit tests, fuzz tests, and mock contracts using Foundry.
⚠ Note: This repository serves as a testing and experimental workspace for the buildswithking-security library. It is not versioned, and features here may change without notice. For stable modules, use the main BuildsWithKing-Security repo.
Audit-ready, modular Solidity security suite tested with Foundry.
- 🛡 BuildsWithKing-KingSecurity
This KingSecurity suite enforces ownership, pausing, and authority mechanics in a way that is:
- ✅ Transparent
- ✅ Modular
- ✅ Audit-friendly
Each module is shipped with:
- Custom errors (gas-optimized revert reasons)
- Events (state-change transparency)
- Modifiers & Guards (king-only execution, contract/EOA filtering)
- Extensive test coverage (unit + fuzzing)
Smart contract exploits often arise from improper access control, missing pause mechanisms, or weak invariants.
This project tackles those pain points by building security extensions that can be plugged into larger protocols.
This repository is not a step-by-step guide, but a reference testing suite for the main BuildsWithKing-Security repository.
-
Kingable.sol
- Introduces the “King” role (customizable ownership).
- Supports transferring and renouncing kingship.
-
KingImmutable.sol
- Immutable king set at deployment.
- No transfer or renounce allowed (one true king forever).
-
KingAccessControlLite.sol
- Minimal and Gas-efficient role-based access control module for king-based contracts.
-
KingPausable.sol
- Pause/Activate core functions.
- Prevents unexpected activity during upgrades or active exploit scenarios.
-
KingableContracts.sol
- Restricts kingship transfer to contract addresses only.
-
KingableEOAs.sol
- Restricts kingship transfer to externally owned accounts (EOAs) only.
-
KingablePausable.sol
- Hybrid extension combining Kingable + Pausable in one contract.
-
KingClaimMistakenETH.sol
- Allows users to claim ETH mistakenly transferred to the child contract.
-
KingRejectETH.sol
- Rejects ETH transfer on child contracts.
- KingReentrancyGuard.sol
- Prevents reentrancy attacks using the
nonReentrantmodifier.
- Prevents reentrancy attacks using the
- Core, modular ERC-20 implementation (balances, transfers, allowances, events).
- Built to be inherited by extensions (mintable, burnable, capped) so the base remains minimal and auditable.
- Uses custom errors and address validation for gas efficiency and clearer reverts.
-
KingERC20Burnable.sol
- Role-based burning extension leveraging KingAccessControlLite.
- Allows the King to assign/remove BURNER_ROLE and authorized burner to burn tokens.
- Designed to integrate with any ERC20 needing controlled burn logic; calls _burn on the base contract.
-
KingERC20Capped.sol
- Enforces a maximum supply cap, preventing minting above the defined limit.
- Overrides _mint to check s_totalSupply + amount <= cap.
- Ideal for tokens with fixed maximum issuance.
-
KingERC20Mintable.sol
- Role-based minting extension leveraging KingAccessControlLite.
- Allows the King to assign/remove MINTER_ROLE and authorized minters to mint tokens.
- Suitable for controlled inflation, staking rewards, or staged issuance.
-
KingERC20Pausable.sol
- Adds emergency whenActive gating to core write functions (transfer, approve, mint, burn).
- Inherits behavior from KingPausable; enhances safety during maintenance, upgrades, or exploit responses.
-
IERC20.sol
- Minimal ERC-20 interface defining core events and functions.
- Ensures interoperability and standard compliance with ERC-20 ecosystem tools.
-
IERC20Metadata.sol
- ERC-20 metadata interface exposing name, symbol, and decimals.
- Keeps the base contract lightweight and modular.
- KingERC20Errors.sol
- Centralized collection of custom errors for the entire ERC-20 stack (e.g., InsufficientBalance, ZeroInitialSupply).
- Reduces duplicate revert messages and saves gas compared to require strings.
-
KingReentrancyAttacker.sol
- Reusable attacker contract for testing reentrancy vulnerabilities.
-
KingVulnerableContract.sol
- A deliberately insecure contract used to simulate reentrancy attacks.
-
KingCheckAddressLib.sol
- Lightweight utility library that validates addresses.
- Replaces repetitive
if(account == address(0))checks for cleaner code. - Gas-efficient and improves consistency across contracts.
Testing is powered by Foundry.
All contracts are verified against unit, fuzz, and mock tests to ensure correctness, robustness, and edge-case coverage.
All tests were written manually and run under Foundry
1.2.3-stable.
- Verifies constructor initialization and state setup.
- Validate access control (Unauthorized, InvalidKing, etc.).
- Confirm expected state transitions.
- Stress test random inputs across key functions (transferKingship, pauseContract, activateContract).
- Ensure safety invariants hold under arbitrary addresses.
- Enable isolated testing of abstract contracts.
- Dummy contracts simulate invalid inputs (e.g., contract vs. EOA).
Below is the current coverage report snapshot (100%).

This tree illustrates a 1:1 mapping between production modules and their corresponding test suites (unit, fuzz, mock).
src
├── core
│ ├── KingAccessControlLite.sol
│ ├── KingImmutable.sol
│ └── Kingable.sol
├── extensions
│ ├── KingPausable.sol
│ ├── KingableContracts.sol
│ ├── KingableEOAs.sol
│ └── KingablePausable.sol
├── guards
│ ├── KingClaimMistakenETH.sol
│ └── KingRejectETH.sol
├── security
│ └── KingReentrancyGuard.sol
├── tokens
│ ├── ERC20
│ │ ├── KingERC20.sol
│ │ ├── extensions
│ │ │ ├── KingERC20Burnable.sol
│ │ │ ├── KingERC20Capped.sol
│ │ │ ├── KingERC20Mintable.sol
│ │ │ └── KingERC20Pausable.sol
│ │ └── interfaces
│ │ ├── IERC20.sol
│ │ └── IERC20Metadata.sol
│ └── errors
│ └── KingERC20Errors.sol
└── utils
| ├── KingCheckAddressLib.sol
| ├── KingReentrancyAttacker.sol
| └── KingVulnerableContract.sol
|
test
├── fuzz
│ ├── corefuzz
│ │ ├── KingAccessControlLiteFuzzTest.t.sol
│ │ └── KingableFuzzTest.t.sol
│ ├── extensionsFuzz
│ │ ├── KingPausableFuzzTest.t.sol
│ │ ├── KingableContractsFuzzTest.t.sol
│ │ ├── KingableEOAsFuzzTest.t.sol
│ │ └── KingablePausableFuzzTest.t.sol
│ └── guardsfuzz
│ └── KingClaimMistakenETHFuzzTest.t.sol
├── mocks
│ ├── KingAccessControlLiteMockTest.t.sol
│ ├── KingClaimMistakenETHMockTest.t.sol
│ ├── KingERC20BurnableMockTest.t.sol
│ ├── KingERC20CappedMockTest.t.sol
│ ├── KingERC20MintableMockTest.t.sol
│ ├── KingERC20MockTest.t.sol
│ ├── KingERC20PausableMockTest.t.sol
│ ├── KingImmutableMockTest.t.sol
│ ├── KingPausableMockTest.t.sol
│ ├── KingRejectETHMockTest.t.sol
│ ├── KingableContractsMockTest.t.sol
│ ├── KingableEOAsMockTest.t.sol
│ ├── KingableMockTest.t.sol
│ └── KingablePausableMockTest.t.sol
└── unit
├── BaseTest.t.sol
├── DummyContract.t.sol
├── coreunit
│ ├── KingAccessControlLiteUnitTest.t.sol
│ ├── KingImmutableUnitTest.t.sol
│ └── KingableUnitTest.t.sol
├── extensionsunit
│ ├── KingPausableUnitTest.t.sol
│ ├── KingableContractsUnitTest.t.sol
│ ├── KingableEOAsUnitTest.t.sol
│ └── KingablePausableUnitTest.t.sol
├── guardsunit
│ ├── KingClaimMistakenETHUnitTest.t.sol
│ └── KingRejectETHTest.t.sol
├── tokens
│ └── ERC20
│ ├── KingERC20FuzzTest.t.sol
│ ├── KingERC20UnitTest.t.sol
│ └── extensionsunit
│ ├── KingERC20BurnableUnitTest.t.sol
│ ├── KingERC20CappedUnitTest.t.sol
│ ├── KingERC20MintableUnitTest.t.sol
│ └── KingERC20PausableUnitTest.t.sol
└── utilsunit
└── KingReentracyAttackerUnitTest.t.solFoundry install
To explore or run tests locally:
git clone https://github.com/BuildsWithKing/BuildsWithKing-KingSecurity.git
cd BuildsWithKing-KingSecurity
forge install
forge build
forge test -vvvv
forge coverage
forge snapshot
Install this package into your Foundry/Hardhat project by adding it as a Git submodule or using forge install:
forge install BuildsWithKing/buildswithking-securityThen import module with:
import {Kingable} from "buildswithking-security/contracts/access/core/Kingable.sol";
import {KingReentrancyGuard} from "buildswithking-security/contracts/security/KingReentrancyGuard.sol";To inherit Kingable & KingReentrancyGuard in your contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
import {Kingable} from "buildswithking-security/contracts/access/core/Kingable.sol";
import {KingReentrancyGuard} from "buildswithking-security/contracts/security/KingReentrancyGuard.sol";
contract MyDapp is KingReentrancyGuard, Kingable {
constructor(address _kingAddress) Kingable(_kingAddress) {}
function doKingStuff() external onlyKing nonReentrant {
// only the King can call this
}
}This repo is a security primitives library, not a production protocol.
Audit your integration when using these contracts in live deployments.
Includes custom errors and reverts for gas savings and safety.
Note: These contracts are battle-tested through fuzzing and mocks but should still undergo external audit review before mainnet deployment.
Michealking (@BuildsWithKing)
Solidity Smart Contract Developer
Security-focused, building transparent protocols
📡 Twitter/X: @BuildsWithKing
⭐ Star this repo if you find it helpful — contributions and feedback are welcome!
This project is licensed under the MIT License.
