1.0.7 • Published 5 months ago

cngn-typescript-library v1.0.7

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

cngn-typescript-library

cngn-typescript-library is a TypeScript library for interacting with the cNGN API. It provides a simple interface for various operations such as checking balance, withdraw, depositing for redemption, creating virtual accounts, generating wallet addresses, and more.

Table of Contents

Installation

To install cngn-typescript-library and its dependencies, run:

npm install cngn-typescript-library

Usage

First, import the necessary classes and types:

import {
  cNGNManager,
  WalletManager,
  Secrets,
  IWithdraw,
  RedeemAsset,
  CreateVirtualAccount,
  UpdateExternalAccount,
  Network,
  Swap
} from 'cngn-typescript-library';

Then, create an instance of cNGNManager with your secrets:

const secrets: Secrets = {
    apiKey: 'your-api-key',
    privateKey: 'your-private-key',
    encryptionKey: 'your-encryption-key'
};

const manager = new cNGNManager(secrets);

// Example: Get balance
manager.getBalance().then(balance => console.log(balance));

Networks

The library supports multiple blockchain networks:

  • Network.bsc - Binance Smart Chain
  • Network.atc - Asset Chain
  • Network.xbn - Bantu Chain
  • Network.eth - Ethereum
  • Network.matic - Polygon (Matic)
  • Network.trx - Tron
  • Network.base - Base

Available Methods

cNGNManager Methods

Get Balance

const balance = await manager.getBalance();

Get Transaction History

const page = 1;
const limit = 10;
const transactions = await manager.getTransactionHistory(page, limit);

Withdraw

const withdrawData: IWithdraw = {
    amount: 1000,
    address: '0x789...',
    network: Network.bsc,
    shouldSaveAddress: true
};
const withdrawResult = await manager.withdraw(withdrawData);

Verify Withdrawal Reference

const tnxRef: string = '123-456-789-789405';
const withdrawResult = await manager.verifyWithdrawal(tnxRef);

Redeem Asset

const redeemData: RedeemAsset = {
    amount: 1000,
    bankCode: '123',
    accountNumber: '1234567890',
    saveDetails: true
};
const redeemResult = await manager.redeemAsset(redeemData);

Create Virtual Account

const mintData: CreateVirtualAccount = {
    provider: 'korapay', // default provider
    bank_code: '011'
};
const virtualAccount = await manager.createVirtualAccount(mintData);

Swap Asset

const swapData: Swap = {
    destinationNetwork: Network.bsc,
    destinationAddress: '0x123...',
    originNetwork: Network.eth,
    callbackUrl: 'https://your-callback-url.com' // optional
};
const swapResult = await manager.swapAsset(swapData);

Update Business

const updateData: UpdateExternalAccount = {
    walletAddress: {
        bscAddress: '0x123...', //Note: Only one address can be updated at a time
    },
    bankDetails: {
        bankName: 'Test Bank',
        bankAccountName: 'Test Account',
        bankAccountNumber: '1234567890'
    }
};
const updateResult = await manager.updateExternalAccounts(updateData);

Get Banks

const banks = await manager.getBanks();

WalletManager Methods

Generate Wallet Address

const wallet = await WalletManager.generateWalletAddress(Network.bsc);

Response format:

interface GeneratedWalletAddress {
    mnemonic: string;
    address: string;
    network: Network;
    privateKey: string;
}

Testing

To run the tests:

  1. Install dependencies:
npm install
  1. Run tests:
npm test
  1. Run tests with coverage:
npm test --coverage

Test files follow the naming convention: *.test.ts and are located in the __tests__ directory.

Error Handling

The library implements comprehensive error handling:

try {
    const result = await manager.getBalance();
} catch (error) {
    // Error will contain detailed message about the failure
    console.error(error.message);
}

Common error types:

  • API errors (with status codes)
  • Network connectivity issues
  • Invalid parameters
  • Wallet generation errors

Types

The library includes TypeScript definitions for all parameters and return types:

  • Secrets - API credentials and keys
  • IResponse<T> - Standard API response wrapper
  • Balance - Account balance information
  • Transactions - Transaction details
  • Withdraw - Withdraw parameters
  • RedeemAsset - Asset redemption details
  • CreateVirtualAccount - Virtual account creation parameters
  • UpdateExternalAccount - Business update parameters and wallet addresses
  • GeneratedWalletAddress - Wallet generation response
  • Swap - Asset swap parameters

Security

  • Uses AES encryption for request data
  • Implements Ed25519 decryption for responses
  • Requires secure storage of API credentials

Contributing

To contribute: 1. Fork the repository 2. Create a feature branch 3. Commit your changes 4. Create a Pull Request

Support

For support, please:

  • Open an issue in the GitHub repository
  • Check existing documentation
  • Contact the support team

License

ISC

1.0.7

5 months ago

1.0.6

7 months ago

1.0.5

7 months ago

1.0.1

8 months ago

1.1.7

8 months ago

1.1.6

8 months ago

1.1.5

8 months ago

1.1.4

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.0

8 months ago