Skip to main content

Authentication

Authentication with Clearnode can be done in two ways: using your main wallet as a root signer for all requests, or delegating to session keys via a secure 3-step challenge-response protocol.


Overview

There are two authentication approaches:

  1. Main Wallet (Root Signer): Sign every request with your main wallet. Simple but requires user interaction for each operation.

  2. Session Keys (Delegated): Establish an authenticated session once, then use a session key for subsequent operations without repeatedly prompting the main wallet.

Main Wallet as Root Signer

You can skip the session key flow entirely and use your main wallet to sign all requests. This provides maximum security but requires wallet interaction for every operation. Simply sign each request with your main wallet's private key instead of creating a session key.

Why Session Keys?

Session keys provide flexible security management:

  • Granular Permissions: Specify which operations the session key can perform
  • Spending Allowances: Set maximum spending limits per asset
  • Time-Bounded: Automatic expiration reduces risk of key compromise
  • Application-Scoped: Different keys for different apps
  • User Experience: No repeated wallet prompts during active session
Flexible Security Management

Session keys give users a flexible way to manage security of their funds by providing specific permissions and allowances for specific apps, balancing convenience with security.

Choosing Your Approach

AspectMain Wallet (Root Signer)Session Keys (Delegated)
SetupNone - use immediatelyOne-time 3-step flow
UXWallet prompt for every operationSign once, use for duration
SecurityMaximum - full control alwaysBalanced - limited by allowances
Use CaseSingle operations, high-value transactionsInteractive apps, frequent operations
RevocationNot neededCan be revoked anytime
Best ForOne-time actions, security-critical operationsGaming, trading bots, dApps with frequent interactions
When to Use Each
  • Use Main Wallet: For single channel creation, large transfers, or when maximum security is required
  • Use Session Keys: For interactive applications, gaming, automated operations, or when user experience matters

Session Key Authentication Flow

The 3-step process ensures both security and usability:

Challenge-Response Pattern

This pattern ensures that:

  1. User owns the main wallet (EIP-712 signature in Step 3)
  2. Challenge is unique and cannot be replayed
  3. No private keys are ever transmitted
  4. Session key is authorized by the main wallet

Step 1: auth_request

Name

auth_request

Usage

Initiates authentication with Clearnode by registering a session key. The client sends authentication parameters to register a session key that can act on their behalf. The session key can have restricted permissions including spending limits (allowances), operation scope, and expiration time.

Important: auth_request is a public endpoint and does not require a signature. The client simply needs to prepare and send the authentication parameters.

When to Use

Optional: Use this when you want to delegate signing to a session key instead of using your main wallet for every request. This is the first step in establishing an authenticated session with Clearnode.

If you prefer to use your main wallet as a root signer for all operations, you can skip this entire authentication flow.

Prerequisites

  • User has a wallet with funds
  • Client can generate a keypair (e.g., secp256k1)
  • Client can prepare authentication parameters locally

Request

ParameterTypeRequiredDescriptionDefaultExampleNotes
addressstring (wallet address)YesUser's main wallet address that owns the funds-"0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb"-
session_keystring (wallet address)YesWallet address of the locally-generated session keypair-"0x9876543210fedcba9876543210fedcba98765432"The private key never leaves the client
applicationstringNoApplication identifier for analytics and session management"clearnode""chess-game-app"Helps track which app is using which session
allowancesArray<Allowance>NoSpending limits for this session key

Structure (per allowance):
asset (string) - Asset identifier (e.g., "usdc", "eth")
amount (string) - Maximum amount this session can spend
Unrestricted if omitted/empty[{"asset": "usdc", "amount": "100.0"}]If empty/omitted, no spending cap is enforced
scopestringNoComma-separated list of permitted operationsAll operations permitted"app.create,app.submit,transfer"Future feature, not fully enforced yet
expires_atnumberYesUnix timestamp (milliseconds) when the session key expires1762417328000Provide a 13-digit Unix ms timestamp; no server default is applied
Spending Allowances

If you omit allowances the session key is unrestricted. Specify explicit allowances to bound risk if a session key is compromised.

Allowances are validated against the broker’s supported assets. Unsupported symbols will cause authentication to fail.

Response

ParameterTypeDescriptionFormatExamplePurpose
challenge_messagestringUUID that client must sign with session key to prove ownershipUUID v4"550e8400-e29b-41d4-a716-446655440000"Proves client controls session key without exposing private key

Signature

Request does NOT require a signature as auth_request is a public endpoint.

Process:

  1. Client prepares authentication parameters (address, session_key, application, allowances, expires_at)
  2. Client stores these parameters locally for use in Step 3 (auth_verify)
  3. Client sends request to Clearnode
  4. Clearnode validates all parameters before generating a challenge
Parameter Storage

Keep the authentication parameters (especially address, session_key, application, allowances, scope, and expires_at) stored locally until Step 3, as you'll need them to create the EIP-712 signature.

Next Step

Upon receiving the challenge_message, client must prepare an EIP-712 signature (or reuse a previously issued jwt) and call auth_verify.

Error Cases

Error Codes

Currently, the protocol does not use standardized error codes. Errors are returned as descriptive messages.

Common error scenarios:

ErrorDescriptionRecovery
Invalid address formatMain wallet address is malformedVerify address format (0x + 40 hex chars)
Invalid session key formatSession key address is malformedVerify session key format
Invalid parametersOne or more parameters are invalid or missingCheck all required parameters
Session key already registeredThis session key is already in useGenerate a new session keypair

Step 2: auth_challenge

Name

auth_challenge

Usage

Server-generated response to auth_request containing a challenge that the client must sign to prove control of the session key. This implements a challenge-response authentication pattern to prevent replay attacks and verify the client controls the private key of the session key they registered.

When to Use

Automatically sent by Clearnode in response to valid auth_request. Client does not explicitly call this; it's part of the authentication flow.

Request

N/A (server-initiated response to auth_request)

Response

ParameterTypeDescriptionFormatPurposeExampleGenerationLifetime
challenge_messagestringRandomly generated UUID that client must signUUID v4Prevents replay attacks, proves session key ownership"550e8400-e29b-41d4-a716-446655440000"Cryptographically secure random UUIDSingle use, expires after 5 minutes if not verified

Signature

The challenge is returned as a normal RPC response (server signs the envelope like any other RPC response).

Next Step

Client signs the challenge with session key private key and calls auth_verify.

Challenge Uniqueness

Each challenge is unique and single-use. It expires after 5 minutes if not verified. This prevents replay attacks where an attacker might try to reuse a captured challenge signature.


Step 3: auth_verify

Name

auth_verify

Usage

Completes the authentication flow by submitting the signed challenge from auth_challenge. If the signature is valid and matches the registered session key, the authentication is complete and the session key can be used to sign subsequent requests. This proves the client controls the private key without ever transmitting it.

When to Use

Immediately after receiving auth_challenge response. This is the final step in authentication.

Prerequisites

  • Completed auth_request and received auth_challenge
  • Have the challenge_message
  • Have the session key private key (client-side only)

Request

ParameterTypeRequiredDescriptionExampleNotes
challengestringYesThe challenge_message received from auth_challenge"550e8400-e29b-41d4-a716-446655440000"Must be the exact challenge from Step 2
jwtstringNoExisting JWT for re-login without signature"eyJhbGciOi..."If provided, signature is not required

Response

ParameterTypeDescriptionExampleNotes
addressstring (wallet address)Authenticated user's main wallet address"0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb"Confirms which account is authenticated
session_keystring (wallet address)Confirmed session key wallet address"0x9876543210fedcba9876543210fedcba98765432"The authorized session key
jwt_tokenstringJWT token for authenticated API calls"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."Store securely; validity follows the provided expires_at
successbooleanAuthentication success indicatortrueIndicates if authentication completed successfully

Signature

If jwt is omitted, the request MUST include an EIP-712 signature signed by the main wallet (NOT the session key). If jwt is present, no signature is required.

EIP-712 Typed Data Structure:

