Something.cool
  • Getting started
    • Why Something.cool?
    • 1st stage - Bonding curve
    • 2nd stage - CoolDEX trading
  • Zero-fee coins
    • What is Zero-fee coin?
    • How to launch Zero-fee coin
  • Community coins
    • What is Community coin?
    • Community contribution / How it works?
      • Holders
      • Liquidity providers
      • Burning
    • How to launch Сommunity coin
  • Features
    • Coin Explore
      • New Pairs
      • Coin Vision
    • Spaces
    • Rewards
  • Community
    • Referral program
    • X
    • Media kit
  • Developer Resources
  • Platform Architecture
    • Technical Introduction
    • Overview
    • Components
  • SC Bonding Curve
    • Overview
    • Functions
      • Create Token
      • Buy Token
      • Sell Token
      • Migrate
      • Admin Set Properties
    • Price Calculation
    • Fees
  • CoolDEX
    • Overview
    • Functions
      • Initialize Pool
      • Swap Base In
      • Swap Base Out
      • Deposit
      • Withdraw
      • Withdraw PnL
    • Price Calculation
    • Fees
  • Token Types
    • Token Types
  • Migration Process
    • Migration Process
  • Rewards Claiming
    • Overview
    • Authentication
    • Endpoints
    • Errors
    • Example
  • Reference
    • SC Bonding Curve IDL
    • CoolDEX IDL
    • Log Structure
    • DEVNET EXAMPLES
  • INTERNAL API
    • Overview
    • Register
  • Example: Registering a User via Internal API (JavaScript)
Powered by GitBook
On this page

Example: Registering a User via Internal API (JavaScript)

The following JavaScript example demonstrates how to:

  1. Request a message and nonce from the internal API.

  2. Sign the message using a user's private key.

  3. Send a registration request with the signature, public key, and nonce.

This example uses tweetnacl for signing and axios for HTTP requests.

npm install tweetnacl bs58 axios
const nacl = require('tweetnacl');
const bs58 = require('bs58');
const axios = require('axios');

// Replace with your API key and private/public keys
const API_KEY = 'your-api-key-here';
const PRIVATE_KEY_BASE58 = 'your-user-private-key';
const PUBLIC_KEY_BASE58 = 'your-user-public-key';

async function registerUser() {
  try {
    // 1. Get message and nonce
    const getMessageRes = await axios.get('https://BASE_URL/v1/internal/auth/get-message', {
      headers: { 'x-api-key': API_KEY },
    });

    const { message, nonce } = getMessageRes.data;

    // 2. Sign the message
    const privateKeyUint8 = bs58.decode(PRIVATE_KEY_BASE58);
    const messageUint8 = new TextEncoder().encode(message);
    const signature = nacl.sign.detached(messageUint8, privateKeyUint8);
    const signatureBase64 = Buffer.from(signature).toString('base64');

    // 3. Send registration request
    const registerRes = await axios.post(
      'https://api.yourdomain.com/v1/internal/auth/register',
      {
        publicKey: PUBLIC_KEY_BASE58,
        signature: signatureBase64,
        nonce: nonce,
      },
      {
        headers: {
          'x-api-key': API_KEY,
          'Content-Type': 'application/json',
        },
      }
    );

    console.log('User registration successful:', registerRes.data);
  } catch (error) {
    console.error('Registration failed:', error.response?.data || error.message);
  }
}

registerUser();

Notes

  • The private key must be in raw Uint8Array format (not PEM).

  • Make sure the public/private keys match.

  • Use secure key management when handling private keys.

PreviousRegister

Last updated 1 month ago