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.
Connecting to OKX Wallet#
window.okxwallet.stacks.connect()
Description
Connect to OKX Wallet by calling window.okxwallet.stacks.connect()
When window.okxwallet.stacks.connect()
has been successfully called, the OKX Wallet connection page will be displayed. You decide whether to connect to the current DApp or not. If you agree to connect, the address
and publicKey
key will be returned.
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."}
}
Calling contracts#
window.okxwallet.stacks.signTransaction(transaction)
Parameters
- transaction - object
- stxAddress - string: The STX address of the currently connected wallet
- txType - string: Transaction type, which must be
contract_call
- contractName - string: Contract name
- contractAddress - string: Contract address
- functionName - string: Function name
- functionArgs - array<string>: Hexadecimal function call parameters
- postConditionMode - number: Whether to allow postconditions (optional)
- 1: Allow
- 2: Refuse
- postConditions - array<string>: Parameters of postconditions (optional)
- anchorMode - number: (Not required)how a transaction should get appended to the Stacks blockchain (optional)
- 1: The transaction MUST be included in an anchored block
- 2: The transaction MUST be included in a microblock
- 3: The leader can choose where to include the transaction (anchored block or microblock)
Return value
- result - object
- txHash - string: Transaction hash
- signature - string: Signature of transaction
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);
}
Transfers#
Parameters
- transaction - object
- stxAddress - string: The STX address of the currently connected wallet
- txType - string: transaction type, which must be
token_transfer
- recipient - string: Recipient address
- amount - string: Number of STX to send
- memo - string: Memo of transaction (optional)
- anchorMode - number: How a transaction should get appended to the Stacks blockchain (optional)
- 1: The transaction MUST be included in an anchored block
- 2: The transaction MUST be included in a microblock
- 3: The leader can choose where to include the transaction (anchored block or microblock)
Return value
- result - object
- txHash - string: Transaction hash
- signature - string: Signature of transaction
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);
}
Signing messages#
window.okxwallet.stacks.signMessage(data)
Parameters
- data - object
- message - string: Signed data required
Return value
- result - object
- publicKey - string: The public key that verifies the signature
- signature - string: Signature of data
try {
const data = {
message: '1234'
};
const {publicKey, signature} = await window.okxwallet.stacks.signMessage(data);
console.location({publicKey, signature});
} catch (error) {
console.log(error);
}