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 Feb 18, 2025
1 parent dc33085 commit 5571e07
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 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,77 @@ 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 t => {
const [walletQuery, userDoc] = await Promise.all([
t.get(
walletUserCollection.where('evmWallet', '==', evmWallet).limit(1)
),
t.get(walletUserCollection.doc(wallet)),
]);
if (walletQuery.size > 0) {
throw new Error('WALLET_ALREADY_USED');
}
if (!userDoc.exists) {
throw new Error('USER_NOT_FOUND');
}
const userData = userDoc.data();
if (userData.evmWallet) {
throw new Error('WALLET_ALREADY_MIGRATED');
}
const likeWallet = userDoc.id;
t.update(walletUserCollection.doc(wallet), {
evmWallet,
likeWallet,
});
return {
id: likeWallet,
likeWallet,
...userData,
};
});
} 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 5571e07

Please sign in to comment.