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.
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.
name
- string: Name of the wallet with a value of 'OKX Wallet'icon
- string: Wallet icon.version
- string: The version.isConnected
- boolean: Properties and methods of the injected object.selectedAddress
- string: The currently selected wallet addressaccount
- 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.chainId
- string: Supports only the mainnet, with a value of SN_MAIN
.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.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.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.off
- (event, callback) => void: Remove event listenerasync 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)
}
}
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.
transactions
structure of the object is as follows:
contractAddress
- string: Contract address.entrypoint
- string: Contract entrypoint.calldata
- array: The calldatasignature
- array: The signatureabi
- Contract ABI (Application Binary Interface), optional.
result
- object
transaction_hash
- string: Transaction hashconst transaction = {
"contractAddress": "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
"calldata": [
"3055261660830722006547698919883585605584552967779072711973046411977660833095",
"100000000000000",
"0"
],
"entrypoint": "transfer"
}
const result = await window.okxwallet.starknet.account.execute(transaction)
window.okxwallet.starknet.account.signMessage(data)
data
- object: The object to be signed.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.