@calimero-network/mero-js 15.0.0 → 15.1.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/crypto/internal.d.ts +30 -0
- package/dist/crypto/internal.js +93 -0
- package/dist/device-cert/device-cert.d.ts +68 -0
- package/dist/device-cert/device-cert.js +110 -0
- package/dist/device-cert/index.d.ts +2 -0
- package/dist/device-cert/index.js +2 -0
- package/dist/index.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +100 -21
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +100 -21
- package/dist/index.mjs.map +3 -3
- package/dist/warrant/warrant.d.ts +1 -0
- package/dist/warrant/warrant.js +1 -79
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -35,6 +35,7 @@ __export(index_exports, {
|
|
|
35
35
|
SseClient: () => SseClient,
|
|
36
36
|
WebHttpClient: () => WebHttpClient,
|
|
37
37
|
WsClient: () => WsClient,
|
|
38
|
+
accountForRoot: () => accountForRoot,
|
|
38
39
|
buildAuthLoginUrl: () => buildAuthLoginUrl,
|
|
39
40
|
combineSignals: () => combineSignals,
|
|
40
41
|
compareSemver: () => compareSemver,
|
|
@@ -53,14 +54,17 @@ __export(index_exports, {
|
|
|
53
54
|
createRetryableMethod: () => createRetryableMethod,
|
|
54
55
|
createTimeoutSignal: () => createTimeoutSignal,
|
|
55
56
|
createUniversalHttpClient: () => createUniversalHttpClient,
|
|
57
|
+
deviceCertPayload: () => deviceCertPayload,
|
|
56
58
|
discoverLocalNodes: () => discoverLocalNodes,
|
|
57
59
|
hasCap: () => hasCap,
|
|
58
60
|
intentHash: () => intentHash,
|
|
59
61
|
jsonCodec: () => jsonCodec,
|
|
60
62
|
localNodeUrl: () => localNodeUrl,
|
|
63
|
+
mintDeviceId: () => mintDeviceId,
|
|
61
64
|
nodeEndpoint: () => nodeEndpoint,
|
|
62
65
|
parseAuthCallback: () => parseAuthCallback,
|
|
63
66
|
probeNodeHealth: () => probeNodeHealth,
|
|
67
|
+
signDeviceCert: () => signDeviceCert,
|
|
64
68
|
signWarrant: () => signWarrant,
|
|
65
69
|
withCap: () => withCap,
|
|
66
70
|
withRetry: () => withRetry,
|
|
@@ -2592,9 +2596,7 @@ var CloudClient = class {
|
|
|
2592
2596
|
}
|
|
2593
2597
|
};
|
|
2594
2598
|
|
|
2595
|
-
// src/
|
|
2596
|
-
var SIGN_DOMAIN = new TextEncoder().encode("calimero.warrant.v1");
|
|
2597
|
-
var INTENT_DOMAIN = new TextEncoder().encode("calimero.warrant.intent.v1");
|
|
2599
|
+
// src/crypto/internal.ts
|
|
2598
2600
|
var PKCS8_ED25519_PREFIX = new Uint8Array([
|
|
2599
2601
|
48,
|
|
2600
2602
|
46,
|
|
@@ -2613,6 +2615,11 @@ var PKCS8_ED25519_PREFIX = new Uint8Array([
|
|
|
2613
2615
|
4,
|
|
2614
2616
|
32
|
|
2615
2617
|
]);
|
|
2618
|
+
function u32le(value) {
|
|
2619
|
+
const out = new Uint8Array(4);
|
|
2620
|
+
new DataView(out.buffer).setUint32(0, value, true);
|
|
2621
|
+
return out;
|
|
2622
|
+
}
|
|
2616
2623
|
function hex(bytes) {
|
|
2617
2624
|
return Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
2618
2625
|
}
|
|
@@ -2652,10 +2659,6 @@ async function domainHash(domain, parts) {
|
|
|
2652
2659
|
await crypto.subtle.digest("SHA-256", concat(...chunks))
|
|
2653
2660
|
);
|
|
2654
2661
|
}
|
|
2655
|
-
async function intentHash(method, argsJson) {
|
|
2656
|
-
const args = new TextEncoder().encode(JSON.stringify(argsJson));
|
|
2657
|
-
return domainHash(INTENT_DOMAIN, [new TextEncoder().encode(method), args]);
|
|
2658
|
-
}
|
|
2659
2662
|
async function importSigningKey(secretHex) {
|
|
2660
2663
|
const seed = fromHex(secretHex, "deviceSecret", 32);
|
|
2661
2664
|
const pkcs8 = concat(PKCS8_ED25519_PREFIX, seed);
|
|
@@ -2674,6 +2677,31 @@ async function importSigningKey(secretHex) {
|
|
|
2674
2677
|
);
|
|
2675
2678
|
}
|
|
2676
2679
|
}
|
|
2680
|
+
async function derivePublicKey(secretHex) {
|
|
2681
|
+
const seed = fromHex(secretHex, "deviceSecret", 32);
|
|
2682
|
+
const pkcs8 = concat(PKCS8_ED25519_PREFIX, seed);
|
|
2683
|
+
const key = await crypto.subtle.importKey(
|
|
2684
|
+
"pkcs8",
|
|
2685
|
+
pkcs8,
|
|
2686
|
+
{ name: "Ed25519" },
|
|
2687
|
+
true,
|
|
2688
|
+
["sign"]
|
|
2689
|
+
);
|
|
2690
|
+
const jwk = await crypto.subtle.exportKey("jwk", key);
|
|
2691
|
+
const raw = jwk.x;
|
|
2692
|
+
return Uint8Array.from(
|
|
2693
|
+
atob(raw.replace(/-/g, "+").replace(/_/g, "/")),
|
|
2694
|
+
(c) => c.charCodeAt(0)
|
|
2695
|
+
);
|
|
2696
|
+
}
|
|
2697
|
+
|
|
2698
|
+
// src/warrant/warrant.ts
|
|
2699
|
+
var SIGN_DOMAIN = new TextEncoder().encode("calimero.warrant.v1");
|
|
2700
|
+
var INTENT_DOMAIN = new TextEncoder().encode("calimero.warrant.intent.v1");
|
|
2701
|
+
async function intentHash(method, argsJson) {
|
|
2702
|
+
const args = new TextEncoder().encode(JSON.stringify(argsJson));
|
|
2703
|
+
return domainHash(INTENT_DOMAIN, [new TextEncoder().encode(method), args]);
|
|
2704
|
+
}
|
|
2677
2705
|
async function signWarrant(input) {
|
|
2678
2706
|
const context = fromHex(input.context, "context", 32);
|
|
2679
2707
|
const authorAccount = fromHex(input.authorAccount, "authorAccount", 32);
|
|
@@ -2708,21 +2736,72 @@ async function signWarrant(input) {
|
|
|
2708
2736
|
)
|
|
2709
2737
|
);
|
|
2710
2738
|
}
|
|
2711
|
-
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2715
|
-
|
|
2716
|
-
|
|
2717
|
-
|
|
2718
|
-
|
|
2719
|
-
|
|
2739
|
+
|
|
2740
|
+
// src/device-cert/device-cert.ts
|
|
2741
|
+
var CERT_DOMAIN = new TextEncoder().encode("calimero.device.cert.v1");
|
|
2742
|
+
var ACCOUNT_ID_DOMAIN = new TextEncoder().encode("calimero.account.genesis.v1");
|
|
2743
|
+
var DEVICE_ID_DOMAIN = new TextEncoder().encode("calimero.device.id.v1");
|
|
2744
|
+
var ACCOUNT_GENESIS_VERSION = 1;
|
|
2745
|
+
async function mintDeviceId(account, nonce) {
|
|
2746
|
+
if (nonce.length !== 16) {
|
|
2747
|
+
throw new Error(`nonce must be 16 bytes, got ${nonce.length}`);
|
|
2748
|
+
}
|
|
2749
|
+
const accountBytes = fromHex(account, "account", 32);
|
|
2750
|
+
return hex(await domainHash(DEVICE_ID_DOMAIN, [accountBytes, nonce]));
|
|
2751
|
+
}
|
|
2752
|
+
async function deviceCertPayload(input) {
|
|
2753
|
+
return domainHash(CERT_DOMAIN, [
|
|
2754
|
+
fromHex(input.account, "account", 32),
|
|
2755
|
+
fromHex(input.device, "device", 32),
|
|
2756
|
+
fromHex(input.signPublicKey, "signPublicKey", 32),
|
|
2757
|
+
fromHex(input.kemPublicKey, "kemPublicKey", 32),
|
|
2758
|
+
u32le(input.keyEpoch),
|
|
2759
|
+
u32le(input.deviceEpoch)
|
|
2760
|
+
]);
|
|
2761
|
+
}
|
|
2762
|
+
async function signDeviceCert(input) {
|
|
2763
|
+
const rootPublicKey = await derivePublicKey(input.rootSecret);
|
|
2764
|
+
const account = hex(
|
|
2765
|
+
await domainHash(ACCOUNT_ID_DOMAIN, [
|
|
2766
|
+
concat(new Uint8Array([ACCOUNT_GENESIS_VERSION]), rootPublicKey)
|
|
2767
|
+
])
|
|
2720
2768
|
);
|
|
2721
|
-
const
|
|
2722
|
-
const
|
|
2723
|
-
|
|
2724
|
-
|
|
2725
|
-
|
|
2769
|
+
const keyEpoch = 0;
|
|
2770
|
+
const payload = await deviceCertPayload({
|
|
2771
|
+
account,
|
|
2772
|
+
device: input.device,
|
|
2773
|
+
signPublicKey: input.signPublicKey,
|
|
2774
|
+
kemPublicKey: input.kemPublicKey,
|
|
2775
|
+
keyEpoch,
|
|
2776
|
+
deviceEpoch: input.deviceEpoch
|
|
2777
|
+
});
|
|
2778
|
+
const key = await importSigningKey(input.rootSecret);
|
|
2779
|
+
const signature = new Uint8Array(
|
|
2780
|
+
await crypto.subtle.sign("Ed25519", key, payload)
|
|
2781
|
+
);
|
|
2782
|
+
const credential = concat(
|
|
2783
|
+
// AccountGenesis
|
|
2784
|
+
new Uint8Array([ACCOUNT_GENESIS_VERSION]),
|
|
2785
|
+
rootPublicKey,
|
|
2786
|
+
// chain: Vec<RootKeyHandoff>, empty
|
|
2787
|
+
u32le(0),
|
|
2788
|
+
// DeviceCert
|
|
2789
|
+
fromHex(account, "account", 32),
|
|
2790
|
+
fromHex(input.device, "device", 32),
|
|
2791
|
+
fromHex(input.signPublicKey, "signPublicKey", 32),
|
|
2792
|
+
fromHex(input.kemPublicKey, "kemPublicKey", 32),
|
|
2793
|
+
u32le(keyEpoch),
|
|
2794
|
+
u32le(input.deviceEpoch),
|
|
2795
|
+
signature
|
|
2796
|
+
);
|
|
2797
|
+
return hex(credential);
|
|
2798
|
+
}
|
|
2799
|
+
async function accountForRoot(rootSecret) {
|
|
2800
|
+
const rootPublicKey = await derivePublicKey(rootSecret);
|
|
2801
|
+
return hex(
|
|
2802
|
+
await domainHash(ACCOUNT_ID_DOMAIN, [
|
|
2803
|
+
concat(new Uint8Array([ACCOUNT_GENESIS_VERSION]), rootPublicKey)
|
|
2804
|
+
])
|
|
2726
2805
|
);
|
|
2727
2806
|
}
|
|
2728
2807
|
|