Skip to content

Sui

Bitget Wallet 钱包仅在以下版本支持此功能

PlatformVersionDescription
Chrome Extension>=v1.4.6mainnet
App(iOS)>=v7.3.4mainnet
App(Android)>=v7.3.4mainnet

获取 provider

Sui 钱包基于 wallet-standard 实现, 不同于其他异构链, 是通过事件的方式获取 provider

js
const chainName = 'suiMainnet';
const GlobalWallet = {
    register: (wallet) => {
        GlobalWallet[chainName] = wallet
    }
}
const event = new CustomEvent('wallet-standard:app-ready', { detail: GlobalWallet });
window.dispatchEvent(event);

const provider = GlobalWallet[chainName]
const chainName = 'suiMainnet';
const GlobalWallet = {
    register: (wallet) => {
        GlobalWallet[chainName] = wallet
    }
}
const event = new CustomEvent('wallet-standard:app-ready', { detail: GlobalWallet });
window.dispatchEvent(event);

const provider = GlobalWallet[chainName]

connect

链接钱包

js
await provider.features['standard:connect'].connect();
await provider.features['standard:connect'].connect();

account

获取钱包账户地址

js
const accounts = provider.account

accounts = [account, account,,, account]

//数据格式
account = {
  address: '0x,,,,,,,,,,,,,,b1c',
  chains: ['sui:mainet']
};
const accounts = provider.account

accounts = [account, account,,, account]

//数据格式
account = {
  address: '0x,,,,,,,,,,,,,,b1c',
  chains: ['sui:mainet']
};

standard:events

事件监听

js
let on = provider.features['standard:events'].on;

on("connect", (event) => console.log("sui connected.", event));
on("disconnect", (event) => console.log("sui disconnect.", event));
on("accountChanged", (event) => console.log("sui accountChanged.", event));
let on = provider.features['standard:events'].on;

on("connect", (event) => console.log("sui connected.", event));
on("disconnect", (event) => console.log("sui disconnect.", event));
on("accountChanged", (event) => console.log("sui accountChanged.", event));

signTransactionBlock

签署一笔交易

provider.features['sui:signTransactionBlock'].signTransactionBlock

js
//https://docs.sui.io/guides/developer/sui-101/building-ptb

import { TransactionBlock } from "@mysten/sui.js/transactions"; 
const tx = new TransactionBlock();
tx.moveCall({
  target: 'xxx',
  arguments: [
    tx.pure('Bitget'),
    tx.pure('wallet'),
  ],
});
const input = {
  transactionBlockSerialized: tx.serialize(),
  options: {
    showEffects: true,
  }
}

const signTransactionBlock = provider.features['sui:signTransactionBlock'].signTransactionBlock;
const transaction = await signTransactionBlock({ transactionBlock: tx })
//https://docs.sui.io/guides/developer/sui-101/building-ptb

import { TransactionBlock } from "@mysten/sui.js/transactions"; 
const tx = new TransactionBlock();
tx.moveCall({
  target: 'xxx',
  arguments: [
    tx.pure('Bitget'),
    tx.pure('wallet'),
  ],
});
const input = {
  transactionBlockSerialized: tx.serialize(),
  options: {
    showEffects: true,
  }
}

const signTransactionBlock = provider.features['sui:signTransactionBlock'].signTransactionBlock;
const transaction = await signTransactionBlock({ transactionBlock: tx })

signAndExecuteTransactionBlock

签署一笔交易并广播

provider.features['sui:signAndExecuteTransactionBlock'].signAndExecuteTransactionBlock

js
import { TransactionBlock } from "@mysten/sui.js/transactions";

const handleTransaction = async () => {
    const tx = new TransactionBlock()
    tx.moveCall({
      target: `${packageId}::${moduleName}::${functionName}`,
      arguments: [
        tx.pure(params1),
        tx.pure(params2),
      ],
      typeArguments: [],
    })
    const signAndExecuteTransactionBlock = provider.features['sui:signAndExecuteTransactionBlock'].signAndExecuteTransactionBlock;
    const result = await signAndExecuteTransactionBlock({
      transactionBlock: tx,
      options: { showEffects: true },
    })
    console.log('result', result)
    // 通过result?.effects?.status?.status获取交易状态,成功为 'success',失败为'failure'
}
import { TransactionBlock } from "@mysten/sui.js/transactions";

const handleTransaction = async () => {
    const tx = new TransactionBlock()
    tx.moveCall({
      target: `${packageId}::${moduleName}::${functionName}`,
      arguments: [
        tx.pure(params1),
        tx.pure(params2),
      ],
      typeArguments: [],
    })
    const signAndExecuteTransactionBlock = provider.features['sui:signAndExecuteTransactionBlock'].signAndExecuteTransactionBlock;
    const result = await signAndExecuteTransactionBlock({
      transactionBlock: tx,
      options: { showEffects: true },
    })
    console.log('result', result)
    // 通过result?.effects?.status?.status获取交易状态,成功为 'success',失败为'failure'
}

signMessage

对单个交易签署消息

这里使用 TextEncoder 来处理 UTF-8 bytes

js
const msg = 'Hello Bitget Wallet!'
const msgBytes = new TextEncoder().encode(msg)
      
// const message = ethers.utils.toUtf8Bytes('bitget')

const signMessage = suiWallet.features['sui:signMessage'].signMessage;
const { signature, messageBytes } = await signMessage({ message: msgBytes })
const msg = 'Hello Bitget Wallet!'
const msgBytes = new TextEncoder().encode(msg)
      
// const message = ethers.utils.toUtf8Bytes('bitget')

const signMessage = suiWallet.features['sui:signMessage'].signMessage;
const { signature, messageBytes } = await signMessage({ message: msgBytes })

参考文档

  1. Sui 官方文档
  2. Wallet Standard
  3. TextEncoder