Skip to Content
dAppsAlgorand

Algorand

Integrate Algorand blockchain using OneKey’s provider. Access via window.$onekey.algo.

OneKey’s Algorand provider supports both legacy and ARC-0001 compliant interfaces.



Provider Detection

// Detect OneKey Algorand provider const provider = window.$onekey?.algo if (!provider) { throw new Error('OneKey Algorand provider not detected') } // Check compatibility console.log('isOneKey:', provider.isOneKey) // true

Quick Start

Connect (Legacy API)

const { address } = await provider.connect() console.log('Connected:', address) // Check connection status console.log('isConnected:', provider.isConnected) console.log('Address:', provider.address)

Enable (ARC-0001)

const result = await provider.enable({ genesisID: 'mainnet-v1.0', genesisHash: 'wGHE2Pwdvd7S12BL5FaOP20EGYesN73ktiC1qzkkit8=', }) console.log('Accounts:', result.accounts) console.log('Genesis Hash:', result.genesisHash)

Disconnect

await provider.disconnect() console.log('Disconnected')

Transactions

Sign Transactions (ARC-0001)

Sign one or more transactions:

import algosdk from 'algosdk' // Create transaction const suggestedParams = await algodClient.getTransactionParams().do() const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: senderAddress, to: recipientAddress, amount: 1000000, // 1 ALGO (microAlgos) suggestedParams, }) // Prepare for signing const walletTxns = [{ txn: Buffer.from(algosdk.encodeUnsignedTransaction(txn)).toString('base64'), }] // Sign const signedTxns = await provider.signTxns(walletTxns) console.log('Signed transactions:', signedTxns)

Sign Multiple Transactions

// Group transactions const txns = [txn1, txn2, txn3] algosdk.assignGroupID(txns) // Prepare for signing const walletTxns = txns.map(txn => ({ txn: Buffer.from(algosdk.encodeUnsignedTransaction(txn)).toString('base64'), })) // Sign all const signedTxns = await provider.signTxns(walletTxns)

Partial Signing (Multi-sig)

// Only sign specific transactions in a group const walletTxns = [ { txn: encodedTxn1 }, { txn: encodedTxn2, signers: [] }, // Skip this one { txn: encodedTxn3 }, ] const signedTxns = await provider.signTxns(walletTxns) // signedTxns[1] will be null

Post Transactions

Broadcast signed transactions:

const completeTxns = signedTxns.filter((txn) => txn !== null) if (completeTxns.length !== signedTxns.length) { throw new Error('Complete all required signatures before broadcasting the group') } const result = await provider.postTxns(completeTxns) console.log('Transaction IDs:', result.txnIDs)

Sign and Post

Sign and broadcast in one step:

const result = await provider.signAndPostTxns(walletTxns) console.log('Transaction IDs:', result.txnIDs)

Legacy Transaction API

Sign Transaction (Legacy)

// Create transaction as Uint8Array const txnBytes = algosdk.encodeUnsignedTransaction(txn) // Sign const signedTxnBytes = await provider.signTransaction([txnBytes]) // Send const { txId } = await algodClient.sendRawTransaction(signedTxnBytes[0]).do()

Sign and Send (Legacy)

const result = await provider.signAndSendTransaction([txnBytes]) console.log('Transaction ID:', result.txId)

Message Signing

Sign Arbitrary Message

