Skip to content

Commit f1ba0de

Browse files
committed
fix merge
2 parents 1ba661f + 851c580 commit f1ba0de

File tree

10 files changed

+55
-35
lines changed

10 files changed

+55
-35
lines changed

api/resolvers/wallet.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,10 @@ async function upsertWallet (
854854

855855
if (testCreateInvoice) {
856856
try {
857-
await testCreateInvoice(data)
857+
const pr = await testCreateInvoice(data)
858+
if (!pr || typeof pr !== 'string' || !pr.startsWith('lnbc')) {
859+
throw new GqlInputError('not a valid payment request')
860+
}
858861
} catch (err) {
859862
const message = 'failed to create test invoice: ' + (err.message || err.toString?.())
860863
logger.error(message)

lib/cln.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ export const createInvoice = async ({ msats, description, expiry }, { socket, ru
1010

1111
const url = `${agent.protocol}//${socket}/v1/invoice`
1212

13+
const method = 'POST'
1314
let res
1415
try {
1516
res = await fetch(url, {
16-
method: 'POST',
17+
method,
1718
headers: {
1819
'Content-Type': 'application/json',
1920
Rune: rune,
@@ -41,8 +42,8 @@ export const createInvoice = async ({ msats, description, expiry }, { socket, ru
4142
throw err
4243
}
4344

44-
assertResponseOk(res)
45-
assertContentTypeJson(res)
45+
assertResponseOk(res, { method })
46+
assertContentTypeJson(res, { method })
4647

4748
const inv = await res.json()
4849
if (inv.error) {

lib/url.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,22 +213,30 @@ export function parseNwcUrl (walletConnectUrl) {
213213
return params
214214
}
215215

216-
export function assertResponseOk (res) {
217-
if (!res.ok) {
216+
class ResponseAssertError extends Error {
217+
constructor (res, { method } = {}) {
218+
if (method) {
219+
super(`${method} ${res.url}: ${res.status} ${res.statusText}`)
220+
} else {
221+
super(`${res.url}: ${res.status} ${res.statusText}`)
222+
}
223+
this.name = 'ResponseAssertError'
218224
// consume response body to avoid memory leaks
219225
// see https://github.com/nodejs/node/issues/51162
220226
res.text().catch(() => {})
221-
throw new Error(`POST ${res.url}: ${res.status} ${res.statusText}`)
222227
}
223228
}
224229

225-
export function assertContentTypeJson (res) {
230+
export function assertResponseOk (res, { method } = {}) {
231+
if (!res.ok) {
232+
throw new ResponseAssertError(res, { method })
233+
}
234+
}
235+
236+
export function assertContentTypeJson (res, { method } = {}) {
226237
const contentType = res.headers.get('content-type')
227238
if (!contentType || !contentType.includes('application/json')) {
228-
// consume response body to avoid memory leaks
229-
// see https://github.com/nodejs/node/issues/51162
230-
res.text().catch(() => {})
231-
throw new Error(`POST ${res.url}: ${res.status} ${res.statusText}`)
239+
throw new ResponseAssertError(res, { method })
232240
}
233241
}
234242

wallets/blink/common.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ export async function getWallet ({ apiKey, currency }, { signal }) {
3535
}
3636

3737
export async function request ({ apiKey, query, variables = {} }, { signal }) {
38+
const method = 'POST'
3839
const res = await fetchWithTimeout(galoyBlinkUrl, {
39-
method: 'POST',
40+
method,
4041
headers: {
4142
'Content-Type': 'application/json',
4243
'X-API-KEY': apiKey
@@ -45,8 +46,8 @@ export async function request ({ apiKey, query, variables = {} }, { signal }) {
4546
signal
4647
})
4748

48-
assertResponseOk(res)
49-
assertContentTypeJson(res)
49+
assertResponseOk(res, { method })
50+
assertContentTypeJson(res, { method })
5051

5152
return res.json()
5253
}

wallets/lightning-address/server.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ export const createInvoice = async (
2727
}
2828

2929
// call callback with amount and conditionally comment
30-
const res = await fetchWithTimeout(callbackUrl.toString(), { signal })
30+
const method = 'GET'
31+
const res = await fetchWithTimeout(callbackUrl.toString(), { method, signal })
3132

32-
assertResponseOk(res)
33-
assertContentTypeJson(res)
33+
assertResponseOk(res, { method })
34+
assertContentTypeJson(res, { method })
3435

3536
const body = await res.json()
3637
if (body.status === 'ERROR') {

wallets/lnbits/client.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ async function getWallet ({ url, adminKey, invoiceKey }, { signal }) {
3333
headers.append('Content-Type', 'application/json')
3434
headers.append('X-Api-Key', adminKey || invoiceKey)
3535

36-
const res = await fetchWithTimeout(url + path, { method: 'GET', headers, signal })
36+
const method = 'GET'
37+
const res = await fetchWithTimeout(url + path, { method, headers, signal })
3738

38-
assertContentTypeJson(res)
39+
assertContentTypeJson(res, { method })
3940
if (!res.ok) {
4041
const errBody = await res.json()
4142
throw new Error(errBody.detail)
@@ -55,9 +56,10 @@ async function postPayment (bolt11, { url, adminKey }, { signal }) {
5556

5657
const body = JSON.stringify({ bolt11, out: true })
5758

58-
const res = await fetchWithTimeout(url + path, { method: 'POST', headers, body, signal })
59+
const method = 'POST'
60+
const res = await fetchWithTimeout(url + path, { method, headers, body, signal })
5961

60-
assertContentTypeJson(res)
62+
assertContentTypeJson(res, { method })
6163
if (!res.ok) {
6264
const errBody = await res.json()
6365
throw new Error(errBody.detail)
@@ -75,9 +77,10 @@ async function getPayment (paymentHash, { url, adminKey }, { signal }) {
7577
headers.append('Content-Type', 'application/json')
7678
headers.append('X-Api-Key', adminKey)
7779

78-
const res = await fetchWithTimeout(url + path, { method: 'GET', headers, signal })
80+
const method = 'GET'
81+
const res = await fetchWithTimeout(url + path, { method, headers, signal })
7982

80-
assertContentTypeJson(res)
83+
assertContentTypeJson(res, { method })
8184
if (!res.ok) {
8285
const errBody = await res.json()
8386
throw new Error(errBody.detail)

wallets/lnbits/server.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ export async function createInvoice (
4242
}
4343

4444
let res
45+
const method = 'POST'
4546
try {
4647
res = await fetch(`${agent.protocol}//${hostname}${path}`, {
47-
method: 'POST',
48+
method,
4849
headers,
4950
agent,
5051
body,
@@ -54,12 +55,12 @@ export async function createInvoice (
5455
if (err.name === 'AbortError') {
5556
// XXX node-fetch doesn't throw our custom TimeoutError but throws a generic error so we have to handle that manually.
5657
// see https://github.com/node-fetch/node-fetch/issues/1462
57-
throw new FetchTimeoutError('POST', url, WALLET_CREATE_INVOICE_TIMEOUT_MS)
58+
throw new FetchTimeoutError(method, url, WALLET_CREATE_INVOICE_TIMEOUT_MS)
5859
}
5960
throw err
6061
}
6162

62-
assertContentTypeJson(res)
63+
assertContentTypeJson(res, { method })
6364
if (!res.ok) {
6465
const errBody = await res.json()
6566
throw new Error(errBody.detail)

wallets/nwc/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ export async function testCreateInvoice ({ nwcUrlRecv }, { signal }) {
2222

2323
export async function createInvoice ({ msats, description, expiry }, { nwcUrlRecv }, { signal }) {
2424
const result = await nwcTryRun(
25-
nwc => nwc.sendReq('make_invoice', { amount: msats, description, expiry }),
25+
nwc => nwc.req('make_invoice', { amount: msats, description, expiry }),
2626
{ nwcUrl: nwcUrlRecv }, { signal }
2727
)
28-
return result.invoice
28+
return result.result.invoice
2929
}

wallets/phoenixd/client.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ export async function sendPayment (bolt11, { url, primaryPassword }, { signal })
2222
const body = new URLSearchParams()
2323
body.append('invoice', bolt11)
2424

25+
const method = 'POST'
2526
const res = await fetchWithTimeout(url + path, {
26-
method: 'POST',
27+
method,
2728
headers,
2829
body,
2930
signal
3031
})
3132

32-
assertResponseOk(res)
33-
assertContentTypeJson(res)
33+
assertResponseOk(res, { method })
34+
assertContentTypeJson(res, { method })
3435

3536
const payment = await res.json()
3637
const preimage = payment.paymentPreimage

wallets/phoenixd/server.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,17 @@ export async function createInvoice (
3131
const hostname = url.replace(/^https?:\/\//, '').replace(/\/+$/, '')
3232
const agent = getAgent({ hostname })
3333

34+
const method = 'POST'
3435
const res = await fetchWithTimeout(`${agent.protocol}//${hostname}${path}`, {
35-
method: 'POST',
36+
method,
3637
headers,
3738
agent,
3839
body,
3940
signal
4041
})
4142

42-
assertResponseOk(res)
43-
assertContentTypeJson(res)
43+
assertResponseOk(res, { method })
44+
assertContentTypeJson(res, { method })
4445

4546
const payment = await res.json()
4647
return payment.serialized

0 commit comments

Comments
 (0)