Refactor Blink function signatures (#1725)
This makes them consistent with function signatures of other wallets
This commit is contained in:
parent
77d22cfd77
commit
3cdfe620d0
|
@ -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,
|
||||
|
|
|
@ -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 || []
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue