Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

More robust actor keys creation

Chocobozzz172304686dbc
diff --git a/server/core/lib/activitypub/actors/keys.ts b/server/core/lib/activitypub/actors/keys.ts
index bf7973756c..25ff9cb49f 100644
--- a/server/core/lib/activitypub/actors/keys.ts
+++ b/server/core/lib/activitypub/actors/keys.ts
@@ -1,8 +1,7 @@
import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto.js'
import { MActor } from '@server/types/models/index.js'
-// Set account keys, this could be long so process after the account creation and do not block the client
-async function generateAndSaveActorKeys <T extends MActor> (actor: T) {
+export async function generateAndSaveActorKeys<T extends MActor> (actor: T) {
const { publicKey, privateKey } = await createPrivateAndPublicKeys()
actor.publicKey = publicKey
@@ -10,7 +9,3 @@ async function generateAndSaveActorKeys <T extends MActor> (actor: T) {
return actor.save()
}
-
-export {
- generateAndSaveActorKeys
-}
diff --git a/server/core/lib/job-queue/handlers/actor-keys.ts b/server/core/lib/job-queue/handlers/actor-keys.ts
index 40dfc6d49a..fd87120824 100644
--- a/server/core/lib/job-queue/handlers/actor-keys.ts
+++ b/server/core/lib/job-queue/handlers/actor-keys.ts
@@ -4,7 +4,7 @@ import { ActorModel } from '@server/models/actor/actor.js'
import { ActorKeysPayload } from '@peertube/peertube-models'
import { logger } from '../../../helpers/logger.js'
-async function processActorKeys (job: Job) {
+export async function processActorKeys (job: Job) {
const payload = job.data as ActorKeysPayload
logger.info('Processing actor keys in job %s.', job.id)
@@ -12,9 +12,3 @@ async function processActorKeys (job: Job) {
await generateAndSaveActorKeys(actor)
}
-
-// ---------------------------------------------------------------------------
-
-export {
- processActorKeys
-}
diff --git a/server/core/lib/user.ts b/server/core/lib/user.ts
index f0db8d0d2c..3ab9059707 100644
--- a/server/core/lib/user.ts
+++ b/server/core/lib/user.ts
@@ -26,6 +26,7 @@ import { buildActorInstance, findAvailableLocalActorName } from './local-actor.j
import { Redis } from './redis.js'
import { createLocalVideoChannelWithoutKeys } from './video-channel.js'
import { createWatchLaterPlaylist } from './video-playlist.js'
+import { createPrivateAndPublicKeys } from '@server/helpers/peertube-crypto.js'
type ChannelNames = { name: string, displayName: string }
@@ -91,7 +92,10 @@ export async function createUserAccountAndChannelAndPlaylist (parameters: {
}): Promise<{ user: MUserDefault, account: MAccountDefault, videoChannel: MChannelActor }> {
const { userToCreate, userDisplayName, channelNames, validateUser = true } = parameters
- const { user, account, videoChannel } = await sequelizeTypescript.transaction(async t => {
+ const accountKeys = await createPrivateAndPublicKeys()
+ const channelKeys = await createPrivateAndPublicKeys()
+
+ return sequelizeTypescript.transaction(async t => {
const userOptions = {
transaction: t,
validate: validateUser
@@ -107,25 +111,23 @@ export async function createUserAccountAndChannelAndPlaylist (parameters: {
applicationId: null,
t
})
+ accountCreated.Actor.publicKey = accountKeys.publicKey
+ accountCreated.Actor.privateKey = accountKeys.privateKey
+ await accountCreated.Actor.save({ transaction: t })
+
userCreated.Account = accountCreated
const channelAttributes = await buildChannelAttributes({ user: userCreated, transaction: t, channelNames })
const videoChannel = await createLocalVideoChannelWithoutKeys(channelAttributes, accountCreated, t)
+ videoChannel.Actor.publicKey = channelKeys.publicKey
+ videoChannel.Actor.privateKey = channelKeys.privateKey
+ await videoChannel.Actor.save({ transaction: t })
+
const videoPlaylist = await createWatchLaterPlaylist(accountCreated, t)
return { user: userCreated, account: accountCreated, videoChannel, videoPlaylist }
})
-
- const [ accountActorWithKeys, channelActorWithKeys ] = await Promise.all([
- generateAndSaveActorKeys(account.Actor),
- generateAndSaveActorKeys(videoChannel.Actor)
- ])
-
- account.Actor = accountActorWithKeys
- videoChannel.Actor = channelActorWithKeys
-
- return { user, account, videoChannel }
}
export async function createLocalAccountWithoutKeys (parameters: {
@@ -147,6 +149,7 @@ export async function createLocalAccountWithoutKeys (parameters: {
const url = getLocalAccountActivityPubUrl(name)
const actor = buildActorInstance(type, url, name)
actor.accountId = account.id
+
await actor.save({ transaction: t })
return Object.assign(account, { Actor: actor })