@calimero-network/mero-js 13.3.0 → 13.4.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
@@ -55,12 +55,14 @@ __export(index_exports, {
55
55
  createUniversalHttpClient: () => createUniversalHttpClient,
56
56
  discoverLocalNodes: () => discoverLocalNodes,
57
57
  hasCap: () => hasCap,
58
+ intentHash: () => intentHash,
58
59
  jsonCodec: () => jsonCodec,
59
60
  localNodeUrl: () => localNodeUrl,
60
61
  nodeEndpoint: () => nodeEndpoint,
61
62
  parseAuthCallback: () => parseAuthCallback,
62
63
  probeNodeHealth: () => probeNodeHealth,
63
64
  sameAccount: () => sameAccount,
65
+ signWarrant: () => signWarrant,
64
66
  toAccountBase58: () => toAccountBase58,
65
67
  toAccountHex: () => toAccountHex,
66
68
  withCap: () => withCap,
@@ -1190,7 +1192,8 @@ var AdminApiClient = class {
1190
1192
  * CAN_AUTHOR_ON_BEHALF to …", or that the nonce is already spent — so a caller
1191
1193
  * can tell which precondition failed rather than only that one did.
1192
1194
  *
1193
- * This SDK does not mint warrants; see {@link PerformIntentRequest.warrant}.
1195
+ * Mint the warrant with {@link signWarrant}; see
1196
+ * {@link PerformIntentRequest.warrant}.
1194
1197
  */
1195
1198
  async performIntent(contextId, request) {
1196
1199
  return unwrap(
@@ -2566,10 +2569,10 @@ function toHex(bytes) {
2566
2569
  for (const byte of bytes) out += byte.toString(16).padStart(2, "0");
2567
2570
  return out;
2568
2571
  }
2569
- function fromHex(hex) {
2570
- const bytes = new Uint8Array(hex.length / 2);
2572
+ function fromHex(hex2) {
2573
+ const bytes = new Uint8Array(hex2.length / 2);
2571
2574
  for (let i = 0; i < bytes.length; i++) {
2572
- bytes[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
2575
+ bytes[i] = parseInt(hex2.slice(i * 2, i * 2 + 2), 16);
2573
2576
  }
2574
2577
  return bytes;
2575
2578
  }
@@ -2674,6 +2677,182 @@ var CloudClient = class {
2674
2677
  }
2675
2678
  };
2676
2679
 
2680
+ // src/warrant/warrant.ts
2681
+ var SIGN_DOMAIN = new TextEncoder().encode("calimero.warrant.v1");
2682
+ var INTENT_DOMAIN = new TextEncoder().encode("calimero.warrant.intent.v1");
2683
+ var PKCS8_ED25519_PREFIX = new Uint8Array([
2684
+ 48,
2685
+ 46,
2686
+ 2,
2687
+ 1,
2688
+ 0,
2689
+ 48,
2690
+ 5,
2691
+ 6,
2692
+ 3,
2693
+ 43,
2694
+ 101,
2695
+ 112,
2696
+ 4,
2697
+ 34,
2698
+ 4,
2699
+ 32
2700
+ ]);
2701
+ var B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
2702
+ function hex(bytes) {
2703
+ return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
2704
+ }
2705
+ function fromHex2(value, label, expectedBytes) {
2706
+ const clean = value.trim();
2707
+ if (!/^[0-9a-fA-F]*$/.test(clean) || clean.length !== expectedBytes * 2) {
2708
+ throw new Error(
2709
+ `${label} must be ${expectedBytes * 2} hex characters, got ${clean.length}`
2710
+ );
2711
+ }
2712
+ const bytes = new Uint8Array(expectedBytes);
2713
+ for (let i = 0; i < expectedBytes; i += 1) {
2714
+ bytes[i] = Number.parseInt(clean.slice(i * 2, i * 2 + 2), 16);
2715
+ }
2716
+ return bytes;
2717
+ }
2718
+ function fromBase58(value, label) {
2719
+ const clean = value.trim();
2720
+ let n = 0n;
2721
+ for (const char of clean) {
2722
+ const digit = B58.indexOf(char);
2723
+ if (digit < 0) {
2724
+ throw new Error(`${label} is not base58: unexpected character '${char}'`);
2725
+ }
2726
+ n = n * 58n + BigInt(digit);
2727
+ }
2728
+ const out = new Uint8Array(32);
2729
+ for (let i = 31; i >= 0; i -= 1) {
2730
+ out[i] = Number(n & 0xffn);
2731
+ n >>= 8n;
2732
+ }
2733
+ if (n !== 0n) {
2734
+ throw new Error(`${label} decodes to more than 32 bytes`);
2735
+ }
2736
+ if (toBase58(out) !== clean) {
2737
+ throw new Error(
2738
+ `${label} is not a canonical base58 32-byte id. If you have it in hex, note that ${label} is base58 here while accounts are hex`
2739
+ );
2740
+ }
2741
+ return out;
2742
+ }
2743
+ function toBase58(bytes) {
2744
+ let n = 0n;
2745
+ for (const byte of bytes) {
2746
+ n = n << 8n | BigInt(byte);
2747
+ }
2748
+ let out = "";
2749
+ while (n > 0n) {
2750
+ out = B58[Number(n % 58n)] + out;
2751
+ n /= 58n;
2752
+ }
2753
+ let leadingZeros = 0;
2754
+ while (leadingZeros < bytes.length && bytes[leadingZeros] === 0) {
2755
+ leadingZeros += 1;
2756
+ }
2757
+ return "1".repeat(leadingZeros) + out;
2758
+ }
2759
+ function u64le(value) {
2760
+ const out = new Uint8Array(8);
2761
+ new DataView(out.buffer).setBigUint64(0, BigInt(value), true);
2762
+ return out;
2763
+ }
2764
+ function concat(...parts) {
2765
+ const out = new Uint8Array(parts.reduce((total, p) => total + p.length, 0));
2766
+ let offset = 0;
2767
+ for (const part of parts) {
2768
+ out.set(part, offset);
2769
+ offset += part.length;
2770
+ }
2771
+ return out;
2772
+ }
2773
+ async function domainHash(domain, parts) {
2774
+ const chunks = [u64le(domain.length), domain];
2775
+ for (const part of parts) {
2776
+ chunks.push(u64le(part.length), part);
2777
+ }
2778
+ return new Uint8Array(
2779
+ await crypto.subtle.digest("SHA-256", concat(...chunks))
2780
+ );
2781
+ }
2782
+ async function intentHash(method, argsJson) {
2783
+ const args = new TextEncoder().encode(JSON.stringify(argsJson));
2784
+ return domainHash(INTENT_DOMAIN, [new TextEncoder().encode(method), args]);
2785
+ }
2786
+ async function importSigningKey(secretHex) {
2787
+ const seed = fromHex2(secretHex, "deviceSecret", 32);
2788
+ const pkcs8 = concat(PKCS8_ED25519_PREFIX, seed);
2789
+ try {
2790
+ return await crypto.subtle.importKey(
2791
+ "pkcs8",
2792
+ pkcs8,
2793
+ { name: "Ed25519" },
2794
+ false,
2795
+ ["sign"]
2796
+ );
2797
+ } catch (cause) {
2798
+ const detail = cause instanceof Error ? `: ${cause.message}` : "";
2799
+ throw new Error(
2800
+ "this runtime has no WebCrypto Ed25519, so a warrant cannot be signed here. Node 18.4+, Safari 17+, Firefox 129+ and Chrome 137+ have it; otherwise mint the warrant with `merod account warrant`" + detail
2801
+ );
2802
+ }
2803
+ }
2804
+ async function signWarrant(input) {
2805
+ const context = fromBase58(input.context, "context");
2806
+ const authorAccount = fromHex2(input.authorAccount, "authorAccount", 32);
2807
+ const executor = fromHex2(input.executor, "executor", 32);
2808
+ const key = await importSigningKey(input.deviceSecret);
2809
+ const publicKey = await derivePublicKey(input.deviceSecret);
2810
+ const commitment = await intentHash(input.method, input.argsJson);
2811
+ const nonce = u64le(input.nonce);
2812
+ const notAfter = u64le(input.notAfter);
2813
+ const preimage = await domainHash(SIGN_DOMAIN, [
2814
+ context,
2815
+ authorAccount,
2816
+ publicKey,
2817
+ executor,
2818
+ commitment,
2819
+ nonce,
2820
+ notAfter
2821
+ ]);
2822
+ const signature = new Uint8Array(
2823
+ await crypto.subtle.sign({ name: "Ed25519" }, key, preimage)
2824
+ );
2825
+ return hex(
2826
+ concat(
2827
+ context,
2828
+ authorAccount,
2829
+ publicKey,
2830
+ executor,
2831
+ commitment,
2832
+ nonce,
2833
+ notAfter,
2834
+ signature
2835
+ )
2836
+ );
2837
+ }
2838
+ async function derivePublicKey(secretHex) {
2839
+ const seed = fromHex2(secretHex, "deviceSecret", 32);
2840
+ const pkcs8 = concat(PKCS8_ED25519_PREFIX, seed);
2841
+ const key = await crypto.subtle.importKey(
2842
+ "pkcs8",
2843
+ pkcs8,
2844
+ { name: "Ed25519" },
2845
+ true,
2846
+ ["sign"]
2847
+ );
2848
+ const jwk = await crypto.subtle.exportKey("jwk", key);
2849
+ const raw = jwk.x;
2850
+ return Uint8Array.from(
2851
+ atob(raw.replace(/-/g, "+").replace(/_/g, "/")),
2852
+ (c) => c.charCodeAt(0)
2853
+ );
2854
+ }
2855
+
2677
2856
  // src/capabilities.ts
2678
2857
  var CAPABILITIES = {
2679
2858
  CAN_CREATE_CONTEXT: 1 << 0,