const message = new TextEncoder().encode('Hello Algorand!') const { signature, address } = await provider.signMessage( message, 'utf8' // or 'hex' ) console.log('Signature:', signature) console.log('Signer:', address)

Verify Signature

import nacl from 'tweetnacl' const message = new TextEncoder().encode('Hello Algorand!') const { signature } = await provider.signMessage(message) // Get public key from address const publicKey = algosdk.decodeAddress(provider.address).publicKey const isValid = nacl.sign.detached.verify(message, signature, publicKey) console.log('Valid:', isValid)

Event Handling

Listen for Account Changes

provider.on('accountChanged', (address) => { if (address) { console.log('Account changed:', address) } else { console.log('Disconnected') } })

Listen for Connection

provider.on('connect', ({ address }) => { console.log('Connected:', address) }) provider.on('disconnect', () => { console.log('Disconnected') })

Using with Algod Client

Get Client from Provider

// Get pre-configured clients const algodTransport = await provider.getAlgodv2Client() const indexerTransport = await provider.getIndexerClient() // These are BaseHTTPClient transports, not Algodv2 or Indexer SDK instances. // Use the manual setup below for SDK methods such as status().do().

Manual Client Setup

import algosdk from 'algosdk' const algodClient = new algosdk.Algodv2( '', // Token (not needed for public nodes) 'https://mainnet-api.algonode.cloud', '' ) const indexerClient = new algosdk.Indexer( '', 'https://mainnet-idx.algonode.cloud', '' )

API Reference

Methods

MethodDescription
connect()Connect wallet (legacy)
disconnect()Disconnect wallet
enable(opts?)Enable wallet (ARC-0001)
signTxns(transactions)Sign transactions (ARC-0001)
postTxns(signedTxns)Broadcast transactions
signAndPostTxns(transactions)Sign and broadcast
signTransaction(txns)Sign transactions (legacy)
signAndSendTransaction(txns)Sign and send (legacy)
signMessage(message, encoding?)Sign arbitrary message
getAlgodv2Client()Get Algod client
getIndexerClient()Get Indexer client

Types

interface EnableOpts { genesisID?: string genesisHash?: string } interface EnableResult { genesisID: string genesisHash: string accounts: string[] } interface WalletTransaction { txn: string // Base64 encoded unsigned transaction signers?: string[] // Addresses that should sign (empty = skip) stxn?: string // Pre-signed transaction message?: string // Message to display msig?: MultisigMetadata authAddr?: string // Rekeyed auth address } type SignTxnsResult = (string | null)[]; // Base64 signed transactions; null means skipped. interface PostTxnsResult { txnIDs: string[] } interface TransactionResult { txId: string }

Properties

PropertyTypeDescription
isConnectedbooleanConnection status
addressstring | nullConnected address
isOneKeybooleanOneKey identifier

Events

EventParametersDescription
connect{ address }Wallet connected
disconnect-Wallet disconnected
accountChangedaddressAccount changed

Error Handling

try { const signedTxns = await provider.signTxns(walletTxns) } catch (error) { if (error.code === 4001) { console.log('User rejected the request') } else if (error.code === 4100) { console.log('The account or method is not authorized') } else { console.error('Error:', error.message) } }

Common Error Codes

CodeDescription
4001User rejected request
4100Unauthorized
4200Unsupported method
-32602Invalid parameters

Complete Example

import algosdk from 'algosdk' async function sendAlgo() { // Connect const provider = window.$onekey?.algo if (!provider) throw new Error("OneKey Algorand provider is unavailable") const { address } = await provider.connect() // Setup client const algodClient = new algosdk.Algodv2( '', 'https://mainnet-api.algonode.cloud', '' ) // Get params const suggestedParams = await algodClient.getTransactionParams().do() // Create transaction const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: address, to: 'RECIPIENT_ADDRESS', amount: 1000000, // 1 ALGO suggestedParams, }) // Sign const walletTxns = [{ txn: Buffer.from(algosdk.encodeUnsignedTransaction(txn)).toString('base64'), }] const signedTxns = await provider.signTxns(walletTxns) if (!signedTxns[0]) throw new Error("The transaction was not signed") // Send const { txId } = await algodClient.sendRawTransaction( Buffer.from(signedTxns[0], 'base64') ).do() console.log('Transaction ID:', txId) // Wait for confirmation await algosdk.waitForConfirmation(algodClient, txId, 4) console.log('Confirmed!') }
Last updated on