Skip to content

Commit

Permalink
fix: store connection addresses to not request it twice (#3405)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoruka authored Dec 9, 2024
1 parent 7747f6a commit 1968eb8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
15 changes: 15 additions & 0 deletions packages/adapters/bitcoin/src/connectors/LeatherConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { bitcoin, bitcoinTestnet } from '@reown/appkit/networks'
export class LeatherConnector extends SatsConnectConnector {
static readonly ProviderId = 'LeatherProvider'

private connectedAccounts?: BitcoinConnector.AccountAddress[]

constructor({ connector }: LeatherConnector.ConstructorParams) {
if (connector.wallet.id !== LeatherConnector.ProviderId) {
throw new Error('LeatherConnector: wallet must be a LeatherProvider')
Expand All @@ -23,8 +25,21 @@ export class LeatherConnector extends SatsConnectConnector {
})
}

override async getAccountAddresses(): Promise<BitcoinConnector.AccountAddress[]> {
// Keep the connected accounts in memory to avoid repeated requests because Leather doesn't have a connect method
if (this.connectedAccounts) {
return this.connectedAccounts
}

this.connectedAccounts = await super.getAccountAddresses()

return this.connectedAccounts
}

public override disconnect(): Promise<void> {
// Leather doesn't have disconnect method
this.connectedAccounts = undefined

return Promise.resolve()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,72 @@ describe('LeatherConnector', () => {
it('should disconnect', async () => {
await expect(connector.disconnect()).resolves.not.toThrow()
})

it('should keep accounts and reuse if getAccountAddresses is called multiple times', async () => {
const requestSpy = vi.spyOn(mocks.wallet, 'request')

requestSpy.mockResolvedValueOnce(
mockSatsConnectProvider.mockRequestResolve({
addresses: [
{
address: 'mock_address',
purpose: 'receive',
addressType: 'p2pkh',
gaiaAppKey: 'mock_gaia_app_key',
gaiaHubUrl: 'mock_gaia_hub_url',
publicKey: 'mock_public_key'
}
]
})
)

const addressesFirstCall = await connector.getAccountAddresses()
const addressesSecondCall = await connector.getAccountAddresses()

expect(requestSpy).toHaveBeenCalledTimes(1)
expect(addressesFirstCall).toEqual(addressesSecondCall)
})

it('should clean up accounts after disconnection', async () => {
const requestSpy = vi.spyOn(mocks.wallet, 'request')

requestSpy.mockResolvedValueOnce(
mockSatsConnectProvider.mockRequestResolve({
addresses: [
{
address: 'mock_address',
purpose: 'receive',
addressType: 'p2pkh',
gaiaAppKey: 'mock_gaia_app_key',
gaiaHubUrl: 'mock_gaia_hub_url',
publicKey: 'mock_public_key'
}
]
})
)

const addressesFirstCall = await connector.getAccountAddresses()
await connector.disconnect()

requestSpy.mockResolvedValueOnce(
mockSatsConnectProvider.mockRequestResolve({
addresses: [
{
address: 'mock_address_2',
purpose: 'receive',
addressType: 'p2pkh',
gaiaAppKey: 'mock_gaia_app_key',
gaiaHubUrl: 'mock_gaia_hub_url',
publicKey: 'mock_public_key'
}
]
})
)
const addressesSecondCall = await connector.getAccountAddresses()

expect(requestSpy).toHaveBeenCalledTimes(2)
expect(addressesFirstCall).not.toBe(addressesSecondCall)

requestSpy.mockRestore()
})
})

0 comments on commit 1968eb8

Please sign in to comment.