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 through the following methods:
window.okxwallet.near
- Recommendedwindow.near
Connecting to OKX Wallet#
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>
Only get the accountId#
try {
const { accountId } = window.okxwallet.near.requestSignIn();
} catch (_) {
// something error
}
Example of return value:
{
"accountId": "efad2c...9dae",
}
Get accessKey#
Request sign in with the contractId, given the needed view and change methods, you will get the access key
const contractId = 'wrap.near';
const methodNames = ['ft_metadata'];
try {
const { accountId, accessKey } = window.okxwallet.near.requestSignIn({ contractId, methodNames });
} catch (_) {
// something error
}
Example of return value:
{
"accountId": "efad2c...9dae",
"accessKey": {
"secretKey": "5S9Ngi...Uku6",
"publicKey": "ed25519:9RivAy...Hxc8"
}
}
signOut()#
Disconnect to the wallet. However, disconnection can also be initiated by the wallet in addition to the application.
window.okxwallet.near.signOut(): void;
isSignedIn()#
Check whether the current account has connected
window.okxwallet.near.isSignedIn(): boolean;
getAccountId()#
Get the accountId of current connected
window.okxwallet.near.getAccountId(): string;
signMessage#
near.signMessage({ message: string, recipient: string, nonce: Buffer }): Response;
signMessage, demo:
const message = {
message: 'hello world',
recipient: 'test.testnet',
nonce: Buffer.from("4268ebc14ff247f5450d4a8682bec3729a06d268f83b0cb363083ab05b65486b", "hex")
}
const result = await window.okxwallet.near.signMessage(message);
Example of return value:
{
"accountId": "efad2c...9dae",
"publicKey": "ed25519:H8bbdL...ucKF",
"signature": "zYbw0Z+YabpZTnYA1REkvAX5KeXt/qRgHkorYfjRR5dD5keySfFuWGMafkfi/RPUpG1EAqbUf9VFt4tTBebcDQ=="
}
Contract interaction#
signAndSendTransaction()#
near.signAndSendTransaction({ receiverId: string, actions: Action[]}): Response;
sign and send one single transaction, demo:
const tx = {
receiverId: 'wrap.near',
actions: [
{
methodName: 'near_deposit',
args: {},
deposit: '1250000000000000000000',
},
],
}
const result = await window.okxwallet.near.signAndSendTransaction(tx);
Example of return value:
{
"method": "signAndSendTransaction",
"txHash": "2bNbuT...UdSA",
"code": 0
}
Note: dapp needs to obtain the result of transaction broadcast through txHash
requestSignTransactions()#
near.requestSignTransactions({transactions: Transaction[]}): Response;
Batch sign transactions, demo:
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 });
Example of return value:
{
"txs": [
{
"signedTx": "QAAAAG...kAoH",
"txHash": "71MuUA...KVxt"
},
{
"signedTx": "QAAAAG...gksH",
"txHash": "8RHzw4...hvLN"
}
],
"code": 0,
"method": "requestSignTransactions"
}
Note: Batch signature transaction wallets only sign and do not broadcast The DAPP side needs to handle the logic of broadcasting
Events#
signIn#
The OXK wallet has connected
window.okxwallet.near.on("signIn", ((accountId) => {
// accountId: current connected accountId
});
signOut#
The OXK wallet has disconnected
window.okxwallet.near.on("signIn", (() => {
// do something
});
accountChanged#
Listen to the current account changed
window.okxwallet.near.on("accountChanged", ((accountId) => {
// accountId: the accountId after change
});