{
types: {
EIP712Domain: [
{ name: "name", type: "string" }
],
Policy: [
{ name: "challenge", type: "string" },
{ name: "scope", type: "string" },
{ name: "wallet", type: "address" },
{ name: "session_key", type: "address" },
{ name: "expires_at", type: "uint64" },
{ name: "allowances", type: "Allowance[]" }
],
Allowance: [
{ name: "asset", type: "string" },
{ name: "amount", type: "string" }
]
},
primaryType: "Policy",
domain: {
name: <application_name> // From auth_request
},
message: {
challenge: <challenge_message>, // From auth_challenge
scope: <scope>, // From auth_request
wallet: <address>, // From auth_request
session_key: <session_key>, // From auth_request
expires_at: <expires_at>, // From auth_request (13-digit Unix ms)
allowances: <allowances> // From auth_request
}
}

Signing Process:

  1. Client creates EIP-712 typed data with challenge and all parameters from Step 1
  2. User's wallet signs the typed data: signature = signTypedData(typedData, mainWalletPrivateKey)
  3. Client sends request with EIP-712 signature in sig array
Critical Security Requirement

The auth_verify signature MUST be an EIP-712 signature signed by the main wallet, not the session key. This proves the main wallet owner authorizes the session key to act on their behalf. The signature binds the challenge to the session key authorization.

Next Step

Session is authenticated. All subsequent private method calls should be signed with the session key. You may also re-authenticate later by sending auth_verify with the previously issued jwt (no signature required).

Error Cases

Error Codes

Currently, the protocol does not use standardized error codes. Errors are returned as descriptive messages.

Common error scenarios:

ErrorDescriptionRecovery
Invalid signatureEIP-712 signature doesn't match main wallet or is malformedVerify main wallet private key used for signing, check EIP-712 structure
Challenge expiredChallenge older than 5 minutesRestart auth flow from auth_request
Challenge already usedChallenge has been verified alreadyGenerate new session or use existing if still valid
Invalid challengeChallenge not found in pending authsEnsure auth_request succeeded first
Challenge mismatchChallenge doesn't match pending authUse exact challenge from auth_challenge

Complete Authentication Flow Example

Putting it all together:


Session Management

Session Lifecycle

  1. Creation: After successful auth_verify
  2. Active: Can perform operations until expiration or allowance exceeded
  3. Expiration: Automatic after specified duration
  4. Invalidation: When spending allowances exhausted
  5. Revocation: User or the clearnode can revoke manually

Checking Session Status

Use get_session_keys to view active sessions and their remaining allowances. The response includes session details with current allowance usage and respects the expires_at provided during auth_request.

Session Expiration Handling

When a session expires according to the expires_at you provided, the clearnode will return an error response:

{
"res": [
<requestId>,
"error",
{
"error": "session expired, please re-authenticate"
},
<timestamp>
],
"sig": [<clearnode_signature>]
}
Error Format

The protocol does not use numeric error codes. Errors are returned as method "error" with a descriptive message in the params.

Recovery: Re-authenticate by running the 3-step flow again.

Spending Allowance Tracking

The clearnode tracks spending by monitoring all ledger debit operations:

Initial state:
allowance = specified_limit
used = 0
remaining = specified_limit

After operations:
allowance = specified_limit (unchanged)
used = sum_of_all_debits
remaining = allowance - used

When operation exceeds remaining (for assets with an allowance):
Error: "Session key allowance exceeded: amount_required, remaining_available"
Allowance Enforcement

When a session key reaches its spending cap, all further operations are rejected. The user must create a new session with fresh allowances or use their main wallet directly.


Security Best Practices

For Users

  1. Set Spending Limits: Always specify allowances when creating sessions
  2. Short Expirations: Use shorter expiration times for sensitive operations
  3. Application Scoping: Use different session keys for different applications
  4. Monitor Usage: Regularly check session key spending via get_session_keys
  5. Revoke When Done: Revoke sessions when application use is complete

For Developers

  1. Secure Storage: Store session key private keys securely (encrypted storage, secure enclaves)
  2. Never Transmit: Never send session key private keys over network
  3. Handle Expiration: Implement automatic re-authentication on session expiry
  4. Clear on Logout: Delete session keys when user logs out
  5. Verify Signatures: Always verify the clearnode's signatures on responses

Next Steps

Now that you're authenticated, you can:

For protocol fundamentals, see: