Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creator node Track owner validation from discovery #3851

Merged
merged 6 commits into from
Sep 21, 2022
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions creator-node/src/routes/tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const {
validateStateForImageDirCIDAndReturnFileUUID,
currentNodeShouldHandleTranscode
} = require('../utils')
const asyncRetry = require('../utils/asyncRetry')
const {
authMiddleware,
ensurePrimaryMiddleware,
Expand Down Expand Up @@ -312,6 +313,37 @@ router.post(
})
)

const validateTrackOwner = async ({
libs,
logger,
trackId,
userId,
blockNumber
}) => {
const asyncFn = async () => {
const discoveryTrackResponse = await libs.Track.getTracks(1, 0, [trackId])
if (
!Array.isArray(discoveryTrackResponse) ||
discoveryTrackResponse.length === 0 ||
!discoveryTrackResponse[0].hasOwnProperty('blocknumber')
) {
throw new Error('Missing or malformatted track fetched from discprov.')
}
const track = discoveryTrackResponse[0]
if (track.blocknumber >= blockNumber) {
return parseInt(userId) === track.owner_id
}
throw new Error(
`Block not yet indexed: Waiting for ${track.blocknumber}, but at ${blockNumber}`
)
}

return await asyncRetry({
asyncFn,
logger
})
}

/**
* Given track blockchainTrackId, blockNumber, and metadataFileUUID, creates/updates Track DB track entry
* and associates segment & image file entries with track. Ends track creation/update process.
Expand Down Expand Up @@ -427,19 +459,18 @@ router.post(
throw new Error('Cannot create track without transcodedTrackUUID.')
}

// Verify that blockchain track id is owned by user attempting to upload it
const serviceRegistry = req.app.get('serviceRegistry')
const { libs } = serviceRegistry
const trackResp = await libs.contracts.TrackFactoryClient.getTrack(
blockchainTrackId
)
if (
!trackResp?.trackOwnerId ||
parseInt(trackResp.trackOwnerId, 10) !==
parseInt(req.session.userId, 10)
) {
// Verify that track id is owned by user attempting to upload it
const libs = req.app.get('audiusLibs')
const isValidTrackOwner = await validateTrackOwner({
libs,
trackId: blockchainTrackId,
userId: req.session.userId,
logger: req.logger,
blockNumber
})
if (!isValidTrackOwner) {
throw new Error(
`Owner ID ${trackResp.trackOwnerId} of blockchainTrackId ${blockchainTrackId} does not match the ID of the user attempting to write this track: ${req.session.userId}`
`Owner ID of track ${blockchainTrackId} does not match ${req.session.userId}`
)
}

Expand Down