From 328c3a643ac3a47065e9326b418a8752073b4f87 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Tue, 24 Oct 2023 11:47:19 +0530 Subject: [PATCH 1/4] feat: use token events to subscribe to the token changes --- package.json | 2 +- .../background-script/connectors/alby.ts | 19 ++++++++++++------- yarn.lock | 9 +++++---- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index b02c3d7ab0..3ee588b642 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "dependencies": { "@bitcoin-design/bitcoin-icons-react": "^0.1.10", "@bitcoinerlab/secp256k1": "^1.0.5", - "@getalby/sdk": "^2.2.3", + "@getalby/sdk": "^2.5.0", "@headlessui/react": "^1.7.16", "@lightninglabs/lnc-web": "^0.2.4-alpha", "@noble/curves": "^1.1.0", diff --git a/src/extension/background-script/connectors/alby.ts b/src/extension/background-script/connectors/alby.ts index 3df2a38fd7..2d38f7c81f 100644 --- a/src/extension/background-script/connectors/alby.ts +++ b/src/extension/background-script/connectors/alby.ts @@ -254,11 +254,18 @@ export default class Alby implements Connector { token: this.config.oAuthToken, // initialize with existing token }); + authClient.on("tokenRefreshed", async (token: Token) => { + await this._updateOAuthToken(token); + }); + + authClient.on("tokenRefreshFailed", async (error: Error) => { + console.error("Failed to Refresh token", error); + }); + if (this.config.oAuthToken) { try { if (authClient.isAccessTokenExpired()) { - const token = await authClient.refreshAccessToken(); - await this._updateOAuthToken(token.token); + await authClient.refreshAccessToken(); } return authClient; } catch (error) { @@ -302,7 +309,6 @@ export default class Alby implements Connector { if (!this._authUser || !this._client) { throw new Error("Alby client was not initialized"); } - const oldToken = this._authUser?.token; let result: T; try { result = await func(this._client); @@ -311,10 +317,9 @@ export default class Alby implements Connector { throw error; } finally { - const newToken = this._authUser.token; - if (newToken && newToken !== oldToken) { - await this._updateOAuthToken(newToken); - } + this._authUser.on("tokenRefreshed", async (token: Token) => { + await this._updateOAuthToken(token); + }); } return result; } diff --git a/yarn.lock b/yarn.lock index 65d8d8878e..0b4f5c3369 100644 --- a/yarn.lock +++ b/yarn.lock @@ -572,12 +572,13 @@ resolved "https://registry.npmjs.org/@eslint/js/-/js-8.48.0.tgz" integrity sha512-ZSjtmelB7IJfWD2Fvb7+Z+ChTIKWq6kjda95fLcQKNS5aheVHn4IkfgRQE3sIIzTcSLwLcLZUD9UBt+V7+h+Pw== -"@getalby/sdk@^2.2.3": - version "2.2.3" - resolved "https://registry.npmjs.org/@getalby/sdk/-/sdk-2.2.3.tgz" - integrity sha512-8NvzGtpyne8EmRlCcg/sF3kUZWeHRXqZwS1HNuP1ytNNfmKFUZpo3GOauwzEpFFmaD+Fht5bjT3Y/XLk0QtFSw== +"@getalby/sdk@^2.5.0": + version "2.5.0" + resolved "https://registry.yarnpkg.com/@getalby/sdk/-/sdk-2.5.0.tgz#d1b0a22cbcf986755c4b684096d97f52ed0b469d" + integrity sha512-MRLgI6WxCCLgrar+qDqm/UhKs+V6yXzNm4y1bJRAuN72nkKT+TjTJHCmk9GjTngR3FrOfLbeMsPwBxCmbvfrLQ== dependencies: crypto-js "^4.1.1" + events "^3.3.0" nostr-tools "1.13.1" "@headlessui/react@^1.7.16": From 57e8da10fb14be6408cf05daf0634fcf7deaee8b Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Tue, 24 Oct 2023 14:27:56 +0530 Subject: [PATCH 2/4] chore: cleanup request function --- src/extension/background-script/connectors/alby.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/extension/background-script/connectors/alby.ts b/src/extension/background-script/connectors/alby.ts index 2d38f7c81f..d9a5521c28 100644 --- a/src/extension/background-script/connectors/alby.ts +++ b/src/extension/background-script/connectors/alby.ts @@ -316,10 +316,6 @@ export default class Alby implements Connector { console.error(error); throw error; - } finally { - this._authUser.on("tokenRefreshed", async (token: Token) => { - await this._updateOAuthToken(token); - }); } return result; } From d46c7f9d2179b244c154ad490372c98ef118b4c0 Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Fri, 3 Nov 2023 12:14:12 +0530 Subject: [PATCH 3/4] chore: minor improvements --- src/extension/background-script/connectors/alby.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/extension/background-script/connectors/alby.ts b/src/extension/background-script/connectors/alby.ts index d9a5521c28..c383988131 100644 --- a/src/extension/background-script/connectors/alby.ts +++ b/src/extension/background-script/connectors/alby.ts @@ -254,11 +254,15 @@ export default class Alby implements Connector { token: this.config.oAuthToken, // initialize with existing token }); - authClient.on("tokenRefreshed", async (token: Token) => { - await this._updateOAuthToken(token); + //// Currently the JS SDK guarantees request of a new refresh token is done synchronously. + // The only way a refresh should fail is if the refresh token has expired, which is handled when the connector is initialized. + // If a token refresh fails after init then the connector will be unusable, but we will still log errors here so that this can be debugged if it does ever happen. + + authClient.on("tokenRefreshed", (token: Token) => { + this._updateOAuthToken(token); }); - authClient.on("tokenRefreshFailed", async (error: Error) => { + authClient.on("tokenRefreshFailed", (error: Error) => { console.error("Failed to Refresh token", error); }); From a39e66f768e31e0c0678492f9561c3e22c131f1b Mon Sep 17 00:00:00 2001 From: pavanjoshi914 Date: Mon, 6 Nov 2023 11:51:48 +0530 Subject: [PATCH 4/4] chore: add comment --- src/extension/background-script/connectors/alby.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/extension/background-script/connectors/alby.ts b/src/extension/background-script/connectors/alby.ts index c383988131..059b53febe 100644 --- a/src/extension/background-script/connectors/alby.ts +++ b/src/extension/background-script/connectors/alby.ts @@ -254,14 +254,12 @@ export default class Alby implements Connector { token: this.config.oAuthToken, // initialize with existing token }); - //// Currently the JS SDK guarantees request of a new refresh token is done synchronously. - // The only way a refresh should fail is if the refresh token has expired, which is handled when the connector is initialized. - // If a token refresh fails after init then the connector will be unusable, but we will still log errors here so that this can be debugged if it does ever happen. - authClient.on("tokenRefreshed", (token: Token) => { this._updateOAuthToken(token); }); - + // Currently the JS SDK guarantees request of a new refresh token is done synchronously. + // The only way a refresh should fail is if the refresh token has expired, which is handled when the connector is initialized. + // If a token refresh fails after init then the connector will be unusable, but we will still log errors here so that this can be debugged if it does ever happen. authClient.on("tokenRefreshFailed", (error: Error) => { console.error("Failed to Refresh token", error); });