import { buildDigest } from '@server/helpers/peertube-crypto' ; import { getServerActor } from '@server/models/application/application' ; import { ContextType } from '@shared/models/activitypub/context' ; import { buildSignedActivity } from '../../../../helpers/activitypub' ; import { ACTIVITY_PUB , HTTP_SIGNATURE } from '../../../../initializers/constants' ; import { ActorModel } from '../../../../models/actor/actor' ; import { MActor } from '../../../../types/models' ; type Payload < T > = { body : T ; contextType ? : ContextType ; signatureActorId ? : number ; } ; async function computeBody < T > ( payload ) { let body = payload . body ; if ( payload . signatureActorId ) { const actorSignature = await ActorModel . load ( payload . signatureActorId ) ; if ( ! actorSignature ) throw new Error ( 'Unknown signature actor id.' ) ; body = await buildSignedActivity ( actorSignature , payload . body , payload . contextType ) ; } return body ; } async function buildSignedRequestOptions ( payload ) { let actor ; if ( payload . signatureActorId ) { actor = await ActorModel . load ( payload . signatureActorId ) ; if ( ! actor ) throw new Error ( 'Unknown signature actor id.' ) ; } else { actor = await getServerActor ( ) ; } const keyId = actor . url ; return { algorithm : HTTP_SIGNATURE . ALGORITHM , authorizationHeaderName : HTTP_SIGNATURE . HEADER_NAME , keyId , key : actor . privateKey , headers : HTTP_SIGNATURE . HEADERS_TO_SIGN } ; } function buildGlobalHeaders ( body ) { return { 'digest' : buildDigest ( body ) , 'content-type' : 'application/activity+json' , 'accept' : ACTIVITY_PUB . ACCEPT_HEADER } ; } export { buildGlobalHeaders , computeBody , buildSignedRequestOptions } ;