@ binance/w3w-多链连接器
这个 @binance/w3w-multichain-connector 软件包为 Binance Wallet 扩展多链连接器.
安装
pnpm add @binance/w3w-multichain-connector
# or
npm install @binance/w3w-multichain-connector
使用量
导入
import { getMultichainWallet } from '@binance/w3w-multichain-connector'
获取钱包实例
const wallet = getMultichainWallet()
if (!wallet) {
console.log('Binance Wallet extension not installed')
}
检查扩展已安装
import { checkExtensionInstalled } from '@binance/w3w-multichain-connector'
const isInstalled = checkExtensionInstalled()
检查扩展特性支持
import { checkExtensionSupported, Features } from '@binance/w3w-multichain-connector'
const isSupported = checkExtensionSupported(Features.requestConnect)
连接钱包
const connectedWallets = await wallet.requestConnect(['56', 'CT_501'])
连接 SAS
// Single wallet SAS
const { wallet: connectedWallet, aggreeEnableSAS } = await wallet.requestConnectWithSAS({
binanceChainIds: ['56'],
userId: 'user-id',
enabledWalletInfo: [
{ walletType: 'SEED_PHRASE', chain: 'evm', userPublicKeyHex: '0x...' }
]
})
// Multi wallet SAS
const { walletList, enabledWalletIds, connectId } = await wallet.requestConnectWithMultiWalletSAS({
binanceChainIds: ['56'],
userId: 'user-id'
})
检查已连接的钱包
const wallets = await wallet.checkConnectedWallet()
// The user's active wallet will be the first element in the array
获取钱包信息
const walletInfo = await wallet.getWalletInfo('0x...address', '56')
切换链
await wallet.switchChain('56')
断开连接
// Disconnect all wallets
await wallet.disconnect()
// Disconnect specific accounts
await wallet.disconnectWallets(['account-id-1', 'account-id-2'])
签名顺序
const signature = await wallet.signOrder({
walletId: 'wallet-id',
walletAddress: '0x...',
binanceChainId: '56',
txData: '...',
feeAmount: '0.001',
feeAmountLevel: 'Medium', // 'Low' | 'Medium' | 'High' | 'Custom'
digest: '...',
digestSign: '...'
})
自动签名
await wallet.requestAutoSign()
console.log('Auto sign enabled:', wallet.autoSignEnabled)
SAS业务
// Enable SAS
const { aggreeEnableSAS } = await wallet.requestEnableSAS({
walletId: 'wallet-id',
userId: 'user-id',
userEmail: '[email protected]'
})
// Enable multi-wallet SAS
const { enabledWalletIds } = await wallet.requestEnableMultiWalletSAS({
walletIds: ['wallet-1', 'wallet-2'],
userId: 'user-id',
userEmail: '[email protected]'
})
// Get SAS Keyshare
const keyshares = await wallet.getSASKeyshare({
walletIds: ['wallet-id'],
userId: 'user-id',
spsVersion: '1.0',
isSilent: false // if true, won't prompt unlock when wallet is locked
})
// Sync SAS Status
await wallet.syncSASStatus({
userId: 'user-id',
statusList: [
{ walletId: 'wallet-id', enabled: true }
]
})
// Get ProofKey Signature (before placing order)
const { timeStamp, proofKeyPublicKey, timestampSignature } = await wallet.getSASProofKeySignature({
walletId: 'wallet-id',
userId: 'user-id'
})
// Get Connected Wallets with ProofKey
const walletsWithProofKey = await wallet.getConnectedWalletAndProofKey({
userId: 'user-id'
})
处理事件
const wallet = getMultichainWallet()
// Wallet lock status changed
wallet.on('walletLockedChanged', (isLocked) => {
console.log('Wallet locked:', isLocked)
})
// Auto sign status changed
wallet.on('autoSignEnabledChanged', (isEnabled) => {
console.log('Auto sign enabled:', isEnabled)
})
// Connected wallets changed
wallet.on('connectedWalletsChanged', (connectedWallets) => {
console.log('Connected wallets:', connectedWallets)
})
// Connected chain changed
wallet.on('connectedChainIdChanged', (binanceChainId) => {
console.log('Chain changed:', binanceChainId)
})
属性
| 财产 | 类型 | 说明 |
version | string | 当前扩展版本 |
autoSignEnabled | boolean | 是否启用自动标志 |
walletLocked | boolean | 钱包是否锁定 |
extensionId | string | 扩展独有标识符 |
类型
IConnectedWallet
| 外地 | 类型 | 说明 |
walletIcon | string | 钱包图标 (Base64) |
walletName | string | 钱包名称 |
walletId | string | 钱包独有的标识符 |
walletType | WalletType | 钱包类型( 私人 Key / 种子词组) |
addressList | Array<{ address, binanceChainId, publicKeyHex }> | 钱包地址列表 |
groupID? | string | 钱包组 ID |
groupName? | string | 钱包组名 |
WalletType
| 数值 | 说明 |
privateKey | 私钥钱包 |
seedPhrase (单位:千美元)hd(中文(简体) ) | 种子词组( mnemonic) 钱包 |
Features
| 数值 | 说明 |
| 连接 | |
requestConnect | 连接钱包 |
requestConnectWithSAS | 用单钱包连接钱包 SAS |
requestConnectWithMultiWalletSAS | 用多钱包 SAS 连接钱包 |
checkConnectedWallet | 检查连接的钱包 |
getWalletInfo | 通过地址和链条获取钱包信息 |
disconnect | 断开所有钱包 |
disconnectWallets | 断开特定钱包 |
switchChain | 切换链 |
| 萨斯 | |
requestEnableSAS | 请求启用 SAS( 单个钱包) |
requestEnableMultiWalletSAS | 请求启用 SAS( 多钱包) |
getSASKeyshare | 获取 SAS 密钥共享 |
syncSASStatus | 同步 SAS 状态 |
getConnectedWalletAndProofKey | 与 ProofKey 连接钱包 |
getSASProofKeySignature | 在放置命令前获取 ProfKey 签名 |
| 签名 | |
requestAutoSign | 请求自动签名 |
signOrder | 签署交易命令 |
| 活动 | |
eventWalletLockedChanged | 钱包锁状态已更改 |
eventAutoSignEnabledChanged | 自动签名状态已更改 |
eventConnectedWalletsChanged | 已连接的钱包已更改事件 |
eventConnectedChainIdChanged | 连接链已更改事件 |