钱包标准
为 Sui 构建的浏览器扩展钱包使用 Wallet Standard 进行定义。这是一个跨链标准,定义了如何从 dApps 中自动发现和与钱包进行交互。
如果你正在构建一个钱包,我们发布了一个辅助库 @mysten/wallet-standard
,该库提供了类型和实用程序,使入门变得简单。
创建钱包接口
你需要创建一个代表你的钱包的类。你可以使用 @mysten/wallet-standard
中的 Wallet
接口来帮助确保你的类符合该标准。
import { SUI_DEVNET_CHAIN, Wallet } from '@mysten/wallet-standard';
class YourWallet implements Wallet {
get version() {
// Return the version of the Wallet Standard this implements (in this case, 1.0.0).
return '1.0.0';
}
get name() {
return 'Wallet Name';
}
get icon() {
return 'some-icon-data-url';
}
// Return the Sui chains that your wallet supports.
get chains() {
return [SUI_DEVNET_CHAIN];
}
}
实现功能
功能是消费者可以使用的标准方法,用于与钱包进行交互。要在 Sui 钱包适配器中列出,你必须在你的钱包中实现以下功能:
standard:connect
- 用于启动与钱包的连接。standard:events
- 用于监听钱包内发生的更改,例如添加或删除账户。sui:signPersonalMessage
- 用于提示用户签署个人消息,并将消息签名返回给 dApp。这可用于验证用户的公钥。sui:signTransactionBlock
- 用于提示用户签署交易块,并将序列化的交易块和签名返回给 dApp。此方法不提交交易块进行执行。sui:signAndExecuteTransactionBlock
- 用于提示用户签署交易,然后将其提交到区块链进行执行。
你可以在钱包类的 features
属性下实现这些功能:
import {
ConnectFeature,
ConnectMethod,
EventsFeature,
EventsOnMethod,
SuiFeatures,
SuiSignPersonalMessageMethod,
SuiSignTransactionBlockMethod,
SuiSignAndExecuteTransactionBlockMethod
} from "@mysten/wallet-standard";
class YourWallet implements Wallet {
/* ... existing code from above ... */
get features(): ConnectFeature & EventsFeature & SuiFeatures {
return {
"standard:connect": {
version: "1.0.0",
connect: this.#connect,
},
"standard:events": {
version: "1.0.0",
on: this.#on,
},
"sui:signPersonalMessage": {
version: "1.0.0",
signPersonalMessage: this.#signPersonalMessage,
},
"sui:signTransactionBlock": {
version: "1.0.0",
signTransactionBlock: this.#signTransactionBlock,
},
"sui:signAndExecuteTransactionBlock": {
version: "1.0.0",
signAndExecuteTransactionBlock: this.#signAndExecuteTransactionBlock,
},
};
},
#on: EventsOnMethod = () => {
// Your wallet's events on implementation.
};
#connect: ConnectMethod = () => {
// Your wallet's connect implementation
};
#signPersonalMessage: SuiSignPersonalMessageMethod = () => {
// Your wallet's signTransaction implementation
};
#signTransactionBlock: SuiSignTransactionBlockMethod = () => {
// Your wallet's signTransaction implementation
};
#signAndExecuteTransactionBlock: SuiSignAndExecuteTransactionBlockMethod = () => {
// Your wallet's signAndExecuteTransaction implementation
};
}
暴露账户
钱包接口的最后一个要求是暴露一个 accounts
接口。这应该暴露已连接的 dApp 可访问的所有账户。在通过 standard:connect
功能启动连接之前,它可以为空。
账户可以使用 ReadonlyWalletAccount
类轻松构建与所需接口匹配的账户。
import { ReadonlyWalletAccount } from '@mysten/wallet-standard';
class YourWallet implements Wallet {
get accounts() {
// Assuming we already have some internal representation of accounts:
return someWalletAccounts.map(
(walletAccount) =>
// Return
new ReadonlyWalletAccount({
address: walletAccount.suiAddress,
publicKey: walletAccount.pubkey,
// The Sui chains that your wallet supports.
chains: [SUI_DEVNET_CHAIN],
// The features that this account supports. This can be a subset of the wallet's supported features.
// These features must exist on the wallet as well.
features: [
'sui:signPersonalMessage',
'sui:signTransactionBlock',
'sui:signAndExecuteTransactionBlock',
],
}),
);
}
}
在窗口中注册
一旦你拥有与你的钱包兼容的接口,你可以使用 registerWallet
函数进行注册。
import { registerWallet } from '@mysten/wallet-standard';
registerWallet(new YourWallet());