stacker.news/pages/api/lnauth.js

37 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-06-27 03:18:32 +00:00
// verify it's signed
// store pubkey in db
// create user with pubkey and name truncated pubkey
2023-07-25 00:50:55 +00:00
import { secp256k1 } from '@noble/curves/secp256k1'
2021-06-27 03:18:32 +00:00
import models from '../../api/models'
2023-01-18 18:49:20 +00:00
const HOUR = 1000 * 60 * 60
2021-06-27 03:18:32 +00:00
export default async ({ query }, res) => {
try {
2021-08-12 23:46:15 +00:00
const sig = Buffer.from(query.sig, 'hex')
const k1 = Buffer.from(query.k1, 'hex')
const key = Buffer.from(query.key, 'hex')
2023-07-25 14:14:45 +00:00
if (secp256k1.verify(sig, k1, key)) {
2023-01-18 18:49:20 +00:00
const auth = await models.lnAuth.findUnique({ where: { k1: query.k1 } })
if (!auth || auth.pubkey || auth.createdAt < Date.now() - HOUR) {
return res.status(400).json({ status: 'ERROR', reason: 'token expired' })
}
2021-06-27 03:18:32 +00:00
await models.lnAuth.update({ where: { k1: query.k1 }, data: { pubkey: query.key } })
return res.status(200).json({ status: 'OK' })
}
} catch (error) {
console.log(error)
}
2021-08-12 23:46:15 +00:00
let reason = 'signature verification failed'
if (!query.sig) {
reason = 'no sig query variable provided'
} else if (!query.k1) {
reason = 'no k1 query variable provided'
} else if (!query.key) {
reason = 'no key query variable provided'
}
return res.status(400).json({ status: 'ERROR', reason })
2021-06-27 03:18:32 +00:00
}