@mysten/wallet-standard
A suite of standard utilities for implementing wallets and libraries based on the Wallet Standard.
Implementing the Wallet Standard in an extension wallet
Creating a wallet interface
You need to create a class that represents your wallet. You can use the Wallet
interface from @mysten/wallet-standard
to help ensure your class adheres to the standard.
import { Wallet, SUI_DEVNET_CHAIN } from "@mysten/wallet-standard";
class YourWallet implements Wallet {
get version() {
return "1.0.0";
}
get name() {
return "Wallet Name";
}
get icon() {
return "some-icon-data-url";
}
get chains() {
return [SUI_DEVNET_CHAIN];
}
}
Implementing features
Features are standard methods consumers can use to interact with a wallet. To be listed in the Sui wallet adapter, you must implement the following features in your wallet:
standard:connect
- Used to initiate a connection to the wallet.standard:events
- Used to listen for changes that happen within the wallet, such as accounts being added or removed.sui:signAndExecuteTransaction
- Used to prompt the user to sign a transaction, then submit it for execution to the blockchain.
You can implement these features in your wallet class under the features
property:
import {
ConnectFeature,
ConnectMethod,
EventsFeature,
EventsOnMethod,
SuiSignAndExecuteTransactionFeature,
SuiSignAndExecuteTransactionMethod
} from "@mysten/wallet-standard";
class YourWallet implements Wallet {
get features(): ConnectFeature & EventsFeature & SuiSignAndExecuteTransactionFeature {
return {
"standard:connect": {
version: "1.0.0",
connect: this.#connect,
},
"standard:events": {
version: "1.0.0",
on: this.#on,
}
"sui:signAndExecuteTransaction": {
version: "1.0.0",
signAndExecuteTransaction: this.#signAndExecuteTransaction,
},
};
},
#on: EventsOnMethod = () => {
};
#connect: ConnectMethod = () => {
};
#signAndExecuteTransaction: SuiSignAndExecuteTransactionMethod = () => {
};
}
Exposing accounts
The last requirement of the wallet interface is to expose an acccounts
interface. This should expose all of the accounts that a connected dapp has access to. It can be empty prior to initiating a connection through the standard:connect
feature.
The accounts can use the ReadonlyWalletAccount
class to easily construct an account matching the required interface.
import { ReadonlyWalletAccount } from "@mysten/wallet-standard";
class YourWallet implements Wallet {
get accounts() {
return someWalletAccounts.map(
(walletAccount) =>
new ReadonlyWalletAccount({
address: walletAccount.suiAddress,
publicKey: walletAccount.pubkey,
chains: [SUI_DEVNET_CHAIN],
features: ["sui:signAndExecuteTransaction", "standard:signMessage"],
})
);
}
}
Registering in the window
Once you have a compatible interface for your wallet, you can register it in the window under the window.navigator.wallets
interface. Wallets self-register by pushing their standard wallet interface to this array-like interface.
declare const window: import("@mysten/wallet-standard").WalletsWindow;
(window.navigator.wallets || []).push(({ register }) => {
register(new YourWallet());
});
Note that while this interface is array-like, it is not always an array, and the only method that should be called on it is push
.