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 }) {
|
export async function testSendPayment ({ apiKey, currency }, { logger }) {
|
||||||
logger.info('trying to fetch ' + currency + ' wallet')
|
logger.info('trying to fetch ' + currency + ' wallet')
|
||||||
const scopes = await getScopes(apiKey)
|
|
||||||
|
const scopes = await getScopes({ apiKey })
|
||||||
if (!scopes.includes(SCOPE_READ)) {
|
if (!scopes.includes(SCOPE_READ)) {
|
||||||
throw new Error('missing READ scope')
|
throw new Error('missing READ scope')
|
||||||
}
|
}
|
||||||
|
@ -12,46 +13,48 @@ export async function testSendPayment ({ apiKey, currency }, { logger }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
currency = currency ? currency.toUpperCase() : 'BTC'
|
currency = currency ? currency.toUpperCase() : 'BTC'
|
||||||
await getWallet(apiKey, currency)
|
await getWallet({ apiKey, currency })
|
||||||
|
|
||||||
logger.ok(currency + ' wallet found')
|
logger.ok(currency + ' wallet found')
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function sendPayment (bolt11, { apiKey, currency }) {
|
export async function sendPayment (bolt11, { apiKey, currency }) {
|
||||||
const wallet = await getWallet(apiKey, currency)
|
const wallet = await getWallet({ apiKey, currency })
|
||||||
return await payInvoice(apiKey, wallet, bolt11)
|
return await payInvoice(bolt11, { apiKey, wallet })
|
||||||
}
|
}
|
||||||
|
|
||||||
async function payInvoice (authToken, wallet, invoice) {
|
async function payInvoice (bolt11, { apiKey, wallet }) {
|
||||||
const walletId = wallet.id
|
const out = await request({
|
||||||
const out = await request(authToken, `
|
apiKey,
|
||||||
mutation LnInvoicePaymentSend($input: LnInvoicePaymentInput!) {
|
query: `
|
||||||
|
mutation LnInvoicePaymentSend($input: LnInvoicePaymentInput!) {
|
||||||
lnInvoicePaymentSend(input: $input) {
|
lnInvoicePaymentSend(input: $input) {
|
||||||
status
|
status
|
||||||
errors {
|
errors {
|
||||||
message
|
message
|
||||||
path
|
path
|
||||||
code
|
code
|
||||||
}
|
}
|
||||||
transaction {
|
transaction {
|
||||||
settlementVia {
|
settlementVia {
|
||||||
... on SettlementViaIntraLedger {
|
... on SettlementViaIntraLedger {
|
||||||
preImage
|
preImage
|
||||||
}
|
}
|
||||||
... on SettlementViaLn {
|
... on SettlementViaLn {
|
||||||
preImage
|
preImage
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}`,
|
||||||
`,
|
variables: {
|
||||||
{
|
input: {
|
||||||
input: {
|
paymentRequest: bolt11,
|
||||||
paymentRequest: invoice,
|
walletId: wallet.id
|
||||||
walletId
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const status = out.data.lnInvoicePaymentSend.status
|
const status = out.data.lnInvoicePaymentSend.status
|
||||||
const errors = out.data.lnInvoicePaymentSend.errors
|
const errors = out.data.lnInvoicePaymentSend.errors
|
||||||
if (errors && errors.length > 0) {
|
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
|
// 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))
|
await new Promise(resolve => setTimeout(resolve, 100))
|
||||||
|
|
||||||
const txInfo = await getTxInfo(authToken, wallet, invoice)
|
const txInfo = await getTxInfo(bolt11, { apiKey, wallet })
|
||||||
// settled
|
// settled
|
||||||
if (txInfo.status === 'SUCCESS') {
|
if (txInfo.status === 'SUCCESS') {
|
||||||
if (!txInfo.preImage) throw new Error('no preimage')
|
if (!txInfo.preImage) throw new Error('no preimage')
|
||||||
|
@ -95,35 +98,36 @@ async function payInvoice (authToken, wallet, invoice) {
|
||||||
throw new Error('unexpected error')
|
throw new Error('unexpected error')
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getTxInfo (authToken, wallet, invoice) {
|
async function getTxInfo (bolt11, { apiKey, wallet }) {
|
||||||
const walletId = wallet.id
|
|
||||||
let out
|
let out
|
||||||
try {
|
try {
|
||||||
out = await request(authToken, `
|
out = await request({
|
||||||
query GetTxInfo($walletId: WalletId!, $paymentRequest: LnPaymentRequest!) {
|
apiKey,
|
||||||
me {
|
query: `
|
||||||
defaultAccount {
|
query GetTxInfo($walletId: WalletId!, $paymentRequest: LnPaymentRequest!) {
|
||||||
walletById(walletId: $walletId) {
|
me {
|
||||||
transactionsByPaymentRequest(paymentRequest: $paymentRequest) {
|
defaultAccount {
|
||||||
status
|
walletById(walletId: $walletId) {
|
||||||
direction
|
transactionsByPaymentRequest(paymentRequest: $paymentRequest) {
|
||||||
settlementVia {
|
status
|
||||||
|
direction
|
||||||
|
settlementVia {
|
||||||
... on SettlementViaIntraLedger {
|
... on SettlementViaIntraLedger {
|
||||||
preImage
|
preImage
|
||||||
}
|
}
|
||||||
... on SettlementViaLn {
|
... on SettlementViaLn {
|
||||||
preImage
|
preImage
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}`,
|
||||||
|
variables: {
|
||||||
|
paymentRequest: bolt11,
|
||||||
|
walletId: wallet.Id
|
||||||
}
|
}
|
||||||
`,
|
|
||||||
{
|
|
||||||
paymentRequest: invoice,
|
|
||||||
walletId
|
|
||||||
})
|
})
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// something went wrong during the query,
|
// something went wrong during the query,
|
||||||
|
|
|
@ -7,38 +7,41 @@ export const SCOPE_READ = 'READ'
|
||||||
export const SCOPE_WRITE = 'WRITE'
|
export const SCOPE_WRITE = 'WRITE'
|
||||||
export const SCOPE_RECEIVE = 'RECEIVE'
|
export const SCOPE_RECEIVE = 'RECEIVE'
|
||||||
|
|
||||||
export async function getWallet (authToken, currency) {
|
export async function getWallet ({ apiKey, currency }) {
|
||||||
const out = await request(authToken, `
|
const out = await request({
|
||||||
|
apiKey,
|
||||||
|
query: `
|
||||||
query me {
|
query me {
|
||||||
me {
|
me {
|
||||||
defaultAccount {
|
defaultAccount {
|
||||||
wallets {
|
wallets {
|
||||||
id
|
id
|
||||||
walletCurrency
|
walletCurrency
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
`, {})
|
}`
|
||||||
|
})
|
||||||
|
|
||||||
const wallets = out.data.me.defaultAccount.wallets
|
const wallets = out.data.me.defaultAccount.wallets
|
||||||
for (const wallet of wallets) {
|
for (const wallet of wallets) {
|
||||||
if (wallet.walletCurrency === currency) {
|
if (wallet.walletCurrency === currency) {
|
||||||
return wallet
|
return wallet
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new Error(`wallet ${currency} not found`)
|
throw new Error(`wallet ${currency} not found`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function request (authToken, query, variables = {}) {
|
export async function request ({ apiKey, query, variables = {} }) {
|
||||||
const options = {
|
const res = await fetch(galoyBlinkUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'X-API-KEY': authToken
|
'X-API-KEY': apiKey
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ query, variables })
|
body: JSON.stringify({ query, variables })
|
||||||
}
|
})
|
||||||
const res = await fetch(galoyBlinkUrl, options)
|
|
||||||
|
|
||||||
assertResponseOk(res)
|
assertResponseOk(res)
|
||||||
assertContentTypeJson(res)
|
assertContentTypeJson(res)
|
||||||
|
@ -46,14 +49,16 @@ export async function request (authToken, query, variables = {}) {
|
||||||
return res.json()
|
return res.json()
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getScopes (authToken) {
|
export async function getScopes ({ apiKey }) {
|
||||||
const out = await request(authToken, `
|
const out = await request({
|
||||||
query scopes {
|
apiKey,
|
||||||
|
query: `
|
||||||
|
query scopes {
|
||||||
authorization {
|
authorization {
|
||||||
scopes
|
scopes
|
||||||
}
|
}
|
||||||
}
|
}`
|
||||||
`, {})
|
})
|
||||||
const scopes = out?.data?.authorization?.scopes
|
const scopes = out?.data?.authorization?.scopes
|
||||||
return scopes || []
|
return scopes || []
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { msatsToSats } from '@/lib/format'
|
||||||
export * from '@/wallets/blink'
|
export * from '@/wallets/blink'
|
||||||
|
|
||||||
export async function testCreateInvoice ({ apiKeyRecv, currencyRecv }) {
|
export async function testCreateInvoice ({ apiKeyRecv, currencyRecv }) {
|
||||||
const scopes = await getScopes(apiKeyRecv)
|
const scopes = await getScopes({ apiKey: apiKeyRecv })
|
||||||
if (!scopes.includes(SCOPE_READ)) {
|
if (!scopes.includes(SCOPE_READ)) {
|
||||||
throw new Error('missing READ scope')
|
throw new Error('missing READ scope')
|
||||||
}
|
}
|
||||||
|
@ -22,40 +22,43 @@ export async function testCreateInvoice ({ apiKeyRecv, currencyRecv }) {
|
||||||
|
|
||||||
export async function createInvoice (
|
export async function createInvoice (
|
||||||
{ msats, description, expiry },
|
{ msats, description, expiry },
|
||||||
{ apiKeyRecv, currencyRecv }) {
|
{ apiKeyRecv: apiKey, currencyRecv: currency }) {
|
||||||
currencyRecv = currencyRecv ? currencyRecv.toUpperCase() : 'BTC'
|
currency = currency ? currency.toUpperCase() : 'BTC'
|
||||||
|
|
||||||
const wallet = await getWallet(apiKeyRecv, currencyRecv)
|
const wallet = await getWallet({ apiKey, currency })
|
||||||
|
|
||||||
if (currencyRecv !== 'BTC') {
|
if (currency !== 'BTC') {
|
||||||
throw new Error('unsupported currency ' + currencyRecv)
|
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, {
|
const out = await request({
|
||||||
input: {
|
apiKey,
|
||||||
amount: msatsToSats(msats),
|
query: `
|
||||||
expiresIn: Math.floor(expiry / 60) || 1,
|
mutation LnInvoiceCreate($input: LnInvoiceCreateInput!) {
|
||||||
memo: description,
|
lnInvoiceCreate(input: $input) {
|
||||||
walletId: wallet.id
|
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 res = out.data.lnInvoiceCreate
|
||||||
const errors = res.errors
|
const errors = res.errors
|
||||||
if (errors && errors.length > 0) {
|
if (errors && errors.length > 0) {
|
||||||
throw new Error(errors.map(e => e.code + ' ' + e.message).join(', '))
|
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