下载

arrow_down

构建

arrow_down

更多

arrow_down
activityactivityactivityactivity
  • themelight
  • languageIcon

  • menu
Skip to Content
Zh CnDocs
参考
DappOmniConnect

OmniConnect

OmniConnectBitget Wallet推出的面向开发者的软件开发工具包,旨在实现 Telegram 生态系统内的 Mini-Apps 与多链环境的无缝集成。集成此工具包后,Telegram Mini Apps 可以直接与 Bitget Wallet 进行跨多链的签名、交易和其他 DApp 操作。

  • OmniConnect:一个开放协议,通过桥接服务器连接钱包和 DApps(Web3 应用程序),在 TG MiniApp 和/或 Bitget Wallet Lite 之间建立远程连接。
  • OmniConnect 官方支持的链
    • EVM(以太坊虚拟机兼容区块链,如 Fantom、Base、Arbitrum 等)
  • OmniConnect 支持的钱包
    • Bitget Wallet Lite
  • 在线演示快速体验
  • NPM 包

安装和初始化

要将 Omni Connect 集成到您的 DApp 中,您可以使用 pnpm:

pnpm add @bitget-wallet/omni-connect

在连接钱包之前,您需要实例化一个对象以进行后续操作,如连接钱包和发送交易。

const connector = new OmniConnect({ metadata, namespace })

请求参数

  • metaData - object:应用程序元数据
    • name - string:应用程序名称
    • iconUrl - string:应用程序图标 URL
    • url - string:应用程序的主网站
    • privacyPolicyUrl - string(可选):
    • termsOfUseUrl - string(可选):
  • namespace - object:请求连接的必要命名空间信息。值必须在支持范围内,否则连接将失败。
    • eip155 - object:EVM 值为”eip155”
      • chains: - Array:链 ID 信息

示例

import { OmniConnect, PreventType } from '@bitget-wallet/omni-connect' const connector = new OmniConnect({ metadata: { name: '<您的DApp名称>', iconUrl: '<您的图标URL>', url: '<您的网站URL>', privacyPolicyUrl: '', // 可选 termsOfUseUrl: '', // 可选 }, namespace: { eip155: { chains: ['1', '56'], }, }, })

连接钱包

连接到钱包以获取钱包地址,该地址用作标识符,是签名交易所必需的; connector.connect(options)

请求参数

  • options - object(可选)
    • ret - string:PreventType.CLOSE 您可以指定连接后不关闭钱包(默认行为是关闭它)

恢复连接

如果用户之前已连接其钱包,请使用此方法恢复连接状态。(SDK 已默认恢复连接)

示例

connector.restoreConnection()

签名消息

向钱包签名消息的方法,支持签名;

connector.signMessage({ method, params: [address, message] })

请求参数

  • method - string:请求方法名称值 eth_sign | personal_sign | eth_signTypedData | eth_signTypedData_v4
  • params - Array:
    • [0] - address - string:钱包地址
    • [1] - message - string | object:要签名的消息
  • settings - object(可选):打开 TG 钱包配置
    • preventPopup - string:PreventType.OPEN(防止打开新窗口)

示例

const address = '0x..' try { await connector.signMessage({ method: 'eth_sign', params: [address, 'Hello World!'], }) } catch (error) { console.log(error) }

发送交易

向钱包发送消息以支持交易的方法;

connector.sendTransaction({ method: "eth_sendTransaction", params: [txData], })

请求参数

  • method - string:请求方法名称值 eth_sendTransaction
  • params - Array:
    • [0] - txData - object:
      • chainId - hexstring:链 ID
      • from - string:发送者地址。
      • to - string:接收者地址,如果这是合约创建交易则为 null。
      • value - hexstring:要转移的值,以 wei 为单位。
      • nonce - hexstring(可选):防重放参数。
      • gasLimit - hexstring:gasLimit
      • maxPriorityFeePerGas - hexstring(可选):发送者愿意在基础费用之上为每个 gas 支付的最大费用,以 wei 为单位。
      • maxFeePerGas - hexstring(可选):发送者愿意为每个 gas 支付的最大总费用(基础费用+优先费用),以 wei 为单位。
  • settings - object(可选):打开 TG 钱包配置
    • preventPopup - string:PreventType.OPEN(防止打开新窗口)

示例

const address = '0x..' try { await connector.signTransaction({ method: 'eth_signTransaction', params: [ { chainId: '0x38', data: '0x', from: address, to: '0x..', value: '0x0', gasLimit: '0x5208', maxPriorityFeePerGas: '0x3b9aca00', //wei maxFeePerGas: '0x2540be400', //wei }, ], }) } catch (error) { console.log(error) }

监控钱包状态变化

钱包状态包括:连接状态、签名结果等。您可以使用此方法获取状态。

onStatusChange( callback: (walletInfo) => void, errorsHandler?: (err) => void ): () => void;

请求参数

  • callback - (walletInfo) => void
    • id - number | string:请求 ID
    • namespaceKey - string:‘eip155’
    • event - string:事件类型
    • connected - boolean:连接状态
    • result - object
      • address - string:钱包连接地址
      • signature - string:签名结果
  • errorsHandler - (err) => void
    • code - number:错误代码
    • message - string:错误消息

示例

const subscription = connector.onStatusChange( (walletInfo) => { console.log('onStatusChange', walletInfo) const { id, namespaceKey, event, connected, result } = walletInfo switch (event) { case 'connect': case 'disconnect': // 连接或断开连接逻辑.. break case 'signMessage': break case 'signTransaction': case 'sendTransaction': // 处理result?.signature, result?.reciept break default: break } }, (err) => { const { code, message } = err console.error(`错误流:代码:${code},消息:${message}`) } )

当您不再需要监听更新时,调用unsubscribe以节省资源。

subscription?.unsubscribe()

断开连接

断开已连接的钱包

connector.disconnect()

错误代码

连接期间可能抛出的异常:

UNKNOWN_ERROR 未知异常
BAD_REQUEST_ERROR 请求错误
UNKNOWN_APP_ERROR 未知应用异常
USER_REJECTS_ERROR 用户拒绝
METHOD_NOT_SUPPORTED 方法不支持

export enum SIGN_DATA_ERROR_CODES { UNKNOWN_ERROR = 0, BAD_REQUEST_ERROR = 1, UNKNOWN_APP_ERROR = 100, USER_REJECTS_ERROR = 300, METHOD_NOT_SUPPORTED = 400 } export enum SEND_TRANSACTION_ERROR_CODES { UNKNOWN_ERROR = 0, BAD_REQUEST_ERROR = 1, UNKNOWN_APP_ERROR = 100, USER_REJECTS_ERROR = 300, METHOD_NOT_SUPPORTED = 400 }

完整开发示例

OmniConnect DappDemo

支持的链 ID

参见支持的主网

Last updated on