@calimero-network/mero-js 19.15.0 → 19.16.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/dist/index.d.ts CHANGED
@@ -24,7 +24,8 @@ export type { DeviceCertInput } from "./device-cert/index.js";
24
24
  export { signMemberJoinOp, encodeSignedInvitation, SIGNED_NAMESPACE_OP_SCHEMA_VERSION, } from "./namespace-op/index.js";
25
25
  export type { SignMemberJoinInput } from "./namespace-op/index.js";
26
26
  export type { WarrantInput } from "./warrant/index.js";
27
- export { requestBodyHash, signRequest } from "./request/index.js";
27
+ export { requestBodyHash, signRequest, callerProof, createProofSigner, } from "./request/index.js";
28
+ export type { CallerProofInput, ProofRequest, ProofSignerOptions, } from "./request/index.js";
28
29
  export type { RequestSigInput } from "./request/index.js";
29
30
  export { signLoginStatement } from "./login/index.js";
30
31
  export type { Audience, LoginStatementInput } from "./login/index.js";
package/dist/index.js CHANGED
@@ -29,7 +29,7 @@ export { generateAccountRoot, accountRootFromPhrase, accountRootFromSecret, sign
29
29
  export { signDeviceCert, mintDeviceId, deviceCertPayload, accountForRoot, } from "./device-cert/index.js";
30
30
  export { signMemberJoinOp, encodeSignedInvitation, SIGNED_NAMESPACE_OP_SCHEMA_VERSION, } from "./namespace-op/index.js";
31
31
  // Login-statement signing — the device's half of a password-free session
32
- export { requestBodyHash, signRequest } from "./request/index.js";
32
+ export { requestBodyHash, signRequest, callerProof, createProofSigner, } from "./request/index.js";
33
33
  export { signLoginStatement } from "./login/index.js";
34
34
  export { login, generateSessionKey } from "./login/index.js";
35
35
  // Member capability bitmask constants & helpers
package/dist/index.mjs CHANGED
@@ -85,6 +85,14 @@ function headersToRecord(headers) {
85
85
  });
86
86
  return record;
87
87
  }
88
+ function pathForProof(url) {
89
+ try {
90
+ return new URL(url).pathname;
91
+ } catch {
92
+ const query = url.indexOf("?");
93
+ return query === -1 ? url : url.slice(0, query);
94
+ }
95
+ }
88
96
  var WebHttpClient = class {
89
97
  constructor(transport) {
90
98
  this.transport = transport;
@@ -211,6 +219,16 @@ var WebHttpClient = class {
211
219
  if (init?.keepalive !== void 0) {
212
220
  requestInit.keepalive = init.keepalive;
213
221
  }
222
+ if (this.transport.getProof) {
223
+ const proof = await this.transport.getProof({
224
+ method: requestInit.method ?? "GET",
225
+ path: pathForProof(url),
226
+ body: requestInit.body
227
+ });
228
+ if (proof) {
229
+ headersObj["X-Calimero-Proof"] = proof;
230
+ }
231
+ }
214
232
  try {
215
233
  const response = await this.transport.fetch(url, requestInit);
216
234
  if (!response.ok) {
@@ -407,6 +425,7 @@ function createBrowserHttpClient(options) {
407
425
  fetch: (url, init) => globalThis.fetch(url, init),
408
426
  baseUrl: options.baseUrl,
409
427
  getAuthToken: options.getAuthToken,
428
+ getProof: options.getProof,
410
429
  onTokenRefresh: options.onTokenRefresh,
411
430
  refreshToken: options.refreshToken,
412
431
  onAuthRevoked: options.onAuthRevoked,
@@ -433,6 +452,7 @@ function createNodeHttpClient(options) {
433
452
  fetch: isDefaultFetch ? (url, init) => globalThis.fetch(url, init) : (url, init) => fetchImpl(url, init),
434
453
  baseUrl: options.baseUrl,
435
454
  getAuthToken: options.getAuthToken,
455
+ getProof: options.getProof,
436
456
  onTokenRefresh: options.onTokenRefresh,
437
457
  refreshToken: options.refreshToken,
438
458
  onAuthRevoked: options.onAuthRevoked,
@@ -2572,6 +2592,17 @@ function fromHex(value, label, expectedBytes) {
2572
2592
  }
2573
2593
  return bytes;
2574
2594
  }
2595
+ function fromHexUnsized(value, label) {
2596
+ const clean = value.trim();
2597
+ if (!/^[0-9a-fA-F]*$/.test(clean) || clean.length % 2 !== 0) {
2598
+ throw new Error(`${label} must be an even number of hex characters`);
2599
+ }
2600
+ const bytes = new Uint8Array(clean.length / 2);
2601
+ for (let i = 0; i < bytes.length; i += 1) {
2602
+ bytes[i] = Number.parseInt(clean.slice(i * 2, i * 2 + 2), 16);
2603
+ }
2604
+ return bytes;
2605
+ }
2575
2606
  function u64le(value) {
2576
2607
  const out = new Uint8Array(8);
2577
2608
  new DataView(out.buffer).setBigUint64(0, BigInt(value), true);
@@ -6481,6 +6512,47 @@ async function signRequest(input) {
6481
6512
  );
6482
6513
  }
6483
6514
 
6515
+ // src/request/caller-proof.ts
6516
+ function callerProof(input) {
6517
+ const credential = fromHexUnsized(input.credential, "credential");
6518
+ const request = fromHexUnsized(input.request, "request signature");
6519
+ const session = input.session === void 0 ? new Uint8Array([0]) : concat(new Uint8Array([1]), fromHexUnsized(input.session, "session statement"));
6520
+ return hex(concat(credential, session, request));
6521
+ }
6522
+
6523
+ // src/request/proof-signer.ts
6524
+ function bodyBytes(body) {
6525
+ if (body === void 0 || body === null) return new Uint8Array(0);
6526
+ if (typeof body === "string") return body;
6527
+ if (body instanceof Uint8Array) return body;
6528
+ if (body instanceof ArrayBuffer) return new Uint8Array(body);
6529
+ if (ArrayBuffer.isView(body)) {
6530
+ return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
6531
+ }
6532
+ throw new Error(
6533
+ `a request-carried proof cannot commit to a ${body?.constructor?.name ?? typeof body} body: pass a string or bytes, or use a session for this call`
6534
+ );
6535
+ }
6536
+ function createProofSigner(options) {
6537
+ const ttl = options.ttlSeconds ?? 120;
6538
+ const clock = options.now ?? (() => Math.floor(Date.now() / 1e3));
6539
+ return async (request) => {
6540
+ const issuedAt = clock();
6541
+ return callerProof({
6542
+ credential: options.credential,
6543
+ session: options.session,
6544
+ request: await signRequest({
6545
+ method: request.method,
6546
+ path: request.path,
6547
+ body: bodyBytes(request.body),
6548
+ issuedAt,
6549
+ expiresAt: issuedAt + ttl,
6550
+ signerSecret: options.signerSecret
6551
+ })
6552
+ });
6553
+ };
6554
+ }
6555
+
6484
6556
  // src/login/login.ts
6485
6557
  var SIGN_DOMAIN3 = new TextEncoder().encode("calimero.auth.login.v1");
6486
6558
  var AUDIENCE_TAG = {
@@ -6722,6 +6794,7 @@ export {
6722
6794
  accountRootFromPhrase,
6723
6795
  accountRootFromSecret,
6724
6796
  buildAuthLoginUrl,
6797
+ callerProof,
6725
6798
  combineSignals,
6726
6799
  compareSemver,
6727
6800
  connectCloud,
@@ -6739,6 +6812,7 @@ export {
6739
6812
  createNodeAdminApiClient,
6740
6813
  createNodeAuthApiClient,
6741
6814
  createNodeHttpClient,
6815
+ createProofSigner,
6742
6816
  createRetryableMethod,
6743
6817
  createTimeoutSignal,
6744
6818
  createUniversalHttpClient,