stacker.news/wallets/blink/common.js

60 lines
1.4 KiB
JavaScript
Raw Normal View History

import { assertContentTypeJson, assertResponseOk } from '@/lib/url'
2024-10-16 17:46:33 +00:00
export const galoyBlinkUrl = 'https://api.blink.sv/graphql'
export const galoyBlinkDashboardUrl = 'https://dashboard.blink.sv/'
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, `
query me {
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 = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-KEY': authToken
},
body: JSON.stringify({ query, variables })
}
const res = await fetch(galoyBlinkUrl, options)
assertResponseOk(res)
assertContentTypeJson(res)
2024-10-16 17:46:33 +00:00
return res.json()
}
export async function getScopes (authToken) {
const out = await request(authToken, `
query scopes {
authorization {
scopes
}
}
`, {})
const scopes = out?.data?.authorization?.scopes
return scopes || []
}