Make sure to update to version 6.96.0 or later to start integrating OKX Connect into your DApp can be done using npm:
npm install @okxconnect/universal-provider
Before connecting to a wallet, you need to create an object for subsequent operations such as connecting to the wallet and sending transactions.
OKXUniversalProvider.init({dappMetaData: {name, icon}})
Request parameters
Return Value
Example
import { OKXUniversalProvider } from "@okxconnect/universal-provider";
const okxUniversalProvider = await OKXUniversalProvider.init({
dappMetaData: {
name: "application name",
icon: "application icon url"
},
})
Connect to the wallet to get the wallet address as an identifier and the necessary parameters for signing transactions.
okxUniversalProvider.connect(connectParams: ConnectParams);
Request Parameters
Return value
<SessionTypes.Struct | undefined>
Record<string, Namespace>
; namespace information for a successful connection;
var session = await okxUniversalProvider.connect({
namespaces: {
tron: {
chains: [
"tron:mainnet",
],
}
},
sessionConfig: {
redirect: "tg://resolve"
}
})
First create an OKXTronProvider object, with the constructor passing in OKXUniversalProvider
import { OKXTronProvider } from ‘@okxconnect/universal-provider’ ;
let okxTronProvider = new OKXTronProvider(okxUniversalProvider)
okxTronProvider.getAccount(chainId?)
Request parameters
Return value
address: string wallet address, ****Return Value
Example
let result = okxTronProvider.getAccount(‘tron:mainnet’)
// Return structure
{
"address": "THyDJCGXYnwCSYNQeGYW98pptEVSHwaYx7"
}
okxTronProvider.signMessage(message, chainId?)
Request Parameters
Return Value
Example
let chainId = "tron:mainnet"
let signStr = "data need to sign ..."
let result = okxTronProvider.signMessage(signStr, chainId)
//返回:0xfc9003b1c8e68fdc93409aad911af274de1987130a36516f1c7c9353716463bf42bb400e0d6bffd4adface92dd3a01079ba32f8aebe3db1d5914f084b9f802711c
okxTronProvider.signMessageV2(message, chainId?)
Request Parameters
Return Value
Example
let chainId = "tron:mainnet"
let signStr = "data need to sign ..."
let result = okxTronProvider.signMessageV2(signStr, chainId)
//Return:0xfc9003b1c8e68fdc93409aad911af274de1987130a36516f1c7c9353716463bf42bb400e0d6bffd4adface92dd3a01079ba32f8aebe3db1d5914f084b9f802711c
okxTronProvider.signTransaction(transaction: any, chainId?: string)
Request parameters
transaction - object, transaction information, signed in a fixed format, can be generated by TronWeb.transactionBuilder.
chainId? - string, the chain in which the request signature is executed, not mandatory, e.g. tron:mainnet Return Value
Promise - Object signed transaction
Example
let tronWeb = new TronWeb({
"fullHost": 'https://api.trongrid.io',
"headers": {},
"privateKey": ''
})
let address = okxTronProvider.getAccount("tron:mainnet").address
const transaction = await tronWeb.transactionBuilder.sendTrx("TGBcVLMnVtvJzjPWZpPiYBgwwb7th1w3BF", 1000, address);
let res = await okxTronProvider.signTransaction(transaction,"tron:mainnet")
/**Return results
{
"visible": true,
"txID": "cf93bbfb0152d832fcdb1c65cb12a979eab5a631de1b3d7d6437757e1b16ed40",
"raw_data": {
"contract": [{
"parameter": {
"type_url": "type.googleapis.com/protocol.TransferContract",
"value": {
"amount": 1000,
"contract_address": "",
"owner_address": "THyDJCGXYnwCSYNQeGYW98pptEVSHwaYx7",
"to_address": "TGBcVLMnVtvJzjPWZpPiYBgwwb7th1w3BF"
}
},
"type": "TransferContract"
}],
"expiration": 1732073850000,
"ref_block_bytes": "7ecf",
"ref_block_hash": "7b3a6bc87d9edb9e",
"timestamp": 1732073790000
},
"raw_data_hex": "0a027ecf22087b3a6bc87d9edb9e40908996bdb4325a66080112620a2d747970652e676f6f676c65617069732e636f6d2f70726f746f636f6c2e5472616e73666572436f6e747261637412310a154157c140be01fa2bbabf7f055ab879d0c05725293c12154144295a45f811a9d595562562a2e27685291a715818e80770b0b492bdb432",
"signature": ["239b402a7605199c6969f6f4da37a355452bd942c222adfc625721d18a1fff3223f92c1d8eaf5856c0e41ce80761fd2adb80d026276d6710ad183a713af7a78d00"]
}
*/
okxTronProvider.signAndSendTransaction(transaction, chainId?)
Request parameters
Return Value
Example
let tronWeb = new TronWeb({
"fullHost": 'https://api.trongrid.io',
"headers": {},
"privateKey": ''
})
let address = okxTronProvider.getAccount("tron:mainnet").address
const transaction = await tronWeb.transactionBuilder.sendTrx("TGBcVLMnVtvJzjPWZpPiYBgwwb7th1w3BF", 1000, address); //转账TRX
let res = await okxTronProvider.signAndSendTransaction(transaction, "tron:mainnet")
//Return Value:50a47e450024c079510a39433e28de0bcac8406d731aadab7d772998dfce2aab
Disconnect the connected wallet and delete the current session. If you want to switch the connected wallet, please disconnect the current wallet first.
okxUniversalProvider.disconnect()