接入钱包
Provider API

Provider API#

什么是 Injected provider API?#

欧易 Injected providers API 是一个 JavaScript API,欧易将其注入用户访问的网站。您的 DApp 可以使用此 API 请求用户帐户,从用户连接的区块链读取数据,帮助用户签署消息和交易。

连接账户#

window.okxwallet.stacks.connect()

描述

通过调用 window.okxwallet.stacks.connect() 连接欧易 Web3 钱包。

当成功调用 window.okxwallet.stacks.connect(),将会唤起欧易 Web3 钱包连接钱包页面,用户可以决定是否连接当前 DApp,如果用户同意将会返回地址 (address) 和公钥 (public key)。

try {
    const response = await window.okxwallet.stacks.connect();
    console.log(response);
    // { address: string, publicKey: string }
  } catch (error) {
    console.log(error);
    // { code: 4001, message: "User rejected the request."}
  }

合约调用#

window.okxwallet.stacks.signTransaction(transaction)

参数

  • transaction - object
    • stxAddress - string: 当前连接的钱包的 stx 地址
    • txType - string: 交易类型,必须传入 contract_call
    • contractName - string: 合约名称
    • contractAddress - string: 合约地址
    • functionName - string: 函数名称
    • functionArgs - array<string>: 16进制序列化的合约调用数据
    • postConditionMode - number: (非必需)是否允许后置条件
      • 1: 允许
      • 2: 拒绝
    • postConditions - array<string>: (非必需)后置条件的参数
    • anchorMode - number: (非必需)交易上链的方式
      • 1: 交易必须被 anchored block 接收
      • 2: 交易必须被 microblock 接收
      • 3: 可以任意选择一种接收方式

返回值

  • result - object
    • txHash - string: 交易哈希
    • signature - string: 签名字符串
try {
    const transaction = {
      "stxAddress": "",
      "txType": "contract_call",
      "contractName": "amm-swap-pool-v1-1",
      "contractAddress": "SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9",
      "functionName": "swap-helper",
      "functionArgs": [
          "0616e685b016b3b6cd9ebf35f38e5ae29392e2acd51d0a746f6b656e2d77737478",
          "0616e685b016b3b6cd9ebf35f38e5ae29392e2acd51d176167653030302d676f7665726e616e63652d746f6b656e",
          "0100000000000000000000000005f5e100",
          "01000000000000000000000000000f4240",
          "0a010000000000000000000000000078b854"
      ],
      "postConditionMode": 2,
      "postConditions": [
          "000216c03b5520cf3a0bd270d8e41e5e19a464aef6294c010000000000002710",
          "010316e685b016b3b6cd9ebf35f38e5ae29392e2acd51d0f616c65782d7661756c742d76312d3116e685b016b3b6cd9ebf35f38e5ae29392e2acd51d176167653030302d676f7665726e616e63652d746f6b656e04616c657803000000000078b854"
      ],
      "anchorMode": 3,
    };
    const {txHash, signature} = await window.okxwallet.stacks.signTransaction(transaction);
    console.location({txHash, signature});
  } catch (error) {
    console.log(error);
  }

转账交易#

window.okxwallet.stacks.signTransaction(transaction)

参数

  • transaction - object
    • stxAddress - string: 当前连接的钱包的 stx 地址
    • txType - string: 交易类型,必须传入 token_transfer
    • recipient - string: 接收地址
    • amount - stirng: 发送的数量
    • memo - stirng: (非必需)备注信息
    • anchorMode - number: (非必需)交易上链的方式
      • 1: 交易必须被 anchored block 接收
      • 2: 交易必须被 microblock 接收
      • 3: 可以任意选择一种接收方式

返回值

  • result - object
    • txHash - string: 交易哈希
    • signature - string: 签名字符串
try {
    const transaction = {
      stxAddress: '',
      txType: 'token_transfer',
      recipient: '',
      amount: '10000',
      memo: 'test'
    };
    const {txHash, signature} = await window.okxwallet.stacks.signTransaction(transaction);
    console.location({txHash, signature});
  } catch (error) {
    console.log(error);
  }

签名消息#

window.okxwallet.stacks.signMessage(data)

参数

  • data - object
    • message - string: 需要签名的数据

返回值

  • result - object
    • publicKey - string: 验签的公钥
    • signature - string: 签名字符串
try {
    const data = {
      message: '1234'
    };
    const {publicKey, signature} = await window.okxwallet.stacks.signMessage(data);
    console.location({publicKey, signature});
  } catch (error) {
    console.log(error);
  }