Skip to content

Commit

Permalink
Refactor Blink function signatures (#1725)
Browse files Browse the repository at this point in the history
This makes them consistent with function signatures of other wallets
  • Loading branch information
ekzyis authored Dec 14, 2024
1 parent 77d22cf commit 3cdfe62
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 96 deletions.
100 changes: 52 additions & 48 deletions wallets/blink/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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) {
Expand All @@ -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')
Expand All @@ -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,
Expand Down
49 changes: 27 additions & 22 deletions wallets/blink/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,58 @@ 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)

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 || []
}
55 changes: 29 additions & 26 deletions wallets/blink/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand All @@ -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({
apiKey,
query: `
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
}`,
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
}

0 comments on commit 3cdfe62

Please sign in to comment.