Skip to content

Commit

Permalink
✨ Add evm migration API
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong committed Jan 21, 2025
1 parent ac6d5f2 commit 41119f4
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion src/server/api/routes/users/v2/wallet.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Router } = require('express');
const { walletUserCollection } = require('../../../../modules/firebase');
const { db, walletUserCollection } = require('../../../../modules/firebase');
const { GET_WALLET_API_SECRET } = require('../../../../config/config');

const router = Router();
Expand Down Expand Up @@ -58,4 +58,76 @@ router.get('/wallet', async (req, res, next) => {
}
});

router.post('/wallet/evm/migrate', async (req, res, next) => {
try {
const key = req.headers['x-likerland-api-key'];
const { wallet } = req.query;
const { evmWallet } = req.body;
if (!wallet) {
res.status(400).send('INVALID_QUERY');
return;
}
if (!evmWallet) {
res.status(400).send('MISSING_BODY');
return;
}
if (!key || key !== GET_WALLET_API_SECRET) {
res.status(403).send('INVALID_KEY');
return;
}
let user;
try {
user = await db.startTransaction(async transaction => {
const walletQuery = await walletUserCollection
.where('evmWallet', '==', evmWallet)
.limit(1)
.get();
if (walletQuery.size > 0) {
throw new Error('WALLET_ALREADY_USED');
}
const userDoc = await walletUserCollection.doc(wallet).get();
if (!userDoc.exists) {
throw new Error('USER_NOT_FOUND');
}
const user = userDoc.data();
if (user.evmWallet) {
throw new Error('WALLET_ALREADY_MIGRATED');
}
const likeWallet = userDoc.id;
transaction.update(walletUserCollection.doc(wallet), {
evmWallet,
likeWallet,
});
return {
id: likeWallet,
likeWallet,
...user,
};
});
} catch (err) {
res.status(400).send(err.message);
return;
}
const {
lastLoginMethod,
registerLoginMethod,
displayName,
email: docEmail,
emailUnconfirmed,
} = user;
res.json({
id: user.id,
likeWallet: user.likeWallet,
evmWallet: user.evmWallet,
lastLoginMethod,
registerLoginMethod,
displayName,
email: docEmail,
emailUnconfirmed,
});
} catch (err) {
next(err);
}
});

module.exports = router;

0 comments on commit 41119f4

Please sign in to comment.