npm.io
2.0.0 • Published 5 months ago

@aptos-labs/wallet-standard

Licence
Apache-2.0
Version
2.0.0
Deps
0
Size
119 kB
Vulns
0
Weekly
0
Stars
18

Aptos Wallet Standard

npm version npm downloads CI codecov License

The Aptos Wallet Standard defines TypeScript interfaces and utilities for wallet and dapp interactions in the Aptos ecosystem. It implements AIP-62, providing a consistent way for wallets to integrate with applications.

Built on top of the chain-agnostic Wallet Standard, this library extends it with Aptos-specific features like transaction signing, network management, and account handling.

Installation

# npm
npm install @aptos-labs/wallet-standard

# pnpm
pnpm add @aptos-labs/wallet-standard

Peer Dependencies

This package requires the following peer dependencies:

For Dapp Developers

Detecting Wallets

Use getAptosWallets() to discover all registered Aptos-compatible wallets. This function filters for wallets that implement the required feature set.

import { getAptosWallets } from "@aptos-labs/wallet-standard";

const { aptosWallets, on } = getAptosWallets();

// Use currently registered wallets
console.log("Available wallets:", aptosWallets);

// Listen for new wallets that register after page load
const removeListener = on("register", (wallet) => {
  console.log("New wallet registered:", wallet);
});
Checking for Optional Features

Use isWalletWithRequiredFeatureSet() to check if a wallet supports additional features beyond the required set:

import { isWalletWithRequiredFeatureSet } from "@aptos-labs/wallet-standard";
import type { AptosSignAndSubmitTransactionFeature } from "@aptos-labs/wallet-standard";

if (
  isWalletWithRequiredFeatureSet<AptosSignAndSubmitTransactionFeature>(wallet, [
    "aptos:signAndSubmitTransaction",
  ])
) {
  // Wallet supports sign-and-submit
}

For Wallet Developers

To make your wallet compatible with the Aptos Wallet Standard, implement the AptosWallet interface and register it using registerWallet from @wallet-standard/core.

1. Implement the Wallet Interface
import { APTOS_CHAINS } from "@aptos-labs/wallet-standard";
import type { AptosWallet, AptosFeatures } from "@aptos-labs/wallet-standard";

class MyWallet implements AptosWallet {
  readonly name = "My Wallet";
  readonly url = "https://mywallet.com";
  readonly version = "1.0.0";
  readonly icon = "data:image/png;base64,...";
  readonly chains = APTOS_CHAINS;
  accounts = [];

  get features(): AptosFeatures {
    return {
      "aptos:connect": { version: "1.0.0", connect: this.connect },
      "aptos:disconnect": { version: "1.0.0", disconnect: this.disconnect },
      "aptos:account": { version: "1.0.0", account: this.account },
      "aptos:network": { version: "1.0.0", network: this.network },
      "aptos:signTransaction": {
        version: "1.0.0",
        signTransaction: this.signTransaction,
      },
      "aptos:signMessage": {
        version: "1.0.0",
        signMessage: this.signMessage,
      },
      "aptos:onAccountChange": {
        version: "1.0.0",
        onAccountChange: this.onAccountChange,
      },
      "aptos:onNetworkChange": {
        version: "1.0.0",
        onNetworkChange: this.onNetworkChange,
      },
    };
  }

  // ... implement each feature method
}
2. Register the Wallet

For browser extension wallets, call registerWallet when the page loads:

import { registerWallet } from "@aptos-labs/wallet-standard";

const myWallet = new MyWallet();
registerWallet(myWallet);

See the full example wallet implementation for a complete template with detailed guidance.

API Reference

Interfaces
Interface Description
AptosWallet Main wallet interface extending WalletWithAptosFeatures with a url property
AptosWalletAccount Account interface extending WalletAccount with a signingScheme field
AccountInfo Serializable account info class with address, publicKey, and optional ansName
NetworkInfo Network details including name, chainId, and optional url
Required Features

All Aptos wallets must implement these features:

Feature Description
aptos:connect Connect a user's account to the dapp
aptos:disconnect Disconnect the account from the dapp
aptos:account Get the currently connected account info
aptos:network Get the current network the wallet is connected to
aptos:signTransaction Sign a transaction without submitting
aptos:signMessage Sign an arbitrary message
aptos:onAccountChange Listen for account change events
aptos:onNetworkChange Listen for network change events
Optional Features
Feature Description
aptos:signAndSubmitTransaction Sign and submit a transaction in one step
aptos:changeNetwork Request the wallet to switch networks
aptos:openInMobileApp Open the wallet's mobile app
aptos:signIn Sign in with the wallet
Chains

Pre-defined chain identifiers:

import {
  APTOS_MAINNET_CHAIN, // "aptos:mainnet"
  APTOS_TESTNET_CHAIN, // "aptos:testnet"
  APTOS_DEVNET_CHAIN, // "aptos:devnet"
  APTOS_LOCALNET_CHAIN, // "aptos:localnet"
  APTOS_CHAINS, // All of the above
} from "@aptos-labs/wallet-standard";
Error Handling

The library provides structured errors via AptosWalletError:

import {
  AptosWalletError,
  AptosWalletErrorCode,
} from "@aptos-labs/wallet-standard";

// Error codes:
// 4100 - Unauthorized: method/account not authorized by the user
// 4200 - Unsupported: requested feature is not supported
// -30001 - InternalError: something went wrong within the wallet
User Responses

Methods that prompt the user for approval return a UserResponse<T>, a discriminated union that lets the wallet signal either approval (with a result) or rejection. The following features use this shape:

  • aptos:connect
  • aptos:signTransaction
  • aptos:signAndSubmitTransaction
  • aptos:signMessage
  • aptos:signIn
  • aptos:changeNetwork

Other required features return their value directly (still asynchronously): aptos:account returns Promise<AccountInfo>, aptos:network returns Promise<NetworkInfo>, aptos:disconnect returns Promise<void>, and the onAccountChange / onNetworkChange features register subscriptions.

import { UserResponseStatus } from "@aptos-labs/wallet-standard";

const response = await wallet.features["aptos:connect"].connect();

if (response.status === UserResponseStatus.APPROVED) {
  console.log("Connected:", response.args); // AccountInfo
} else {
  console.log("User rejected the connection");
}

Development

# Install dependencies
pnpm install

# Build
pnpm build

# Lint
pnpm lint

# Format
pnpm format

License

Apache-2.0