@r4security/sdk 0.0.2

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.
package/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # @r4security/sdk
2
+
3
+ Official R4 SDK for Node.js. Provides zero-trust programmatic access to R4 vault secrets for agent runtimes.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @r4security/sdk
9
+ ```
10
+
11
+ Requires Node.js >= 18.0.0 (uses native `fetch`).
12
+ The published SDK is self-contained and does not require any other R4 package at runtime.
13
+
14
+ ## Quick Start
15
+
16
+ ```typescript
17
+ import R4 from '@r4security/sdk'
18
+
19
+ const r4 = await R4.create({
20
+ apiKey: 'agent_access_key.secret',
21
+ privateKeyPath: './agent-private-key.pem',
22
+ })
23
+
24
+ const password = r4.env.PRODUCTION_DB_PASSWORD
25
+ ```
26
+
27
+ ## API
28
+
29
+ ### `R4.create(config): Promise<R4>`
30
+
31
+ Static factory that creates and initializes an R4 instance. This is the recommended entry point.
32
+
33
+ ### `r4.env: Record<string, string>`
34
+
35
+ Access project environment variables as a flat key-value map. Keys are `SCREAMING_SNAKE_CASE` (`VAULT_ITEM_NAME_FIELD_NAME`).
36
+
37
+ ### `r4.refresh(): Promise<void>`
38
+
39
+ Re-fetches environment variables from the API. Useful for long-running processes that need fresh secrets.
40
+
41
+ ## Configuration
42
+
43
+ | Option | Required | Description |
44
+ | ----------- | -------- | ------------------------------------------------- |
45
+ | `apiKey` | Yes | AGENT-scoped API key in format `{accessKey}.{secret}` |
46
+ | `privateKey` | No | PEM-encoded RSA private key kept locally by the agent |
47
+ | `privateKeyPath` | No | Path to the PEM-encoded RSA private key |
48
+ | `projectId` | No | Optional project ID filter for accessible vaults |
49
+ | `dev` | No | Use `https://dev.r4.dev` when `baseUrl` is not set |
50
+ | `trustStorePath` | No | Optional path for the signer trust-store JSON |
51
+ | `baseUrl` | No | API base URL (defaults to `https://r4.dev`) |
52
+
53
+ You must provide either `privateKey` or `privateKeyPath`.
54
+ When both `dev` and `baseUrl` are provided, `baseUrl` wins.
55
+
56
+ ## How It Works
57
+
58
+ 1. Authenticates with an AGENT-scoped API key
59
+ 2. Registers the runtime's public key through `POST /api/v1/machine/vault/public-key`
60
+ 3. Lists accessible vaults through the machine API
61
+ 4. Fetches each vault's wrapped DEK and signed org user-key directory plus transparency proof
62
+ 5. Verifies the signed org directory, checks the append-only transparency proof, and continuity-pins the signer in the local trust store
63
+ 6. Verifies wrapped-DEK signatures against that authenticated signer set
64
+ 7. Unwraps the DEK locally with the agent private key
65
+ 8. Verifies signed vault summary/detail checkpoints, then decrypts field ciphertext locally into a flat `SCREAMING_SNAKE_CASE` env map
66
+
67
+ ## Trust Store
68
+
69
+ The SDK trust store persists:
70
+
71
+ - continuity pins for org-directory signer/user keys
72
+ - append-only transparency head pins for each org directory
73
+ - rollback pins for signed metadata versions
74
+
75
+ In production `r4.dev` environments, the first trusted org-directory head is
76
+ now anchored to the public witness at `https://transparency.r4.dev` and
77
+ verified with the embedded witness root before the SDK accepts the API's first
78
+ signer set. Later directory updates must still present a valid append-only
79
+ proof from the pinned head, later signer rotations must present continuity
80
+ signatures, and every vault-specific signer subset must match the signed org
81
+ directory returned by the machine API.
82
+
83
+ The remaining gap is that the public witness still lives in the same AWS
84
+ account, so this is stronger than API-only TOFU but not yet a fully independent
85
+ external auditor or gossip network.
86
+ By default the SDK stores this beside the private key as `<privateKeyPath>.trust.json`,
87
+ or in `./.r4-trust-store.json` when the private key is passed inline.
88
+
89
+ ## Development
90
+
91
+ ```bash
92
+ pnpm run test # Run mocked zero-trust SDK tests
93
+ pnpm run build # Build with tsup
94
+ pnpm run clean # Remove lib/
95
+ ```