@calimero-network/mero-js 19.2.0 → 19.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.browser.mjs +2 -2
- package/dist/index.browser.mjs.map +4 -4
- package/dist/index.cjs +168 -0
- package/dist/index.cjs.map +4 -4
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/index.mjs +168 -0
- package/dist/index.mjs.map +4 -4
- package/dist/login/index.d.ts +4 -0
- package/dist/login/index.js +3 -0
- package/dist/login/login.d.ts +101 -0
- package/dist/login/login.js +109 -0
- package/dist/login/session.d.ts +108 -0
- package/dist/login/session.js +148 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -63,15 +63,18 @@ __export(index_exports, {
|
|
|
63
63
|
deviceCertPayload: () => deviceCertPayload,
|
|
64
64
|
discoverLocalNodes: () => discoverLocalNodes,
|
|
65
65
|
encodeSignedInvitation: () => encodeSignedInvitation,
|
|
66
|
+
generateSessionKey: () => generateSessionKey,
|
|
66
67
|
hasCap: () => hasCap,
|
|
67
68
|
intentHash: () => intentHash,
|
|
68
69
|
jsonCodec: () => jsonCodec,
|
|
69
70
|
localNodeUrl: () => localNodeUrl,
|
|
71
|
+
login: () => login,
|
|
70
72
|
mintDeviceId: () => mintDeviceId,
|
|
71
73
|
nodeEndpoint: () => nodeEndpoint,
|
|
72
74
|
parseAuthCallback: () => parseAuthCallback,
|
|
73
75
|
probeNodeHealth: () => probeNodeHealth,
|
|
74
76
|
signDeviceCert: () => signDeviceCert,
|
|
77
|
+
signLoginStatement: () => signLoginStatement,
|
|
75
78
|
signMemberJoinOp: () => signMemberJoinOp,
|
|
76
79
|
signWarrant: () => signWarrant,
|
|
77
80
|
withCap: () => withCap,
|
|
@@ -3690,6 +3693,171 @@ async function signMemberJoinOp(input) {
|
|
|
3690
3693
|
return hex(concat(signable, signature, unendorsed));
|
|
3691
3694
|
}
|
|
3692
3695
|
|
|
3696
|
+
// src/login/login.ts
|
|
3697
|
+
var SIGN_DOMAIN2 = new TextEncoder().encode("calimero.auth.login.v1");
|
|
3698
|
+
var AUDIENCE_TAG = {
|
|
3699
|
+
webOrigin: 0,
|
|
3700
|
+
codeSigningId: 1,
|
|
3701
|
+
cli: 2
|
|
3702
|
+
};
|
|
3703
|
+
function audienceBody(audience) {
|
|
3704
|
+
const encoder = new TextEncoder();
|
|
3705
|
+
switch (audience.kind) {
|
|
3706
|
+
case "webOrigin":
|
|
3707
|
+
return encoder.encode(audience.origin);
|
|
3708
|
+
case "codeSigningId":
|
|
3709
|
+
return encoder.encode(audience.id);
|
|
3710
|
+
case "cli":
|
|
3711
|
+
return new Uint8Array(0);
|
|
3712
|
+
}
|
|
3713
|
+
}
|
|
3714
|
+
function audienceSigningBytes(audience) {
|
|
3715
|
+
return concat(
|
|
3716
|
+
Uint8Array.from([AUDIENCE_TAG[audience.kind]]),
|
|
3717
|
+
audienceBody(audience)
|
|
3718
|
+
);
|
|
3719
|
+
}
|
|
3720
|
+
function audienceWire(audience) {
|
|
3721
|
+
const tag = Uint8Array.from([AUDIENCE_TAG[audience.kind]]);
|
|
3722
|
+
if (audience.kind === "cli") {
|
|
3723
|
+
return tag;
|
|
3724
|
+
}
|
|
3725
|
+
const body = audienceBody(audience);
|
|
3726
|
+
return concat(tag, u32le(body.length), body);
|
|
3727
|
+
}
|
|
3728
|
+
async function signLoginStatement(input) {
|
|
3729
|
+
const node = fromHex(input.node, "node", 32);
|
|
3730
|
+
const challenge = fromHex(input.challenge, "challenge", 32);
|
|
3731
|
+
const sessionKey = fromHex(input.sessionKey, "sessionKey", 32);
|
|
3732
|
+
const key = await importSigningKey(input.deviceSecret);
|
|
3733
|
+
const deviceKey = await derivePublicKey(input.deviceSecret);
|
|
3734
|
+
const issuedAt = u64le(input.issuedAt);
|
|
3735
|
+
const expiresAt = u64le(input.expiresAt);
|
|
3736
|
+
const preimage = await domainHash(SIGN_DOMAIN2, [
|
|
3737
|
+
node,
|
|
3738
|
+
audienceSigningBytes(input.audience),
|
|
3739
|
+
challenge,
|
|
3740
|
+
sessionKey,
|
|
3741
|
+
deviceKey,
|
|
3742
|
+
issuedAt,
|
|
3743
|
+
expiresAt
|
|
3744
|
+
]);
|
|
3745
|
+
const signature = new Uint8Array(
|
|
3746
|
+
await crypto.subtle.sign({ name: "Ed25519" }, key, preimage)
|
|
3747
|
+
);
|
|
3748
|
+
return hex(
|
|
3749
|
+
concat(
|
|
3750
|
+
node,
|
|
3751
|
+
audienceWire(input.audience),
|
|
3752
|
+
challenge,
|
|
3753
|
+
sessionKey,
|
|
3754
|
+
deviceKey,
|
|
3755
|
+
issuedAt,
|
|
3756
|
+
expiresAt,
|
|
3757
|
+
signature
|
|
3758
|
+
)
|
|
3759
|
+
);
|
|
3760
|
+
}
|
|
3761
|
+
|
|
3762
|
+
// src/login/session.ts
|
|
3763
|
+
var DEFAULT_TTL_SECONDS2 = 300;
|
|
3764
|
+
async function generateSessionKey() {
|
|
3765
|
+
const secret = hex(crypto.getRandomValues(new Uint8Array(32)));
|
|
3766
|
+
return { publicKey: hex(await derivePublicKey(secret)), secret };
|
|
3767
|
+
}
|
|
3768
|
+
function audienceLabel(audience) {
|
|
3769
|
+
switch (audience.kind) {
|
|
3770
|
+
case "webOrigin":
|
|
3771
|
+
return audience.origin;
|
|
3772
|
+
case "codeSigningId":
|
|
3773
|
+
return audience.id;
|
|
3774
|
+
case "cli":
|
|
3775
|
+
return "cli";
|
|
3776
|
+
}
|
|
3777
|
+
}
|
|
3778
|
+
async function login(config) {
|
|
3779
|
+
const baseUrl = config.nodeUrl.replace(/\/+$/, "");
|
|
3780
|
+
const doFetch = config.fetch ?? globalThis.fetch;
|
|
3781
|
+
const timeoutMs = config.timeoutMs ?? 1e4;
|
|
3782
|
+
const request = async (path, init) => {
|
|
3783
|
+
const controller = new AbortController();
|
|
3784
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
3785
|
+
let response;
|
|
3786
|
+
try {
|
|
3787
|
+
response = await doFetch(`${baseUrl}${path}`, {
|
|
3788
|
+
...init,
|
|
3789
|
+
signal: controller.signal
|
|
3790
|
+
});
|
|
3791
|
+
} catch (err) {
|
|
3792
|
+
throw new HTTPError(
|
|
3793
|
+
0,
|
|
3794
|
+
err instanceof Error ? err.name : "NetworkError",
|
|
3795
|
+
`${baseUrl}${path}`,
|
|
3796
|
+
new Headers(),
|
|
3797
|
+
err instanceof Error ? err.message : String(err)
|
|
3798
|
+
);
|
|
3799
|
+
} finally {
|
|
3800
|
+
clearTimeout(timer);
|
|
3801
|
+
}
|
|
3802
|
+
const text = await response.text().catch(() => "");
|
|
3803
|
+
if (!response.ok) {
|
|
3804
|
+
throw new HTTPError(
|
|
3805
|
+
response.status,
|
|
3806
|
+
response.statusText,
|
|
3807
|
+
`${baseUrl}${path}`,
|
|
3808
|
+
response.headers,
|
|
3809
|
+
text
|
|
3810
|
+
);
|
|
3811
|
+
}
|
|
3812
|
+
return text ? JSON.parse(text) : {};
|
|
3813
|
+
};
|
|
3814
|
+
const challengeBody = await request("/auth/challenge", {
|
|
3815
|
+
method: "GET",
|
|
3816
|
+
headers: { Accept: "application/json" }
|
|
3817
|
+
});
|
|
3818
|
+
const challenge = challengeBody.challenge ?? challengeBody.data?.challenge;
|
|
3819
|
+
if (!challenge) {
|
|
3820
|
+
throw new Error("the node issued no challenge");
|
|
3821
|
+
}
|
|
3822
|
+
const session = await generateSessionKey();
|
|
3823
|
+
const issuedAt = Math.floor(Date.now() / 1e3);
|
|
3824
|
+
const statement = await signLoginStatement({
|
|
3825
|
+
node: config.node,
|
|
3826
|
+
audience: config.audience,
|
|
3827
|
+
challenge,
|
|
3828
|
+
sessionKey: session.publicKey,
|
|
3829
|
+
issuedAt,
|
|
3830
|
+
expiresAt: issuedAt + (config.ttlSeconds ?? DEFAULT_TTL_SECONDS2),
|
|
3831
|
+
deviceSecret: config.deviceSecret
|
|
3832
|
+
});
|
|
3833
|
+
const tokenBody = await request("/auth/token", {
|
|
3834
|
+
method: "POST",
|
|
3835
|
+
headers: { "Content-Type": "application/json", Accept: "application/json" },
|
|
3836
|
+
body: JSON.stringify({
|
|
3837
|
+
auth_method: "account_proof",
|
|
3838
|
+
public_key: session.publicKey,
|
|
3839
|
+
client_name: config.clientName ?? audienceLabel(config.audience),
|
|
3840
|
+
timestamp: issuedAt,
|
|
3841
|
+
provider_data: {
|
|
3842
|
+
challenge,
|
|
3843
|
+
login_statement: statement,
|
|
3844
|
+
account_proof: config.accountProof
|
|
3845
|
+
}
|
|
3846
|
+
})
|
|
3847
|
+
});
|
|
3848
|
+
const accessToken = tokenBody.access_token ?? tokenBody.data?.access_token;
|
|
3849
|
+
const refreshToken = tokenBody.refresh_token ?? tokenBody.data?.refresh_token;
|
|
3850
|
+
if (!accessToken) {
|
|
3851
|
+
throw new Error("the node minted no session");
|
|
3852
|
+
}
|
|
3853
|
+
return {
|
|
3854
|
+
accessToken,
|
|
3855
|
+
refreshToken: refreshToken ?? "",
|
|
3856
|
+
sessionKey: session.publicKey,
|
|
3857
|
+
sessionSecret: session.secret
|
|
3858
|
+
};
|
|
3859
|
+
}
|
|
3860
|
+
|
|
3693
3861
|
// src/capabilities.ts
|
|
3694
3862
|
var CAPABILITIES = {
|
|
3695
3863
|
CAN_CREATE_CONTEXT: 1 << 0,
|