L2 Signature
How To Sign Message
Overview
In Layer 2 trading systems, to ensure transaction security and prevent unauthorized operations, critical operations (such as limit orders, transfers, withdrawals, etc.) require users to provide digital signatures to prove the legitimacy of the operations. This signature is passed through the l2Signature parameter.
How To GET Your L2 Private Key
To generate l2Signature, you first need to obtain your L2 private key. This key is used to generate signatures that authorize various actions on the platform.

⚠️ Security Warning:
Keep your private key secure and never share it with anyone
Anyone with access to your private key can sign messages and execute operations on your behalf
Signature Algorithm
Two algorithms are used in the signature process:
ECDSA (Elliptic Curve Digital Signature Algorithm) is used to generate and verify signatures.
Pedersen Hash which consumes more CPU resources compared to regular Ethereum signatures, is used for message hashing.
L2Signature generation follows these standard steps:
Collect Parameters: Gather all required parameters for the operation
Calculate Hash: Use Pedersen Hash to compute the message hash
Generate Signature: Use ECDSA algorithm and L2 private key to sign the hash
Format Output: Format the signature according to API requirements
Java L2Signature Example
Below is a Java implementation of the Ecdsa signature algorithm. This example demonstrates how to sign a message using a private key.
Signature Construction Guide
This section provides detailed instructions for constructing signatures for various actions on the platform. Each operation has specific message formats and parameter requirements.
Withdrawal Signature
Used to authorize withdrawing assets from Layer 2 to an Ethereum address.
Parameters
assetIdCollateral- Asset ID for the collateral token frommeta_data.coinList.starkExAssetIdpositionId- User's account ID in Layer 2ethAddress- Destination Ethereum address for withdrawalnonce- Unique transaction identifier to prevent replay attacksexpirationTimestamp- Unix timestamp when signature expiresamount- Amount to withdraw in base units
Calculation
The following TypeScript function constructs the withdrawal message for signing:
Limit Order Signature
Used to authorize a limit order for perpetual trading.
Parameters
assetIdSynthetic- Synthetic asset ID frommeta_data.contractList.starkExSyntheticAssetIdassetIdCollateral- Collateral asset ID frommeta_data.coinList.starkExAssetIdisBuyingSynthetic-truefor buy orders,falsefor sell ordersassetIdFee- Fee token asset ID frommeta_data.coinList.starkExAssetIdamountSynthetic- Amount of synthetic assetamountCollateral- Amount of collateral assetmaxAmountFee- Maximum fee amount allowednonce- Unique order identifierpositionId- User's position IDexpirationTimestamp- Unix timestamp when order expires
Calculation
The following TypeScript function constructs the limit order message for signing:
Transfer Signature
Used to authorize transfers between Layer 2 accounts.
Parameters
assetId- Asset ID being transferredreceiverPublicKey- Recipient's public keysenderPositionId- Sender's position IDreceiverPositionId- Recipient's position IDsrcFeePositionId- Fee source position IDnonce- Unique transfer identifieramount- Transfer amountexpirationTimestamp- Unix timestamp when transfer expiresassetIdFee- Fee token asset ID (optional, default '0')maxAmountFee- Maximum fee amount (optional, default '0')
Calculation
The following TypeScript function constructs the transfer message for signing:
For more details on the signature construction, see the StarkEx documentation.
Last updated