@fleetmesh/leaf 0.2.0 → 0.2.1
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/dist/did.d.ts +14 -0
- package/dist/did.js +36 -0
- package/dist/encoding.d.ts +17 -4
- package/dist/encoding.js +17 -6
- package/package.json +6 -2
- package/src/did.ts +39 -0
- package/src/encoding.ts +26 -6
package/dist/did.d.ts
CHANGED
|
@@ -57,3 +57,17 @@ export declare function nodeOfDid(didOrKid: string): string;
|
|
|
57
57
|
export declare function didcommService(desc: Descriptor): Service | null;
|
|
58
58
|
/** The node profile a did:nostr resolver emits for a kind 11801, with its aliases. */
|
|
59
59
|
export declare function document(desc: Descriptor): Document;
|
|
60
|
+
/** Hex of SHA-256(seed), extended with SHA-256(seed + ":1") when more than 64
|
|
61
|
+
* hex digits are asked for. The one derivation every package uses for
|
|
62
|
+
* synthetic, reproducible identifiers. */
|
|
63
|
+
export declare function deterministicHexHash(seed: string, length?: number): string;
|
|
64
|
+
/** A DNS-label-safe slug for a human name; `fallback` when nothing survives. */
|
|
65
|
+
export declare function slugifyDidName(raw: string, fallback?: string): string;
|
|
66
|
+
export interface DidWebDomain {
|
|
67
|
+
domain: string;
|
|
68
|
+
didWeb: string;
|
|
69
|
+
wellKnownUrl: string;
|
|
70
|
+
}
|
|
71
|
+
/** Normalises user input (a bare domain, a URL, or a did:web) to the did:web
|
|
72
|
+
* triple. Input with no dot becomes `<slug>.<defaultZone>`. */
|
|
73
|
+
export declare function normalizeDidWebDomain(rawInput: string, defaultZone?: string, slugFallback?: string): DidWebDomain;
|
package/dist/did.js
CHANGED
|
@@ -12,6 +12,7 @@ import { schnorr } from '@noble/curves/secp256k1.js';
|
|
|
12
12
|
import { base58 } from '@scure/base';
|
|
13
13
|
import { DESCRIPTOR, DID } from './constants.js';
|
|
14
14
|
import { b64u, b64uDec, bytesToHex, canonicalJson, fromUtf8, hexToBytes, isHex, utf8 } from './encoding.js';
|
|
15
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
15
16
|
import { KEY_KINDS, multikey, parseMultikey } from './keys.js';
|
|
16
17
|
const CODEC = {
|
|
17
18
|
node: hexToBytes(DID.multicodec.node),
|
|
@@ -219,3 +220,38 @@ export function document(desc) {
|
|
|
219
220
|
service: services,
|
|
220
221
|
};
|
|
221
222
|
}
|
|
223
|
+
// ---------------------------------------------------------------------------
|
|
224
|
+
// did:web and deterministic identifiers shared by the protocol packages.
|
|
225
|
+
/** Hex of SHA-256(seed), extended with SHA-256(seed + ":1") when more than 64
|
|
226
|
+
* hex digits are asked for. The one derivation every package uses for
|
|
227
|
+
* synthetic, reproducible identifiers. */
|
|
228
|
+
export function deterministicHexHash(seed, length = 64) {
|
|
229
|
+
const full = bytesToHex(sha256(utf8(seed)));
|
|
230
|
+
if (length <= 64)
|
|
231
|
+
return full.slice(0, length);
|
|
232
|
+
return (full + bytesToHex(sha256(utf8(`${seed}:1`)))).slice(0, length);
|
|
233
|
+
}
|
|
234
|
+
/** A DNS-label-safe slug for a human name; `fallback` when nothing survives. */
|
|
235
|
+
export function slugifyDidName(raw, fallback = 'fleetmesh-entity') {
|
|
236
|
+
const cleaned = (raw || '')
|
|
237
|
+
.toLowerCase()
|
|
238
|
+
.replace(/[^a-z0-9-_]+/g, '-')
|
|
239
|
+
.replace(/-+/g, '-')
|
|
240
|
+
.replace(/^-|-$/g, '');
|
|
241
|
+
return cleaned || fallback;
|
|
242
|
+
}
|
|
243
|
+
/** Normalises user input (a bare domain, a URL, or a did:web) to the did:web
|
|
244
|
+
* triple. Input with no dot becomes `<slug>.<defaultZone>`. */
|
|
245
|
+
export function normalizeDidWebDomain(rawInput, defaultZone = 'fleetmesh.org', slugFallback = 'node') {
|
|
246
|
+
let clean = (rawInput || '').trim().toLowerCase();
|
|
247
|
+
if (clean.startsWith('did:web:'))
|
|
248
|
+
clean = clean.slice('did:web:'.length);
|
|
249
|
+
clean = clean
|
|
250
|
+
.replace(/^https?:\/\//, '')
|
|
251
|
+
.replace(/\/.*$/, '')
|
|
252
|
+
.replace(/:\d+$/, '')
|
|
253
|
+
.replace(/[^a-z0-9.-]/g, '');
|
|
254
|
+
if (!clean || !clean.includes('.'))
|
|
255
|
+
clean = `${slugifyDidName(rawInput || slugFallback, slugFallback)}.${defaultZone}`;
|
|
256
|
+
return { domain: clean, didWeb: `did:web:${clean}`, wellKnownUrl: `https://${clean}/.well-known/did.json` };
|
|
257
|
+
}
|
package/dist/encoding.d.ts
CHANGED
|
@@ -8,7 +8,20 @@ export declare function b64u(b: Uint8Array): string;
|
|
|
8
8
|
/** Strict base64url without padding: the alphabet only, and canonical (no
|
|
9
9
|
* trailing bits), so two strings never decode to the same bytes. */
|
|
10
10
|
export declare function b64uDec(s: string): Uint8Array;
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export declare
|
|
11
|
+
/** base64url (no padding) of a string's UTF-8 bytes: JWS-style segment encoding. */
|
|
12
|
+
export declare const b64uUtf8: (s: string) => string;
|
|
13
|
+
/** Inverse of `b64uUtf8`; strict, like `b64uDec`. */
|
|
14
|
+
export declare const b64uUtf8Dec: (s: string) => string;
|
|
15
|
+
export interface CanonicalJsonOptions {
|
|
16
|
+
/** `'integers'` (default) refuses any non-safe-integer number: Python spells 1.0 as
|
|
17
|
+
* "1.0" and JS as "1", so a float cannot be made to agree across runtimes and the
|
|
18
|
+
* caller must fix its data. `'js'` accepts finite floats and spells them as
|
|
19
|
+
* `JSON.stringify` does; this is the byte form the textile shards were published
|
|
20
|
+
* with and must keep. */
|
|
21
|
+
numbers?: 'integers' | 'js';
|
|
22
|
+
}
|
|
23
|
+
/** Canonical JSON: keys sorted by code point at every level (as Python
|
|
24
|
+
* `json.dumps(sort_keys=True, separators=(',', ':'))`), `undefined` members
|
|
25
|
+
* dropped, `undefined` array items spelled `null`, no whitespace, non-ASCII left as
|
|
26
|
+
* is. See `CanonicalJsonOptions.numbers` for the one semantic choice. */
|
|
27
|
+
export declare function canonicalJson(v: unknown, opts?: CanonicalJsonOptions): string;
|
package/dist/encoding.js
CHANGED
|
@@ -35,6 +35,10 @@ export function b64uDec(s) {
|
|
|
35
35
|
throw new Error('not canonical base64url');
|
|
36
36
|
return out;
|
|
37
37
|
}
|
|
38
|
+
/** base64url (no padding) of a string's UTF-8 bytes: JWS-style segment encoding. */
|
|
39
|
+
export const b64uUtf8 = (s) => b64u(utf8(s));
|
|
40
|
+
/** Inverse of `b64uUtf8`; strict, like `b64uDec`. */
|
|
41
|
+
export const b64uUtf8Dec = (s) => fromUtf8(b64uDec(s));
|
|
38
42
|
// Python sorts keys by code point; JS's default sort is by UTF-16 unit, which
|
|
39
43
|
// differs above the BMP.
|
|
40
44
|
function byCodePoint(a, b) {
|
|
@@ -46,23 +50,30 @@ function byCodePoint(a, b) {
|
|
|
46
50
|
}
|
|
47
51
|
return x.length - y.length;
|
|
48
52
|
}
|
|
49
|
-
/** Canonical JSON: sorted
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
|
|
53
|
+
/** Canonical JSON: keys sorted by code point at every level (as Python
|
|
54
|
+
* `json.dumps(sort_keys=True, separators=(',', ':'))`), `undefined` members
|
|
55
|
+
* dropped, `undefined` array items spelled `null`, no whitespace, non-ASCII left as
|
|
56
|
+
* is. See `CanonicalJsonOptions.numbers` for the one semantic choice. */
|
|
57
|
+
export function canonicalJson(v, opts = {}) {
|
|
58
|
+
const numbers = opts.numbers ?? 'integers';
|
|
53
59
|
if (v === null || typeof v === 'boolean' || typeof v === 'string')
|
|
54
60
|
return JSON.stringify(v);
|
|
55
61
|
if (typeof v === 'number') {
|
|
62
|
+
if (numbers === 'js') {
|
|
63
|
+
if (!Number.isFinite(v))
|
|
64
|
+
throw new Error('canonical JSON cannot carry NaN or Infinity');
|
|
65
|
+
return JSON.stringify(v);
|
|
66
|
+
}
|
|
56
67
|
if (!Number.isSafeInteger(v))
|
|
57
68
|
throw new Error('canonical JSON carries integers only');
|
|
58
69
|
return String(v);
|
|
59
70
|
}
|
|
60
71
|
if (Array.isArray(v))
|
|
61
|
-
return '[' + v.map((el) => canonicalJson(el === undefined ? null : el)).join(',') + ']';
|
|
72
|
+
return '[' + v.map((el) => canonicalJson(el === undefined ? null : el, opts)).join(',') + ']';
|
|
62
73
|
if (typeof v === 'object') {
|
|
63
74
|
const o = v;
|
|
64
75
|
return '{' + Object.keys(o).filter((k) => o[k] !== undefined).sort(byCodePoint)
|
|
65
|
-
.map((k) => JSON.stringify(k) + ':' + canonicalJson(o[k])).join(',') + '}';
|
|
76
|
+
.map((k) => JSON.stringify(k) + ':' + canonicalJson(o[k], opts)).join(',') + '}';
|
|
66
77
|
}
|
|
67
78
|
throw new Error('not JSON: ' + typeof v);
|
|
68
79
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fleetmesh/leaf",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Fleetmesh leaf profile in TypeScript: browsers and PWAs as mediated mesh leaves with NIP-59 gift wraps, DIDComm v2, and NIP-77 Negentropy reconciliation",
|
|
5
5
|
"license": "CC0-1.0",
|
|
6
6
|
"type": "module",
|
|
@@ -11,8 +11,12 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"default": "./dist/index.js"
|
|
13
13
|
},
|
|
14
|
-
"
|
|
14
|
+
"./*": {
|
|
15
|
+
"types": "./dist/*.d.ts",
|
|
16
|
+
"default": "./dist/*.js"
|
|
17
|
+
}
|
|
15
18
|
},
|
|
19
|
+
"sideEffects": false,
|
|
16
20
|
"files": [
|
|
17
21
|
"dist",
|
|
18
22
|
"src"
|
package/src/did.ts
CHANGED
|
@@ -12,6 +12,7 @@ import { schnorr } from '@noble/curves/secp256k1.js';
|
|
|
12
12
|
import { base58 } from '@scure/base';
|
|
13
13
|
import { DESCRIPTOR, DID } from './constants.ts';
|
|
14
14
|
import { b64u, b64uDec, bytesToHex, canonicalJson, fromUtf8, hexToBytes, isHex, utf8 } from './encoding.ts';
|
|
15
|
+
import { sha256 } from '@noble/hashes/sha2.js';
|
|
15
16
|
import { type KeyKind, KEY_KINDS, multikey, parseMultikey } from './keys.ts';
|
|
16
17
|
|
|
17
18
|
export interface ServiceEndpoint { uri?: string; accept?: string[]; routingKeys?: string[] }
|
|
@@ -220,3 +221,41 @@ export function document(desc: Descriptor): Document {
|
|
|
220
221
|
service: services,
|
|
221
222
|
};
|
|
222
223
|
}
|
|
224
|
+
|
|
225
|
+
// ---------------------------------------------------------------------------
|
|
226
|
+
// did:web and deterministic identifiers shared by the protocol packages.
|
|
227
|
+
|
|
228
|
+
/** Hex of SHA-256(seed), extended with SHA-256(seed + ":1") when more than 64
|
|
229
|
+
* hex digits are asked for. The one derivation every package uses for
|
|
230
|
+
* synthetic, reproducible identifiers. */
|
|
231
|
+
export function deterministicHexHash(seed: string, length = 64): string {
|
|
232
|
+
const full = bytesToHex(sha256(utf8(seed)));
|
|
233
|
+
if (length <= 64) return full.slice(0, length);
|
|
234
|
+
return (full + bytesToHex(sha256(utf8(`${seed}:1`)))).slice(0, length);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** A DNS-label-safe slug for a human name; `fallback` when nothing survives. */
|
|
238
|
+
export function slugifyDidName(raw: string, fallback = 'fleetmesh-entity'): string {
|
|
239
|
+
const cleaned = (raw || '')
|
|
240
|
+
.toLowerCase()
|
|
241
|
+
.replace(/[^a-z0-9-_]+/g, '-')
|
|
242
|
+
.replace(/-+/g, '-')
|
|
243
|
+
.replace(/^-|-$/g, '');
|
|
244
|
+
return cleaned || fallback;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export interface DidWebDomain { domain: string; didWeb: string; wellKnownUrl: string }
|
|
248
|
+
|
|
249
|
+
/** Normalises user input (a bare domain, a URL, or a did:web) to the did:web
|
|
250
|
+
* triple. Input with no dot becomes `<slug>.<defaultZone>`. */
|
|
251
|
+
export function normalizeDidWebDomain(rawInput: string, defaultZone = 'fleetmesh.org', slugFallback = 'node'): DidWebDomain {
|
|
252
|
+
let clean = (rawInput || '').trim().toLowerCase();
|
|
253
|
+
if (clean.startsWith('did:web:')) clean = clean.slice('did:web:'.length);
|
|
254
|
+
clean = clean
|
|
255
|
+
.replace(/^https?:\/\//, '')
|
|
256
|
+
.replace(/\/.*$/, '')
|
|
257
|
+
.replace(/:\d+$/, '')
|
|
258
|
+
.replace(/[^a-z0-9.-]/g, '');
|
|
259
|
+
if (!clean || !clean.includes('.')) clean = `${slugifyDidName(rawInput || slugFallback, slugFallback)}.${defaultZone}`;
|
|
260
|
+
return { domain: clean, didWeb: `did:web:${clean}`, wellKnownUrl: `https://${clean}/.well-known/did.json` };
|
|
261
|
+
}
|
package/src/encoding.ts
CHANGED
|
@@ -37,6 +37,11 @@ export function b64uDec(s: string): Uint8Array {
|
|
|
37
37
|
return out;
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
/** base64url (no padding) of a string's UTF-8 bytes: JWS-style segment encoding. */
|
|
41
|
+
export const b64uUtf8 = (s: string): string => b64u(utf8(s));
|
|
42
|
+
/** Inverse of `b64uUtf8`; strict, like `b64uDec`. */
|
|
43
|
+
export const b64uUtf8Dec = (s: string): string => fromUtf8(b64uDec(s));
|
|
44
|
+
|
|
40
45
|
// Python sorts keys by code point; JS's default sort is by UTF-16 unit, which
|
|
41
46
|
// differs above the BMP.
|
|
42
47
|
function byCodePoint(a: string, b: string): number {
|
|
@@ -48,20 +53,35 @@ function byCodePoint(a: string, b: string): number {
|
|
|
48
53
|
return x.length - y.length;
|
|
49
54
|
}
|
|
50
55
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
export interface CanonicalJsonOptions {
|
|
57
|
+
/** `'integers'` (default) refuses any non-safe-integer number: Python spells 1.0 as
|
|
58
|
+
* "1.0" and JS as "1", so a float cannot be made to agree across runtimes and the
|
|
59
|
+
* caller must fix its data. `'js'` accepts finite floats and spells them as
|
|
60
|
+
* `JSON.stringify` does; this is the byte form the textile shards were published
|
|
61
|
+
* with and must keep. */
|
|
62
|
+
numbers?: 'integers' | 'js';
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** Canonical JSON: keys sorted by code point at every level (as Python
|
|
66
|
+
* `json.dumps(sort_keys=True, separators=(',', ':'))`), `undefined` members
|
|
67
|
+
* dropped, `undefined` array items spelled `null`, no whitespace, non-ASCII left as
|
|
68
|
+
* is. See `CanonicalJsonOptions.numbers` for the one semantic choice. */
|
|
69
|
+
export function canonicalJson(v: unknown, opts: CanonicalJsonOptions = {}): string {
|
|
70
|
+
const numbers = opts.numbers ?? 'integers';
|
|
55
71
|
if (v === null || typeof v === 'boolean' || typeof v === 'string') return JSON.stringify(v);
|
|
56
72
|
if (typeof v === 'number') {
|
|
73
|
+
if (numbers === 'js') {
|
|
74
|
+
if (!Number.isFinite(v)) throw new Error('canonical JSON cannot carry NaN or Infinity');
|
|
75
|
+
return JSON.stringify(v);
|
|
76
|
+
}
|
|
57
77
|
if (!Number.isSafeInteger(v)) throw new Error('canonical JSON carries integers only');
|
|
58
78
|
return String(v);
|
|
59
79
|
}
|
|
60
|
-
if (Array.isArray(v)) return '[' + v.map((el) => canonicalJson(el === undefined ? null : el)).join(',') + ']';
|
|
80
|
+
if (Array.isArray(v)) return '[' + v.map((el) => canonicalJson(el === undefined ? null : el, opts)).join(',') + ']';
|
|
61
81
|
if (typeof v === 'object') {
|
|
62
82
|
const o = v as Record<string, unknown>;
|
|
63
83
|
return '{' + Object.keys(o).filter((k) => o[k] !== undefined).sort(byCodePoint)
|
|
64
|
-
.map((k) => JSON.stringify(k) + ':' + canonicalJson(o[k])).join(',') + '}';
|
|
84
|
+
.map((k) => JSON.stringify(k) + ':' + canonicalJson(o[k], opts)).join(',') + '}';
|
|
65
85
|
}
|
|
66
86
|
throw new Error('not JSON: ' + typeof v);
|
|
67
87
|
}
|