请确保更新到 6.96.0或以后版本,即可开始接入:将 OKX Connect 集成到您的 DApp 中,可以使用 npm:
npm install @okxconnect/universal-provider
连接钱包之前,需要先创建一个对象,用于后续连接钱包、发送交易等操作。
OKXUniversalProvider.init({dappMetaData: {name, icon}})
请求参数
返回值
示例
import { OKXUniversalProvider } from "@okxconnect/universal-provider";
const okxUniversalProvider = await OKXUniversalProvider.init({
dappMetaData: {
name: "application name",
icon: "application icon url"
},
})
连接钱包去获取钱包地址,作为标识符和用于签名交易的必要参数;
okxUniversalProvider.connect(connectParams: ConnectParams);
请求参数
返回值
<SessionTypes.Struct | undefined>
Record<string, Namespace>
; 成功连接的namespace 信息;
var session = await okxUniversalProvider.connect({
namespaces: {
tron: {
chains: [
"tron:mainnet",
],
}
},
sessionConfig: {
redirect: "tg://resolve"
}
})
首先创建一个OKXTronProvider对象,构造函数传入OKXUniversalProvider
import { OKXTronProvider } from "@okxconnect/universal-provider";
let okxTronProvider = new OKXTronProvider(okxUniversalProvider)
okxTronProvider.getAccount(chainId?)
请求参数
返回值
address: string 钱包地址,
示例
let result = okxTronProvider.getAccount("tron:mainnet")
//返回结构
{
"address": "THyDJCGXYnwCSYNQeGYW98pptEVSHwaYx7"
}
okxTronProvider.signMessage(message, chainId?)
请求参数
返回值
示例
let chainId = "tron:mainnet"
let signStr = "data need to sign ..."
let result = okxTronProvider.signMessage(signStr, chainId)
//返回:0xfc9003b1c8e68fdc93409aad911af274de1987130a36516f1c7c9353716463bf42bb400e0d6bffd4adface92dd3a01079ba32f8aebe3db1d5914f084b9f802711c
okxTronProvider.signMessageV2(message, chainId?)
请求参数
返回值
示例
let chainId = "tron:mainnet"
let signStr = "data need to sign ..."
let result = okxTronProvider.signMessageV2(signStr, chainId)
//返回:0xfc9003b1c8e68fdc93409aad911af274de1987130a36516f1c7c9353716463bf42bb400e0d6bffd4adface92dd3a01079ba32f8aebe3db1d5914f084b9f802711c
okxTronProvider.signTransaction(transaction: any, chainId?: string)
请求参数
transaction - object,交易信息 按照固定格式签名,可通过TronWeb.transactionBuilder生成
chainId? - string, 请求签名执行的链,非必传参数, 如tron:mainnet 返回值
Promise - Object 签名后的交易
示例
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")
/**返回结果
{
"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?)
请求参数
返回值
示例
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")
//返回值:50a47e450024c079510a39433e28de0bcac8406d731aadab7d772998dfce2aab
断开已连接钱包,并删除当前会话,如果要切换连接钱包,请先断开当前钱包。
okxUniversalProvider.disconnect()