2021-06-27 03:18:32 +00:00
|
|
|
import { randomBytes } from 'crypto'
|
|
|
|
import { bech32 } from 'bech32'
|
2023-07-23 15:08:43 +00:00
|
|
|
import { GraphQLError } from 'graphql'
|
2023-12-14 17:30:51 +00:00
|
|
|
import assertGofacYourself from './ofac'
|
2024-04-03 20:11:06 +00:00
|
|
|
import assertApiKeyNotPermitted from './apiKey'
|
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: {
|
2024-04-03 20:11:06 +00:00
|
|
|
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) {
|
2023-07-23 15:08:43 +00:00
|
|
|
throw new GraphQLError('you must be logged in', { extensions: { code: 'UNAUTHENTICATED' } })
|
2021-10-28 19:59:53 +00:00
|
|
|
}
|
|
|
|
|
2024-04-03 20:11:06 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|