Introduction
This comprehensive guide provides step-by-step instructions for integrating Cosmos SDK into your application or mini wallet, covering installation, wallet connection, transaction signing, and error handling.
Installation and Initialization
To integrate OKX Connect into your DApp using npm:
npm install @okx-web3/connect@^6.94.0
Required Parameters:
dappMetaData
(object):name
(string): Your application name (non-unique identifier)icon
(string): URL to 180x180px PNG/ICO icon (SVG not supported)
Returns: OKXUniversalProvider
instance
Example:
const provider = new OKXUniversalProvider({
dappMetaData: {
name: "My Cosmos DApp",
icon: "https://example.com/icon.png"
}
});
๐ Need help with wallet integration?
Connecting to a Wallet
Establish connection to retrieve wallet address and signing parameters.
Parameters:
connectParams
(object):namespaces
(object): Required connection info (key: 'cosmos')optionalNamespaces
(object): Additional supported chainssessionConfig
(object):redirect
(string): Post-connection deeplink (e.g., Telegram'stg://resolve
)
Returns:
Promise containing:
- Session topic
- Namespace details
- Chain/account information
- Supported methods
- Default chain
Example:
await provider.connect({
namespaces: {
cosmos: {
chains: ["cosmos:cosmoshub-4"],
methods: ["cosmos_signAmino"]
}
}
});
Wallet Connection Status
Check current wallet connection state.
Returns: boolean
(true if connected)
Example:
const isConnected = provider.isConnected();
Transaction Preparation
Create OKXCosmosProvider
instance for transaction operations:
const cosmosProvider = new OKXCosmosProvider(provider);
๐ Explore advanced transaction features
Account Information Retrieval
Get wallet details for specified chain.
Parameters:
chainId
(string): e.g., "cosmos:cosmoshub-4"
Returns:
Object containing:
- Algorithm type
- Wallet address (both raw and bech32 formats)
- Public key
Example:
const account = await cosmosProvider.getAccount("cosmos:cosmoshub-4");
Message Signing
Sign arbitrary messages with wallet credentials.
Parameters:
chain
(string): Target chainsignerAddress
(string): Wallet addressmessage
(string): Content to sign
Returns:
Promise with:
- Public key details
- Signature result
Example:
const signature = await cosmosProvider.signMessage(
"cosmos:cosmoshub-4",
"cosmos1...",
"Hello Cosmos!"
);
SignAmino Method
Sign transactions using Amino format.
Parameters:
chainId
(string): Target chainsignerAddress
(string): Wallet addresssignDoc
(object): Transaction data in Amino format
Returns:
Promise with signed transaction and signature
Example:
const signedTx = await cosmosProvider.signAmino(
"cosmos:cosmoshub-4",
"cosmos1...",
{ /* signDoc */ }
);
SignDirect Method
Sign transactions using Direct format.
Parameters:
chainId
(string): Target chainsignerAddress
(string): Wallet addresssignDoc
(object): Contains:bodyBytes
(Uint8Array)authInfoBytes
(Uint8Array)chainId
(string)accountNumber
(string)
Returns:
Promise with signed transaction and signature
Disconnecting Wallets
Terminate current session:
await provider.disconnect();
Event Handling
Listen for connection state changes:
provider.on("connect", (session) => {
console.log("Wallet connected:", session);
});
Error Codes Reference
Code | Description |
---|---|
1000 | Unknown Error |
1001 | Wallet Already Connected |
1002 | Wallet Not Connected |
3000 | User Rejected |
4000 | Method Not Supported |
4001 | Chain Not Supported |
4002 | Wallet Not Supported |
5000 | Connection Error |
FAQ
Q: How do I switch between connected wallets?
A: Always disconnect the current wallet first using provider.disconnect()
before connecting a new one.
Q: Can I use SVG icons for my DApp?
A: No, only PNG or ICO formats (180x180px recommended) are supported.
Q: What's the difference between SignAmino and SignDirect?
A: SignAmino uses human-readable JSON format while SignDirect uses binary encoding for better efficiency.
Q: How do I handle unsupported chains?
A: The wallet will automatically reject connections with unsupported chains specified in namespaces
.
Q: Can I customize the post-connection redirect?
A: Yes, through the sessionConfig.redirect
parameter (supports deeplinks).
Q: What happens if a user rejects the connection?
A: The promise will reject with error code 3000 ("USER_REJECTS_ERROR").
This Markdown document follows SEO best practices with:
- Natural keyword integration ("Cosmos SDK", "wallet integration", "DEX API")
- Structured hierarchy with H2/H3 headings
- Concise code examples
- FAQ section for common queries
- Optimized anchor texts linking to OKX