# Dapp JS API

# 属性

属性名称 描述 类型 示例
bytom2.account 当前钱包账户地址和公钥 Json Object { address: string, xpub: string, uid?: string }
bytom2.chain 当前链的类型 String 'bytom','bytom1','vapor'
bytom2.net 当前网络类型 String 'mainnet','testnet'`
bytom2.version DApp JS 版本 String '1.0'
bytom2.language 当前语言 String 'zh','en'

# 方法

# bytom2.enable

弹出钱包授权弹窗,允许授权后返回当前钱包账户和钱包设置

type Result = {
  net: 'mainnet' | 'testnet'
  chain: 'bytom' | 'bytom1' | 'vapor'
  language: 'zh' | 'en'
  /** 账户地址 **/
  address: string
  /** 账户公钥 **/
  xpub: string
}
bytom2.enable(): Promise<Result>

# bytom2.disconnect

断开钱包连接

bytom2.disconnect(): Promise<void>

# bytom2.getUserInfo

获取用户信息(只在Bycoin中有效),当获取不到用户信息时,会弹出提示窗。

type Params = {
  notbindDialog?: {
    show: boolean
    content: string
  }
}
type Result = {
  uid: string
  token: string
}

bytom2.getUserInfo(Params): Promise<Result>

# bytom2.signMessage

调用钱包签名消息

type Params = {
  message: string
}

type Result = {
  signature: string
}

bytom2.signMessage(message: Params): Promise<Result>

# bytom2.signTransaction

调用钱包签名交易。 参数和返回值均为数组,可同时签名多笔交易。

type Params = Array<{
  rawTransaction: string
  signingInstructions: object[]
  tx: object
}>

type Result = Array<{
  signatures: string[][]
}>

bytom2.signTransaction(params: Params): Promise<Result>

# bytom2.sendTransaction

调用钱包签名并发送交易

type Params = {
  /** 资产ID **/
  asset: string
  /** 发送地址 **/
  from: string
  /** 接收地址和金额,可以多个 **/
  to: {
    [address: string]: string
  }
}

type Result = {
  /** 交易哈希 **/
  hash: string
}

bytom2.sendTransaction(params: Params): Promise<Params>

示例

const result = await bytom2.sendTransaction({
  asset: 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
  from: 'tm1ql9gvemv4tjxcdk4tap65gzckmp28nszvw8ffne',
  to: {
    tm1qjtd6lp75zd69rcm9tydrf0ns68q9trhrk82q40: '0.5',
    tm1ql9gvemv4tjxcdk4tap65gzckmp28nszvw8ffne: '0.5'
  }
})

# bytom2.sendAdvancedTransaction

调用钱包发送高级交易

type Params = {
  address: string
  confirmations: number
  gas: string
  input: []
  output: []
  args?: []
}

type Result = {
  hash: string
}

bytom2.sendAdvancedTransaction(params: Params): Promise<Result>

# 事件

DApp 实现了与 Node EventEmitter 类似的4个事件 API:

bytom2.on(eventName: string, listener: function)
bytom2.off(eventName: string, listener: function)
bytom2.once(eventName: string, listener: function)
bytom2.emit(eventName: string, params)

# connect

已连接

bytom2.on('connect', (address: string) => void)

# accountChanged

切换账号时触发

type Account = {
  address: string
  xpub: string
  uid?: string
}
bytom2.on('accountChanged', (account: Account) => void)

# chainChanged

切换链时触发

bytom2.on('chainChanged', (chain: string) => void)

# message

接收到钱包消息时触发

type Response = {
  id: number,
  result: {
    code: number
    message?: string
    data?: string
  }
}
bytom2.on('message', (response: Response) => void)

# 错误处理

调用出错将返回统一的格式

type Error = Promise<{
  code: number
  message?: string
  data?: string
}>

# Code

200 请求成功

400 DApp JS 异常

403 用户拒绝授权

404 DApp JS 未实现此方法

500 钱包端异常

501 该钱包不支持 DApp

上次更新: 2021/9/7下午4:26:41