npl-presence-sdk 1.0.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/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # @nextera.one/npl-sdk
2
+
3
+ TypeScript SDK for **NPL (Network Presence Layer)** - cryptographic presence verification.
4
+
5
+ > "IP moves packets. DNS finds locators. NPL proves presence."
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ npm install @nextera.one/npl-sdk
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```typescript
16
+ import {
17
+ createPresence,
18
+ computeNplId,
19
+ signPresence,
20
+ validatePresence,
21
+ generateKeypair,
22
+ } from '@nextera.one/npl-sdk';
23
+
24
+ // Generate a keypair
25
+ const keypair = await generateKeypair();
26
+
27
+ // Create a presence object
28
+ const presence = createPresence('device:sensor-001', ['net', 'log'], 5);
29
+
30
+ // Compute NPL-ID
31
+ const nplId = computeNplId(presence);
32
+ console.log('NPL-ID:', nplId);
33
+
34
+ // Sign the presence
35
+ const signedPresence = await signPresence(presence, keypair);
36
+
37
+ // Validate
38
+ const result = validatePresence(signedPresence);
39
+ console.log('Valid:', result.ok);
40
+ ```
41
+
42
+ ## API Reference
43
+
44
+ ### Presence Objects
45
+
46
+ | Function | Description |
47
+ | ------------------------------------------------- | ----------------------------- |
48
+ | `createPresence(subject, scopes?, ttl?, anchor?)` | Create a new presence object |
49
+ | `computeNplId(presence)` | Compute NPL-ID hash |
50
+ | `signPresence(presence, keypair)` | Sign with Ed25519 |
51
+ | `validatePresence(presence)` | Validate structure and timing |
52
+ | `isPresenceValid(presence)` | Check if within time window |
53
+
54
+ ### Resolution Records
55
+
56
+ | Function | Description |
57
+ | ------------------------------------------------ | ------------------------------- |
58
+ | `createResolutionRecord(nplId, ips, ttl?, ctx?)` | Create RR binding NPL-ID to IPs |
59
+ | `signResolutionRecord(rr, keypair)` | Sign the resolution record |
60
+ | `validateResolutionRecord(rr)` | Validate RR structure |
61
+
62
+ ### Cryptographic Utilities
63
+
64
+ | Function | Description |
65
+ | --------------------------------------- | ------------------------ |
66
+ | `generateKeypair()` | Generate Ed25519 keypair |
67
+ | `sign(message, privateKey)` | Sign raw bytes |
68
+ | `verify(signature, message, publicKey)` | Verify signature |
69
+ | `hash(data)` | SHA-256 hash |
70
+
71
+ ## License
72
+
73
+ Apache-2.0
@@ -0,0 +1,203 @@
1
+ /**
2
+ * NPL SDK - Core Type Definitions
3
+ * Network Presence Layer - Cryptographically Verifiable Presence
4
+ */
5
+ type NplAlgorithm = 'ed25519';
6
+ type NplScope = 'net' | 'log' | 'auth' | 'exec';
7
+ type AnchorType = 'geo' | 'carrier' | 'org' | 'dns' | 'hardware' | 'jur';
8
+ interface NplPayload {
9
+ sub: string;
10
+ iat: string;
11
+ exp: string;
12
+ scp: NplScope[];
13
+ nonce: string;
14
+ anchor?: AnchorType;
15
+ }
16
+ interface NplPresenceObject {
17
+ ver: 1;
18
+ alg: NplAlgorithm;
19
+ pay: NplPayload;
20
+ sig?: string;
21
+ }
22
+ interface NplResolutionRecord {
23
+ ver: 1;
24
+ typ: 'NPL-RR';
25
+ alg: NplAlgorithm;
26
+ npl_id: string;
27
+ ips: Array<{
28
+ ip: string;
29
+ fam: 4 | 6;
30
+ }>;
31
+ nbf: string;
32
+ exp: string;
33
+ ctx?: {
34
+ port?: number[];
35
+ proto?: ('tcp' | 'quic' | 'udp')[];
36
+ scope?: NplScope[];
37
+ };
38
+ sig?: string;
39
+ }
40
+ interface NplClientHello {
41
+ typ: 'NPL-CH';
42
+ ver: 1;
43
+ cap: string[];
44
+ want: string[];
45
+ ts: string;
46
+ }
47
+ interface NplServerChallenge {
48
+ typ: 'NPL-SC';
49
+ ver: 1;
50
+ cookie: string;
51
+ ttl_ms: number;
52
+ srv_ts: string;
53
+ req: {
54
+ need: string[];
55
+ scope: NplScope[];
56
+ };
57
+ }
58
+ interface NplClientProof {
59
+ typ: 'NPL-CP';
60
+ ver: 1;
61
+ cookie: string;
62
+ presence: NplPresenceObject;
63
+ rr?: NplResolutionRecord;
64
+ bind: {
65
+ sid: string;
66
+ cnonce: string;
67
+ ts: string;
68
+ };
69
+ sig: string;
70
+ }
71
+ interface ValidationResult {
72
+ ok: boolean;
73
+ errors: string[];
74
+ }
75
+ interface NplKeypair {
76
+ privateKey: Uint8Array;
77
+ publicKey: Uint8Array;
78
+ }
79
+
80
+ /**
81
+ * NPL SDK - Canonicalization
82
+ * Deterministic JSON serialization for NPL-ID computation
83
+ */
84
+ /**
85
+ * Canonicalize an object for hashing.
86
+ * Keys are sorted lexicographically, no whitespace.
87
+ */
88
+ declare function canonicalize(obj: unknown): string;
89
+
90
+ /**
91
+ * NPL SDK - Cryptographic Operations
92
+ * Ed25519 signing and verification using @noble/ed25519
93
+ */
94
+
95
+ /**
96
+ * Generate a new Ed25519 keypair
97
+ */
98
+ declare function generateKeypair(): Promise<NplKeypair>;
99
+ /**
100
+ * Sign a message with Ed25519
101
+ */
102
+ declare function sign(message: Uint8Array, privateKey: Uint8Array): Promise<Uint8Array>;
103
+ /**
104
+ * Verify an Ed25519 signature
105
+ */
106
+ declare function verify(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): Promise<boolean>;
107
+ /**
108
+ * Compute SHA-256 hash
109
+ */
110
+ declare function hash(data: Uint8Array): Uint8Array;
111
+ /**
112
+ * Convert bytes to hex string
113
+ */
114
+ declare function bytesToHex(bytes: Uint8Array): string;
115
+ /**
116
+ * Convert hex string to bytes
117
+ */
118
+ declare function hexToBytes(hex: string): Uint8Array;
119
+ /**
120
+ * Base64url encode
121
+ */
122
+ declare function base64urlEncode(bytes: Uint8Array): string;
123
+ /**
124
+ * Base64url decode
125
+ */
126
+ declare function base64urlDecode(str: string): Uint8Array;
127
+ /**
128
+ * Generate random bytes
129
+ */
130
+ declare function randomBytes(length: number): Uint8Array;
131
+ /**
132
+ * Generate random nonce as hex string
133
+ */
134
+ declare function generateNonce(length?: number): string;
135
+
136
+ /**
137
+ * NPL SDK - Presence Object
138
+ * Create, validate, and sign NPL Presence Objects
139
+ */
140
+
141
+ /**
142
+ * Create a new NPL Presence Object
143
+ */
144
+ declare function createPresence(subject: string, scopes?: NplScope[], ttlMinutes?: number, anchor?: AnchorType): NplPresenceObject;
145
+ /**
146
+ * Compute NPL-ID from a presence object.
147
+ * NPL-ID = SHA256("NPL:v1:" || Canonical(Payload))
148
+ */
149
+ declare function computeNplId(presence: NplPresenceObject): string;
150
+ /**
151
+ * Sign a presence object
152
+ */
153
+ declare function signPresence(presence: NplPresenceObject, keypair: NplKeypair): Promise<NplPresenceObject>;
154
+ /**
155
+ * Validate an NPL Presence Object structure
156
+ */
157
+ declare function validatePresence(presence: NplPresenceObject): ValidationResult;
158
+ /**
159
+ * Check if presence is currently valid (within time window)
160
+ */
161
+ declare function isPresenceValid(presence: NplPresenceObject): boolean;
162
+ /**
163
+ * Get remaining TTL in seconds
164
+ */
165
+ declare function getPresenceTtl(presence: NplPresenceObject): number;
166
+ /**
167
+ * Format presence for display
168
+ */
169
+ declare function formatPresence(presence: NplPresenceObject): string;
170
+
171
+ /**
172
+ * NPL SDK - Resolution Records
173
+ * Create and validate NPL Resolution Records
174
+ */
175
+
176
+ /**
177
+ * Create a Resolution Record
178
+ */
179
+ declare function createResolutionRecord(nplId: string, ips: Array<{
180
+ ip: string;
181
+ fam: 4 | 6;
182
+ }>, ttlMinutes?: number, ctx?: {
183
+ port?: number[];
184
+ proto?: ('tcp' | 'quic' | 'udp')[];
185
+ scope?: NplScope[];
186
+ }): NplResolutionRecord;
187
+ /**
188
+ * Sign a Resolution Record
189
+ */
190
+ declare function signResolutionRecord(rr: NplResolutionRecord, keypair: NplKeypair): Promise<NplResolutionRecord>;
191
+ /**
192
+ * Validate a Resolution Record structure
193
+ */
194
+ declare function validateResolutionRecord(rr: NplResolutionRecord): {
195
+ ok: boolean;
196
+ errors: string[];
197
+ };
198
+ /**
199
+ * Check if resolution record is currently valid
200
+ */
201
+ declare function isResolutionRecordValid(rr: NplResolutionRecord): boolean;
202
+
203
+ export { type AnchorType, type NplAlgorithm, type NplClientHello, type NplClientProof, type NplKeypair, type NplPayload, type NplPresenceObject, type NplResolutionRecord, type NplScope, type NplServerChallenge, type ValidationResult, base64urlDecode, base64urlEncode, bytesToHex, canonicalize, computeNplId, createPresence, createResolutionRecord, formatPresence, generateKeypair, generateNonce, getPresenceTtl, hash, hexToBytes, isPresenceValid, isResolutionRecordValid, randomBytes, sign, signPresence, signResolutionRecord, validatePresence, validateResolutionRecord, verify };
@@ -0,0 +1,203 @@
1
+ /**
2
+ * NPL SDK - Core Type Definitions
3
+ * Network Presence Layer - Cryptographically Verifiable Presence
4
+ */
5
+ type NplAlgorithm = 'ed25519';
6
+ type NplScope = 'net' | 'log' | 'auth' | 'exec';
7
+ type AnchorType = 'geo' | 'carrier' | 'org' | 'dns' | 'hardware' | 'jur';
8
+ interface NplPayload {
9
+ sub: string;
10
+ iat: string;
11
+ exp: string;
12
+ scp: NplScope[];
13
+ nonce: string;
14
+ anchor?: AnchorType;
15
+ }
16
+ interface NplPresenceObject {
17
+ ver: 1;
18
+ alg: NplAlgorithm;
19
+ pay: NplPayload;
20
+ sig?: string;
21
+ }
22
+ interface NplResolutionRecord {
23
+ ver: 1;
24
+ typ: 'NPL-RR';
25
+ alg: NplAlgorithm;
26
+ npl_id: string;
27
+ ips: Array<{
28
+ ip: string;
29
+ fam: 4 | 6;
30
+ }>;
31
+ nbf: string;
32
+ exp: string;
33
+ ctx?: {
34
+ port?: number[];
35
+ proto?: ('tcp' | 'quic' | 'udp')[];
36
+ scope?: NplScope[];
37
+ };
38
+ sig?: string;
39
+ }
40
+ interface NplClientHello {
41
+ typ: 'NPL-CH';
42
+ ver: 1;
43
+ cap: string[];
44
+ want: string[];
45
+ ts: string;
46
+ }
47
+ interface NplServerChallenge {
48
+ typ: 'NPL-SC';
49
+ ver: 1;
50
+ cookie: string;
51
+ ttl_ms: number;
52
+ srv_ts: string;
53
+ req: {
54
+ need: string[];
55
+ scope: NplScope[];
56
+ };
57
+ }
58
+ interface NplClientProof {
59
+ typ: 'NPL-CP';
60
+ ver: 1;
61
+ cookie: string;
62
+ presence: NplPresenceObject;
63
+ rr?: NplResolutionRecord;
64
+ bind: {
65
+ sid: string;
66
+ cnonce: string;
67
+ ts: string;
68
+ };
69
+ sig: string;
70
+ }
71
+ interface ValidationResult {
72
+ ok: boolean;
73
+ errors: string[];
74
+ }
75
+ interface NplKeypair {
76
+ privateKey: Uint8Array;
77
+ publicKey: Uint8Array;
78
+ }
79
+
80
+ /**
81
+ * NPL SDK - Canonicalization
82
+ * Deterministic JSON serialization for NPL-ID computation
83
+ */
84
+ /**
85
+ * Canonicalize an object for hashing.
86
+ * Keys are sorted lexicographically, no whitespace.
87
+ */
88
+ declare function canonicalize(obj: unknown): string;
89
+
90
+ /**
91
+ * NPL SDK - Cryptographic Operations
92
+ * Ed25519 signing and verification using @noble/ed25519
93
+ */
94
+
95
+ /**
96
+ * Generate a new Ed25519 keypair
97
+ */
98
+ declare function generateKeypair(): Promise<NplKeypair>;
99
+ /**
100
+ * Sign a message with Ed25519
101
+ */
102
+ declare function sign(message: Uint8Array, privateKey: Uint8Array): Promise<Uint8Array>;
103
+ /**
104
+ * Verify an Ed25519 signature
105
+ */
106
+ declare function verify(signature: Uint8Array, message: Uint8Array, publicKey: Uint8Array): Promise<boolean>;
107
+ /**
108
+ * Compute SHA-256 hash
109
+ */
110
+ declare function hash(data: Uint8Array): Uint8Array;
111
+ /**
112
+ * Convert bytes to hex string
113
+ */
114
+ declare function bytesToHex(bytes: Uint8Array): string;
115
+ /**
116
+ * Convert hex string to bytes
117
+ */
118
+ declare function hexToBytes(hex: string): Uint8Array;
119
+ /**
120
+ * Base64url encode
121
+ */
122
+ declare function base64urlEncode(bytes: Uint8Array): string;
123
+ /**
124
+ * Base64url decode
125
+ */
126
+ declare function base64urlDecode(str: string): Uint8Array;
127
+ /**
128
+ * Generate random bytes
129
+ */
130
+ declare function randomBytes(length: number): Uint8Array;
131
+ /**
132
+ * Generate random nonce as hex string
133
+ */
134
+ declare function generateNonce(length?: number): string;
135
+
136
+ /**
137
+ * NPL SDK - Presence Object
138
+ * Create, validate, and sign NPL Presence Objects
139
+ */
140
+
141
+ /**
142
+ * Create a new NPL Presence Object
143
+ */
144
+ declare function createPresence(subject: string, scopes?: NplScope[], ttlMinutes?: number, anchor?: AnchorType): NplPresenceObject;
145
+ /**
146
+ * Compute NPL-ID from a presence object.
147
+ * NPL-ID = SHA256("NPL:v1:" || Canonical(Payload))
148
+ */
149
+ declare function computeNplId(presence: NplPresenceObject): string;
150
+ /**
151
+ * Sign a presence object
152
+ */
153
+ declare function signPresence(presence: NplPresenceObject, keypair: NplKeypair): Promise<NplPresenceObject>;
154
+ /**
155
+ * Validate an NPL Presence Object structure
156
+ */
157
+ declare function validatePresence(presence: NplPresenceObject): ValidationResult;
158
+ /**
159
+ * Check if presence is currently valid (within time window)
160
+ */
161
+ declare function isPresenceValid(presence: NplPresenceObject): boolean;
162
+ /**
163
+ * Get remaining TTL in seconds
164
+ */
165
+ declare function getPresenceTtl(presence: NplPresenceObject): number;
166
+ /**
167
+ * Format presence for display
168
+ */
169
+ declare function formatPresence(presence: NplPresenceObject): string;
170
+
171
+ /**
172
+ * NPL SDK - Resolution Records
173
+ * Create and validate NPL Resolution Records
174
+ */
175
+
176
+ /**
177
+ * Create a Resolution Record
178
+ */
179
+ declare function createResolutionRecord(nplId: string, ips: Array<{
180
+ ip: string;
181
+ fam: 4 | 6;
182
+ }>, ttlMinutes?: number, ctx?: {
183
+ port?: number[];
184
+ proto?: ('tcp' | 'quic' | 'udp')[];
185
+ scope?: NplScope[];
186
+ }): NplResolutionRecord;
187
+ /**
188
+ * Sign a Resolution Record
189
+ */
190
+ declare function signResolutionRecord(rr: NplResolutionRecord, keypair: NplKeypair): Promise<NplResolutionRecord>;
191
+ /**
192
+ * Validate a Resolution Record structure
193
+ */
194
+ declare function validateResolutionRecord(rr: NplResolutionRecord): {
195
+ ok: boolean;
196
+ errors: string[];
197
+ };
198
+ /**
199
+ * Check if resolution record is currently valid
200
+ */
201
+ declare function isResolutionRecordValid(rr: NplResolutionRecord): boolean;
202
+
203
+ export { type AnchorType, type NplAlgorithm, type NplClientHello, type NplClientProof, type NplKeypair, type NplPayload, type NplPresenceObject, type NplResolutionRecord, type NplScope, type NplServerChallenge, type ValidationResult, base64urlDecode, base64urlEncode, bytesToHex, canonicalize, computeNplId, createPresence, createResolutionRecord, formatPresence, generateKeypair, generateNonce, getPresenceTtl, hash, hexToBytes, isPresenceValid, isResolutionRecordValid, randomBytes, sign, signPresence, signResolutionRecord, validatePresence, validateResolutionRecord, verify };