Skip to content

NO SIDE EFFECTS: need isConnected, need explicit requestAccess, and getAddress should NEVER open a modal #65

@chadoh

Description

@chadoh

This is going to require some work with the full set of wallets y'all interface with, but it solves a BIG problem.

Today, SWK is extremely hard to use (at least, with standard app building patterns) because getAddress is overloaded. It currently does three, maybe four, things:

  1. Checks if the user has opened the wallet and given it permission to access the current app. Freighter has two methods for this same behavior, and both are useful:
  2. It OPENS THE WALLET EXTENSION'S "CONNECT" MODAL!! I'm going to shout about this some more because it is SO unexpected and leads to horrible twisty app logic. In general, a getter method should NEVER HAVE SIDE EFFECTS. If something has a name that indicates that it's doing a simple data look-up, it should not also do something as obnoxious or user-effecting as opening a modal.
    • In Freighter, this is done with requestAccess, an action-verb name that gives the developer a hint at its repercussions.
  3. It actually gets the currently-selected address from the connected wallet

Since SWK currently provides no way to do this, apps today need to implement some impoverished version of 1 above using localStorage. In Scaffold Stellar today, we use localStorage. There's all sorts of side effects we need to code around, or simply can't or shouldn't code around:

  • Our onWalletSelected logic only sets localStorage after a successful subsequent call to getAddress, because without that our polling logic starts requesting getAddress and opening duplicate "connect" modals.
  • If the user connects on Day 1, the localStorage will still be set on Day 2, which will cause getAddress to be called at page load, opening the "connect" modal. Perhaps the only way around this is to reverse-engineer specific wallet extensions' session expiration logic, and implement more app-side logic to expire this localStorage. This is error-prone and bloated.
  • Rather than straightforwardly asking the wallet extension itself if the user isConnected and if the extension isAllowed to access the page, we need to check if our localStorage item is set. As noted, this is not the same thing, and leads to hard-to-maintain code and janky user experiences.

I suggest following Freighter's lead here, adding isConnected, isAllowed, and requestAccess.

I realize that I am suggesting interface methods beyond what is specified by SEP-43. To me, the challenge of using bare-minimum, current-day SEP-43 indicates to me that it is too limited, and we need to update it. SWK ought to be the most powerful voice in this process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions