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.

How To GET Your L2 Private Key

⚠️ 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:

  1. ECDSA (Elliptic Curve Digital Signature Algorithm) is used to generate and verify signatures.

  2. Pedersen Hash which consumes more CPU resources compared to regular Ethereum signatures, is used for message hashing.

L2Signature generation follows these standard steps:

  1. Collect Parameters: Gather all required parameters for the operation

  2. Calculate Hash: Use Pedersen Hash to compute the message hash

  3. Generate Signature: Use ECDSA algorithm and L2 private key to sign the hash

  4. Format Output: Format the signature according to API requirements

Python L2Signature Demo

Golang L2Signature Demo

Java Script L2Signature Demo

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 from meta_data.coinList.starkExAssetId

  • positionId - User's account ID in Layer 2

  • ethAddress - Destination Ethereum address for withdrawal

  • nonce - Unique transaction identifier to prevent replay attacks

  • expirationTimestamp - Unix timestamp when signature expires

  • amount - 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

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 transferred

  • receiverPublicKey - Recipient's public key

  • senderPositionId - Sender's position ID

  • receiverPositionId - Recipient's position ID

  • srcFeePositionId - Fee source position ID

  • nonce - Unique transfer identifier

  • amount - Transfer amount

  • expirationTimestamp - Unix timestamp when transfer expires

  • assetIdFee - 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