@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.cjs CHANGED
@@ -42,6 +42,7 @@ __export(index_exports, {
42
42
  accountRootFromPhrase: () => accountRootFromPhrase,
43
43
  accountRootFromSecret: () => accountRootFromSecret,
44
44
  buildAuthLoginUrl: () => buildAuthLoginUrl,
45
+ callerProof: () => callerProof,
45
46
  combineSignals: () => combineSignals,
46
47
  compareSemver: () => compareSemver,
47
48
  connectCloud: () => connectCloud,
@@ -59,6 +60,7 @@ __export(index_exports, {
59
60
  createNodeAdminApiClient: () => createNodeAdminApiClient,
60
61
  createNodeAuthApiClient: () => createNodeAuthApiClient,
61
62
  createNodeHttpClient: () => createNodeHttpClient,
63
+ createProofSigner: () => createProofSigner,
62
64
  createRetryableMethod: () => createRetryableMethod,
63
65
  createTimeoutSignal: () => createTimeoutSignal,
64
66
  createUniversalHttpClient: () => createUniversalHttpClient,
@@ -180,6 +182,14 @@ function headersToRecord(headers) {
180
182
  });
181
183
  return record;
182
184
  }
185
+ function pathForProof(url) {
186
+ try {
187
+ return new URL(url).pathname;
188
+ } catch {
189
+ const query = url.indexOf("?");
190
+ return query === -1 ? url : url.slice(0, query);
191
+ }
192
+ }
183
193
  var WebHttpClient = class {
184
194
  constructor(transport) {
185
195
  this.transport = transport;
@@ -306,6 +316,16 @@ var WebHttpClient = class {
306
316
  if (init?.keepalive !== void 0) {
307
317
  requestInit.keepalive = init.keepalive;
308
318
  }
319
+ if (this.transport.getProof) {
320
+ const proof = await this.transport.getProof({
321
+ method: requestInit.method ?? "GET",
322
+ path: pathForProof(url),
323
+ body: requestInit.body
324
+ });
325
+ if (proof) {
326
+ headersObj["X-Calimero-Proof"] = proof;
327
+ }
328
+ }
309
329
  try {
310
330
  const response = await this.transport.fetch(url, requestInit);
311
331
  if (!response.ok) {
@@ -502,6 +522,7 @@ function createBrowserHttpClient(options) {
502
522
  fetch: (url, init) => globalThis.fetch(url, init),
503
523
  baseUrl: options.baseUrl,
504
524
  getAuthToken: options.getAuthToken,
525
+ getProof: options.getProof,
505
526
  onTokenRefresh: options.onTokenRefresh,
506
527
  refreshToken: options.refreshToken,
507
528
  onAuthRevoked: options.onAuthRevoked,
@@ -528,6 +549,7 @@ function createNodeHttpClient(options) {
528
549
  fetch: isDefaultFetch ? (url, init) => globalThis.fetch(url, init) : (url, init) => fetchImpl(url, init),
529
550
  baseUrl: options.baseUrl,
530
551
  getAuthToken: options.getAuthToken,
552
+ getProof: options.getProof,
531
553
  onTokenRefresh: options.onTokenRefresh,
532
554
  refreshToken: options.refreshToken,
533
555
  onAuthRevoked: options.onAuthRevoked,
@@ -2667,6 +2689,17 @@ function fromHex(value, label, expectedBytes) {
2667
2689
  }
2668
2690
  return bytes;
2669
2691
  }
2692
+ function fromHexUnsized(value, label) {
2693
+ const clean = value.trim();
2694
+ if (!/^[0-9a-fA-F]*$/.test(clean) || clean.length % 2 !== 0) {
2695
+ throw new Error(`${label} must be an even number of hex characters`);
2696
+ }
2697
+ const bytes = new Uint8Array(clean.length / 2);
2698
+ for (let i = 0; i < bytes.length; i += 1) {
2699
+ bytes[i] = Number.parseInt(clean.slice(i * 2, i * 2 + 2), 16);
2700
+ }
2701
+ return bytes;
2702
+ }
2670
2703
  function u64le(value) {
2671
2704
  const out = new Uint8Array(8);
2672
2705
  new DataView(out.buffer).setBigUint64(0, BigInt(value), true);
@@ -6576,6 +6609,47 @@ async function signRequest(input) {
6576
6609
  );
6577
6610
  }
6578
6611
 
6612
+ // src/request/caller-proof.ts
6613
+ function callerProof(input) {
6614
+ const credential = fromHexUnsized(input.credential, "credential");
6615
+ const request = fromHexUnsized(input.request, "request signature");
6616
+ const session = input.session === void 0 ? new Uint8Array([0]) : concat(new Uint8Array([1]), fromHexUnsized(input.session, "session statement"));
6617
+ return hex(concat(credential, session, request));
6618
+ }
6619
+
6620
+ // src/request/proof-signer.ts
6621
+ function bodyBytes(body) {
6622
+ if (body === void 0 || body === null) return new Uint8Array(0);
6623
+ if (typeof body === "string") return body;
6624
+ if (body instanceof Uint8Array) return body;
6625
+ if (body instanceof ArrayBuffer) return new Uint8Array(body);
6626
+ if (ArrayBuffer.isView(body)) {
6627
+ return new Uint8Array(body.buffer, body.byteOffset, body.byteLength);
6628
+ }
6629
+ throw new Error(
6630
+ `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`
6631
+ );
6632
+ }
6633
+ function createProofSigner(options) {
6634
+ const ttl = options.ttlSeconds ?? 120;
6635
+ const clock = options.now ?? (() => Math.floor(Date.now() / 1e3));
6636
+ return async (request) => {
6637
+ const issuedAt = clock();
6638
+ return callerProof({
6639
+ credential: options.credential,
6640
+ session: options.session,
6641
+ request: await signRequest({
6642
+ method: request.method,
6643
+ path: request.path,
6644
+ body: bodyBytes(request.body),
6645
+ issuedAt,
6646
+ expiresAt: issuedAt + ttl,
6647
+ signerSecret: options.signerSecret
6648
+ })
6649
+ });
6650
+ };
6651
+ }
6652
+
6579
6653
  // src/login/login.ts
6580
6654
  var SIGN_DOMAIN3 = new TextEncoder().encode("calimero.auth.login.v1");
6581
6655
  var AUDIENCE_TAG = {