连接浏览器插件钱包

Provider API#

什么是 Injected provider API?#

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

获取注入的对象#

Dapp 可以通过如下方式访问注入的对象:

  • window.okxwallet.near - 推荐
  • window.near

连接钱包#

requestSignIn()#

/**
* @param {String} contractId contract account id
* @param {Array} methodNames methods on the contract should be allowed to be called.
* @returns { accountId, accessKey } accountId and signed in access key
*/
window.okxwallet.near.requestSignIn({ contractId = '', methodNames = []}): Promise<Result>

仅获取 accountId#

不传 contractId 和 methodNames 只会获取到用户的 NEAR 地址.

try {
  const { accountId } = window.okxwallet.near.requestSignIn();
} catch (_) {
  // something error
}

返回值示例:

{
  "accountId": "efad2c...9dae",
}

获取 accessKey#

传入 contractId 和 methodNames, 钱包会返回 accessKey

const contractId = 'wrap.near';
const methodNames = ['ft_metadata'];
try {
  const { accountId, accessKey } = window.okxwallet.near.requestSignIn({ contractId, methodNames });
} catch (_) {
  // something error
}

返回值示例:

{
    "accountId": "efad2c...9dae",
    "accessKey": {
        "secretKey": "5S9Ngi...Uku6",
        "publicKey": "ed25519:9RivAy...Hxc8"
    }
}

signOut()#

断开钱包连接

window.okxwallet.near.signOut(): void;

isSignedIn()#

判断当前账户是否处于连接中状态

window.okxwallet.near.isSignedIn(): boolean;

getAccountId()#

获取当前连接的 accountId

window.okxwallet.near.getAccountId(): string;

signMessage#

near.signMessage({ message: string, recipient: string, nonce: Buffer }): Response;

签名, 示例:

const message = {
  message: 'hello world',
  recipient: 'test.testnet',
  nonce: Buffer.from("4268ebc14ff247f5450d4a8682bec3729a06d268f83b0cb363083ab05b65486b", "hex")
}

const result = await window.okxwallet.near.signMessage(message);

返回值示例:

{
    "accountId": "efad2c...9dae",
    "publicKey": "ed25519:H8bbdL...ucKF",
    "signature": "zYbw0Z+YabpZTnYA1REkvAX5KeXt/qRgHkorYfjRR5dD5keySfFuWGMafkfi/RPUpG1EAqbUf9VFt4tTBebcDQ=="
}

合约交互#

signAndSendTransaction()#

near.signAndSendTransaction({ receiverId: string, actions: Action[]}): Response;

签名并广播交易, 示例:

const tx = {
  receiverId: 'wrap.near',
  actions: [
    {
      methodName: 'near_deposit',
      args: {},
      deposit: '1250000000000000000000',
    },
  ],
}

const result = await window.okxwallet.near.signAndSendTransaction(tx);

返回值示例:

{
    "method": "signAndSendTransaction",
    "txHash": "2bNbuT...UdSA",
    "code": 0
}

注意: dapp 需要通过 txHash 获取交易广播的结果

requestSignTransactions()#

near.requestSignTransactions({transactions: Transaction[]}): Response;

批量签名交易, 示例:

const transactions = [
  {
    receiverId: 'wrap.near',
    actions: [
      {
        methodName: 'near_deposit',
        args: {},
        deposit: '1000000000000000',
      },
    ],
  },
  {
    receiverId: 'wrap.near',
    actions: [
      {
        methodName: 'ft_transfer',
        args: {
          receiver_id: 'efad2c...9dae',
          amount: '10000000000',
        },
        deposit: '1',
      },
    ],
  },
]

const result = await window.okxwallet.near.signAndSendTransaction({ transactions });

返回值示例:

{
    "txs": [
        {
            "signedTx": "QAAAAG...kAoH",
            "txHash": "71MuUA...KVxt"
        },
        {
            "signedTx": "QAAAAG...gksH",
            "txHash": "8RHzw4...hvLN"
        }
    ],
    "code": 0,
    "method": "requestSignTransactions"
}

注意: 批量签名交易钱包只会签名, 不会广播. dapp 侧需要承接广播的逻辑.

事件#

signIn#

连接钱包成功

window.okxwallet.near.on("signIn", ((accountId) => {
  // accountId: 当前连接账户的 accountId
});

signOut#

连接钱包成功

window.okxwallet.near.on("signIn", (() => {
  // do something
});

accountChanged#

钱包侧切换了账户

window.okxwallet.near.on("accountChanged", ((accountId) => {
  // accountId: 切换后账户的 accountId
});