Skip to content

Commit

Permalink
fix: Blink connector to use the API key from the Blink Dashboard (#2887)
Browse files Browse the repository at this point in the history
* fix: use X-API-KEY header for Blink

update link to the graphql endpoint
remove deprecated link to web wallet

* chore: remove outdated translations for galoy

* chore: add separate instructions for galoy-blink

* chore: decide header according to the access token

* feat: change the url and account name for staging

* fix: checkPayment function for galoy

* feat: implement getTransactions, BTC wallet only

* chore: clean config, Bitcoin Jungle compatibility

* docs: recommend a longer expiry for the API key

* fix: backward compatibility

fix: use walletId to identify wallet
docs: default no expiry API key

* chore: sync translations

* chore: remova alpha warning
  • Loading branch information
openoms authored Dec 11, 2023
1 parent f724363 commit 2e31bcf
Show file tree
Hide file tree
Showing 27 changed files with 267 additions and 573 deletions.
100 changes: 61 additions & 39 deletions src/app/screens/connectors/ConnectGaloy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ export const galoyUrls = {
label: "Blink Wallet",
website: "https://www.blink.sv/",
logo: galoyBlink,
url: process.env.BLINK_GALOY_URL || "https://api.mainnet.galoy.io/graphql",
url: process.env.BLINK_GALOY_URL || "https://api.blink.sv/graphql",
getHeaders: (authToken: string) => ({
Accept: "application/json",
"Content-Type": "application/json",
"X-API-KEY": authToken,
}),
apiCompatibilityMode: false,
},
"galoy-bitcoin-jungle": {
i18nPrefix: "bitcoin_jungle",
Expand All @@ -28,61 +34,61 @@ export const galoyUrls = {
url:
process.env.BITCOIN_JUNGLE_GALOY_URL ||
"https://api.mainnet.bitcoinjungle.app/graphql",
getHeaders: (authToken: string) => ({
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${authToken}`,
}),
apiCompatibilityMode: true,
},
} as const;

const defaultHeaders = {
Accept: "application/json",
"Content-Type": "application/json",
};

type Props = {
instance: keyof typeof galoyUrls;
};

export default function ConnectGaloy(props: Props) {
const { instance } = props;
const { url, label, website, i18nPrefix, logo } = galoyUrls[instance];
const { url, label, website, i18nPrefix, logo, apiCompatibilityMode } =
galoyUrls[instance];

const navigate = useNavigate();
const { t } = useTranslation("translation", {
keyPrefix: "choose_connector",
});
const [loading, setLoading] = useState(false);
const [jwt, setJwt] = useState<string | undefined>();
const [authToken, setAuthToken] = useState<string | undefined>();

function handleJwtChange(event: React.ChangeEvent<HTMLInputElement>) {
setJwt(event.target.value.trim());
function handleAuthTokenChange(event: React.ChangeEvent<HTMLInputElement>) {
setAuthToken(event.target.value.trim());
}

async function loginWithJwt(event: React.FormEvent<HTMLFormElement>) {
async function loginWithAuthToken(event: React.FormEvent<HTMLFormElement>) {
event.preventDefault();
setLoading(true);
const meQuery = {
query: `
query getinfo {
me {
defaultAccount {
defaultWalletId
wallets {
walletCurrency
id
}
}
}
}
`,
};
try {
if (!jwt) {
if (!authToken) {
const errorMsg = `${t("galoy.errors.missing_token")}`;
throw new Error(errorMsg);
}
const authToken = jwt;

const headers = galoyUrls[instance].getHeaders(authToken);

const { data: meData } = await axios.post(url, meQuery, {
headers: {
...defaultHeaders,
Authorization: `Bearer ${authToken}`,
},
headers: headers,
adapter: fetchAdapter,
});

if (meData.error || meData.errors) {
const error = meData.error || meData.errors;
console.error(error);
Expand All @@ -92,8 +98,12 @@ export default function ConnectGaloy(props: Props) {
}`;
toast.error(alertMsg);
} else {
const walletId = meData.data.me.defaultAccount.defaultWalletId;
saveAccount({ authToken, walletId });
// Find the BTC wallet and get its ID
const btcWallet = meData.data.me.defaultAccount.wallets.find(
(w: Wallet) => w.walletCurrency === "BTC"
);
const walletId = btcWallet.id;
saveAccount({ headers, walletId });
}
} catch (e: unknown) {
console.error(e);
Expand All @@ -112,15 +122,16 @@ export default function ConnectGaloy(props: Props) {
}
}

async function saveAccount(config: { authToken: string; walletId: string }) {
async function saveAccount(config: { headers: Headers; walletId: string }) {
setLoading(true);

const account = {
name: label,
config: {
url,
accessToken: config.authToken,
headers: config.headers,
walletId: config.walletId,
apiCompatibilityMode,
},
connector: "galoy",
};
Expand Down Expand Up @@ -167,40 +178,38 @@ export default function ConnectGaloy(props: Props) {
logo={logo}
submitLabel={t("galoy.actions.login")}
submitLoading={loading}
onSubmit={loginWithJwt}
onSubmit={loginWithAuthToken}
description={
<Trans
i18nKey={"galoy.token.info"}
i18nKey={`${i18nPrefix}.token.info`}
t={t}
values={{ label }}
components={[
// eslint-disable-next-line react/jsx-key
<a
href="https://wallet.mainnet.galoy.io"
href="https://dashboard.blink.sv"
className="underline"
target="_blank"
rel="noopener noreferrer"
key="Blink Dashboard"
></a>,
// eslint-disable-next-line react/jsx-key
<br />,
// eslint-disable-next-line react/jsx-key
<b></b>,
]}
/>
}
>
{
<div className="mt-6">
<label
htmlFor="jwt"
htmlFor="authToken"
className="block font-medium text-gray-800 dark:text-white"
>
{t("galoy.token.label")}
{t(`${i18nPrefix}.token.label`)}
</label>
<div className="mt-1">
<Input
id="jwt"
name="jwt"
id="authToken"
name="authToken"
required
onChange={handleJwtChange}
onChange={handleAuthTokenChange}
autoFocus={true}
/>
</div>
Expand All @@ -209,3 +218,16 @@ export default function ConnectGaloy(props: Props) {
</ConnectorForm>
);
}

type Headers = {
[key: string]: string;
};

type Props = {
instance: keyof typeof galoyUrls;
};

type Wallet = {
walletCurrency: string;
id: string;
};
Loading

0 comments on commit 2e31bcf

Please sign in to comment.