Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New wallet adapter interface #473

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft

Conversation

hardsetting
Copy link
Contributor

@hardsetting hardsetting commented Feb 5, 2025

My proposal for a new interface of the Aptos Wallet Adapter.

Key objectives:

  • prioritize newer features (keep interface as close as possible to wallet standard)
  • stay backward compatible in a way that doesn't bloat the "happy path"
  • reduce the amount of wrapper code
  • ensure the state of the adapter stays consistent
  • simplify the developer's life

Changes in core package:

  • Defined AdaptedWallet class, that will take as an input a wallet interface with minimum required features and expose a unified and clean interface for accessing it. Internally, this class performs feature version negotiation, ensuring we're using the most up-to-date version or fallback to older but still compatible versions.
  • Defined a structured way to access registered wallets (using WalletAdapter).
  • This package will not deal with persisted state e.g. window.localStorage. This will be the responsibility of more specialized packages
  • Create utility conversion function walletFromLegacyPlugin so that dapps that rely on legacy plugins can still use the new interface (all required features need to be supported)

Changes in react package:

  • Provide "reactive" interface to aptos wallets.
  • Define hooks that listen to events and keep values up-to-date at all times (ensures state consistency)
  • Intended for web-based dapps (might extract web logic into separate package if there's value)
  • Can be used to build custom wallet selectors
  • Defines the concept of "active wallet" and the logic to persist its state
  • The value returned by the hook useActiveWallet has different states. Union discrimination enables intuitive and expressive access to features based on the current state.

example:

function Navbar() {
  const wallet = useActiveWallet();
  return (
    <nav>
      <div>Logo</div>
      {wallet.isConnected ? <ActiveAccountNavItem wallet={wallet} /> : <WalletSelector />}
    </nav>
  );
}

// Requires connected wallet as input
export function ActiveAccountNavItem({ wallet }: { wallet: ConnectedWallet }) {
  const [isDropdownOpen, setIsDropdownOpen] = useState(false);

  const onToggle = () => {
    setIsDropdownOpen((prev) => !prev);
  };

  const onDisconnect = async () => {
    // only available if the wallet is connected
    await wallet.disconnect();
  };

  return (
    <button onClick={onToggle}>
      <div>{wallet.activeAccount.address.toString()}</div>
      {isDropdownOpen ? (
        <ul>
          <li>Copy address</li>
          <li onClick={onDisconnect}>Disconnect</li>
        </ul>
      ) : null}
    </button>
  )
}

TODO:

  • [ui] build on top of the react package and showcase it's expressivenesss
  • [core] bring back analytics and formalize events
  • [core] bring back opting out of certain wallets
  • [react/ui] figure out how to approach "sdk" wallets. should we pass them during context providing? should we just register them at startup like extension wallets?
  • [ui] bring back "opt-in" wallets and explore universal links

@hardsetting hardsetting self-assigned this Feb 5, 2025
@hardsetting hardsetting force-pushed the gabriele/new-interface branch from 1e834ce to 138f74d Compare February 6, 2025 01:55
@hardsetting hardsetting force-pushed the gabriele/new-interface branch from 138f74d to d62ecbf Compare February 6, 2025 10:59
@hardsetting hardsetting force-pushed the gabriele/new-interface branch from cbce48d to 1ad431a Compare February 6, 2025 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant