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.
Special Notes#
OKX Wallet's TON API is fully compliant with the Ton Connect protocol.
Dapps can use the TON Connect SDK to more easily integrate with OKX Wallet.
Getting the Injected Object#
OKX Wallet injects the following properties into Dapps according to the TON Connect protocol specification:
window.okxTonWallet.tonconnect
The data structure of the object it points to is as follows:
interface TonConnectBridge {
deviceInfo: DeviceInfo;
walletInfo?: WalletInfo;
protocolVersion: number;
connect(protocolVersion: number, message: ConnectRequest): Promise<ConnectEvent>;
restoreConnection(): Promise<ConnectEvent>;
send(message: AppRequest): Promise<WalletResponse>;
listen(callback: (event: WalletEvent) => void): () => void;
}
deviceInfo#
To obtain device information, the data structure is as follows:
{
platform: 'browser',
appName: 'OKX Wallet',
appVersion: '3.3.19',
maxProtocolVersion: 2,
features: [
'SendTransaction',
{
name: 'SendTransaction',
maxMessages: 4,
},
],
}
platform
: Device platformappName
: Wallet nameappVersion
: Wallet versionmaxProtocolVersion
: Supported maximum protocol versionfeatures
: Features supported by the wallet
walletInfo#
To obtain wallet information, the data structure is as follows:
{
name: 'OKX Wallet',
app_name: 'okxTonWallet',
image: 'https://static.okx.com/cdn/assets/imgs/247/58E63FEA47A2B7D7.png',
about_url: 'https://www.okx.com/web3',
platforms: ['chrome', 'firefox', 'safari'],
}
name
: Wallet nameapp_name
: Unique identifier for the wallet applicationimage
: Wallet iconabout_url
: Wallet introduction pageplatforms
: Platforms supported by the wallet
protocolVersion#
The version of Ton Connect supported by OKX Wallet is currently 2
connect#
Method to connect the wallet. During the connection, the wallet can also be verified with a signature:
connect(protocolVersion: number, message: ConnectRequest): Promise<ConnectEvent>;
Parameters#
protocolVersion
: The version of Ton Connect that the Dapp expects the wallet to support. If the wallet does not support this version, an error will be returned immediately.message
: Connection request information
message parameter
type ConnectRequest = {
manifestUrl: string;
items: ConnectItem[], // Data items shared with the application
}
type ConnectItem = TonAddressItem | TonProofItem
type TonAddressItem = {
name: "ton_addr";
}
type TonProofItem = {
name: "ton_proof";
payload: string; // Arbitrary payload, such as nonce + expiration timestamp.
}
manifestUrl
: The URL of the Dapp's manifest.json file, which contains the Dapp's metadata, with the following data structure:{ "url": "<app-url>", // Required "name": "<app-name>", // Required "iconUrl": "<app-icon-url>", // Required "termsOfUseUrl": "<terms-of-use-url>", // Optional "privacyPolicyUrl": "<privacy-policy-url>" // Optional }
items
: List of instructions requested from the wallet, currently supporting two instructions:ton_addr
: Obtain the user's address, public key, and other informationton_proof
: Verify the wallet with a signature
Return value#
Returns a Promise object, with the result being ConnectEvent
and the following data structure:
type ConnectEvent = ConnectEventSuccess | ConnectEventError;
type ConnectEventSuccess = {
event: "connect";
id: number; // increasing event counter
payload: {
items: ConnectItemReply[];
device: DeviceInfo;
}
}
type ConnectEventError = {
event: "connect_error",
id: number; // increasing event counter
payload: {
code: number;
message: string;
}
}
// Identical to the deviceInfo on the window.okxTonWallet.tonconnect object
type DeviceInfo = {
platform: "iphone" | "ipad" | "android" | "windows" | "mac" | "linux";
appName: string;
appVersion: string;
maxProtocolVersion: number;
features: Feature[];
}
type Feature = { name: 'SendTransaction', maxMessages: number } // `maxMessages` is maximum number of messages in one `SendTransaction` that the wallet supports
type ConnectItemReply = TonAddressItemReply | TonProofItemReply;
// Untrusted data returned by the wallet.
// If you need a guarantee that the user owns this address and public key, you need to additionally request a ton_proof.
type TonAddressItemReply = {
name: "ton_addr";
address: string; // TON address raw (`0:<hex>`)
network: NETWORK; // network global_id
publicKey: string; // HEX string without 0x
walletStateInit: string; // Base64 (not url safe) encoded stateinit cell for the wallet contract
}
type TonProofItemReply = {
name: "ton_proof";
proof: {
timestamp: string; // 64-bit unix epoch time of the signing operation (seconds)
domain: {
lengthBytes: number; // AppDomain Length
value: string; // app domain name (as url part, without encoding)
};
signature: string; // base64-encoded signature
payload: string; // payload from the request
}
}
// Currently supports only the mainnet
enum NETWORK {
MAINNET = '-239',
TESTNET = '-3'
}
Example#
Just to obtain the user's address, public key, and other information:
const result = await window.okxTonWallet.tonconnect.connect(2, {
manifestUrl: 'https://example.com/manifest.json',
items: [{ name: 'ton_addr' }]
})
if (result.event === 'connect') {
console.log(result.payload.items[0].address)
} else {
console.log(result.payload.message)
}
Obtain the user's address, public key, and other information, and verify the wallet with a signature:
const result = await window.okxTonWallet.tonconnect.connect(2, {
manifestUrl: 'https://example.com/manifest.json',
items: [
{ name: 'ton_addr' },
{ name: 'ton_proof', payload: '123' }
]
})
if(result.event === 'connect') {
console.log(result.payload.items[0].address)
console.log(result.payload.items[1].proof)
} else {
console.log(result.payload.message)
}
restoreConnection#
Method to restore the connection, only returns the result of the ton_addr
instruction. If the wallet cannot be connected, an error is returned.
restoreConnection(): Promise<ConnectEvent>;
Example#
const result = await window.okxTonWallet.tonconnect.restoreConnection()
if(result.event === 'connect') {
console.log(result.payload.items[0].address)
} else {
console.log(result.payload.message)
}
send#
Method to send a message to the wallet.
send(message: AppRequest): Promise<WalletResponse>;
Parameters#
message
: Message body sent to the wallet
message parameter
interface AppRequest {
method: string;
params: string[];
id: string;
}
method
: Name of the message, currently supportssendTransaction
anddisconnect
params
: Parameters of the messageid
: Incremental identifier to match requests and responses
sendTransaction message#
Used to sign and broadcast transactions.
Parameters:
interface SendTransactionRequest {
method: 'sendTransaction';
params: [<transaction-payload>];
id: string;
}
Where <transaction-payload>
is JSON with following properties:
valid_until
(integer, optional): unix timestamp. after th moment transaction will be invalid.network
(NETWORK, optional): Currently supports only the mainnetfrom
(string in wc:hex format, optional): The sender address from which DAppintends to send the transaction.messages
(array of messages): 1-4 outgoing messages from the wallet contract to other accounts. All messages are sent out in order, however the wallet cannot guarantee that messages will be delivered and executed in same order.
Message structure:
address
(string): message destinationamount
(decimal string): number of nanocoins to send.payload
(string base64, optional): raw one-cell BoC encoded in Base64.stateInit
(string base64, optional): raw once-cell BoC encoded in Base64.
Example:
{
"valid_until": 1658253458,
"network": "-239",
"from": "0:348bcf827469c5fc38541c77fdd91d4e347eac200f6f2d9fd62dc08885f0415f",
"messages": [
{
"address": "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F",
"amount": "20000000",
"stateInit": "base64bocblahblahblah==" //deploy contract
},{
"address": "0:E69F10CC84877ABF539F83F879291E5CA169451BA7BCE91A37A5CED3AB8080D3",
"amount": "60000000",
"payload": "base64bocblahblahblah==" //transfer nft to new deployed account 0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F
}
]
}
Return value:
type SendTransactionResponse = SendTransactionResponseSuccess | SendTransactionResponseError;
interface SendTransactionResponseSuccess {
result: <boc>;
id: string;
}
interface SendTransactionResponseError {
error: { code: number; message: string };
id: string;
}
Where result is the signed signature string.
disconnect message#
Used to disconnect the wallet.
Parameters:
interface DisconnectRequest {
method: 'disconnect';
params: [];
id: string;
}
Return value:
type DisconnectResponse = DisconnectResponseSuccess | DisconnectResponseError;
interface DisconnectResponseSuccess {
result: {};
id: string;
}
interface DisconnectResponseError {
error: { code: number; message: string };
id: string;
}
listen#
Method to listen to wallet events.
listen(callback: (event: WalletEvent) => void): () => void;
Parameters#
callback
: method to listen to wallet events.
interface WalletEvent {
event: WalletEventName;
id: number; // increasing event counter
payload: <event-payload>; // specific payload for each event
}
type WalletEventName = 'connect' | 'connect_error' | 'disconnect';
Return value#
Returns a function to cancel the listening.
on / off#
Add/remove event listeners. Currently supported events include:
connect
: This event is triggered when the wallet is connected.disconnect
: This event is triggered when the user disconnects.accountChanged
: This event is triggered when the user switches accounts.
const accountChanged = () => {}
window.okxTonWallet.tonconnect.on('accountChanged', accountChanged)
window.okxTonWallet.tonconnect.off('accountChanged', accountChanged)