2024-04-10 07:51:00 +02:00
|
|
|
import { config } from "config-manager";
|
2024-04-13 14:20:12 +02:00
|
|
|
import type * as Lysand from "lysand-types";
|
|
|
|
|
import { type User, getUserUri } from "./User";
|
2024-04-10 07:51:00 +02:00
|
|
|
|
|
|
|
|
export const objectToInboxRequest = async (
|
|
|
|
|
object: Lysand.Entity,
|
|
|
|
|
author: User,
|
|
|
|
|
userToSendTo: User,
|
|
|
|
|
): Promise<Request> => {
|
2024-04-11 13:39:07 +02:00
|
|
|
if (!userToSendTo.instanceId || !userToSendTo.endpoints?.inbox) {
|
2024-04-10 09:13:45 +02:00
|
|
|
throw new Error("UserToSendTo has no inbox or is a local user");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (author.instanceId) {
|
|
|
|
|
throw new Error("Author is a remote user");
|
2024-04-10 07:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const privateKey = await crypto.subtle.importKey(
|
|
|
|
|
"pkcs8",
|
|
|
|
|
Uint8Array.from(atob(author.privateKey ?? ""), (c) => c.charCodeAt(0)),
|
|
|
|
|
"Ed25519",
|
|
|
|
|
false,
|
|
|
|
|
["sign"],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const digest = await crypto.subtle.digest(
|
|
|
|
|
"SHA-256",
|
|
|
|
|
new TextEncoder().encode(JSON.stringify(object)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const userInbox = new URL(userToSendTo.endpoints.inbox);
|
|
|
|
|
|
|
|
|
|
const date = new Date();
|
|
|
|
|
|
|
|
|
|
const signature = await crypto.subtle.sign(
|
|
|
|
|
"Ed25519",
|
|
|
|
|
privateKey,
|
|
|
|
|
new TextEncoder().encode(
|
|
|
|
|
`(request-target): post ${userInbox.pathname}\n` +
|
|
|
|
|
`host: ${userInbox.host}\n` +
|
|
|
|
|
`date: ${date.toISOString()}\n` +
|
|
|
|
|
`digest: SHA-256=${btoa(
|
|
|
|
|
String.fromCharCode(...new Uint8Array(digest)),
|
|
|
|
|
)}\n`,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const signatureBase64 = btoa(
|
|
|
|
|
String.fromCharCode(...new Uint8Array(signature)),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return new Request(userInbox, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
Date: date.toISOString(),
|
2024-04-10 08:37:38 +02:00
|
|
|
Origin: new URL(config.http.base_url).host,
|
2024-04-10 09:13:45 +02:00
|
|
|
Signature: `keyId="${getUserUri(
|
|
|
|
|
author,
|
|
|
|
|
)}",algorithm="ed25519",headers="(request-target) host date digest",signature="${signatureBase64}"`,
|
2024-04-10 07:51:00 +02:00
|
|
|
},
|
|
|
|
|
body: JSON.stringify(object),
|
|
|
|
|
});
|
|
|
|
|
};
|