Skip to Content
dAppsAptos

Aptos

Integrate Aptos blockchain using OneKey’s Petra-compatible provider. Access via window.$onekey.aptos.

OneKey exposes Petra-compatible methods and an Aptos Wallet Standard adapter. Their method signatures differ; use the interface documented for your integration.



Provider Detection

// Detect OneKey Aptos provider const provider = window.$onekey?.aptos if (!provider) { throw new Error('OneKey Aptos provider not detected') } // Check provider info console.log('Provider:', provider.isOneKey ? 'OneKey' : 'Unknown')

Quick Start

Connect Wallet

// Connect and get account info const response = await provider.connect() console.log({ address: response.address, // Account address publicKey: response.publicKey, // Public key })

Check Connection Status

const isConnected = await provider.isConnected() console.log('Connected:', isConnected)

Disconnect

await provider.disconnect()

Account Management

Get Current Account

const account = await provider.account() console.log({ address: account.address, publicKey: account.publicKey, })

Get Network Info

const network = await provider.getNetwork() console.log({ name: network.name, // 'mainnet', 'testnet', 'devnet' chainId: network.chainId, // Network chain ID url: network.url, // RPC URL })

Listen for Account Changes

provider.onAccountChange((newAccount) => { if (newAccount) { console.log('Account changed:', newAccount) } else { console.log('Wallet disconnected') } })

Listen for Network Changes

provider.onNetworkChange((network) => { console.log('Network changed:', network) })

Transactions

Sign and Submit Transaction

// Simple APT transfer const payload = { type: 'entry_function_payload', function: '0x1::aptos_account::transfer', type_arguments: [], arguments: [ '0x1234...', // Recipient address '100000000', // Amount in Octas (1 APT = 100000000 Octas) ], } const response = await provider.signAndSubmitTransaction(payload) console.log('Transaction hash:', response.hash)

Sign Transaction (Without Submitting)

const payload = { type: 'entry_function_payload', function: '0x1::coin::transfer', type_arguments: ['0x1::aptos_coin::AptosCoin'], arguments: ['0x1234...', '100000000'], } const signedTxn = await provider.signTransaction(payload) // Submit later using Aptos SDK // const pendingTxn = await client.submitTransaction(signedTxn)

Sign Transaction V2 (BCS Serialized)

For advanced use cases with BCS-serialized transactions:

const rawTransaction = '<BCS_SERIALIZED_TRANSACTION>'; // Use the encoding expected by your wallet version const signedTxn = await provider.signTransactionV2({ transaction: rawTransaction, transactionType: 'simple' })

Transaction Options

const payload = { type: 'entry_function_payload', function: '0x1::aptos_account::transfer', type_arguments: [], arguments: ['0x1234...', '100000000'], } // The legacy direct method takes only a payload. const response = await provider.signAndSubmitTransaction(payload) // For gas options, use signAndSubmitTransactionV2 with the matching Wallet Standard input.

Message Signing

Sign Message

Sign arbitrary messages for authentication or verification:

const message = 'Welcome to MyApp!\n\nClick to sign in.\n\nNonce: abc123' const response = await provider.signMessage({ message, nonce: 12345, // Example only; obtain a single-use challenge from your server }) console.log({ signature: response.signature, // Hex-encoded signature fullMessage: response.fullMessage, // Full signed message prefix: response.prefix, // Message prefix used })

Verify Message (Off-chain)

import nacl from 'tweetnacl' const { signature, fullMessage } = await provider.signMessage({ message: 'Hello Aptos', nonce: 12345, }) const account = await provider.account() const pubKeyBytes = Buffer.from(account.publicKey.slice(2), 'hex') const signatureBytes = Buffer.from(signature.slice(2), 'hex') const messageBytes = Buffer.from(fullMessage) const isValid = nacl.sign.detached.verify(messageBytes, signatureBytes, pubKeyBytes) console.log('Signature valid:', isValid)

Smart Contract Interaction

Call View Function

// View functions don't require signing const result = await client.view({ function: '0x1::coin::balance', type_arguments: ['0x1::aptos_coin::AptosCoin'], arguments: [accountAddress], }) console.log('Balance:', result[0])

Call Entry Function

// Mint NFT example const payload = { type: 'entry_function_payload', function: '0x3::token::create_collection_script', type_arguments: [], arguments: [ 'My Collection', // Collection name 'Description', // Description 'https://example.com', // URI '1000', // Maximum supply [false, false, false], // Mutability config ], } const response = await provider.signAndSubmitTransaction(payload)

API Reference

Methods

MethodDescription
connect()Connect wallet and get account
disconnect()Disconnect wallet
isConnected()Check connection status
account()Get current account info
network()Get the current network name as a string
signMessage(request)Sign arbitrary message
signAndSubmitTransaction(payload)Sign and submit transaction
signTransaction(payload)Sign transaction without submitting
signTransactionV2(params)Sign BCS-serialized transaction
onAccountChange(callback)Listen for account changes
onNetworkChange(callback)Listen for network changes

Types

interface AptosAccount { address: string // Hex address with 0x prefix publicKey: string // Ed25519 public key } interface AptosNetwork { name: string // Network name chainId: number // Chain ID url: string // RPC URL } interface SignMessageRequest { message: string // Message to sign nonce: number // Required by the direct provider API address?: boolean // Include address in message application?: boolean // Include app info chainId?: boolean // Include chain ID } interface SignMessageResponse { signature: string // Hex signature fullMessage: string // Complete signed message prefix: string // APTOS prefix address?: string // Signer address application?: string // Application info chainId?: number // Chain ID nonce: number // Nonce used } interface TransactionPayload { type: 'entry_function_payload' | 'script_payload' | 'module_bundle_payload' function: string // Module::function format type_arguments: string[] arguments: any[] }

Supported Networks

NetworkDescription
MainnetProduction network
TestnetTesting environment
DevnetDevelopment environment

Error Handling

try { await provider.connect() } catch (error) { switch (error.code) { case 4001: console.log('User rejected the request') break case 4100: console.log('The account or method is not authorized') break case 4200: console.log('Unsupported method') break default: console.error('Error:', error.message) } }

Common Error Codes

CodeDescription
4001User rejected request
4100Unauthorized
4200Unsupported method
4201Unsupported network
-32603Internal error

Using with Aptos Wallet Adapter

For React applications, use the official Aptos Wallet Adapter:

npm install @aptos-labs/wallet-adapter-react
import { AptosWalletAdapterProvider } from '@aptos-labs/wallet-adapter-react' function App() { return ( <AptosWalletAdapterProvider> <YourApp /> </AptosWalletAdapterProvider> ) }

Use a Wallet Standard-compatible adapter version and verify that OneKey appears in its discovered wallet list.


Migration from Petra

OneKey’s Aptos provider is Petra-compatible. Update your provider detection:

// Before (Petra only) const provider = window.petra // After (OneKey with Petra fallback) const provider = window.$onekey?.aptos || window.petra
Last updated on