From 691edf503c802240e0f9d4073377be0010bffe1a Mon Sep 17 00:00:00 2001 From: Adithya Vardhan Date: Wed, 22 Feb 2023 19:46:32 +0530 Subject: [PATCH] fix: include account in connector --- .../actions/setup/validateAccount.js | 2 +- .../background-script/connectors/citadel.ts | 18 +++++++----- .../background-script/connectors/commando.ts | 5 +++- .../background-script/connectors/eclair.ts | 13 +++++---- .../background-script/connectors/galoy.ts | 16 ++++++----- .../background-script/connectors/kollider.ts | 5 +++- .../background-script/connectors/lnbits.ts | 13 +++++---- .../background-script/connectors/lnc.ts | 28 +++++++++++-------- .../background-script/connectors/lnd.ts | 5 +++- .../background-script/connectors/lndhub.ts | 11 +++++--- src/extension/background-script/state.ts | 2 +- 11 files changed, 73 insertions(+), 45 deletions(-) diff --git a/src/extension/background-script/actions/setup/validateAccount.js b/src/extension/background-script/actions/setup/validateAccount.js index 8b57c24926..c752da97ab 100644 --- a/src/extension/background-script/actions/setup/validateAccount.js +++ b/src/extension/background-script/actions/setup/validateAccount.js @@ -2,7 +2,7 @@ import connectors from "../../connectors"; const validateAccount = async (message, sender) => { const account = message.args; - const connector = new connectors[account.connector](account.config); + const connector = new connectors[account.connector](account, account.config); await connector.init(); try { diff --git a/src/extension/background-script/connectors/citadel.ts b/src/extension/background-script/connectors/citadel.ts index 03e5dc01c6..3c21fd5523 100644 --- a/src/extension/background-script/connectors/citadel.ts +++ b/src/extension/background-script/connectors/citadel.ts @@ -1,17 +1,19 @@ +import { Account } from "~/types"; + import Connector, { + CheckPaymentArgs, + CheckPaymentResponse, ConnectPeerResponse, - SendPaymentArgs, - SendPaymentResponse, - GetInfoResponse, GetBalanceResponse, + GetInfoResponse, GetInvoicesResponse, + KeysendArgs, MakeInvoiceArgs, MakeInvoiceResponse, + SendPaymentArgs, + SendPaymentResponse, SignMessageArgs, SignMessageResponse, - CheckPaymentArgs, - CheckPaymentResponse, - KeysendArgs, } from "./connector.interface"; interface Config { @@ -27,10 +29,12 @@ type RequestFunction = ( ) => Promise; class CitadelConnector implements Connector { + account: Account; config: Config; jwt: string; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; this.jwt = ""; } diff --git a/src/extension/background-script/connectors/commando.ts b/src/extension/background-script/connectors/commando.ts index c8db9622ae..a6715b84d3 100644 --- a/src/extension/background-script/connectors/commando.ts +++ b/src/extension/background-script/connectors/commando.ts @@ -2,6 +2,7 @@ import Hex from "crypto-js/enc-hex"; import UTF8 from "crypto-js/enc-utf8"; import LnMessage from "lnmessage"; import { v4 as uuidv4 } from "uuid"; +import { Account } from "~/types"; import Connector, { CheckPaymentArgs, @@ -110,10 +111,12 @@ const supportedMethods: string[] = [ ]; export default class Commando implements Connector { + account: Account; config: Config; ln: LnMessage; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; this.ln = new LnMessage({ remoteNodePublicKey: this.config.pubkey, diff --git a/src/extension/background-script/connectors/eclair.ts b/src/extension/background-script/connectors/eclair.ts index 07b9c6f9f9..876b00df1f 100644 --- a/src/extension/background-script/connectors/eclair.ts +++ b/src/extension/background-script/connectors/eclair.ts @@ -1,20 +1,21 @@ import Base64 from "crypto-js/enc-base64"; import UTF8 from "crypto-js/enc-utf8"; +import { Account } from "~/types"; import Connector, { - SendPaymentArgs, - SendPaymentResponse, CheckPaymentArgs, CheckPaymentResponse, ConnectPeerResponse, + GetBalanceResponse, GetInfoResponse, GetInvoicesResponse, - GetBalanceResponse, + KeysendArgs, MakeInvoiceArgs, MakeInvoiceResponse, + SendPaymentArgs, + SendPaymentResponse, SignMessageArgs, SignMessageResponse, - KeysendArgs, } from "./connector.interface"; interface Config { @@ -23,9 +24,11 @@ interface Config { } class Eclair implements Connector { + account: Account; config: Config; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; } diff --git a/src/extension/background-script/connectors/galoy.ts b/src/extension/background-script/connectors/galoy.ts index 695f0258de..b5d57c3c32 100644 --- a/src/extension/background-script/connectors/galoy.ts +++ b/src/extension/background-script/connectors/galoy.ts @@ -1,21 +1,21 @@ -import axios from "axios"; -import { AxiosRequestConfig } from "axios"; +import axios, { AxiosRequestConfig } from "axios"; import lightningPayReq from "bolt11"; +import { Account } from "~/types"; import Connector, { - SendPaymentArgs, - SendPaymentResponse, CheckPaymentArgs, CheckPaymentResponse, ConnectPeerResponse, + GetBalanceResponse, GetInfoResponse, GetInvoicesResponse, - GetBalanceResponse, + KeysendArgs, MakeInvoiceArgs, MakeInvoiceResponse, + SendPaymentArgs, + SendPaymentResponse, SignMessageArgs, SignMessageResponse, - KeysendArgs, } from "./connector.interface"; interface Config { @@ -25,9 +25,11 @@ interface Config { } class Galoy implements Connector { + account: Account; config: Config; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; } diff --git a/src/extension/background-script/connectors/kollider.ts b/src/extension/background-script/connectors/kollider.ts index 7a1bd6723a..c281d7d32f 100644 --- a/src/extension/background-script/connectors/kollider.ts +++ b/src/extension/background-script/connectors/kollider.ts @@ -5,6 +5,7 @@ import sha256 from "crypto-js/sha256"; import { ACCOUNT_CURRENCIES } from "~/common/constants"; import { getBTCToSats, getSatsToBTC } from "~/common/utils/currencyConvert"; import HashKeySigner from "~/common/utils/signer"; +import { Account } from "~/types"; import Connector, { CheckPaymentArgs, @@ -48,6 +49,7 @@ const defaultHeaders = { }; export default class Kollider implements Connector { + account: Account; config: Config; access_token?: string; access_token_created?: number; @@ -58,7 +60,8 @@ export default class Kollider implements Connector { currency: KolliderCurrencies; currentAccountId: string | null; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; this.currency = config.currency; this.currentAccountId = null; diff --git a/src/extension/background-script/connectors/lnbits.ts b/src/extension/background-script/connectors/lnbits.ts index 86ce896dd3..d3c8020908 100644 --- a/src/extension/background-script/connectors/lnbits.ts +++ b/src/extension/background-script/connectors/lnbits.ts @@ -3,23 +3,24 @@ import Hex from "crypto-js/enc-hex"; import sha256 from "crypto-js/sha256"; import utils from "~/common/lib/utils"; import HashKeySigner from "~/common/utils/signer"; +import { Account } from "~/types"; import state from "../state"; import Connector, { - SendPaymentArgs, - SendPaymentResponse, CheckPaymentArgs, CheckPaymentResponse, ConnectorInvoice, ConnectPeerResponse, + GetBalanceResponse, GetInfoResponse, GetInvoicesResponse, - GetBalanceResponse, + KeysendArgs, MakeInvoiceArgs, MakeInvoiceResponse, + SendPaymentArgs, + SendPaymentResponse, SignMessageArgs, SignMessageResponse, - KeysendArgs, } from "./connector.interface"; interface Config { @@ -28,9 +29,11 @@ interface Config { } class LnBits implements Connector { + account: Account; config: Config; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; } diff --git a/src/extension/background-script/connectors/lnc.ts b/src/extension/background-script/connectors/lnc.ts index badd1c4e8c..3dbe3098c9 100644 --- a/src/extension/background-script/connectors/lnc.ts +++ b/src/extension/background-script/connectors/lnc.ts @@ -1,5 +1,4 @@ -import LNC from "@lightninglabs/lnc-web"; -import { CredentialStore } from "@lightninglabs/lnc-web"; +import LNC, { CredentialStore } from "@lightninglabs/lnc-web"; import Base64 from "crypto-js/enc-base64"; import Hex from "crypto-js/enc-hex"; import UTF8 from "crypto-js/enc-utf8"; @@ -8,23 +7,24 @@ import SHA256 from "crypto-js/sha256"; import snakeCase from "lodash.snakecase"; import { encryptData } from "~/common/lib/crypto"; import utils from "~/common/lib/utils"; +import { Account } from "~/types"; import state from "../state"; import Connector, { CheckPaymentArgs, CheckPaymentResponse, - SendPaymentArgs, - SendPaymentResponse, - GetInfoResponse, + ConnectorInvoice, + ConnectPeerResponse, GetBalanceResponse, + GetInfoResponse, GetInvoicesResponse, - ConnectPeerResponse, - ConnectorInvoice, + KeysendArgs, MakeInvoiceArgs, MakeInvoiceResponse, + SendPaymentArgs, + SendPaymentResponse, SignMessageArgs, SignMessageResponse, - KeysendArgs, } from "./connector.interface"; interface Config { @@ -82,9 +82,11 @@ const snakeCaseObjectDeep = (value: FixMe): FixMe => { }; class LncCredentialStore implements CredentialStore { + account: Account; config: Config; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; } @@ -136,7 +138,7 @@ class LncCredentialStore implements CredentialStore { private async _save() { const accounts = state.getState().accounts; const password = state.getState().password as string; - const currentAccountId = state.getState().currentAccountId as string; + const currentAccountId = this.account.id; accounts[currentAccountId].config = encryptData(this.config, password); state.setState({ accounts }); await state.getState().saveToStorage(); @@ -145,13 +147,15 @@ class LncCredentialStore implements CredentialStore { } class Lnc implements Connector { + account: Account; config: Config; lnc: FixMe; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; this.lnc = new LNC({ - credentialStore: new LncCredentialStore(config), + credentialStore: new LncCredentialStore(account, config), }); } diff --git a/src/extension/background-script/connectors/lnd.ts b/src/extension/background-script/connectors/lnd.ts index 09d00da021..39a9b2653c 100644 --- a/src/extension/background-script/connectors/lnd.ts +++ b/src/extension/background-script/connectors/lnd.ts @@ -4,6 +4,7 @@ import UTF8 from "crypto-js/enc-utf8"; import WordArray from "crypto-js/lib-typedarrays"; import SHA256 from "crypto-js/sha256"; import utils from "~/common/lib/utils"; +import { Account } from "~/types"; import Connector, { CheckPaymentArgs, @@ -135,9 +136,11 @@ const pathTemplateParser = ( }; class Lnd implements Connector { + account: Account; config: Config; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; } diff --git a/src/extension/background-script/connectors/lndhub.ts b/src/extension/background-script/connectors/lndhub.ts index 06c6cb690f..5851dc4f83 100644 --- a/src/extension/background-script/connectors/lndhub.ts +++ b/src/extension/background-script/connectors/lndhub.ts @@ -1,5 +1,5 @@ -import axios, { AxiosRequestConfig, Method } from "axios"; import type { AxiosResponse } from "axios"; +import axios, { AxiosRequestConfig, Method } from "axios"; import lightningPayReq from "bolt11"; import Base64 from "crypto-js/enc-base64"; import Hex from "crypto-js/enc-hex"; @@ -7,16 +7,17 @@ import hmacSHA256 from "crypto-js/hmac-sha256"; import sha256 from "crypto-js/sha256"; import utils from "~/common/lib/utils"; import HashKeySigner from "~/common/utils/signer"; +import { Account } from "~/types"; import state from "../state"; import Connector, { CheckPaymentArgs, CheckPaymentResponse, + ConnectorInvoice, + ConnectPeerResponse, GetBalanceResponse, GetInfoResponse, GetInvoicesResponse, - ConnectorInvoice, - ConnectPeerResponse, KeysendArgs, MakeInvoiceArgs, MakeInvoiceResponse, @@ -43,6 +44,7 @@ const defaultHeaders = { }; export default class LndHub implements Connector { + account: Account; config: Config; access_token?: string; access_token_created?: number; @@ -50,7 +52,8 @@ export default class LndHub implements Connector { refresh_token_created?: number; noRetry?: boolean; - constructor(config: Config) { + constructor(account: Account, config: Config) { + this.account = account; this.config = config; } diff --git a/src/extension/background-script/state.ts b/src/extension/background-script/state.ts index 011ca8551d..1c49fbc374 100644 --- a/src/extension/background-script/state.ts +++ b/src/extension/background-script/state.ts @@ -81,7 +81,7 @@ const state = createState((set, get) => ({ const password = get().password as string; const config = decryptData(account.config as string, password); - const connector = new connectors[account.connector](config); + const connector = new connectors[account.connector](account, config); await connector.init(); set({ connector: connector });