packages/api/src/routes/metaplex-upload.js
/**
 * Verifies a self-signed JWT for a metaplex upload.
 *
 * @typedef {object} MetaplexMetadata
 * @property {string} iss the public key of the uploader, as a key:did string
 * @property {string} rootCID the root CID of the upload, as a CIDv1 string
 * @property {string} solanaCluster the name of the solana cluster being targetted
 * @property {string} mintingAgent string identifying the "user agent" that prepared the upload
 * @property {string} [agentVersion] optional string identifying which version of the `mintingAgent` was used
 *
 * @param {string} token - an encoded JWT token from an x-web3auth header.
 * @returns {Promise<MetaplexMetadata>} - metadata extracted from token payload.
 * Rejects with an HTTPError with 401 status if token is invalid or does not contain required payload fields.
 */
Verifies a self-signed JWT for a metaplex upload.

@param {string} token - an encoded JWT token from an x-web3auth header.
@returns {Promise} - metadata extracted from token payload.
Rejects with an HTTPError with 401 status if token is invalid or does not contain required payload fields.
Verifies a self - signed JWT for a metaplex upload . @param { string } token - an encoded JWT token from an x - web3auth header . @returns { Promise } - metadata extracted from token payload . Rejects with an HTTPError with 401 status if token is invalid or does not contain required payload fields .
async function parseMetaplexJWT(token) {
  if (typeof token !== 'string') {
    throw new ErrorInvalidMetaplexToken('token must be a string')
  }
  const tokenParts = token.split('.')
  if (tokenParts.length !== 3) {
    throw new ErrorInvalidMetaplexToken('token must be a signed JWT')
  }
  const header = parseJWTHeader(token)
  const payload = parseJWT(token)
  if (!payload.iss) {
    throw new ErrorInvalidMetaplexToken(
      'required field "iss" not present in payload'
    )
  }
  const { iss } = payload
  const headerPayload = utf8ToUint8Array(tokenParts[0] + '.' + tokenParts[1])
  const pubkey = await keyFromDID(iss)
  const sig = Base64URL.parse(tokenParts[2])
  const validSig = await verifyEd25519Signature(headerPayload, sig, pubkey)
  if (!validSig) {
    throw new ErrorInvalidMetaplexToken('invalid signature')
  }
  if (header.alg !== 'EdDSA') {
    throw new ErrorInvalidMetaplexToken('invalid signing algorithm')
  }
  if (header.typ !== 'JWT') {
    throw new ErrorInvalidMetaplexToken('invalid token type')
  }
  if (!payload.req) {
    throw new ErrorInvalidMetaplexToken(
      'required field "req" not present in payload'
    )
  }
  if (!payload.req.put) {
    throw new ErrorInvalidMetaplexToken(
      'required field "req.put" not present in payload'
    )
  }
  if (typeof payload.req.put.rootCID !== 'string') {
    throw new ErrorInvalidMetaplexToken('root CID not present in payload')
  }
  if (!payload.req.put.tags) {
    throw new ErrorInvalidMetaplexToken('tags not present in payload')
  }
  const { contentCid: rootCID } = parseCid(payload.req.put.rootCID)
  const { solanaCluster, mintingAgent, agentVersion } = parsePutRequestTags(
    payload.req.put.tags
  )
  return {
    iss,
    rootCID,
    solanaCluster,
    mintingAgent,
    agentVersion,
  }
}
async function parseMetaplexJWT ( token ) { if ( typeof token !== 'string' ) { throw new ErrorInvalidMetaplexToken ( 'token must be a string' ) } const tokenParts = token . split ( '.' ) if ( tokenParts . length !== 3 ) { throw new ErrorInvalidMetaplexToken ( 'token must be a signed JWT' ) } const header = parseJWTHeader ( token ) const payload = parseJWT ( token ) if ( ! payload . iss ) { throw new ErrorInvalidMetaplexToken ( 'required field "iss" not present in payload' ) } const { iss } = payload const headerPayload = utf8ToUint8Array ( tokenParts [ 0 ] + '.' + tokenParts [ 1 ] ) const pubkey = await keyFromDID ( iss ) const sig = Base64URL . parse ( tokenParts [ 2 ] ) const validSig = await verifyEd25519Signature ( headerPayload , sig , pubkey ) if ( ! validSig ) { throw new ErrorInvalidMetaplexToken ( 'invalid signature' ) } if ( header . alg !== 'EdDSA' ) { throw new ErrorInvalidMetaplexToken ( 'invalid signing algorithm' ) } if ( header . typ !== 'JWT' ) { throw new ErrorInvalidMetaplexToken ( 'invalid token type' ) } if ( ! payload . req ) { throw new ErrorInvalidMetaplexToken ( 'required field "req" not present in payload' ) } if ( ! payload . req . put ) { throw new ErrorInvalidMetaplexToken ( 'required field "req.put" not present in payload' ) } if ( typeof payload . req . put . rootCID !== 'string' ) { throw new ErrorInvalidMetaplexToken ( 'root CID not present in payload' ) } if ( ! payload . req . put . tags ) { throw new ErrorInvalidMetaplexToken ( 'tags not present in payload' ) } const { contentCid : rootCID } = parseCid ( payload . req . put . rootCID ) const { solanaCluster , mintingAgent , agentVersion } = parsePutRequestTags ( payload . req . put . tags ) return { iss , rootCID , solanaCluster , mintingAgent , agentVersion , } }
Verifies a self-signed JWT for a metaplex upload.
Verifies a self - signed JWT for a metaplex upload .
/** @type {Record<string, any>} */ // validate header