stacker.news/api/resolvers/lnurl.js

60 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-06-27 03:18:32 +00:00
import { randomBytes } from 'crypto'
import { bech32 } from 'bech32'
2023-12-14 17:30:51 +00:00
import assertGofacYourself from './ofac'
import assertApiKeyNotPermitted from './apiKey'
import { GqlAuthenticationError } from '@/lib/error'
2021-10-28 19:59:53 +00:00
function encodedUrl (iurl, tag, k1) {
const url = new URL(iurl)
url.searchParams.set('tag', tag)
url.searchParams.set('k1', k1)
// bech32 encode url
const words = bech32.toWords(Buffer.from(url.toString(), 'utf8'))
return bech32.encode('lnurl', words, 1023)
}
function k1 () {
return randomBytes(32).toString('hex')
}
2021-06-27 03:18:32 +00:00
export default {
Query: {
lnAuth: async (parent, { k1 }, { models }) => {
return await models.lnAuth.findUnique({ where: { k1 } })
2021-10-28 19:59:53 +00:00
},
lnWith: async (parent, { k1 }, { models }) => {
return await models.lnWith.findUnique({ where: { k1 } })
2021-06-27 03:18:32 +00:00
}
},
Mutation: {
createAuth: async (parent, args, { models, me }) => {
assertApiKeyNotPermitted({ me })
2021-10-28 19:59:53 +00:00
return await models.lnAuth.create({ data: { k1: k1() } })
},
2023-12-14 17:30:51 +00:00
createWith: async (parent, args, { me, models, headers }) => {
await assertGofacYourself({ models, headers })
2021-10-28 19:59:53 +00:00
if (!me) {
throw new GqlAuthenticationError()
2021-10-28 19:59:53 +00:00
}
assertApiKeyNotPermitted({ me })
2021-10-28 19:59:53 +00:00
return await models.lnWith.create({ data: { k1: k1(), userId: me.id } })
2021-06-27 03:18:32 +00:00
}
},
LnAuth: {
encodedUrl: async (lnAuth, args, { models }) => {
2021-10-28 19:59:53 +00:00
return encodedUrl(process.env.LNAUTH_URL, 'login', lnAuth.k1)
}
},
LnWith: {
encodedUrl: async (lnWith, args, { models }) => {
return encodedUrl(process.env.LNWITH_URL, 'withdrawRequest', lnWith.k1)
},
user: async (lnWith, args, { models }) => {
return await models.user.findUnique({ where: { id: lnWith.userId } })
2021-06-27 03:18:32 +00:00
}
}
}