Authentication overview
Authentication on the Algorand blockchain operates on a two-tiered system: authenticating on-chain transactions and authenticating access to Algorand nodes. On-chain authentication is fundamentally based on cryptographic digital signatures. Each Algorand account is secured by a pair of public and private keys. The public key serves as the account's address, while the private key is used to sign transactions, proving ownership and authorizing the transfer of assets or execution of smart contracts.
For off-chain interactions, such as querying blockchain data or submitting signed transactions to an Algorand node (like algod or indexer), an API token is typically required. This token acts as a credential to authenticate the application or user making the request to the node's RPC (Remote Procedure Call) endpoint. This separation ensures that sensitive private keys remain offline and are only used for signing, while API tokens manage access to infrastructure components.
Algorand's design prioritizes decentralization and security, placing the responsibility for private key management squarely with the user or application developer. The network itself validates the cryptographic signatures rather than managing user identities directly. This approach aligns with the principles of blockchain technology, where identity is tied to cryptographic proof of ownership rather than centralized identity providers.
Supported authentication methods
Algorand employs distinct authentication methods depending on whether the interaction is directly with the blockchain (on-chain) or with a node's API (off-chain).
On-chain authentication: Digital signatures
All transactions submitted to the Algorand network must be cryptographically signed by the sender's private key. This signature proves that the transaction was authorized by the account owner. Algorand uses the Ed25519 digital signature algorithm, a curve-based public-key signature system. The process involves:
- Constructing a raw transaction object (e.g., payment, asset transfer, application call).
- Hashing the transaction object.
- Signing the hash with the sender's private key.
- Broadcasting the signed transaction to the Algorand network.
The network then verifies the signature against the sender's public key (account address). If valid, the transaction is processed; otherwise, it is rejected.
Off-chain authentication: API tokens
To interact with an Algorand node programmatically, such as submitting signed transactions or querying blockchain state, an API token is used. This token is typically passed in the X-API-Key HTTP header for REST API requests. Node operators configure these tokens to control access to their RPC endpoints. For example, a developer might use an API token provided by a third-party node service or generate one for their own self-hosted Algorand node.
The API token provides a layer of access control for the node's services without exposing any private keys. It ensures that only authorized applications can send requests to the node, preventing unauthorized resource consumption or potential abuse. While the API token authenticates the request to the node, the actual on-chain transaction within that request still requires a digital signature for network validation.
Summary of authentication methods
The table below summarizes the primary authentication methods used with Algorand:
| Method | When to Use | Security Level | Details |
|---|---|---|---|
| Digital Signatures (Private Keys) | Authorizing on-chain transactions (payments, smart contract calls, asset transfers) | High (Cryptographic) | Proves ownership of an account and authorizes specific actions on the blockchain. Essential for all network-modifying operations. Keys are never sent over the network. |
API Tokens (X-API-Key) |
Accessing Algorand node RPC endpoints (e.g., algod, indexer) for queries or submitting signed transactions. |
Medium (Access Control) | Authenticates the client application to the node service. Prevents unauthorized access to node resources. Does not authorize on-chain actions directly. |
Getting your credentials
Algorand accounts and private keys
An Algorand account is generated by creating a public-private key pair. The private key is your primary credential for authenticating on-chain actions. You can generate a new key pair using:
- Algorand SDKs: Most SDKs (Python, JavaScript, Go, Java) provide functions to generate new accounts. For instance, the Go SDK documentation outlines account creation.
- Command-line tools: The Algorand command-line interface (CLI) tools, such as
goal, can generate new accounts. - Wallets: Algorand-compatible wallets (e.g., Pera Wallet, MyAlgo) manage key generation and storage for users.
Example (JavaScript SDK):
import algosdk from 'algosdk';
const account = algosdk.generateAccount();
console.log("Account address: ", account.addr);
console.log("Private key (mnemonic): ", algosdk.secretKeyToMnemonic(account.sk));
The generated private key, often represented as a 25-word mnemonic phrase, must be securely stored. This mnemonic is the only way to recover or access your account if the private key is lost.
API tokens for node access
The method for obtaining an API token depends on how you are accessing the Algorand network:
- Self-hosted Algorand node: If you run your own
algodorindexernode, the API token is typically configured in the node's data directory. Foralgod, the token is found in thealgod.tokenfile within the data directory. Forindexer, it's inindexer.token. You are responsible for ensuring the security of these files. The Algorand node installation guide provides details on setting up and configuring tokens. - Third-party node providers: Many services offer access to Algorand nodes (e.g., PureStake, AlgoNode). When using such services, you will typically register for an account on their platform, and they will provide you with an API key/token to include in your requests. This token is usually specific to their service and your account with them.
Authenticated request example
This example demonstrates how to sign and submit a simple payment transaction using the Algorand JavaScript SDK, including how to authenticate with an Algorand node using an API token.
Prerequisites:
- An Algorand account with a private key (mnemonic).
- An Algorand node URL and its corresponding API token.
JavaScript example:
import algosdk from 'algosdk';
// 1. Configure Algorand client with node URL and API token
const ALGOD_SERVER = 'https://testnet-api.algonode.cloud'; // Example TestNet node
const ALGOD_PORT = ''; // No port needed for HTTPS
const ALGOD_TOKEN = { 'X-API-Key': 'YOUR_ALGOD_API_TOKEN' }; // Replace with your actual API token
const algodClient = new algosdk.Algodv2(ALGOD_TOKEN, ALGOD_SERVER, ALGOD_PORT);
// 2. Sender and receiver account details
const senderMnemonic = "YOUR_SENDER_MNEMONIC"; // Replace with sender's 25-word mnemonic
const senderAccount = algosdk.mnemonicToSecretKey(senderMnemonic);
const receiverAddress = "RECEIVER_ALGORAND_ADDRESS"; // Replace with recipient's address
const amount = 100000; // Amount in microAlgos (1 Algo = 1,000,000 microAlgos)
const note = new Uint8Array(Buffer.from("Hello from apispine!"));
async function sendPaymentTransaction() {
try {
// Get network parameters for transaction construction
const params = await algodClient.getTransactionParams().do();
console.log("Transaction parameters:", params);
// 3. Construct the payment transaction
const txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
from: senderAccount.addr,
to: receiverAddress,
amount: amount,
note: note,
suggestedParams: params,
});
// 4. Sign the transaction with the sender's private key
const signedTxn = txn.signTxn(senderAccount.sk);
console.log("Transaction signed.");
// 5. Submit the signed transaction to the Algorand network via the node
const txId = await algodClient.sendRawTransaction(signedTxn.blob).do();
console.log("Transaction ID: ", txId.txId);
// Wait for the transaction to be confirmed (optional)
const confirmedTxn = await algosdk.waitForConfirmation(algodClient, txId.txId, 4);
console.log("Transaction confirmed in round: ", confirmedTxn['confirmed-round']);
} catch (error) {
console.error("Error during transaction: ", error);
}
}
sendPaymentTransaction();
In this example, the ALGOD_TOKEN object provides the API key for authenticating with the Algorand node. The senderAccount.sk (private key) is used to cryptographically sign the transaction, which is the on-chain authentication step.
Security best practices
Securing your Algorand authentication credentials is paramount to protecting your assets and applications. Adhere to the following best practices:
Private key management
- Never expose private keys: Private keys (or mnemonic phrases) should never be stored in plain text, hardcoded in applications, or transmitted over insecure channels.
- Use secure storage: For applications, consider using hardware security modules (HSMs), secure enclaves, or encrypted key stores. For individual users, hardware wallets are recommended.
- Offline signing: Whenever possible, perform transaction signing on an air-gapped or offline machine to minimize exposure of the private key.
- Seed phrase backup: Store mnemonic phrases in multiple secure, physical locations, preferably offline and encrypted.
- Access control: Implement strict access controls for any system or service that has access to private keys.
- Multi-signature accounts: For high-value transactions or organizational accounts, utilize Algorand's native multi-signature accounts to require multiple approvals for a transaction.
API token security
- Environment variables: Store API tokens as environment variables rather than directly in your code. This prevents accidental exposure in version control systems.
- Access control for nodes: If self-hosting an Algorand node, restrict network access to its RPC endpoints using firewalls to only allow requests from trusted IP addresses or internal networks.
- Least privilege: If your node provider offers different types of API tokens, use tokens with the minimum necessary permissions for your application.
- Token rotation: Regularly rotate your API tokens, especially if there's any suspicion of compromise.
- Secure communication: Always use HTTPS when communicating with Algorand nodes to encrypt data in transit and protect API tokens from eavesdropping.
Application-level security
- Input validation: Validate all user inputs to prevent injection attacks or malformed transactions.
- Rate limiting: Implement rate limiting on your application's API endpoints to prevent denial-of-service attacks.
- Error handling: Implement robust error handling to avoid leaking sensitive information through error messages.
- Dependency security: Regularly audit and update your project dependencies to patch known vulnerabilities.
- Security audits: Conduct regular security audits and penetration testing of your Algorand applications and infrastructure.