diff --git a/package.json b/package.json index 6989249a41..83fbda63ca 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..059b53febe 100644 --- a/src/extension/background-script/connectors/alby.ts +++ b/src/extension/background-script/connectors/alby.ts @@ -254,11 +254,20 @@ export default class Alby implements Connector { token: this.config.oAuthToken, // initialize with existing token }); + 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); + }); + 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 +311,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); @@ -310,11 +318,6 @@ export default class Alby implements Connector { console.error(error); throw error; - } finally { - const newToken = this._authUser.token; - if (newToken && newToken !== oldToken) { - await this._updateOAuthToken(newToken); - } } return result; } diff --git a/yarn.lock b/yarn.lock index bc5eff3c53..4ba068dffc 100644 --- a/yarn.lock +++ b/yarn.lock @@ -666,12 +666,13 @@ resolved "https://registry.yarnpkg.com/@eslint/js/-/js-8.51.0.tgz#6d419c240cfb2b66da37df230f7e7eef801c32fa" integrity sha512-HxjQ8Qn+4SI3/AFv6sOrDB+g6PpUTDwSJiQqOrnneEk8L71161srI9gjzzZvYVbzHiVg/BvcH95+cK/zfIt4pg== -"@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":