Connect to Extension Wallet

Provider API#

What is injected provider API?#

The OKX injected provider API is a JavaScript API that OKX injects into websites visited by our users. Your DApp can use this API to request users' accounts, read data from blockchains users are connected to, and help users sign messages and transactions.

The injected object#

DApps can access the injected object with two methods, which are:

  • window.okxwallet.starknet
  • window.starknet_okxwallet

All two attributes point to the same object, and these two methods are provided for the convenience of DApp usage.

If a DApp wishes to directly access the Starknet object injected by OKX Wallet, it can simply use window.okxwallet.starknet or window.starknet_okxwallet. This helps avoid unintentional references to Starknet objects injected by other wallets.

If a DApp utilizes third-party tool libraries like get-starknet, it'll also be fully supported.

The properties and methods of the injected object#

  1. name - string: Name of the wallet with a value of 'OKX Wallet'
  2. icon - string: Wallet icon.
  3. version - string: The version.
  4. isConnected - boolean: Properties and methods of the injected object.
  5. selectedAddress - string: The currently selected wallet address
  6. account - Account: Accesses the account object, inherited from Account of starknet.js. For specific properties and methods on the instance, please refer to the starknet.js documentation.
  7. chainId - string: Supports only the mainnet, with a value of SN_MAIN.
  8. provider - Provider: Accesses the provider object, utilizing RpcProvider of starknet.js. For specific properties and methods on the instance, please consult the starknet.js documentation.
  9. enable - () => [string]: Used for wallet connection, upon successful invocation, it'll trigger the connection page of OKX Wallet, where you can decide whether to connect to the current DApp or not. If you agree to connect, a one-item array with the selected address will be returned.
  10. on - (event, callback) => void: Add event listener
    • accountsChanged event: This event is triggered when you switch accounts, returning an array with the new address. When the connection is severed, an empty array will be returned.
  11. off - (event, callback) => void: Remove event listener

Simple example of connecting to a wallet#

async function connect() {
    if(window.okxwallet.starknet.isConnected) {
        return
    }

    try {
        const [address] = await window.okxwallet.starknet.enable()

        console.log(address)
        console.log(window.okxwallet.starknet.account)
        console.log(window.okxwallet.starknet.selectedAddress)
        console.log(window.okxwallet.starknet.isConnected)

        window.okxwallet.starknet.on('accountsChanged', ([addr]) => {
            if (addr) {
                console.log('switched address')
            } else {
                console.log('disconnected')
            }
        })
    } catch (e) {
        console.error(e)
    }
}

Calling contracts#

window.okxwallet.starknet.account.execute(transactions [, abi])

This can execute one or more calls. If there is only one call, transactions will be an object, and its contained attributes will be explained below. If there are multiple calls, there'll be an array of objects.

Parameters#

transactions structure of the object is as follows:

  • contractAddress - string: Contract address.
  • entrypoint - string: Contract entrypoint.
  • calldata - array: The calldata
  • signature - array: The signature

abi - Contract ABI (Application Binary Interface), optional.

Return value#

  • result - object
    • transaction_hash - string: Transaction hash
const transaction = {
    "contractAddress": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
    "calldata": [
        "3055261660830722006547698919883585605584552967779072711973046411977660833095",
        "100000000000000",
        "0"
    ],
    "entrypoint": "transfer"
}

const result = await window.okxwallet.starknet.account.execute(transaction)

Signing messages#

window.okxwallet.starknet.account.signMessage(data)

Parameters#

  • data - object: The object to be signed.

Return value#

  • signature - string[]: The result of the signature, which includes two items.
let data = {
   "domain": {
       "name": "OKX",
       "chainId": "SN_MAIN",
       "version": "0.0.1"
   },
   "types": {
       "StarkNetDomain": [
           {
               "name": "name",
               "type": "felt"
           }
       ],
       "Message": [
           {
               "name": "message",
               "type": "felt"
           }
       ]
   },
   "primaryType": "Message",
   "message": {
       "message": "hello"
   }
}

const [r, s] = await window.okxwallet.starknet.account.signMessage(data)

For additional properties and methods on starknet.account and starknet.provider, please refer to the starknet.js documentation.