Skip to content

Commit

Permalink
Improved interface
Browse files Browse the repository at this point in the history
  • Loading branch information
hardsetting committed Feb 6, 2025
1 parent d62ecbf commit 1ad431a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
3 changes: 2 additions & 1 deletion packages/wallet-adapter-core/src/new/AdaptedWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
AptosSignTransactionInputV1_1,
AptosSignTransactionOutput,
AptosSignTransactionOutputV1_1,
WalletIcon,
} from '@aptos-labs/wallet-standard';
import { GA4 } from '../ga';
import { WALLET_ADAPTER_CORE_VERSION } from '../version';
Expand Down Expand Up @@ -39,7 +40,7 @@ export interface AdaptedWalletConfig {
export class AdaptedWallet {
readonly name: string;
readonly url: string;
readonly icon: string;
readonly icon: WalletIcon;
readonly features: AptosFeatures;

readonly availableNetworks: Network[];
Expand Down
6 changes: 3 additions & 3 deletions packages/wallet-adapter-react/src/new/Example.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UserResponseStatus } from '@aptos-labs/wallet-standard';
import { useState } from 'react';
import { useActiveWallet, UseActiveWalletConnectedResult } from './useActiveWallet';
import { ConnectedWallet, useActiveWallet } from './useActiveWallet';
import { setActiveWalletId } from './useActiveWalletId';
import { useAvailableWallets } from './useAvailableWallets';

Expand Down Expand Up @@ -41,7 +41,7 @@ export function WalletSelectorModal() {
}

interface ActiveAccountNavItemProps {
wallet: UseActiveWalletConnectedResult
wallet: ConnectedWallet
}

export function ActiveAccountNavItem({ wallet }: ActiveAccountNavItemProps) {
Expand All @@ -57,7 +57,7 @@ export function ActiveAccountNavItem({ wallet }: ActiveAccountNavItemProps) {

return (
<button onClick={onToggle}>
<div>{wallet.account.address.toString()}</div>
<div>{wallet.activeAccount.address.toString()}</div>
{isDropdownOpen ? (
<ul>
<li>Copy address</li>
Expand Down
47 changes: 25 additions & 22 deletions packages/wallet-adapter-react/src/new/useActiveWallet.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
import { AdaptedWallet, NewNetwork as Network, StandardNetwork } from '@aptos-labs/wallet-adapter-core';
import {
AccountInfo,
AptosFeatures,
AptosSignAndSubmitTransactionInput,
AptosSignMessageInput,
AptosSignTransactionInputV1_1,
WalletIcon,
} from '@aptos-labs/wallet-standard';
import { useEffect, useMemo, useState } from 'react';
import { useActiveWalletId } from './useActiveWalletId';
import { useAvailableWallets } from './useAvailableWallets';

export interface UseActiveWalletUninitializedResult {
export interface UninitializedWallet {
isInitialized: false;
isConnected: false;
account?: undefined;
network?: undefined;
wallet?: undefined;
activeAccount?: undefined;
activeNetwork?: undefined;
}

export interface UseActiveWalletDisconnectedResult {
export interface DisconnectedWallet {
isInitialized: true;
isConnected: false;
account?: undefined;
network?: undefined;
wallet?: undefined;
activeAccount?: undefined;
activeNetwork?: undefined;
}

export interface UseActiveWalletConnectedResult {
export interface ConnectedWallet {
isInitialized: true;
isConnected: true;
account: AccountInfo;
network: Network;
// wallet: Wallet;
activeAccount: AccountInfo;
activeNetwork: Network;
name: string;
icon: WalletIcon;
features: AptosFeatures;
disconnect: () => Promise<void>;
signMessage: (input: AptosSignMessageInput)
=> ReturnType<AdaptedWallet['signMessage']>;
Expand All @@ -41,9 +43,9 @@ export interface UseActiveWalletConnectedResult {
}

type UseActiveWalletResult =
UseActiveWalletUninitializedResult
| UseActiveWalletDisconnectedResult
| UseActiveWalletConnectedResult;
| UninitializedWallet
| DisconnectedWallet
| ConnectedWallet;

export function useActiveWallet(): UseActiveWalletResult {
const availableWallets = useAvailableWallets();
Expand Down Expand Up @@ -89,27 +91,28 @@ export function useActiveWallet(): UseActiveWalletResult {
return {
isInitialized: false,
isConnected: false,
} satisfies UseActiveWalletUninitializedResult;
} satisfies UninitializedWallet;
}

if (!activeWallet || !activeAccount) {
return {
isInitialized: true,
isConnected: false,
} satisfies UseActiveWalletDisconnectedResult;
} satisfies DisconnectedWallet;
}

return {
isInitialized: true,
isConnected: true,
// todo: pass other useful info
// wallet: activeWallet,
account: activeAccount,
network: activeNetwork,
activeAccount,
activeNetwork,
name: activeWallet.name,
icon: activeWallet.icon,
features: activeWallet.features,
disconnect: async () => activeWallet.disconnect(),
signMessage: async (input) => activeWallet.signMessage(input),
signTransaction: async (input) => activeWallet.signTransaction(input),
signAndSubmitTransaction: async (input) => activeWallet.signAndSubmitTransaction(input),
} satisfies UseActiveWalletConnectedResult;
} satisfies ConnectedWallet;
}, [activeAccount, activeNetwork, activeWallet, isInitialized]);
}

0 comments on commit 1ad431a

Please sign in to comment.