@calimero-network/mero-js 13.2.5 → 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/admin-api/admin-client.d.ts +28 -1
- package/dist/admin-api/admin-client.js +29 -0
- package/dist/admin-api/admin-types.d.ts +55 -0
- package/dist/index.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +215 -3
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/index.mjs +215 -3
- package/dist/index.mjs.map +4 -4
- package/dist/warrant/index.d.ts +2 -0
- package/dist/warrant/index.js +2 -0
- package/dist/warrant/warrant.d.ts +77 -0
- package/dist/warrant/warrant.js +196 -0
- package/package.json +1 -1
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,
|
|
@@ -1167,6 +1169,40 @@ var AdminApiClient = class {
|
|
|
1167
1169
|
const node = await this.getNodeIdentity();
|
|
1168
1170
|
return { namespaceId, publicKey: node.publicKey, account: node.accountId };
|
|
1169
1171
|
}
|
|
1172
|
+
/**
|
|
1173
|
+
* Ask this node to run one method on a member's behalf, under a warrant that
|
|
1174
|
+
* member signed.
|
|
1175
|
+
*
|
|
1176
|
+
* The node executes and signs the envelope with its own key; the change is
|
|
1177
|
+
* attributed to the **author**. Both halves travel together, so every peer
|
|
1178
|
+
* re-checks that the member consented instead of trusting the relay.
|
|
1179
|
+
*
|
|
1180
|
+
* Three things have to be true first, and none is implied by the others:
|
|
1181
|
+
*
|
|
1182
|
+
* - the author's **account** is a member of the group owning the context —
|
|
1183
|
+
* their device joins nothing and is in no group's binding rows, which is
|
|
1184
|
+
* exactly the case a device certificate covers;
|
|
1185
|
+
* - this node holds `CAN_AUTHOR_ON_BEHALF` on that group, which is implied by
|
|
1186
|
+
* neither membership nor admin;
|
|
1187
|
+
* - the warrant has not been spent. It is single-use: presenting a valid one
|
|
1188
|
+
* twice is refused, because the signature stays valid forever and replay is
|
|
1189
|
+
* not forgery.
|
|
1190
|
+
*
|
|
1191
|
+
* A refusal comes back as a `403` carrying the reason — "an admin must grant
|
|
1192
|
+
* CAN_AUTHOR_ON_BEHALF to …", or that the nonce is already spent — so a caller
|
|
1193
|
+
* can tell which precondition failed rather than only that one did.
|
|
1194
|
+
*
|
|
1195
|
+
* Mint the warrant with {@link signWarrant}; see
|
|
1196
|
+
* {@link PerformIntentRequest.warrant}.
|
|
1197
|
+
*/
|
|
1198
|
+
async performIntent(contextId, request) {
|
|
1199
|
+
return unwrap(
|
|
1200
|
+
await this.httpClient.post(
|
|
1201
|
+
`/admin-api/contexts/${contextId}/intents`,
|
|
1202
|
+
request
|
|
1203
|
+
)
|
|
1204
|
+
);
|
|
1205
|
+
}
|
|
1170
1206
|
async listNamespacesForApplication(applicationId) {
|
|
1171
1207
|
return unwrap(await this.httpClient.get(`/admin-api/namespaces/for-application/${applicationId}`));
|
|
1172
1208
|
}
|
|
@@ -2533,10 +2569,10 @@ function toHex(bytes) {
|
|
|
2533
2569
|
for (const byte of bytes) out += byte.toString(16).padStart(2, "0");
|
|
2534
2570
|
return out;
|
|
2535
2571
|
}
|
|
2536
|
-
function fromHex(
|
|
2537
|
-
const bytes = new Uint8Array(
|
|
2572
|
+
function fromHex(hex2) {
|
|
2573
|
+
const bytes = new Uint8Array(hex2.length / 2);
|
|
2538
2574
|
for (let i = 0; i < bytes.length; i++) {
|
|
2539
|
-
bytes[i] = parseInt(
|
|
2575
|
+
bytes[i] = parseInt(hex2.slice(i * 2, i * 2 + 2), 16);
|
|
2540
2576
|
}
|
|
2541
2577
|
return bytes;
|
|
2542
2578
|
}
|
|
@@ -2641,6 +2677,182 @@ var CloudClient = class {
|
|
|
2641
2677
|
}
|
|
2642
2678
|
};
|
|
2643
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
|
+
|
|
2644
2856
|
// src/capabilities.ts
|
|
2645
2857
|
var CAPABILITIES = {
|
|
2646
2858
|
CAN_CREATE_CONTEXT: 1 << 0,
|