2024-10-16 17:46:33 +00:00
|
|
|
import { withTimeout } from '@/lib/time'
|
2024-11-16 00:38:14 +00:00
|
|
|
import { getScopes, SCOPE_READ, SCOPE_RECEIVE, SCOPE_WRITE, getWallet, request } from '@/wallets/blink/common'
|
2024-10-16 17:46:33 +00:00
|
|
|
import { msatsToSats } from '@/lib/format'
|
2024-11-16 00:38:14 +00:00
|
|
|
export * from '@/wallets/blink'
|
2024-10-16 17:46:33 +00:00
|
|
|
|
|
|
|
export async function testCreateInvoice ({ apiKeyRecv, currencyRecv }) {
|
|
|
|
const scopes = await getScopes(apiKeyRecv)
|
|
|
|
if (!scopes.includes(SCOPE_READ)) {
|
|
|
|
throw new Error('missing READ scope')
|
|
|
|
}
|
2024-10-18 15:20:24 +00:00
|
|
|
if (scopes.includes(SCOPE_WRITE)) {
|
2024-10-16 17:46:33 +00:00
|
|
|
throw new Error('WRITE scope must not be present')
|
|
|
|
}
|
|
|
|
if (!scopes.includes(SCOPE_RECEIVE)) {
|
|
|
|
throw new Error('missing RECEIVE scope')
|
|
|
|
}
|
|
|
|
|
|
|
|
const timeout = 15_000
|
|
|
|
currencyRecv = currencyRecv ? currencyRecv.toUpperCase() : 'BTC'
|
|
|
|
return await withTimeout(createInvoice({ msats: 1000, expiry: 1 }, { apiKeyRecv, currencyRecv }), timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createInvoice (
|
|
|
|
{ msats, description, expiry },
|
|
|
|
{ apiKeyRecv, currencyRecv }) {
|
|
|
|
currencyRecv = currencyRecv ? currencyRecv.toUpperCase() : 'BTC'
|
|
|
|
|
|
|
|
const wallet = await getWallet(apiKeyRecv, currencyRecv)
|
|
|
|
|
|
|
|
if (currencyRecv !== 'BTC') {
|
|
|
|
throw new Error('unsupported currency ' + currencyRecv)
|
|
|
|
}
|
|
|
|
const mutation = `
|
|
|
|
mutation LnInvoiceCreate($input: LnInvoiceCreateInput!) {
|
|
|
|
lnInvoiceCreate(input: $input) {
|
|
|
|
invoice {
|
|
|
|
paymentRequest
|
|
|
|
}
|
|
|
|
errors {
|
|
|
|
message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
const out = await request(apiKeyRecv, mutation, {
|
|
|
|
input: {
|
|
|
|
amount: msatsToSats(msats),
|
|
|
|
expiresIn: Math.floor(expiry / 60) || 1,
|
|
|
|
memo: description,
|
|
|
|
walletId: wallet.id
|
|
|
|
}
|
|
|
|
})
|
|
|
|
const res = out.data.lnInvoiceCreate
|
|
|
|
const errors = res.errors
|
|
|
|
if (errors && errors.length > 0) {
|
2024-11-08 19:26:40 +00:00
|
|
|
throw new Error(errors.map(e => e.code + ' ' + e.message).join(', '))
|
2024-10-16 17:46:33 +00:00
|
|
|
}
|
|
|
|
const invoice = res.invoice.paymentRequest
|
|
|
|
return invoice
|
|
|
|
}
|