Skip to content

Signature Method

ED25519 Signature Method

Dipcoin Perpetual API uses ED25519 signature algorithm for authentication and order signing. This document describes the signature methods for different scenarios.

About ED25519

ED25519 is a modern elliptic curve signature scheme that provides: - High security with 128-bit security level - Fast signature generation and verification - Small signature size (64 bytes) - Native support in Sui blockchain

Signature Scenarios

1. JWT Authentication (Onboarding)

When authenticating to obtain JWT Token, you need to sign a specific message using your wallet.

Message Payload

The message to sign is a JSON object:

{
    "onboarding": "www.dipcoin.io"
}

Signature Process

  1. Serialize the payload: Convert the JSON object to a UTF-8 string
  2. Sign with wallet: Use Sui wallet's signPersonalMessage method to sign the message
  3. Submit signature: Send the signature along with wallet address to the authentication endpoint

Implementation Example

Using SDK (Keypair)

import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';
import { OrderSigner } from '@dipcoin/perp-library';

const keypair = new Ed25519Keypair();
const payload = { "onboarding": "www.dipcoin.io" };

const sigPK = await OrderSigner.signPayload(payload, keypair);

// Verify signature
const isValid = await OrderSigner.verifySignature(
    payload, 
    sigPK.signature, 
    sigPK.publicKey
);

// For /api/authorize, concatenate as described below
const signatureForAuthorize = `${sigPK.signature}${sigPK.publicKey}`;

signature field for /api/authorize

The request body signature is a single concatenated string in this order:

hex signature bytes + single-character signature type + Base64-encoded public key

Part Description
Hex signature Raw signature bytes from the signing algorithm, as a hex string (no 0x prefix).
Signature type One ASCII character for scheme/source (same meaning as SDK SIGNER_TYPES, e.g. 1 for Keypair Ed25519, 2 for UI wallet PersonalMessage).
Base64 public key The public key bytes, Base64-encoded (often ~44 characters).

When using OrderSigner.signPayload, sigPK.signature already contains hex + type; append sigPK.publicKey and use that as the signature field.

Signature Protocol

The authentication signature uses Sui PersonalMessage protocol, which adds intent bytes [3, 0, 0] and BCS-encoded message length before the actual message.

2. Order Signing

When placing an order, you need to sign the order data using ED25519.

Order Data Structure

interface Order {
    market: string;          // Market ID (PerpetualID)
    creator: string;          // Order owner's wallet address
    isLong: boolean;          // Direction: true for long, false for short
    reduceOnly: boolean;      // Reduce-only order
    postOnly: boolean;        // Post-only order (maker only)
    cancelOnRevert?: boolean; // Auto-cancel on revert
    orderbookOnly: boolean;   // Orderbook only
    ioc: boolean;            // Immediate or Cancel
    quantity: BigNumber;       // Order quantity (in base asset)
    price: BigNumber;        // Order price (in quote asset)
    leverage: BigNumber;      // Leverage multiplier
    expiration: BigNumber;   // Expiration timestamp (milliseconds)
    salt: BigNumber;         // Random salt for replay protection
}

Signature Process

  1. Serialize Order: Encode the order with Payload.order(order) (same as OrderSigner.getSerializedOrder) as a UTF-8 JSON string with a fixed field order (see “Order Serialization Format” below).
  2. Sign: OrderSigner.signOrder passes those UTF-8 bytes to the Keypair’s signPersonalMessage (Sui PersonalMessage). This does not hash the order with SHA-256 first; getOrderHash is only for display or other hash-based use cases.

Implementation Example

Using SDK (Keypair)

import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';
import { OrderSigner } from '@dipcoin/perp-library';

const keypair = new Ed25519Keypair();
const order: Order = { /* ... */ };

// Sign order directly
const sigPK = await OrderSigner.signOrder(order, keypair);

// Verify signature
const isValid = await OrderSigner.verifySignatureUsingOrder(
    order,
    sigPK.signature,
    sigPK.publicKey
);

