opmsec 0.1.0

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 (57) hide show
  1. package/.env.example +14 -0
  2. package/.pnp.cjs +9953 -0
  3. package/.pnp.loader.mjs +2126 -0
  4. package/README.md +266 -0
  5. package/bun.lock +620 -0
  6. package/bunfig.toml +6 -0
  7. package/docker-compose.yml +10 -0
  8. package/package.json +39 -0
  9. package/packages/cli/package.json +7 -0
  10. package/packages/cli/src/commands/audit.tsx +142 -0
  11. package/packages/cli/src/commands/author-view.tsx +247 -0
  12. package/packages/cli/src/commands/info.tsx +109 -0
  13. package/packages/cli/src/commands/install.tsx +362 -0
  14. package/packages/cli/src/commands/passthrough.tsx +36 -0
  15. package/packages/cli/src/commands/push.tsx +321 -0
  16. package/packages/cli/src/components/AgentScores.tsx +32 -0
  17. package/packages/cli/src/components/AuthorInfo.tsx +45 -0
  18. package/packages/cli/src/components/Header.tsx +24 -0
  19. package/packages/cli/src/components/PackageCard.tsx +48 -0
  20. package/packages/cli/src/components/RiskBadge.tsx +32 -0
  21. package/packages/cli/src/components/ScanReport.tsx +50 -0
  22. package/packages/cli/src/components/StatusLine.tsx +30 -0
  23. package/packages/cli/src/index.tsx +111 -0
  24. package/packages/cli/src/services/avatar.ts +10 -0
  25. package/packages/cli/src/services/chainpatrol.ts +25 -0
  26. package/packages/cli/src/services/contract.ts +182 -0
  27. package/packages/cli/src/services/ens.ts +143 -0
  28. package/packages/cli/src/services/fileverse.ts +36 -0
  29. package/packages/cli/src/services/osv.ts +141 -0
  30. package/packages/cli/src/services/signature.ts +22 -0
  31. package/packages/cli/src/services/version.ts +10 -0
  32. package/packages/contracts/contracts/OPMRegistry.sol +253 -0
  33. package/packages/contracts/hardhat.config.ts +32 -0
  34. package/packages/contracts/package-lock.json +7772 -0
  35. package/packages/contracts/package.json +10 -0
  36. package/packages/contracts/scripts/deploy.ts +28 -0
  37. package/packages/contracts/test/OPMRegistry.test.ts +101 -0
  38. package/packages/contracts/tsconfig.json +11 -0
  39. package/packages/core/package.json +7 -0
  40. package/packages/core/src/abi.ts +629 -0
  41. package/packages/core/src/constants.ts +30 -0
  42. package/packages/core/src/index.ts +5 -0
  43. package/packages/core/src/prompt.ts +111 -0
  44. package/packages/core/src/types.ts +104 -0
  45. package/packages/core/src/utils.ts +50 -0
  46. package/packages/scanner/package.json +6 -0
  47. package/packages/scanner/src/agents/agent-configs.ts +24 -0
  48. package/packages/scanner/src/agents/base-agent.ts +75 -0
  49. package/packages/scanner/src/index.ts +25 -0
  50. package/packages/scanner/src/queue/memory-queue.ts +91 -0
  51. package/packages/scanner/src/services/contract-writer.ts +34 -0
  52. package/packages/scanner/src/services/fileverse.ts +89 -0
  53. package/packages/scanner/src/services/npm-registry.ts +159 -0
  54. package/packages/scanner/src/services/openrouter.ts +86 -0
  55. package/packages/scanner/src/services/osv.ts +87 -0
  56. package/packages/scanner/src/services/report-formatter.ts +134 -0
  57. package/tsconfig.json +23 -0
package/README.md ADDED
@@ -0,0 +1,266 @@
1
+ # OPM — On-chain Package Manager
2
+
3
+ A security-first CLI wrapper around npm that brings on-chain verification, multi-agent AI scanning, and decentralized audit trails to the JavaScript package ecosystem.
4
+
5
+ Authors sign packages with Ethereum keys. Three AI agents scan every publish in parallel and submit structured risk scores to a smart contract on Base Sepolia. Scan reports are stored as encrypted documents on Fileverse dDocs. Consumers verify signatures, check CVE databases, and consult on-chain scores before installing.
6
+
7
+ ## Setup
8
+
9
+ ### Prerequisites
10
+
11
+ - [Bun](https://bun.sh) >= 1.2
12
+ - Node.js >= 20
13
+ - An Ethereum wallet with Base Sepolia ETH (for gas)
14
+
15
+ ### Install Dependencies
16
+
17
+ ```bash
18
+ git clone <repo> && cd opm
19
+ cp .env.example .env
20
+ # Fill in your keys (see Environment Variables below)
21
+ bun install
22
+ ```
23
+
24
+ ### Link CLI Globally
25
+
26
+ ```bash
27
+ bun link
28
+ ```
29
+
30
+ This registers `opm` as a global command.
31
+
32
+ ### Deploy Contract
33
+
34
+ ```bash
35
+ cd packages/contracts
36
+ npm install
37
+ npx hardhat compile
38
+ npx hardhat run scripts/deploy.ts --network baseSepolia
39
+ # Copy the deployed address to .env as CONTRACT_ADDRESS
40
+ ```
41
+
42
+ ### Fileverse Setup
43
+
44
+ OPM stores formatted security reports on Fileverse dDocs (encrypted, on-chain synced).
45
+
46
+ 1. Go to [ddocs.new](https://ddocs.new) → Settings → Developer Mode → Generate API key
47
+ 2. Add the key to `.env` as `FILEVERSE_API_KEY`
48
+ 3. Start the local Fileverse API server:
49
+
50
+ ```bash
51
+ npx @fileverse/api --apiKey YOUR_API_KEY
52
+ ```
53
+
54
+ The server runs on `http://localhost:8001` by default.
55
+
56
+ ## Architecture
57
+
58
+ ```
59
+ opm push
60
+ ├─ Pack tarball & compute SHA-256 checksum
61
+ ├─ Sign checksum with author's Ethereum key (ECDSA)
62
+ ├─ Resolve ENS identity (Sepolia + Mainnet)
63
+ ├─ 3 AI agents scan source code in parallel
64
+ │ ├─ Risk score, vulnerabilities, supply chain indicators
65
+ │ ├─ Submit scores to OPMRegistry contract
66
+ │ └─ Upload styled markdown report to Fileverse dDocs
67
+ ├─ BLOCK if aggregate risk >= 80 (CRITICAL)
68
+ ├─ Publish to npm (with --token for automation)
69
+ └─ Register package + signature + report URI on-chain
70
+
71
+ opm install <pkg>
72
+ ├─ Resolve version (on-chain latest or specified)
73
+ ├─ Query CVE database (OSV API) — block CRITICAL, warn HIGH
74
+ ├─ Look up on-chain registry (risk score, agent consensus)
75
+ ├─ Verify ECDSA signature against checksum
76
+ ├─ ChainPatrol fallback for unknown packages
77
+ ├─ Fetch Fileverse report link
78
+ └─ Install via npm if safe
79
+
80
+ opm view <name.eth>
81
+ ├─ Resolve ENS name → address (@ensdomains/ensjs + viem)
82
+ ├─ Fetch ENS text records (avatar, bio, GitHub, Twitter)
83
+ ├─ Render avatar as pixel art in terminal
84
+ ├─ Query on-chain author stats (packages, reputation)
85
+ └─ List all published packages with risk scores + report links
86
+ ```
87
+
88
+ ## Commands
89
+
90
+ ### Security Commands
91
+
92
+ ```bash
93
+ # Sign, scan, publish, and register on-chain
94
+ opm push
95
+ opm push --token <npm-automation-token>
96
+
97
+ # Install with full security pipeline
98
+ opm install lodash
99
+ opm install lodash@4.17.21
100
+ opm install # verify all deps in package.json
101
+
102
+ # Audit all dependencies against on-chain + CVE data
103
+ opm audit
104
+
105
+ # Look up on-chain security info for a package
106
+ opm info lodash
107
+ opm info lodash@4.17.21
108
+
109
+ # View an author's ENS profile, packages, and risk scores
110
+ opm view djpai.eth
111
+ opm whois djpai
112
+ ```
113
+
114
+ ### npm Passthrough
115
+
116
+ All standard npm commands work transparently:
117
+
118
+ ```bash
119
+ opm init opm run dev opm test
120
+ opm start opm build opm uninstall lodash
121
+ opm outdated opm update opm list
122
+ opm link opm pack
123
+ ```
124
+
125
+ **Aliases:** `i` / `add` → install, `rm` → uninstall, `ls` → list
126
+
127
+ ### Standalone Scanner
128
+
129
+ ```bash
130
+ bun run scan -- <package-name> <version>
131
+ ```
132
+
133
+ ## npm Publishing
134
+
135
+ OPM handles npm publish as part of `opm push`. If your npm account has 2FA enabled, use an automation token:
136
+
137
+ 1. Go to [npmjs.com](https://www.npmjs.com) → Access Tokens → Generate New Token → **Automation**
138
+ 2. Pass it via CLI flag or environment:
139
+
140
+ ```bash
141
+ opm push --token npm_xxxxxxxxxxxx
142
+ # or
143
+ NPM_TOKEN=npm_xxxxxxxxxxxx opm push
144
+ # or add NPM_TOKEN to your .env
145
+ ```
146
+
147
+ The scan runs **before** publish — if the aggregate risk score is CRITICAL (>= 80), the publish and on-chain registration are blocked.
148
+
149
+ ## Security Pipeline
150
+
151
+ ### `opm install` Flow
152
+
153
+ 1. **Version resolution** — resolves "latest" from on-chain registry
154
+ 2. **CVE database** — queries [OSV API](https://osv.dev) for known vulnerabilities; computes CVSS v3 base scores
155
+ - CRITICAL CVEs → installation blocked
156
+ - HIGH CVEs → warning displayed
157
+ - Shows CVE ID, severity, summary, and suggested fix version
158
+ 3. **On-chain registry** — fetches agent consensus risk score
159
+ 4. **Signature verification** — verifies ECDSA signature against package checksum
160
+ 5. **ChainPatrol** — fallback blocklist check for packages not in the registry
161
+ 6. **Fileverse report** — links to the full AI scan report
162
+ 7. **Install** — runs `npm install` if all checks pass
163
+
164
+ ### `opm push` Flow
165
+
166
+ 1. **Pack & sign** — SHA-256 checksum, ECDSA signature
167
+ 2. **ENS resolution** — maps author address to ENS name
168
+ 3. **AI security scan** — 3 agents analyze source code, metadata, and version history
169
+ 4. **Risk gate** — blocks publish if score >= 80
170
+ 5. **npm publish** — with token-based auth support
171
+ 6. **On-chain registration** — stores checksum, signature, ENS name, and report URI
172
+
173
+ ## AI Agents
174
+
175
+ Three models scan every package in parallel:
176
+
177
+ | Agent | OpenRouter Model | OpenAI Fallback |
178
+ |-------|-----------------|-----------------|
179
+ | Agent 1 | Claude Sonnet 4 | GPT-4.1 |
180
+ | Agent 2 | Gemini 2.5 Flash | GPT-4.1 Mini |
181
+ | Agent 3 | DeepSeek Chat | GPT-4.1 Nano |
182
+
183
+ If `OPENROUTER_API_KEY` is set, OPM uses diverse models for better consensus. Otherwise falls back to OpenAI variants via `OPENAI_API_KEY`.
184
+
185
+ Each agent produces a structured JSON report covering:
186
+ - Risk score (0-100) and risk level
187
+ - Vulnerability analysis with CVE cross-referencing
188
+ - Supply chain indicators (install scripts, obfuscation, network calls, eval usage)
189
+ - Version history analysis and changelog risk assessment
190
+
191
+ ## ENS Integration
192
+
193
+ OPM uses the official [`@ensdomains/ensjs`](https://github.com/ensdomains/ensjs) SDK with `viem` for:
194
+
195
+ - **Address → Name** resolution across Sepolia and Mainnet
196
+ - **Name → Address** resolution for author lookups
197
+ - **Text record** fetching (avatar, bio, URL, GitHub, Twitter, email)
198
+ - **Terminal avatar** rendering via `terminal-image` (ANSI pixel art)
199
+ - **Author profiles** via `opm view <name.eth>` showing published packages and risk scores
200
+
201
+ The smart contract stores ENS names alongside author addresses for on-chain identity binding.
202
+
203
+ ## Integrations
204
+
205
+ | Integration | Purpose |
206
+ |-------------|---------|
207
+ | **Base Sepolia** | Smart contract deployment (OPMRegistry) |
208
+ | **ENS** | On-chain author identity, profile display, name resolution |
209
+ | **Fileverse dDocs** | Encrypted, decentralized storage for styled security reports |
210
+ | **ChainPatrol** | Fallback blocklist for packages not in the registry |
211
+ | **OSV API** | Real CVE/GHSA vulnerability data with CVSS v3 scoring |
212
+ | **OpenRouter / OpenAI** | Multi-model AI scanning (Claude, Gemini, DeepSeek, GPT) |
213
+
214
+ ## Smart Contract
215
+
216
+ `OPMRegistry.sol` on Base Sepolia:
217
+
218
+ - Package registration with checksum, signature, and ENS binding
219
+ - Authorized agent score submission (gas-funded agent wallets)
220
+ - Fileverse report URI storage per package version
221
+ - Aggregate risk scoring across agents
222
+ - Safest version lookup with configurable lookback
223
+ - Author reputation tracking (average score across all packages)
224
+ - ENS-to-author mapping for reverse lookups
225
+
226
+ ## Project Structure
227
+
228
+ ```
229
+ packages/
230
+ core/ Shared types, constants, ABI, prompts, utilities
231
+ contracts/ OPMRegistry.sol + Hardhat config, tests, deploy script
232
+ scanner/ AI agents, in-memory queue, Fileverse upload, report formatter
233
+ cli/ Ink-based terminal UI
234
+ commands/ push, install, audit, info, author-view, passthrough
235
+ components/ Header, StatusLine, RiskBadge, PackageCard, AuthorInfo
236
+ services/ contract, ens, osv, signature, chainpatrol, fileverse, avatar
237
+ ```
238
+
239
+ ## Environment Variables
240
+
241
+ | Variable | Required | Description |
242
+ |----------|----------|-------------|
243
+ | `OPM_PRIVATE_KEY` | Yes | Author's Ethereum private key for signing |
244
+ | `AGENT_PRIVATE_KEY` | Yes | Agent wallet key for contract gas on Base Sepolia |
245
+ | `CONTRACT_ADDRESS` | Yes | Deployed OPMRegistry contract address |
246
+ | `OPENAI_API_KEY` | * | OpenAI API key (auto-selects gpt-4.1 variants) |
247
+ | `OPENROUTER_API_KEY` | * | OpenRouter API key (multi-model: Claude, Gemini, DeepSeek) |
248
+ | `CHAINPATROL_API_KEY` | Yes | ChainPatrol API key for blocklist checks |
249
+ | `BASE_SEPOLIA_RPC_URL` | No | Base Sepolia RPC (default: https://sepolia.base.org) |
250
+ | `ETH_MAINNET_RPC_URL` | No | Mainnet RPC for ENS (default: https://eth.llamarpc.com) |
251
+ | `ETH_SEPOLIA_RPC_URL` | No | Sepolia RPC for ENS (default: public endpoint) |
252
+ | `FILEVERSE_API_KEY` | Yes | Fileverse API key from ddocs.new Developer Mode |
253
+ | `FILEVERSE_API_URL` | No | Fileverse local API URL (default: http://localhost:8001) |
254
+ | `NPM_TOKEN` | No | npm automation token (alternative to `--token` flag) |
255
+
256
+ \* One of `OPENAI_API_KEY` or `OPENROUTER_API_KEY` is required for AI scanning.
257
+
258
+ ## Run Contract Tests
259
+
260
+ ```bash
261
+ cd packages/contracts && npx hardhat test
262
+ ```
263
+
264
+ ## License
265
+
266
+ MIT