@huitl/sdk 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fabio Seva
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,192 @@
1
+ # @huitl/sdk
2
+
3
+ Built by [HUITL Protocol](https://huitlprotocol.com) — the open verification standard for physical objects.
4
+
5
+ HUITL Protocol proves physical objects are real using standard NFC chips. Every tap produces a unique, one-time cryptographic signature that can never be replicated or reused. This SDK is the core of the protocol — MIT-licensed, zero runtime dependencies, production-ready.
6
+
7
+ The cheapest, simplest path from NFC chip tap to cryptographic proof. Standard hardware. Open protocol. Developer-first.
8
+
9
+ Created by **Fabio Seva** — founder of HUITL Protocol.
10
+
11
+ For questions, partnerships, or enterprise inquiries: [seva@huitlprotocol.com](mailto:seva@huitlprotocol.com)
12
+
13
+ ### Use Cases & Templates
14
+
15
+ | Use Case | Description | Template | Status |
16
+ |----------|-------------|----------|--------|
17
+ | **Product Authentication** | Luxury goods, pharma, wine & spirits, electronics | `huitl init --template auth` | Coming soon |
18
+ | **Digital Product Passport** | EU DPP compliance (batteries Feb 2027, textiles 2027) | `huitl init --template dpp` | Coming soon |
19
+ | **Ticketing & Membership** | Event tickets and membership cards that can't be duplicated | `huitl init --template ticketing` | Coming soon |
20
+ | **Proof of Presence** | Retail, events, tourist sites — no GPS spoofing possible | `huitl init --template presence` | Coming soon |
21
+ | **Anti-Counterfeiting** | Supply chain verification at every touchpoint | `huitl init --template anti-counterfeit` | Coming soon |
22
+
23
+ ## Try It Now
24
+
25
+ No chips, no hardware, no setup. Just run:
26
+
27
+ ```bash
28
+ npx huitl verify "https://huitlprotocol.com/tap?picc_data=2C309D265CFFD369A4B62C6B9F40B669&cmac=6A6595D519DF58BF" --key 00000000000000000000000000000000
29
+ ```
30
+
31
+ ```json
32
+ { "valid": true, "uid": "04112233445566", "readCounter": 42 }
33
+ ```
34
+
35
+ That's a real cryptographic verification — the same math that runs on every NFC tap. The URL contains an encrypted chip identity and a one-time signature. The SDK decrypts, derives a session key, and validates the CMAC. All in one command.
36
+
37
+ ## Installation
38
+
39
+ ```bash
40
+ npm install @huitl/sdk
41
+ ```
42
+
43
+ ## Quick Start
44
+
45
+ ```typescript
46
+ import { parseSunMessage, verifySun } from "@huitl/sdk";
47
+
48
+ // When a user taps an NFC chip, their phone opens a URL like:
49
+ // https://yourapp.com/tap?picc_data=EF963FF7828658A599F3041510671E88&cmac=94EED9EE65337086
50
+
51
+ const sdmKey = Buffer.from("00000000000000000000000000000000", "hex");
52
+ const tapUrl = "https://yourapp.com/tap?picc_data=EF963FF7...&cmac=94EED9EE...";
53
+
54
+ const parsed = parseSunMessage(tapUrl);
55
+ const result = verifySun(sdmKey, parsed.encPiccData, parsed.cmac, parsed.macInput);
56
+
57
+ console.log(result);
58
+ // { valid: true, uid: "04112233445566", readCounter: 42 }
59
+ ```
60
+
61
+ ## API Reference
62
+
63
+ ### `parseSunMessage(url: string): SunMessage`
64
+
65
+ Parse a SUN message URL from an NTAG 424 DNA chip tap.
66
+
67
+ ```typescript
68
+ const { encPiccData, cmac, macInput } = parseSunMessage(url);
69
+ ```
70
+
71
+ ### `verifySun(sdmKey, encPiccData, cmac, macInput): VerifyResult`
72
+
73
+ Full end-to-end SUN verification: decrypts PICC data, derives session key, computes and compares CMAC using `crypto.timingSafeEqual`.
74
+
75
+ ```typescript
76
+ const result = verifySun(sdmKey, encPiccData, cmac, macInput);
77
+ // { valid: boolean, uid: string, readCounter: number }
78
+ ```
79
+
80
+ ### `decryptPiccData(key: Buffer, enc: Buffer): PiccData`
81
+
82
+ Decrypt encrypted PICC data (AES-128-CBC, zero IV) to extract the chip UID and read counter.
83
+
84
+ ### `deriveSessionKey(sdmKey, uid, counter, isMac): Buffer`
85
+
86
+ Derive a session key for SDM MAC or ENC operations per NXP AN12196.
87
+
88
+ ### `aesCmac(key: Buffer, msg: Buffer): Buffer`
89
+
90
+ Compute AES-CMAC per RFC 4493.
91
+
92
+ ## CLI
93
+
94
+ ### `huitl verify <url> --key <hex>`
95
+
96
+ Verify a SUN message URL. Outputs JSON, exits 0 if valid.
97
+
98
+ ```bash
99
+ huitl verify "https://example.com/tap?picc_data=...&cmac=..." --key 00000000000000000000000000000000
100
+ ```
101
+
102
+ ### `huitl keygen [--count N]`
103
+
104
+ Generate random AES-128 key + UID pairs for testing.
105
+
106
+ ```bash
107
+ huitl keygen
108
+ # { "uid": "04A1B2C3D4E5F6", "aesKey": "1234567890ABCDEF1234567890ABCDEF" }
109
+
110
+ huitl keygen --count 5
111
+ # [ { "uid": "...", "aesKey": "..." }, ... ]
112
+ ```
113
+
114
+ ### `huitl dev --local`
115
+
116
+ Start a local development server with SQLite for testing NFC verification.
117
+
118
+ ```bash
119
+ huitl dev --local
120
+ # Server running at http://localhost:3000
121
+ # Tap URL: http://localhost:3000/tap?picc_data=<32hex>&cmac=<16hex>
122
+ ```
123
+
124
+ Endpoints:
125
+ - `GET /tap?picc_data=...&cmac=...` — Verify a tap
126
+ - `GET /api/chips` — List registered test chips
127
+ - `POST /api/chips` — Register a test chip `{ "uid": "...", "aesKey": "...", "name": "..." }`
128
+
129
+ ### `huitl init [directory]`
130
+
131
+ Scaffold a new HUITL project from a starter template.
132
+
133
+ ```bash
134
+ huitl init my-app
135
+ cd my-app && npm install
136
+ ```
137
+
138
+ ## Types
139
+
140
+ ```typescript
141
+ interface PiccData {
142
+ uid: string; // 7-byte UID as uppercase hex
143
+ readCounter: number; // SDM read counter
144
+ }
145
+
146
+ interface VerifyResult {
147
+ valid: boolean;
148
+ uid: string;
149
+ readCounter: number;
150
+ }
151
+
152
+ interface SunMessage {
153
+ encPiccData: Buffer; // Encrypted PICC data (16 bytes)
154
+ cmac: Buffer; // Truncated CMAC (8 bytes)
155
+ macInput: Buffer; // URL portion used as MAC input
156
+ }
157
+ ```
158
+
159
+ ## How It Works
160
+
161
+ 1. An NTAG 424 DNA chip is configured with an AES-128 key and SUN (Secure Unique NFC) messaging
162
+ 2. When tapped, the chip produces a URL containing encrypted PICC data and a CMAC signature
163
+ 3. The PICC data contains the chip's unique 7-byte UID and a rolling read counter
164
+ 4. A session key is derived from the SDM key, UID, and counter
165
+ 5. The CMAC is computed over the URL content using the session key
166
+ 6. Verification checks: correct decryption, valid CMAC, and counter increment (anti-replay)
167
+
168
+ Each tap produces a unique, cryptographically bound message that cannot be replicated or replayed.
169
+
170
+ ## Architecture
171
+
172
+ ```
173
+ ┌──────────────────────────────────────┐
174
+ │ Your Application │
175
+ └──────────────────┬───────────────────┘
176
+ │ uses
177
+ ┌──────────────────▼───────────────────┐
178
+ │ @huitl/sdk │
179
+ │ parseSunMessage() │ verifySun() │
180
+ │ decryptPiccData() │ deriveSessionKey │
181
+ │ aesCmac() │ TypeScript types │
182
+ │ MIT Licensed │
183
+ └──────────────────────────────────────┘
184
+ ```
185
+
186
+ ## Contributing
187
+
188
+ See [CONTRIBUTING.md](./CONTRIBUTING.md).
189
+
190
+ ## License
191
+
192
+ MIT