From 3cdfe620d01a13a84c7f8e68abe9ac65942436bf Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 14 Dec 2024 15:56:45 +0100 Subject: [PATCH] Refactor Blink function signatures (#1725) This makes them consistent with function signatures of other wallets --- wallets/blink/client.js | 100 +++++++++++++++++++++------------------- wallets/blink/common.js | 49 +++++++++++--------- wallets/blink/server.js | 55 +++++++++++----------- 3 files changed, 108 insertions(+), 96 deletions(-) diff --git a/wallets/blink/client.js b/wallets/blink/client.js index 8779d64a..a27df06f 100644 --- a/wallets/blink/client.js +++ b/wallets/blink/client.js @@ -3,7 +3,8 @@ export * from '@/wallets/blink' export async function testSendPayment ({ apiKey, currency }, { logger }) { logger.info('trying to fetch ' + currency + ' wallet') - const scopes = await getScopes(apiKey) + + const scopes = await getScopes({ apiKey }) if (!scopes.includes(SCOPE_READ)) { throw new Error('missing READ scope') } @@ -12,46 +13,48 @@ export async function testSendPayment ({ apiKey, currency }, { logger }) { } currency = currency ? currency.toUpperCase() : 'BTC' - await getWallet(apiKey, currency) + await getWallet({ apiKey, currency }) logger.ok(currency + ' wallet found') } export async function sendPayment (bolt11, { apiKey, currency }) { - const wallet = await getWallet(apiKey, currency) - return await payInvoice(apiKey, wallet, bolt11) + const wallet = await getWallet({ apiKey, currency }) + return await payInvoice(bolt11, { apiKey, wallet }) } -async function payInvoice (authToken, wallet, invoice) { - const walletId = wallet.id - const out = await request(authToken, ` - mutation LnInvoicePaymentSend($input: LnInvoicePaymentInput!) { +async function payInvoice (bolt11, { apiKey, wallet }) { + const out = await request({ + apiKey, + query: ` + mutation LnInvoicePaymentSend($input: LnInvoicePaymentInput!) { lnInvoicePaymentSend(input: $input) { - status - errors { - message - path - code - } - transaction { - settlementVia { - ... on SettlementViaIntraLedger { - preImage - } - ... on SettlementViaLn { - preImage - } - } + status + errors { + message + path + code + } + transaction { + settlementVia { + ... on SettlementViaIntraLedger { + preImage + } + ... on SettlementViaLn { + preImage + } } + } } - } - `, - { - input: { - paymentRequest: invoice, - walletId + }`, + variables: { + input: { + paymentRequest: bolt11, + walletId: wallet.id + } } }) + const status = out.data.lnInvoicePaymentSend.status const errors = out.data.lnInvoicePaymentSend.errors if (errors && errors.length > 0) { @@ -76,7 +79,7 @@ async function payInvoice (authToken, wallet, invoice) { // at some point it should either be settled or fail on the backend, so the loop will exit await new Promise(resolve => setTimeout(resolve, 100)) - const txInfo = await getTxInfo(authToken, wallet, invoice) + const txInfo = await getTxInfo(bolt11, { apiKey, wallet }) // settled if (txInfo.status === 'SUCCESS') { if (!txInfo.preImage) throw new Error('no preimage') @@ -95,35 +98,36 @@ async function payInvoice (authToken, wallet, invoice) { throw new Error('unexpected error') } -async function getTxInfo (authToken, wallet, invoice) { - const walletId = wallet.id +async function getTxInfo (bolt11, { apiKey, wallet }) { let out try { - out = await request(authToken, ` - query GetTxInfo($walletId: WalletId!, $paymentRequest: LnPaymentRequest!) { - me { - defaultAccount { - walletById(walletId: $walletId) { - transactionsByPaymentRequest(paymentRequest: $paymentRequest) { - status - direction - settlementVia { + out = await request({ + apiKey, + query: ` + query GetTxInfo($walletId: WalletId!, $paymentRequest: LnPaymentRequest!) { + me { + defaultAccount { + walletById(walletId: $walletId) { + transactionsByPaymentRequest(paymentRequest: $paymentRequest) { + status + direction + settlementVia { ... on SettlementViaIntraLedger { - preImage + preImage } ... on SettlementViaLn { - preImage + preImage } + } } } } } - } + }`, + variables: { + paymentRequest: bolt11, + walletId: wallet.Id } - `, - { - paymentRequest: invoice, - walletId }) } catch (e) { // something went wrong during the query, diff --git a/wallets/blink/common.js b/wallets/blink/common.js index bf03f078..bcc35a0e 100644 --- a/wallets/blink/common.js +++ b/wallets/blink/common.js @@ -7,38 +7,41 @@ export const SCOPE_READ = 'READ' export const SCOPE_WRITE = 'WRITE' export const SCOPE_RECEIVE = 'RECEIVE' -export async function getWallet (authToken, currency) { - const out = await request(authToken, ` +export async function getWallet ({ apiKey, currency }) { + const out = await request({ + apiKey, + query: ` query me { - me { - defaultAccount { - wallets { - id - walletCurrency - } - } + me { + defaultAccount { + wallets { + id + walletCurrency + } } - } - `, {}) + } + }` + }) + const wallets = out.data.me.defaultAccount.wallets for (const wallet of wallets) { if (wallet.walletCurrency === currency) { return wallet } } + throw new Error(`wallet ${currency} not found`) } -export async function request (authToken, query, variables = {}) { - const options = { +export async function request ({ apiKey, query, variables = {} }) { + const res = await fetch(galoyBlinkUrl, { method: 'POST', headers: { 'Content-Type': 'application/json', - 'X-API-KEY': authToken + 'X-API-KEY': apiKey }, body: JSON.stringify({ query, variables }) - } - const res = await fetch(galoyBlinkUrl, options) + }) assertResponseOk(res) assertContentTypeJson(res) @@ -46,14 +49,16 @@ export async function request (authToken, query, variables = {}) { return res.json() } -export async function getScopes (authToken) { - const out = await request(authToken, ` - query scopes { +export async function getScopes ({ apiKey }) { + const out = await request({ + apiKey, + query: ` + query scopes { authorization { - scopes + scopes } - } - `, {}) + }` + }) const scopes = out?.data?.authorization?.scopes return scopes || [] } diff --git a/wallets/blink/server.js b/wallets/blink/server.js index 937a7eba..31d3a270 100644 --- a/wallets/blink/server.js +++ b/wallets/blink/server.js @@ -4,7 +4,7 @@ import { msatsToSats } from '@/lib/format' export * from '@/wallets/blink' export async function testCreateInvoice ({ apiKeyRecv, currencyRecv }) { - const scopes = await getScopes(apiKeyRecv) + const scopes = await getScopes({ apiKey: apiKeyRecv }) if (!scopes.includes(SCOPE_READ)) { throw new Error('missing READ scope') } @@ -22,40 +22,43 @@ export async function testCreateInvoice ({ apiKeyRecv, currencyRecv }) { export async function createInvoice ( { msats, description, expiry }, - { apiKeyRecv, currencyRecv }) { - currencyRecv = currencyRecv ? currencyRecv.toUpperCase() : 'BTC' + { apiKeyRecv: apiKey, currencyRecv: currency }) { + currency = currency ? currency.toUpperCase() : 'BTC' - const wallet = await getWallet(apiKeyRecv, currencyRecv) + const wallet = await getWallet({ apiKey, currency }) - if (currencyRecv !== 'BTC') { - throw new Error('unsupported currency ' + currencyRecv) + if (currency !== 'BTC') { + throw new Error('unsupported currency ' + currency) } - 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 out = await request({ + apiKey, + query: ` + mutation LnInvoiceCreate($input: LnInvoiceCreateInput!) { + lnInvoiceCreate(input: $input) { + invoice { + paymentRequest + } + errors { + message + } + } + }`, + variables: { + 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) { throw new Error(errors.map(e => e.code + ' ' + e.message).join(', ')) } - const invoice = res.invoice.paymentRequest - return invoice + + return res.invoice.paymentRequest }