Skip to content

Commit

Permalink
Merge pull request #2572 from getAlby/feat/new-js-sdk
Browse files Browse the repository at this point in the history
feat: consume new alby-js-sdk
  • Loading branch information
rolznz authored Jul 24, 2023
2 parents 0a56dd1 + 969a84a commit fc823bc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 32 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
"@noble/curves": "^1.1.0",
"@noble/secp256k1": "^2.0.0",
"@scure/bip32": "^1.3.1",
"@scure/bip39": "^1.2.0",
"@scure/bip39": "^1.2.1",
"@scure/btc-signer": "^0.5.1",
"@tailwindcss/forms": "^0.5.3",
"@vespaiach/axios-fetch-adapter": "^0.3.0",
"alby-js-sdk": "^2.0.0",
"alby-js-sdk": "^2.1.0",
"axios": "^0.27.2",
"bech32": "^2.0.0",
"bolt11": "^1.4.1",
Expand Down
4 changes: 2 additions & 2 deletions src/app/screens/connectors/ConnectAlby/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { GetAccountInformationResponse } from "alby-js-sdk/dist/types";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useNavigate } from "react-router-dom";
Expand All @@ -7,7 +8,6 @@ import { getAlbyAccountName } from "~/app/utils";
import api from "~/common/lib/api";
import msg from "~/common/lib/msg";
import { WebLNNode } from "~/extension/background-script/connectors/connector.interface";
import { AlbyAccountInformation } from "~/types";

export default function ConnectAlby() {
const [loading, setLoading] = useState(false);
Expand All @@ -33,7 +33,7 @@ export default function ConnectAlby() {
}

const accountInfo = validation.info.data as WebLNNode &
AlbyAccountInformation;
GetAccountInformationResponse;

const account = {
...initialAccount,
Expand Down
5 changes: 3 additions & 2 deletions src/app/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GetAccountInformationResponse } from "alby-js-sdk/dist/types";
import { useSettings } from "~/app/context/SettingsContext";
import api from "~/common/lib/api";
import { AlbyAccountInformation, BrowserType, Theme } from "~/types";
import { BrowserType, Theme } from "~/types";

export function classNames(...classes: (string | boolean)[]) {
return classes.filter(Boolean).join(" ");
Expand Down Expand Up @@ -57,7 +58,7 @@ export function isAlbyOAuthAccount(connectorType = "") {
return connectorType === "alby";
}

export function getAlbyAccountName(info: AlbyAccountInformation) {
export function getAlbyAccountName(info: GetAccountInformationResponse) {
// legacy accounts may not have either an email address or lightning address
return info.email || info.lightning_address || "getalby.com";
}
Expand Down
5 changes: 2 additions & 3 deletions src/extension/background-script/actions/ln/sendPayment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ export default async function sendPayment(

if (typeof e === "string") {
errorMessage = e;
} else if ((e as { message: string }).message) {
// TODO: change to e as Error once alby-js-sdk is updated
errorMessage = (e as { message: string }).message;
} else if ((e as Error).message) {
errorMessage = (e as Error).message;
} else {
errorMessage = "Something went wrong";
}
Expand Down
21 changes: 11 additions & 10 deletions src/extension/background-script/connectors/alby.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { auth, Client } from "alby-js-sdk";
import { RequestOptions } from "alby-js-sdk/dist/request";
import { Invoice, Token } from "alby-js-sdk/dist/types";
import {
GetAccountInformationResponse,
Invoice,
Token,
} from "alby-js-sdk/dist/types";
import browser from "webextension-polyfill";
import { getAlbyAccountName } from "~/app/utils";
import { decryptData, encryptData } from "~/common/lib/crypto";
import { Account, AlbyAccountInformation, OAuthToken } from "~/types";
import { Account, OAuthToken } from "~/types";

import state from "../state";
import Connector, {
Expand Down Expand Up @@ -93,12 +97,12 @@ export default class Alby implements Connector {
}

async getInfo(): Promise<
GetInfoResponse<WebLNNode & AlbyAccountInformation>
GetInfoResponse<WebLNNode & GetAccountInformationResponse>
> {
try {
const info = (await this._request((client) =>
const info = await this._request((client) =>
client.accountInformation({})
)) as AlbyAccountInformation; // TODO: remove type once alby-js-sdk is updated
);

const accounts = state.getState().accounts;
if (this.account.id && this.account.id in accounts) {
Expand Down Expand Up @@ -224,6 +228,7 @@ export default class Alby implements Connector {
client_id: clientId,
client_secret: clientSecret,
callback: redirectURL,
user_agent: `lightning-browser-extension:${process.env.VERSION}`,
scopes: [
"invoices:read",
"account:read",
Expand Down Expand Up @@ -324,11 +329,7 @@ export default class Alby implements Connector {
result = await func(client);
} catch (error) {
console.error(error);
// handle AlbyResponseError in alby-js-sdk
// TODO: undo this change once AlbyResponseError `message` field is set.
if ((error as { error: Error }).error) {
throw (error as { error: Error }).error;
}

throw error;
} finally {
const newToken = this._authUser.token;
Expand Down
13 changes: 0 additions & 13 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -851,16 +851,3 @@ export type BitcoinAddress = {
index: number;
address: string;
};

// TODO: take from alby-js-sdk
export type AlbyAccountInformation = {
identifier: string;
email: string;
name?: string;
avatar?: string;
keysend_custom_key: string;
keysend_custom_value: string;
keysend_pubkey: string;
lightning_address?: string;
nostr_pubkey?: string;
};

0 comments on commit fc823bc

Please sign in to comment.