if (isValid) {
    // Submit order
    submitOrder(order, sigPK.signature, sigPK.publicKey);
}

Order Serialization Format

This is not a fixed-width hex concatenation. The SDK serializes with Payload.pack_payload via Payload.order (and OrderSigner.getSerializedOrder), producing JSON text, then signs the UTF-8 bytes of that string with signPersonalMessage. Key names, field order, and stringification must match the implementation.

Field order (same as Payload.order in perp-ts-library):

Key Description
market Market ID (string)
creator Creator address (string)
isLong "true" / "false"
reduceOnly "true" / "false"
postOnly "true" / "false"
orderbookOnly "true" / "false"
ioc "true" / "false" (defaults to false if unset)
quantity, price, leverage, expiration, salt Decimal integer strings (toFixed(0))
orderFlag Decimal integer from encodeOrderFlags (see below)
domain Always "dipcoin.io"

orderFlag bit layout (packed into one integer in JSON, not a separate raw byte):

  • 0th bit: ioc
  • 1st bit: postOnly
  • 2nd bit: reduceOnly
  • 3rd bit: isLong
  • 4th bit: orderbookOnly

Example shape (line breaks for readability; actual output matches pack_payload, including newlines and commas):

{
"market":"0x...",
"creator":"0x...",
"isLong":"true",
"reduceOnly":"false",
"postOnly":"false",
"orderbookOnly":"false",
"ioc":"false",
"quantity":"1000000000000000000",
"price":"50000000000000000000000",
"leverage":"1000000000000000000",
"expiration":"1700000000000",
"salt":"12345678901234567890",
"orderFlag":"0",
"domain":"dipcoin.io"
}

Signature Format

The signature string format is:

{signature_hex}{signature_type}

signature_type matches SDK SIGNER_TYPES — a single character:

  • 0: Secp256k1 Keypair (Sui PersonalMessage)
  • 1: ED25519 Keypair
  • 2: ED25519 UI wallet (PersonalMessage)
  • 3: ZK wallet (if applicable)
  • 4: EVM wallet (signOrderEVM, etc.)

The type character is appended after the hex signature; concatenate with the Base64 public key for the full string (same as the JWT section).

3. Cancel Order Signing

When canceling orders, sign a JSON payload containing order hashes.

Message Payload

{
    "orderHashes": [
        "0xabc123...",
        "0xdef456...",
        "..."
    ]
}

Implementation

Same as JWT onboarding: sign the same payload object with OrderSigner.signPayload, then use ${sigPK.signature}${sigPK.publicKey} as the signature field in the cancel request body (sigPK.signature already includes hex + single type character).

Using SDK (Keypair)

import { Ed25519Keypair } from '@mysten/sui.js/keypairs/ed25519';
import { OrderSigner } from '@dipcoin/perp-library';

const keypair = new Ed25519Keypair();
const payload = {
    orderHashes: ["0x...", "0x..."]
};

const sigPK = await OrderSigner.signPayload(payload, keypair);

const isValid = await OrderSigner.verifySignature(
    payload,
    sigPK.signature,
    sigPK.publicKey
);

// Cancel request body `signature` field — same concatenation as /api/authorize
const signatureForCancel = `${sigPK.signature}${sigPK.publicKey}`;

Verification

All signatures can be verified using the OrderSigner utility:

// Verify authentication signature
const isValid = await OrderSigner.verifySignature(
    payload,
    signature,
    publicKey
);

// Verify order signature
const isValid = await OrderSigner.verifySignatureUsingOrder(
    order,
    signature,
    publicKey
);

Important Notes

Important

  • All numeric values in orders are multiplied by 10^18
  • Salt must be unique for each order to prevent replay attacks
  • Signature type is appended as the last character of the signature string
  • Wallet address must match the public key derived from the signature

Best Practices

  • Use secure random number generation for salt
  • Set appropriate expiration times for orders
  • Store and manage keypairs securely
  • Prefer the SDK (Keypair) with OrderSigner for automation and backend integration