Skip to content

gamandeepsingh/rust-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Solana HTTP Server

A REST API server for Solana blockchain operations built with Rust and Warp. This server provides endpoints for common Solana operations including keypair generation, token operations, message signing/verification, and transaction instruction creation.

Features

  • 🔑 Keypair Generation: Generate new Solana keypairs
  • 🪙 Token Operations: Create and mint SPL tokens
  • ✍️ Message Signing: Sign messages with private keys
  • Signature Verification: Verify message signatures
  • 💸 SOL Transfers: Create SOL transfer instructions
  • 🔄 Token Transfers: Create SPL token transfer instructions

Prerequisites

  • Rust 1.70+
  • Cargo package manager

Installation

  1. Clone the repository:
git clone <repository-url>
cd solana-harkirat
  1. Install dependencies:
cargo build
  1. Run the server:
cargo run

The server will start on http://localhost:3030

API Endpoints

1. Generate Keypair

POST /keypair

Generates a new Solana keypair.

Response:

{
  "success": true,
  "data": {
    "pubkey": "11111111111111111111111111111112",
    "secret": "base58-encoded-private-key"
  }
}

2. Create Token

POST /token/create

Creates a new SPL token initialization instruction.

Request Body:

{
  "mintAuthority": "public-key-string",
  "mint": "mint-public-key-string",
  "decimals": 9
}

Response:

{
  "success": true,
  "data": {
    "program_id": "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
    "accounts": [
      {
        "pubkey": "account-public-key",
        "is_signer": false,
        "is_writable": true
      }
    ],
    "instruction_data": "base64-encoded-instruction-data"
  }
}

3. Mint Tokens

POST /token/mint

Creates a mint-to instruction for SPL tokens.

Request Body:

{
  "mint": "mint-public-key",
  "destination": "destination-account-public-key",
  "authority": "mint-authority-public-key",
  "amount": 1000000000
}

4. Sign Message

POST /message/sign

Signs a message with a private key.

Request Body:

{
  "message": "Hello, Solana!",
  "secret": "base58-encoded-private-key"
}

Response:

{
  "success": true,
  "data": {
    "signature": "base64-encoded-signature",
    "public_key": "signer-public-key",
    "message": "Hello, Solana!"
  }
}

5. Verify Message

POST /message/verify

Verifies a message signature.

Request Body:

{
  "message": "Hello, Solana!",
  "signature": "base64-encoded-signature",
  "pubkey": "signer-public-key"
}

Response:

{
  "success": true,
  "data": {
    "valid": true,
    "message": "Hello, Solana!",
    "pubkey": "signer-public-key"
  }
}

6. Send SOL

POST /send/sol

Creates a SOL transfer instruction.

Request Body:

{
  "from": "sender-public-key",
  "to": "recipient-public-key",
  "lamports": 1000000000
}

Response:

{
  "success": true,
  "data": {
    "program_id": "11111111111111111111111111111112",
    "accounts": [
      "sender-public-key",
      "recipient-public-key"
    ],
    "instruction_data": "base64-encoded-instruction-data"
  }
}

7. Send Tokens

POST /send/token

Creates an SPL token transfer instruction.

Request Body:

{
  "destination": "destination-token-account",
  "mint": "token-mint-public-key",
  "owner": "token-owner-public-key",
  "amount": 1000000
}

Error Handling

All endpoints return error responses in the following format:

{
  "success": false,
  "error": "Error description"
}

Common errors:

  • Invalid public key format
  • Invalid secret key format
  • Missing required fields
  • Failed instruction creation

Development

Project Structure

├── main.rs           # Main server code
├── Cargo.toml        # Dependencies and project configuration
├── Cargo.lock        # Dependency lock file
└── README.md         # This file

Dependencies

  • tokio - Async runtime
  • warp - Web framework
  • serde - Serialization/deserialization
  • solana-sdk - Solana blockchain SDK
  • spl-token - SPL token program
  • spl-associated-token-account - Associated token accounts
  • bs58 - Base58 encoding
  • base64 - Base64 encoding

Building for Production

cargo build --release

The binary will be available at target/release/solana-http-server

Usage Examples

Using curl

  1. Generate a keypair:
curl -X POST http://localhost:3030/keypair
  1. Sign a message:
curl -X POST http://localhost:3030/message/sign \
  -H "Content-Type: application/json" \
  -d '{
    "message": "Hello Solana",
    "secret": "your-base58-private-key"
  }'
  1. Create SOL transfer:
curl -X POST http://localhost:3030/send/sol \
  -H "Content-Type: application/json" \
  -d '{
    "from": "sender-pubkey",
    "to": "recipient-pubkey",
    "lamports": 1000000000
  }'

Security Notes

⚠️ Important Security Considerations:

  • This server is designed for development and testing purposes
  • Never expose private keys or run this server with real funds in production
  • Always validate and sanitize inputs in production environments
  • Consider implementing authentication and rate limiting for production use

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

This project is licensed under the MIT License.

Support

For issues and questions, please open an issue on the GitHub repository.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages