Starknet Wallet Integration: Connect to App or Mini Wallet with DEX API

ยท

Installation and Initialization

To begin integrating OKX Connect into your decentralized application (DApp), ensure you have the OKX App updated to version 6.98.0 or later. You can install the necessary package via npm:

npm install okx-connect

Initializing the Wallet Interface

Before connecting to a wallet, create an object that provides a UI interface for wallet operations like connecting and sending transactions.

Request Parameters:

Return Value:

Example:

const connectUI = new OKXUniversalConnectUI({
  dappMetaData: {
    name: "My DApp",
    icon: "https://example.com/icon.png"
  },
  actionsConfiguration: {
    modals: ['before', 'error'],
    returnStrategy: 'tg://resolve'
  },
  theme: THEME.DARK
});

Connecting to a Wallet

Establish a connection to fetch wallet addresses and transaction signing parameters.

Request Parameters:

Return Value:

Example:

const session = await connectUI.connect({
  namespaces: { starknet: { chains: ['starknet:mainnet'] } },
  sessionConfig: { redirect: 'tg://resolve' }
});

Transaction Preparation

Create an OKXStarknetProvider instance using OKXUniversalProvider:

const provider = new OKXStarknetProvider(OKXUniversalProvider);

Account Information

Fetch wallet details for a specific chain.

Request Parameters:

Return Value:

Example:

const account = await provider.getAccount('starknet:mainnet');

Message Signing

Sign a message with structured data.

Request Parameters:

Return Value:

Example:

const signature = await provider.signMessage(
  '0x123...',
  { domain: {...}, types: {...}, message: {...} }
);

Sending Transactions

Submit a transaction to the network.

Request Parameters:

Return Value:

Example:

const txHash = await provider.sendTransaction(
  '0x123...',
  { calldata: [...], contractAddress: '0x...' }
);

Disconnecting a Wallet

Terminate the current session to switch wallets.

await connectUI.disconnect();

Event Handling

Listen for wallet events (e.g., connection changes).

provider.on('accountsChanged', (accounts) => {
  console.log('Accounts updated:', accounts);
});

Error Codes

Common exceptions during wallet operations:

CodeDescription
UNKNOWN_ERRORUnexpected error.
ALREADY_CONNECTED_ERRORWallet already linked.
USER_REJECTS_ERRORUser declined the request.
CHAIN_NOT_SUPPORTEDUnsupported blockchain.

๐Ÿ‘‰ Explore advanced wallet integrations for more features.


FAQ

How do I handle unsupported chains?

Verify the chain ID matches starknet:mainnet. Use optionalNamespaces for fallback chains.

Can I customize the wallet UI theme?

Yes! Set uiPreferences.theme to THEME.DARK, THEME.LIGHT, or 'SYSTEM'.

What if the user rejects a transaction?

Catch USER_REJECTS_ERROR and prompt a retry or alternative action.

๐Ÿ‘‰ Learn more about secure transactions in our developer docs.