Implementation

The following guide provides a steps-by-step breakdown of Pacifica's signing implementation

1. Setup and Initialization:

import time
import base58
import requests
from solders.keypair import Keypair

PRIVATE_KEY = "your_private_key_here"

# Generate keypair from private key
keypair = Keypair.from_bytes(base58.b58decode(PRIVATE_KEY))
public_key = str(keypair.pubkey())

2. Choose Endpoint and Define Operation Type

For this example, we use the order creation endpoint. Refer to Operation Typesarrow-up-right for a list of all types and corresponding API endpoints.

API_URL = "https://api.pacifica.fi/api/v1/orders/create"
operation_type = "create_order"
operation_data = {
    "symbol": "BTC",
    "price": "100000",
    "amount": "0.1",
    "side": "bid",
    "tif": "GTC",
    "reduce_only": False,
    "client_order_id": str(uuid.uuid4()),
}

3. Create Signature Header

Note that all times specified are denoted in milliseconds. The "expiry_window" field is optional, and defaults to 30_000 (30 seconds) if not specified in the header.

4. Combine Header and Payload

In the case of our example, this creates:

Note that data must be in same level as other headers.

5. Recursively Sort JSON Keys

In the case of our example, this creates:

Note that the recursive sorting alphabetically sorts *all* levels

6. Create Compact JSON

Compact JSON string with no whitespace and standardized seperators

In the case of our example, this creates:

This ensures that all logically identical messages will always produce *identical* signatures

7. Convert to Bytes and Generate Signature

Messages are converted to UTF-8 bytes for signing. The signature generated is then converted to Base58 string for transmission.

8. Build Final Request

Build the header with generated authentication info and combine with operation data (NOT the "data" wrapper!)

In the case of our example, the final request looks like:

Last updated