Mentor Palokaj 66b2598cb9 Backend v1
2021-11-26 11:43:18 +01:00

131 lines
4.5 KiB
JavaScript

const { generateNewOutfitFromId } = require( '../nft-media/changing-room' )
const { db, dataFromSnap } = require( '../modules/firebase' )
const Web3 = require( 'web3' )
const web3 = new Web3()
/* ///////////////////////////////
// POST handler for new avatars
// /////////////////////////////*/
exports.generateNewOutfit = async function( req, res ) {
// Parse the request
let { id } = req.params
if( !id ) return res.json( { error: `No ID specified in URL` } )
// Protect against malformed input
id = Math.floor( Math.abs( id ) )
if( typeof id !== 'number' ) return res.json( { error: `Malformed request` } )
// Set ID to string so firestore can handle it
id = `${ id }`
try {
// Get request data
const { message, signature, signatory } = req.body
if( !message || !signatory || !signature ) throw new Error( `Malformed request` )
// Decode message
const confirmedSignatory = web3.eth.accounts.recover( message, signature )
if( signatory.toLowerCase() !== confirmedSignatory.toLowerCase() ) throw new Error( `Bad signature` )
// Validate message
const messageObject = JSON.parse( message )
const { signer, rocketeerId, chainId } = messageObject
if( signer.toLowerCase() !== confirmedSignatory.toLowerCase() || !rocketeerId || chainId !== chain || !network ) throw new Error( `Invalid message` )
if( rocketeerId != id ) throw new Error( `Invalid Rocketeer in message` )
// Set chain based on envronnment
const chain = process.env.NODE_ENV == 'development' ? '0x4' : '0x1'
const network = chain == '0x1' ? 'mainnet' : 'rinkeby'
// Generate new rocketeer svg
const mediaLink = await generateNewOutfitFromId( id, network )
return res.json( {
outfit: mediaLink
} )
} catch( e ) {
// Log error for debugging
console.error( `POST Changing room api error for ${ id }: `, e )
// Return error to frontend
return res.json( { error: e.mesage || e.toString() } )
}
}
/* ///////////////////////////////
// PUT handler for changing the
// current outfit
// /////////////////////////////*/
exports.setPrimaryOutfit = async function( req, res ) {
// Parse the request
let { id } = req.params
if( !id ) return res.json( { error: `No ID specified in URL` } )
// Protect against malformed input
id = Math.floor( Math.abs( id ) )
if( typeof id !== 'number' ) return res.json( { error: `Malformed request` } )
// Set ID to string so firestore can handle it
id = `${ id }`
try {
// Get request data
const { message, signature, signatory } = req.body
if( !message || !signatory || !signature ) throw new Error( `Malformed request` )
// Decode message
const confirmedSignatory = web3.eth.accounts.recover( message, signature )
if( signatory.toLowerCase() !== confirmedSignatory.toLowerCase() ) throw new Error( `Bad signature` )
// Validate message
const messageObject = JSON.parse( message )
let { signer, outfitId, chainId } = messageObject
if( signer.toLowerCase() !== confirmedSignatory.toLowerCase() || !outfitId || chainId !== chain || !network ) throw new Error( `Invalid message` )
// Validate id format
outfitId = Math.floor( Math.abs( outfitId ) )
if( typeof outfitId !== 'number' ) return res.json( { error: `Malformed request` } )
// Set ID to string so firestore can handle it
outfitId = `${ outfitId }`
// Set chain based on envronnment
const chain = process.env.NODE_ENV == 'development' ? '0x4' : '0x1'
const network = chain == '0x1' ? 'mainnet' : 'rinkeby'
// Retreive old Rocketeer data
const rocketeer = await db.collection( `${ network }Rocketeers` ).doc( id ).get().then( dataFromSnap )
// Grab attributes that will not change
const { value: available_outfits } = rocketeer.attributes.find( ( { trait_type } ) => trait_type == "available outfits" ) || { value: 0 }
// Only allow to set existing outfits
if( available_outfits < outfitId ) throw new Error( `Your Rocketeer has ${ available_outfits }, you can't select outfit ${ outfitId }` )
// Change the primary media file
const imagePath = `${ outfitId == 0 ? id : `${ id }-${ outfitId }` }.jpg`
await db.collection( `${ network }Rocketeers` ).doc( id ).set( {
image: `https://storage.googleapis.com/rocketeer-nft.appspot.com/${ network }Rocketeers/${ imagePath }`
}, { merge: true } )
} catch( e ) {
// Log error for debugging
console.error( `PUT Changing room api error for ${ id }: `, e )
// Return error to frontend
return res.json( { error: e.mesage || e.toString() } )
}
}