@seed-hypermedia/client 0.0.68 → 0.0.69
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 +201 -0
- package/dist/auth.d.ts +79 -0
- package/dist/auth.mjs +265 -0
- package/dist/auto-link.mjs +20 -0
- package/dist/base64.mjs +9 -0
- package/dist/blobs.d.ts +168 -0
- package/dist/blobs.mjs +50 -0
- package/dist/block-diff.mjs +17 -0
- package/dist/blocks-to-markdown.mjs +22 -0
- package/dist/capability.mjs +11 -0
- package/dist/cbor.d.ts +13 -0
- package/dist/cbor.mjs +13 -0
- package/dist/change.mjs +23 -0
- package/dist/chunk-2MD3FXKB.mjs +14 -0
- package/dist/chunk-3TPCJCVO.mjs +24 -0
- package/dist/chunk-52P3E6H6.mjs +459 -0
- package/dist/{chunk-KCR3277A.mjs → chunk-5EWV4OPF.mjs} +1 -1
- package/dist/chunk-CBELM7KX.mjs +29 -0
- package/dist/chunk-DXDF36H5.mjs +289 -0
- package/dist/chunk-EY6RQVAP.mjs +138 -0
- package/dist/chunk-FYUZZ3HT.mjs +199 -0
- package/dist/chunk-GKGWSN6J.mjs +75 -0
- package/dist/chunk-H7L4TI6X.mjs +705 -0
- package/dist/chunk-HBIVZA6C.mjs +102 -0
- package/dist/chunk-I3YUHKYW.mjs +135 -0
- package/dist/chunk-KO4XH5L2.mjs +475 -0
- package/dist/chunk-LYECQKZE.mjs +49 -0
- package/dist/chunk-MKUWQB5W.mjs +297 -0
- package/dist/chunk-N3P7S5YZ.mjs +118 -0
- package/dist/chunk-NPG76WUS.mjs +348 -0
- package/dist/chunk-NX7YFFRX.mjs +124 -0
- package/dist/chunk-OZKQOI2X.mjs +334 -0
- package/dist/chunk-PPAY4WZX.mjs +40 -0
- package/dist/chunk-QS7TPMYT.mjs +411 -0
- package/dist/chunk-S2PVJK2P.mjs +517 -0
- package/dist/chunk-T5BTAJPW.mjs +725 -0
- package/dist/{chunk-EGCUJ6D6.mjs → chunk-UOL3F6XQ.mjs} +0 -12
- package/dist/chunk-VN7F33SS.mjs +108 -0
- package/dist/chunk-W4OGV22Y.mjs +57 -0
- package/dist/chunk-WJQSJ6UM.mjs +159 -0
- package/dist/chunk-XI2LXZM7.mjs +112 -0
- package/dist/chunk-YNVFRCUH.mjs +171 -0
- package/dist/chunk-YZX54L3G.mjs +33 -0
- package/dist/client.mjs +14 -0
- package/dist/comment.mjs +18 -0
- package/dist/contact.mjs +14 -0
- package/dist/document-state.mjs +8 -0
- package/dist/editor-types.mjs +0 -0
- package/dist/editorblock-to-hmblock.mjs +11 -0
- package/dist/encryption.mjs +13 -0
- package/dist/errors.mjs +11 -0
- package/dist/file-to-ipfs.mjs +13 -0
- package/dist/grobid.mjs +11 -0
- package/dist/hm-resolver.mjs +10 -0
- package/dist/hm-types.d.ts +566 -566
- package/dist/hmauth.d.ts +169 -0
- package/dist/hmauth.mjs +52 -0
- package/dist/hmblock-to-editorblock.mjs +12 -0
- package/dist/index.mjs +124 -5453
- package/dist/keyfile.mjs +19 -0
- package/dist/markdown-to-blocks.mjs +15 -0
- package/dist/pdf-to-blocks-embedded.mjs +7 -0
- package/dist/pdf-to-blocks.mjs +11 -0
- package/dist/ref.mjs +12 -0
- package/dist/resource-read.mjs +14 -0
- package/dist/signing.mjs +13 -0
- package/dist/tei-to-blocks.mjs +8 -0
- package/dist/unicode.mjs +19 -0
- package/dist/vault-local.mjs +7 -6
- package/dist/vault.mjs +3 -2
- package/package.json +18 -28
- package/src/index.ts +0 -144
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import {
|
|
2
|
+
decode,
|
|
3
|
+
encode
|
|
4
|
+
} from "./chunk-2MD3FXKB.mjs";
|
|
5
|
+
import {
|
|
6
|
+
ED25519_VARINT_PREFIX,
|
|
7
|
+
createCapability,
|
|
8
|
+
principalFromEd25519,
|
|
9
|
+
principalFromString,
|
|
10
|
+
principalToString
|
|
11
|
+
} from "./chunk-FYUZZ3HT.mjs";
|
|
12
|
+
import {
|
|
13
|
+
encode as encode2
|
|
14
|
+
} from "./chunk-3TPCJCVO.mjs";
|
|
15
|
+
|
|
16
|
+
// src/hmauth.ts
|
|
17
|
+
var PARAM_CLIENT_ID = "client_id";
|
|
18
|
+
var PARAM_REDIRECT_URI = "redirect_uri";
|
|
19
|
+
var PARAM_SESSION_KEY = "session_key";
|
|
20
|
+
var PARAM_STATE = "state";
|
|
21
|
+
var PARAM_TS = "ts";
|
|
22
|
+
var PARAM_PROOF = "proof";
|
|
23
|
+
var PARAM_DATA = "data";
|
|
24
|
+
var PARAM_ERROR = "error";
|
|
25
|
+
var PARAM_EMAIL = "email";
|
|
26
|
+
var PARAM_SITE_NAME = "site_name";
|
|
27
|
+
var DELEGATION_PATH = "/delegate";
|
|
28
|
+
var AUTH_STATE_BYTES = 16;
|
|
29
|
+
var REQUEST_PROOF_MAX_AGE_MS = 24 * 60 * 60 * 1e3;
|
|
30
|
+
var REQUEST_PROOF_FUTURE_SKEW_MS = 60 * 1e3;
|
|
31
|
+
function principalEncode(rawPublicKey) {
|
|
32
|
+
return principalToString(principalFromEd25519(rawPublicKey));
|
|
33
|
+
}
|
|
34
|
+
function principalDecode(principal) {
|
|
35
|
+
const packedPrincipal = principalFromString(principal);
|
|
36
|
+
return new Uint8Array(packedPrincipal.slice(ED25519_VARINT_PREFIX.length));
|
|
37
|
+
}
|
|
38
|
+
async function collectStream(readable) {
|
|
39
|
+
const chunks = [];
|
|
40
|
+
const reader = readable.getReader();
|
|
41
|
+
for (; ; ) {
|
|
42
|
+
const { done, value } = await reader.read();
|
|
43
|
+
if (done) break;
|
|
44
|
+
chunks.push(value);
|
|
45
|
+
}
|
|
46
|
+
const total = chunks.reduce((sum, c) => sum + c.length, 0);
|
|
47
|
+
const result = new Uint8Array(total);
|
|
48
|
+
let offset = 0;
|
|
49
|
+
for (const chunk of chunks) {
|
|
50
|
+
result.set(chunk, offset);
|
|
51
|
+
offset += chunk.length;
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
}
|
|
55
|
+
async function compress(data) {
|
|
56
|
+
const cs = new CompressionStream("gzip");
|
|
57
|
+
const writer = cs.writable.getWriter();
|
|
58
|
+
writer.write(data);
|
|
59
|
+
writer.close();
|
|
60
|
+
return collectStream(cs.readable);
|
|
61
|
+
}
|
|
62
|
+
function hasProofParam(url) {
|
|
63
|
+
return url.includes(`?${PARAM_PROOF}=`) || url.includes(`&${PARAM_PROOF}=`);
|
|
64
|
+
}
|
|
65
|
+
function stripTrailingProofParam(url) {
|
|
66
|
+
const escapedParam = PARAM_PROOF.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
67
|
+
const trailingProofPattern = new RegExp(`[?&]${escapedParam}=[^&#]*$`);
|
|
68
|
+
return url.replace(trailingProofPattern, "");
|
|
69
|
+
}
|
|
70
|
+
function ensureProofIsTrailing(url) {
|
|
71
|
+
const signedUrl = stripTrailingProofParam(url);
|
|
72
|
+
if (hasProofParam(url) && signedUrl === url) {
|
|
73
|
+
throw new Error("Invalid delegation request URL: proof must be the final query parameter");
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
function isLocalhost(hostname) {
|
|
77
|
+
return hostname === "localhost" || hostname === "127.0.0.1";
|
|
78
|
+
}
|
|
79
|
+
function decodeProofSignature(proof) {
|
|
80
|
+
let signature;
|
|
81
|
+
try {
|
|
82
|
+
signature = decode(proof);
|
|
83
|
+
} catch (error) {
|
|
84
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
85
|
+
throw new Error(`Invalid proof signature encoding: ${message}`);
|
|
86
|
+
}
|
|
87
|
+
if (signature.length !== 64) {
|
|
88
|
+
throw new Error(`Invalid proof signature length: expected 64 bytes, got ${signature.length}`);
|
|
89
|
+
}
|
|
90
|
+
return signature;
|
|
91
|
+
}
|
|
92
|
+
function validateClientId(clientId) {
|
|
93
|
+
let parsed;
|
|
94
|
+
try {
|
|
95
|
+
parsed = new URL(clientId);
|
|
96
|
+
} catch {
|
|
97
|
+
throw new Error(`Invalid client_id: not a valid URL: ${clientId}`);
|
|
98
|
+
}
|
|
99
|
+
if (parsed.protocol !== "https:" && !(parsed.protocol === "http:" && isLocalhost(parsed.hostname))) {
|
|
100
|
+
throw new Error(`Invalid client_id: must use HTTPS (HTTP allowed only for localhost): ${clientId}`);
|
|
101
|
+
}
|
|
102
|
+
if (parsed.pathname !== "/" && parsed.pathname !== "") {
|
|
103
|
+
throw new Error(`Invalid client_id: must not have a path: ${clientId}`);
|
|
104
|
+
}
|
|
105
|
+
if (parsed.search) {
|
|
106
|
+
throw new Error(`Invalid client_id: must not have a query string: ${clientId}`);
|
|
107
|
+
}
|
|
108
|
+
if (parsed.hash) {
|
|
109
|
+
throw new Error(`Invalid client_id: must not have a fragment: ${clientId}`);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function validateRedirectUri(redirectUri, clientId) {
|
|
113
|
+
let parsed;
|
|
114
|
+
try {
|
|
115
|
+
parsed = new URL(redirectUri);
|
|
116
|
+
} catch {
|
|
117
|
+
throw new Error(`Invalid redirect_uri: not a valid URL: ${redirectUri}`);
|
|
118
|
+
}
|
|
119
|
+
if (parsed.protocol !== "https:" && !(parsed.protocol === "http:" && isLocalhost(parsed.hostname))) {
|
|
120
|
+
throw new Error(`Invalid redirect_uri: must use HTTPS (HTTP allowed only for localhost): ${redirectUri}`);
|
|
121
|
+
}
|
|
122
|
+
const redirectOrigin = parsed.origin;
|
|
123
|
+
const clientOrigin = new URL(clientId).origin;
|
|
124
|
+
if (redirectOrigin !== clientOrigin) {
|
|
125
|
+
throw new Error(`Invalid redirect_uri: origin "${redirectOrigin}" does not match client_id "${clientOrigin}"`);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
function validateSessionKeyPrincipal(sessionKeyPrincipal) {
|
|
129
|
+
try {
|
|
130
|
+
principalFromString(sessionKeyPrincipal);
|
|
131
|
+
} catch {
|
|
132
|
+
throw new Error(`Invalid session_key principal: ${sessionKeyPrincipal}`);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function validateState(state) {
|
|
136
|
+
const valid = /^[A-Za-z0-9_-]+$/.test(state);
|
|
137
|
+
if (!valid) {
|
|
138
|
+
throw new Error("Invalid state: must be base64url");
|
|
139
|
+
}
|
|
140
|
+
const decoded = decode(state);
|
|
141
|
+
if (decoded.length < AUTH_STATE_BYTES) {
|
|
142
|
+
throw new Error("Invalid state: must be at least 128 bits");
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function validateRequestTimestamp(ts) {
|
|
146
|
+
const parsed = Number(ts);
|
|
147
|
+
if (!Number.isFinite(parsed) || !Number.isInteger(parsed) || parsed <= 0) {
|
|
148
|
+
throw new Error(`Invalid ts: ${ts}`);
|
|
149
|
+
}
|
|
150
|
+
return parsed;
|
|
151
|
+
}
|
|
152
|
+
function parseDelegationRequest(url) {
|
|
153
|
+
const originalUrl = typeof url === "string" ? url : url.toString();
|
|
154
|
+
ensureProofIsTrailing(originalUrl);
|
|
155
|
+
const parsedUrl = new URL(originalUrl);
|
|
156
|
+
const clientId = parsedUrl.searchParams.get(PARAM_CLIENT_ID);
|
|
157
|
+
const redirectUri = parsedUrl.searchParams.get(PARAM_REDIRECT_URI);
|
|
158
|
+
const sessionKey = parsedUrl.searchParams.get(PARAM_SESSION_KEY);
|
|
159
|
+
const state = parsedUrl.searchParams.get(PARAM_STATE);
|
|
160
|
+
const ts = parsedUrl.searchParams.get(PARAM_TS);
|
|
161
|
+
const proof = parsedUrl.searchParams.get(PARAM_PROOF);
|
|
162
|
+
if (!clientId && !redirectUri && !sessionKey) {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
if (!clientId) {
|
|
166
|
+
throw new Error(`Missing required parameter: ${PARAM_CLIENT_ID}`);
|
|
167
|
+
}
|
|
168
|
+
if (!redirectUri) {
|
|
169
|
+
throw new Error(`Missing required parameter: ${PARAM_REDIRECT_URI}`);
|
|
170
|
+
}
|
|
171
|
+
if (!sessionKey) {
|
|
172
|
+
throw new Error(`Missing required parameter: ${PARAM_SESSION_KEY}`);
|
|
173
|
+
}
|
|
174
|
+
if (!state) {
|
|
175
|
+
throw new Error(`Missing required parameter: ${PARAM_STATE}`);
|
|
176
|
+
}
|
|
177
|
+
if (!ts) {
|
|
178
|
+
throw new Error(`Missing required parameter: ${PARAM_TS}`);
|
|
179
|
+
}
|
|
180
|
+
if (!proof) {
|
|
181
|
+
throw new Error(`Missing required parameter: ${PARAM_PROOF}`);
|
|
182
|
+
}
|
|
183
|
+
validateClientId(clientId);
|
|
184
|
+
validateRedirectUri(redirectUri, clientId);
|
|
185
|
+
validateSessionKeyPrincipal(sessionKey);
|
|
186
|
+
validateState(state);
|
|
187
|
+
const requestTs = validateRequestTimestamp(ts);
|
|
188
|
+
const email = parsedUrl.searchParams.get(PARAM_EMAIL) || void 0;
|
|
189
|
+
const siteName = parsedUrl.searchParams.get(PARAM_SITE_NAME) || void 0;
|
|
190
|
+
return {
|
|
191
|
+
originalUrl,
|
|
192
|
+
clientId,
|
|
193
|
+
redirectUri,
|
|
194
|
+
sessionKeyPrincipal: sessionKey,
|
|
195
|
+
state,
|
|
196
|
+
requestTs,
|
|
197
|
+
proof,
|
|
198
|
+
vaultOrigin: parsedUrl.origin,
|
|
199
|
+
email,
|
|
200
|
+
siteName
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
function buildSignedRequestPayload(request, vaultOrigin) {
|
|
204
|
+
if (request.vaultOrigin !== vaultOrigin) {
|
|
205
|
+
throw new Error("Delegation request vault origin mismatch");
|
|
206
|
+
}
|
|
207
|
+
ensureProofIsTrailing(request.originalUrl);
|
|
208
|
+
const signedUrl = stripTrailingProofParam(request.originalUrl);
|
|
209
|
+
return new TextEncoder().encode(signedUrl);
|
|
210
|
+
}
|
|
211
|
+
async function verifyDelegationRequestProof(request, vaultOrigin, now = Date.now()) {
|
|
212
|
+
if (request.requestTs < now - REQUEST_PROOF_MAX_AGE_MS) {
|
|
213
|
+
throw new Error("Delegation request proof expired");
|
|
214
|
+
}
|
|
215
|
+
if (request.requestTs > now + REQUEST_PROOF_FUTURE_SKEW_MS) {
|
|
216
|
+
throw new Error("Delegation request proof timestamp is in the future");
|
|
217
|
+
}
|
|
218
|
+
const signature = decodeProofSignature(request.proof);
|
|
219
|
+
const rawPubKey = principalDecode(request.sessionKeyPrincipal);
|
|
220
|
+
const payload = buildSignedRequestPayload(request, vaultOrigin);
|
|
221
|
+
const publicKey = await crypto.subtle.importKey(
|
|
222
|
+
"raw",
|
|
223
|
+
rawPubKey,
|
|
224
|
+
"Ed25519",
|
|
225
|
+
false,
|
|
226
|
+
["verify"]
|
|
227
|
+
);
|
|
228
|
+
let valid;
|
|
229
|
+
try {
|
|
230
|
+
valid = await crypto.subtle.verify(
|
|
231
|
+
"Ed25519",
|
|
232
|
+
publicKey,
|
|
233
|
+
signature,
|
|
234
|
+
payload
|
|
235
|
+
);
|
|
236
|
+
} catch (error) {
|
|
237
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
238
|
+
throw new Error(`Failed to verify request proof signature: ${message}`);
|
|
239
|
+
}
|
|
240
|
+
if (!valid) {
|
|
241
|
+
throw new Error("Request proof signature does not match session key");
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
async function buildCallbackUrl(redirectUri, state, accountPrincipal, capability, profile, notifyServerUrl) {
|
|
245
|
+
const url = new URL(redirectUri);
|
|
246
|
+
const callbackData = {
|
|
247
|
+
account: accountPrincipal,
|
|
248
|
+
capability: capability.decoded,
|
|
249
|
+
capabilityCid: capability.cid,
|
|
250
|
+
profile: profile.decoded,
|
|
251
|
+
profileCid: profile.cid,
|
|
252
|
+
...notifyServerUrl ? { notifyServerUrl } : {}
|
|
253
|
+
};
|
|
254
|
+
const encodedCbor = encode2(callbackData);
|
|
255
|
+
const compressed = await compress(new Uint8Array(encodedCbor));
|
|
256
|
+
url.searchParams.set(PARAM_DATA, encode(compressed));
|
|
257
|
+
url.searchParams.set(PARAM_STATE, state);
|
|
258
|
+
return url.toString();
|
|
259
|
+
}
|
|
260
|
+
async function createDelegation(issuer, sessionKeyPrincipal, clientId) {
|
|
261
|
+
return await createCapability(issuer, sessionKeyPrincipal, "AGENT", Date.now(), {
|
|
262
|
+
label: `Session key for ${clientId}`
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export {
|
|
267
|
+
PARAM_CLIENT_ID,
|
|
268
|
+
PARAM_REDIRECT_URI,
|
|
269
|
+
PARAM_SESSION_KEY,
|
|
270
|
+
PARAM_STATE,
|
|
271
|
+
PARAM_TS,
|
|
272
|
+
PARAM_PROOF,
|
|
273
|
+
PARAM_DATA,
|
|
274
|
+
PARAM_ERROR,
|
|
275
|
+
PARAM_EMAIL,
|
|
276
|
+
PARAM_SITE_NAME,
|
|
277
|
+
DELEGATION_PATH,
|
|
278
|
+
principalEncode,
|
|
279
|
+
principalDecode,
|
|
280
|
+
validateClientId,
|
|
281
|
+
validateRedirectUri,
|
|
282
|
+
validateSessionKeyPrincipal,
|
|
283
|
+
validateState,
|
|
284
|
+
validateRequestTimestamp,
|
|
285
|
+
parseDelegationRequest,
|
|
286
|
+
verifyDelegationRequestProof,
|
|
287
|
+
buildCallbackUrl,
|
|
288
|
+
createDelegation
|
|
289
|
+
};
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DEFAULT_PARAMS,
|
|
3
|
+
decrypt,
|
|
4
|
+
deriveKeyFromPassword,
|
|
5
|
+
encrypt
|
|
6
|
+
} from "./chunk-UOL3F6XQ.mjs";
|
|
7
|
+
import {
|
|
8
|
+
decode,
|
|
9
|
+
encode
|
|
10
|
+
} from "./chunk-2MD3FXKB.mjs";
|
|
11
|
+
import {
|
|
12
|
+
__export
|
|
13
|
+
} from "./chunk-7P6ASYW6.mjs";
|
|
14
|
+
|
|
15
|
+
// src/keyfile.ts
|
|
16
|
+
var keyfile_exports = {};
|
|
17
|
+
__export(keyfile_exports, {
|
|
18
|
+
create: () => create,
|
|
19
|
+
decrypt: () => decrypt2,
|
|
20
|
+
load: () => load,
|
|
21
|
+
parse: () => parse,
|
|
22
|
+
principalStringFromSeed: () => principalStringFromSeed,
|
|
23
|
+
stringify: () => stringify
|
|
24
|
+
});
|
|
25
|
+
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
26
|
+
import { base58btc } from "multiformats/bases/base58";
|
|
27
|
+
var ED25519_VARINT_PREFIX = new Uint8Array([237, 1]);
|
|
28
|
+
function principalStringFromSeed(seed) {
|
|
29
|
+
const pub = ed25519.getPublicKey(seed);
|
|
30
|
+
const principal = new Uint8Array(ED25519_VARINT_PREFIX.length + pub.length);
|
|
31
|
+
principal.set(ED25519_VARINT_PREFIX, 0);
|
|
32
|
+
principal.set(pub, ED25519_VARINT_PREFIX.length);
|
|
33
|
+
return base58btc.encode(principal);
|
|
34
|
+
}
|
|
35
|
+
async function create(input) {
|
|
36
|
+
const createTime = input.createTime ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
37
|
+
const includedProfile = input.profile && (input.profile.name || input.profile.description) ? input.profile : void 0;
|
|
38
|
+
if (!input.password) {
|
|
39
|
+
return {
|
|
40
|
+
createTime,
|
|
41
|
+
publicKey: input.publicKey,
|
|
42
|
+
keyB64: encode(input.key),
|
|
43
|
+
...includedProfile ? { profile: includedProfile } : {}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
const salt = crypto.getRandomValues(new Uint8Array(16));
|
|
47
|
+
const derivedKey = await deriveKeyFromPassword(input.password, salt, DEFAULT_PARAMS);
|
|
48
|
+
const encrypted = await encrypt(input.key, derivedKey);
|
|
49
|
+
return {
|
|
50
|
+
createTime,
|
|
51
|
+
publicKey: input.publicKey,
|
|
52
|
+
keyB64: encode(encrypted),
|
|
53
|
+
encryption: {
|
|
54
|
+
kdf: "argon2id",
|
|
55
|
+
argon2: {
|
|
56
|
+
memoryCost: DEFAULT_PARAMS.memoryCost,
|
|
57
|
+
timeCost: DEFAULT_PARAMS.timeCost,
|
|
58
|
+
parallelism: DEFAULT_PARAMS.parallelism,
|
|
59
|
+
saltB64: encode(salt)
|
|
60
|
+
},
|
|
61
|
+
cipher: "xchacha20poly1305"
|
|
62
|
+
},
|
|
63
|
+
...includedProfile ? { profile: includedProfile } : {}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function parse(json) {
|
|
67
|
+
const value = JSON.parse(json);
|
|
68
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
69
|
+
throw new Error("Invalid key file payload");
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
function stringify(payload) {
|
|
74
|
+
return `${JSON.stringify(payload, null, 2)}
|
|
75
|
+
`;
|
|
76
|
+
}
|
|
77
|
+
async function decrypt2(payload, password) {
|
|
78
|
+
if (!payload.keyB64) {
|
|
79
|
+
throw new Error("keyB64 is required");
|
|
80
|
+
}
|
|
81
|
+
if (!payload.encryption) {
|
|
82
|
+
return decode(payload.keyB64);
|
|
83
|
+
}
|
|
84
|
+
if (!password) {
|
|
85
|
+
throw new Error("password is required for encrypted key files");
|
|
86
|
+
}
|
|
87
|
+
if (payload.encryption.kdf !== "argon2id") {
|
|
88
|
+
throw new Error(`unsupported key derivation function "${payload.encryption.kdf}"`);
|
|
89
|
+
}
|
|
90
|
+
if (payload.encryption.cipher !== "xchacha20poly1305") {
|
|
91
|
+
throw new Error(`unsupported cipher "${payload.encryption.cipher}"`);
|
|
92
|
+
}
|
|
93
|
+
if (!payload.encryption.argon2) {
|
|
94
|
+
throw new Error("argon2 parameters are required");
|
|
95
|
+
}
|
|
96
|
+
if (!payload.encryption.argon2.memoryCost || !payload.encryption.argon2.timeCost || !payload.encryption.argon2.parallelism) {
|
|
97
|
+
throw new Error("argon2 parameters must be greater than zero");
|
|
98
|
+
}
|
|
99
|
+
if (!payload.encryption.argon2.saltB64) {
|
|
100
|
+
throw new Error("saltB64 is required for encrypted key files");
|
|
101
|
+
}
|
|
102
|
+
const derivedKey = await deriveKeyFromPassword(
|
|
103
|
+
password,
|
|
104
|
+
decode(payload.encryption.argon2.saltB64),
|
|
105
|
+
{
|
|
106
|
+
memoryCost: payload.encryption.argon2.memoryCost,
|
|
107
|
+
timeCost: payload.encryption.argon2.timeCost,
|
|
108
|
+
parallelism: payload.encryption.argon2.parallelism
|
|
109
|
+
}
|
|
110
|
+
);
|
|
111
|
+
return decrypt(decode(payload.keyB64), derivedKey);
|
|
112
|
+
}
|
|
113
|
+
async function load(json, password) {
|
|
114
|
+
const payload = parse(json);
|
|
115
|
+
const seed = await decrypt2(payload, password);
|
|
116
|
+
if (seed.length !== 32) {
|
|
117
|
+
throw new Error(`invalid private key length: expected 32 bytes, got ${seed.length}`);
|
|
118
|
+
}
|
|
119
|
+
const publicKey = principalStringFromSeed(seed);
|
|
120
|
+
if (payload.publicKey && payload.publicKey !== publicKey) {
|
|
121
|
+
throw new Error("publicKey does not match private key");
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
payload,
|
|
125
|
+
seed,
|
|
126
|
+
publicKey
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export {
|
|
131
|
+
principalStringFromSeed,
|
|
132
|
+
create,
|
|
133
|
+
parse,
|
|
134
|
+
stringify,
|
|
135
|
+
decrypt2 as decrypt,
|
|
136
|
+
load,
|
|
137
|
+
keyfile_exports
|
|
138
|
+
};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import {
|
|
2
|
+
code,
|
|
3
|
+
decode,
|
|
4
|
+
encode
|
|
5
|
+
} from "./chunk-3TPCJCVO.mjs";
|
|
6
|
+
|
|
7
|
+
// src/blobs.ts
|
|
8
|
+
import { ed25519 } from "@noble/curves/ed25519.js";
|
|
9
|
+
import { sha256 as sha256hash } from "@noble/hashes/sha2.js";
|
|
10
|
+
import { base58btc } from "multiformats/bases/base58";
|
|
11
|
+
import { CID } from "multiformats/cid";
|
|
12
|
+
import * as Digest from "multiformats/hashes/digest";
|
|
13
|
+
import { sha256 as sha256hasher } from "multiformats/hashes/sha2";
|
|
14
|
+
var ED25519_VARINT_PREFIX = new Uint8Array([237, 1]);
|
|
15
|
+
var ED25519_SIGNATURE_SIZE = 64;
|
|
16
|
+
var ED25519_PUBLIC_KEY_SIZE = 32;
|
|
17
|
+
var ED25519_PRINCIPAL_SIZE = ED25519_VARINT_PREFIX.length + ED25519_PUBLIC_KEY_SIZE;
|
|
18
|
+
var NobleKeyPair = class {
|
|
19
|
+
principal;
|
|
20
|
+
publicKey;
|
|
21
|
+
/** Raw 32-byte Ed25519 private key seed. Needed for vault storage. */
|
|
22
|
+
seed;
|
|
23
|
+
constructor(seed) {
|
|
24
|
+
this.seed = new Uint8Array(seed);
|
|
25
|
+
this.publicKey = new Uint8Array(ed25519.getPublicKey(this.seed));
|
|
26
|
+
this.principal = principalFromEd25519(this.publicKey);
|
|
27
|
+
}
|
|
28
|
+
async sign(data) {
|
|
29
|
+
return new Uint8Array(ed25519.sign(data, this.seed));
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
function generateNobleKeyPair() {
|
|
33
|
+
return new NobleKeyPair(crypto.getRandomValues(new Uint8Array(32)));
|
|
34
|
+
}
|
|
35
|
+
function nobleKeyPairFromSeed(seed) {
|
|
36
|
+
return new NobleKeyPair(seed);
|
|
37
|
+
}
|
|
38
|
+
var WebCryptoKeyPair = class {
|
|
39
|
+
principal;
|
|
40
|
+
publicKey;
|
|
41
|
+
/** The Web Crypto key pair. Exposed for LocalWebIdentity compatibility. */
|
|
42
|
+
keyPair;
|
|
43
|
+
constructor(keyPair, publicKeyRaw) {
|
|
44
|
+
if (keyPair.privateKey.algorithm.name !== "Ed25519" || keyPair.publicKey.algorithm.name !== "Ed25519") {
|
|
45
|
+
throw new Error("WebCryptoKeyPair requires Ed25519 keys");
|
|
46
|
+
}
|
|
47
|
+
this.keyPair = keyPair;
|
|
48
|
+
this.publicKey = new Uint8Array(publicKeyRaw);
|
|
49
|
+
this.principal = principalFromEd25519(this.publicKey);
|
|
50
|
+
}
|
|
51
|
+
async sign(data) {
|
|
52
|
+
const sig = await crypto.subtle.sign("Ed25519", this.keyPair.privateKey, data);
|
|
53
|
+
return new Uint8Array(sig);
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
async function generateWebCryptoKeyPair() {
|
|
57
|
+
const keyPair = await crypto.subtle.generateKey("Ed25519", false, ["sign", "verify"]);
|
|
58
|
+
const publicKeyRaw = new Uint8Array(await crypto.subtle.exportKey("raw", keyPair.publicKey));
|
|
59
|
+
return new WebCryptoKeyPair(keyPair, publicKeyRaw);
|
|
60
|
+
}
|
|
61
|
+
function verify(blob) {
|
|
62
|
+
if (blob.signer[0] !== ED25519_VARINT_PREFIX[0] || blob.signer[1] !== ED25519_VARINT_PREFIX[1]) return false;
|
|
63
|
+
const rawPubKey = blob.signer.slice(ED25519_VARINT_PREFIX.length);
|
|
64
|
+
if (rawPubKey.length !== ED25519_PUBLIC_KEY_SIZE) return false;
|
|
65
|
+
const sigCopy = new Uint8Array(blob.sig);
|
|
66
|
+
const unsigned = { ...blob, sig: new Uint8Array(ED25519_SIGNATURE_SIZE) };
|
|
67
|
+
const data = new Uint8Array(encode(unsigned));
|
|
68
|
+
try {
|
|
69
|
+
return ed25519.verify(sigCopy, data, rawPubKey);
|
|
70
|
+
} catch {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function principalFromEd25519(rawPublicKey) {
|
|
75
|
+
const out = new Uint8Array(ED25519_VARINT_PREFIX.length + rawPublicKey.length);
|
|
76
|
+
out.set(ED25519_VARINT_PREFIX);
|
|
77
|
+
out.set(rawPublicKey, ED25519_VARINT_PREFIX.length);
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
function principalToString(p) {
|
|
81
|
+
return base58btc.encode(p);
|
|
82
|
+
}
|
|
83
|
+
function principalFromString(s) {
|
|
84
|
+
let decoded;
|
|
85
|
+
try {
|
|
86
|
+
decoded = base58btc.decode(s);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
89
|
+
throw new Error(`Invalid principal encoding: ${message}`);
|
|
90
|
+
}
|
|
91
|
+
if (decoded.length !== ED25519_PRINCIPAL_SIZE) {
|
|
92
|
+
throw new Error(`Invalid principal length: expected ${ED25519_PRINCIPAL_SIZE} bytes, got ${decoded.length}`);
|
|
93
|
+
}
|
|
94
|
+
if (decoded[0] !== ED25519_VARINT_PREFIX[0] || decoded[1] !== ED25519_VARINT_PREFIX[1]) {
|
|
95
|
+
throw new Error("Invalid principal multicodec: expected Ed25519 (0xed01)");
|
|
96
|
+
}
|
|
97
|
+
return decoded;
|
|
98
|
+
}
|
|
99
|
+
function principalEqual(a, b) {
|
|
100
|
+
if (a.length !== b.length) return false;
|
|
101
|
+
for (let i = 0; i < a.length; i++) {
|
|
102
|
+
if (a[i] !== b[i]) return false;
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
async function createProfile(signer, opts, ts) {
|
|
107
|
+
opts = { ...opts };
|
|
108
|
+
if (opts.account && principalEqual(opts.account, signer.principal)) {
|
|
109
|
+
delete opts.account;
|
|
110
|
+
}
|
|
111
|
+
if (opts.avatar === void 0) {
|
|
112
|
+
delete opts.avatar;
|
|
113
|
+
}
|
|
114
|
+
if (opts.description === void 0) {
|
|
115
|
+
delete opts.description;
|
|
116
|
+
}
|
|
117
|
+
const blob = {
|
|
118
|
+
type: "Profile",
|
|
119
|
+
signer: signer.principal,
|
|
120
|
+
sig: new Uint8Array(ED25519_SIGNATURE_SIZE),
|
|
121
|
+
ts,
|
|
122
|
+
...opts
|
|
123
|
+
};
|
|
124
|
+
return encode2(await sign(signer, blob));
|
|
125
|
+
}
|
|
126
|
+
async function createProfileAlias(signer, alias, ts) {
|
|
127
|
+
const blob = {
|
|
128
|
+
type: "Profile",
|
|
129
|
+
signer: signer.principal,
|
|
130
|
+
sig: new Uint8Array(ED25519_SIGNATURE_SIZE),
|
|
131
|
+
ts,
|
|
132
|
+
alias
|
|
133
|
+
};
|
|
134
|
+
return encode2(await sign(signer, blob));
|
|
135
|
+
}
|
|
136
|
+
async function createCapability(issuer, delegate, role, ts, opts = {}) {
|
|
137
|
+
const blob = {
|
|
138
|
+
type: "Capability",
|
|
139
|
+
signer: issuer.principal,
|
|
140
|
+
sig: new Uint8Array(ED25519_SIGNATURE_SIZE),
|
|
141
|
+
ts,
|
|
142
|
+
delegate,
|
|
143
|
+
role,
|
|
144
|
+
...opts
|
|
145
|
+
};
|
|
146
|
+
return encode2(await sign(issuer, blob));
|
|
147
|
+
}
|
|
148
|
+
async function sign(signer, blob) {
|
|
149
|
+
const unsigned = { ...blob, sig: new Uint8Array(ED25519_SIGNATURE_SIZE) };
|
|
150
|
+
const data = new Uint8Array(encode(unsigned));
|
|
151
|
+
const sig = await signer.sign(data);
|
|
152
|
+
return { ...unsigned, sig };
|
|
153
|
+
}
|
|
154
|
+
function encode2(blob) {
|
|
155
|
+
const data = new Uint8Array(encode(blob));
|
|
156
|
+
const hash = sha256hash(data);
|
|
157
|
+
const digest = Digest.create(sha256hasher.code, hash);
|
|
158
|
+
const cid = CID.createV1(code, digest);
|
|
159
|
+
return { cid, data, decoded: blob };
|
|
160
|
+
}
|
|
161
|
+
function decodeBlob(data, expectedCid) {
|
|
162
|
+
const decoded = decode(data);
|
|
163
|
+
const reencoded = encode2(decoded);
|
|
164
|
+
if (reencoded.cid.toString() !== expectedCid.toString()) {
|
|
165
|
+
throw new Error(`CID mismatch: expected ${expectedCid}, got ${reencoded.cid}`);
|
|
166
|
+
}
|
|
167
|
+
return { cid: expectedCid, data, decoded };
|
|
168
|
+
}
|
|
169
|
+
async function generateKeyPair() {
|
|
170
|
+
return generateNobleKeyPair();
|
|
171
|
+
}
|
|
172
|
+
async function keyPairFromPrivateKey(rawPrivateKey) {
|
|
173
|
+
return nobleKeyPairFromSeed(rawPrivateKey);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export {
|
|
177
|
+
ED25519_VARINT_PREFIX,
|
|
178
|
+
ED25519_SIGNATURE_SIZE,
|
|
179
|
+
ED25519_PUBLIC_KEY_SIZE,
|
|
180
|
+
ED25519_PRINCIPAL_SIZE,
|
|
181
|
+
NobleKeyPair,
|
|
182
|
+
generateNobleKeyPair,
|
|
183
|
+
nobleKeyPairFromSeed,
|
|
184
|
+
WebCryptoKeyPair,
|
|
185
|
+
generateWebCryptoKeyPair,
|
|
186
|
+
verify,
|
|
187
|
+
principalFromEd25519,
|
|
188
|
+
principalToString,
|
|
189
|
+
principalFromString,
|
|
190
|
+
principalEqual,
|
|
191
|
+
createProfile,
|
|
192
|
+
createProfileAlias,
|
|
193
|
+
createCapability,
|
|
194
|
+
sign,
|
|
195
|
+
encode2 as encode,
|
|
196
|
+
decodeBlob,
|
|
197
|
+
generateKeyPair,
|
|
198
|
+
keyPairFromPrivateKey
|
|
199
|
+
};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {
|
|
2
|
+
resolveId
|
|
3
|
+
} from "./chunk-YNVFRCUH.mjs";
|
|
4
|
+
import {
|
|
5
|
+
createSeedClient
|
|
6
|
+
} from "./chunk-MKUWQB5W.mjs";
|
|
7
|
+
import {
|
|
8
|
+
unpackHmId
|
|
9
|
+
} from "./chunk-SQEUO7FO.mjs";
|
|
10
|
+
|
|
11
|
+
// src/resource-read.ts
|
|
12
|
+
async function resolveIdWithClient(rawId, options = {}) {
|
|
13
|
+
const commentId = extractCommentIdFromReadableUrl(rawId);
|
|
14
|
+
const parsed = commentId ? commentIdToHmId(commentId) : unpackHmId(rawId);
|
|
15
|
+
if (parsed) {
|
|
16
|
+
if (isWebUrl(rawId)) {
|
|
17
|
+
const origin2 = new URL(rawId).origin;
|
|
18
|
+
return { id: parsed, client: createSeedClient(origin2), serverUrl: origin2 };
|
|
19
|
+
}
|
|
20
|
+
const client = options.client ?? createSeedClient(normalizeServerUrl(options.serverUrl || "https://hyper.media"));
|
|
21
|
+
return { id: parsed, client, serverUrl: client.baseUrl };
|
|
22
|
+
}
|
|
23
|
+
const id = await resolveId(rawId, { domainResolver: options.domainResolver });
|
|
24
|
+
const origin = new URL(rawId).origin;
|
|
25
|
+
return { id, client: createSeedClient(origin), serverUrl: origin };
|
|
26
|
+
}
|
|
27
|
+
var COMMENT_VIEW_TERMS = /* @__PURE__ */ new Set([":comments", ":comment", ":discussions"]);
|
|
28
|
+
function extractCommentIdFromReadableUrl(rawId) {
|
|
29
|
+
if (isWebUrl(rawId)) {
|
|
30
|
+
const url = new URL(rawId);
|
|
31
|
+
const panelCommentId = extractCommentIdFromPanel(url.searchParams.get("panel"));
|
|
32
|
+
if (panelCommentId) return panelCommentId;
|
|
33
|
+
return extractCommentIdFromPath(url.pathname.split("/").filter(Boolean));
|
|
34
|
+
}
|
|
35
|
+
const [withoutHash] = rawId.split("#");
|
|
36
|
+
const [withoutQuery] = withoutHash.split("?");
|
|
37
|
+
const parsed = unpackHmId(withoutQuery);
|
|
38
|
+
return (parsed == null ? void 0 : parsed.path) ? extractCommentIdFromPath(parsed.path) : null;
|
|
39
|
+
}
|
|
40
|
+
function extractCommentIdFromPanel(panel) {
|
|
41
|
+
if (!panel) return null;
|
|
42
|
+
const [viewTerm, first, second] = panel.split("/");
|
|
43
|
+
if (viewTerm !== "comments" && viewTerm !== "comment" && viewTerm !== "discussions") return null;
|
|
44
|
+
if (first && second) return `${first}/${second}`;
|
|
45
|
+
return first || null;
|
|
46
|
+
}
|
|
47
|
+
function extractCommentIdFromPath(pathParts) {
|
|
48
|
+
if (pathParts.length >= 3) {
|
|
49
|
+
const thirdToLast = pathParts[pathParts.length - 3];
|
|
50
|
+
if (thirdToLast && COMMENT_VIEW_TERMS.has(thirdToLast)) {
|
|
51
|
+
return `${pathParts[pathParts.length - 2]}/${pathParts[pathParts.length - 1]}`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
if (pathParts.length >= 2) {
|
|
55
|
+
const secondToLast = pathParts[pathParts.length - 2];
|
|
56
|
+
if (secondToLast && COMMENT_VIEW_TERMS.has(secondToLast)) return pathParts[pathParts.length - 1] || null;
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
function commentIdToHmId(commentId) {
|
|
61
|
+
const parsed = unpackHmId(`hm://${commentId}`);
|
|
62
|
+
if (!parsed) throw new Error(`Invalid comment ID: ${commentId}`);
|
|
63
|
+
return parsed;
|
|
64
|
+
}
|
|
65
|
+
function isWebUrl(rawId) {
|
|
66
|
+
return rawId.startsWith("http://") || rawId.startsWith("https://");
|
|
67
|
+
}
|
|
68
|
+
function normalizeServerUrl(serverUrl) {
|
|
69
|
+
const withScheme = serverUrl.startsWith("http://") || serverUrl.startsWith("https://") ? serverUrl : `https://${serverUrl}`;
|
|
70
|
+
return withScheme.replace(/\/+$/, "");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export {
|
|
74
|
+
resolveIdWithClient
|
|
75
|
+
};
|