Home

AkashicPay - Node SDK

A library to interact with the AkashicChain network, written in TypeScript.

Installing

Install the package with:

npm install @akashicpay/sdk
#or
yarn add @akashicpay/sdk

Usage

AkashicPay works with both ESM and CommonJS formats:

import { AkashicPay } from "@akashicpay/sdk";
const { AkashicPay } = require("@akashicpay/sdk");

Features

  • Send crypto via Layer 1 and Layer 2 (Akashic Chain)
  • Create wallets for your users into which they can deposit crypto
  • Fetch balance and transaction details
  • Completely Web3: No login or API-key necessary. Just supply your Akashic private key, which stays on your server. The SDK signs your transactions with your key and sends them to Akashic Chain.
  • Supports Ethereum and Tron

Getting Started

  1. Create an account on AkashicLink (Google Chrome Extension or iPhone/Android App)

  2. Visit AkashicPay and connect with AkashicLink. Set up the URL(s) you wish to receive callbacks for.

  3. Integrate the SDK in your code. This example configuration uses many optional build arguments, for illustration purposes:

    import type { ILogger } from "@akashicpay/sdk";
    import {
      ACDevNodes,
      ACNodes,
      AkashicPay,
      Environment,
    } from "@akashicpay/sdk";
    import axios from "axios";
    import pino from "pino";
    
    // use whatever secret management tool you prefer to load the private key
    // from your AkashicLink account. It should be of the form:
    // `"0x2d99270559d7702eadd1c5a483d0a795566dc76c18ad9d426c932de41bfb78b7"`
    // In development, each developer could have their own, or omit this (and
    // the l2Address), in which case the SDK will create and use a new pair.
    // you can instead use your Akashic Link account's 12-word phrase, using the
    // `build()` argument `recoveryPhrase`
    const privateKey = process.env.akashicKey;
    // this is the address of your AkashicLink account. Of the form "AS1234..."
    const l2Address = process.env.l2Address;
    // in development, you will use our testnet and testnet L1 chains
    const environment =
      process.env.environment == "production"
        ? Environment.Production
        : Environment.Development;
    // you're strongly encouraged to pass an instance of your preferred logger
    const logger: ILogger = pino({ name: "AkashicPaySDK" });
    // optional, the SDK will try to find the fastest node if omitted
    const targetNode =
      environment == "development" ? ACDevNodes.Singapore1 : ACNodes.Singapore1;
    
    // instantiate an SDK instance, ready to use
    const akashicPay = await AkashicPay.build({
      privateKey,
      l2Address,
      environment,
      targetNode,
      logger,
    });
    

AkashicPay is now fully setup and ready to use.

Dependency injection

The SDK uses dependency injection to allow for easy testing. You can pass in your own implementations of the following interfaces to AkashicPay.build():

{
  logger: ILogger;
}

Configuring a logger

We strongly advise you to inject a logger. If one is not supplied, we will default to console logging, which is almost certainly not what you want in production or testing.

You can use any logger that implements the ILogger interface. The interface is modelled on Pino's, and injecting a Pino logger instance will work out of the box.

If you use an alternative logger in your app, you can write a class that does implement the ILogger interface, and wraps your logger.

Example logger wrapper class for Winston:

import type { Logger } from "winston";
import winston, { format } from "winston";
import type { ILogger, LogFn } from "@akashicpay/sdk";

class WinstonWrapper implements ILogger {
  private logger: Logger;
  constructor(logger: Logger) {
    // Enables winston to accept interpolation arguments
    logger.format = format.combine(format.splat());
    this.logger = logger;
  }

  private log(
    level: string,
    contextObj: unknown,
    msg?: string,
    ...interpolationArgs: unknown[]
  ): void;
  private log(
    level: string,
    msg: string,
    ...interpolationArgs: unknown[]
  ): void;
  private log(
    level: string,
    objOrMsg: unknown | string,
    msgOrFirstInterpolation?: string | unknown,
    ...interpolations: unknown[]
  ) {
    if (typeof objOrMsg === "string") {
      if (msgOrFirstInterpolation) {
        this.logger.log(
          level,
          objOrMsg,
          msgOrFirstInterpolation,
          ...interpolations
        );
      } else {
        this.logger.log(level, objOrMsg);
      }
    } else {
      const metadata = objOrMsg;
      const msg = msgOrFirstInterpolation as string;
      this.logger.log(level, msg, metadata, ...interpolations);
    }
  }

  fatal: LogFn = (a: any, b: any, ...args: any[]) =>
    this.log("error", a, b, ...args);
  error: LogFn = (a: any, b: any, ...args: any[]) =>
    this.log("error", a, b, ...args);
  warn: LogFn = (a: any, b: any, ...args: any[]) =>
    this.log("warn", a, b, ...args);
  info: LogFn = (a: any, b: any, ...args: any[]) =>
    this.log("info", a, b, ...args);
  debug: LogFn = (a: any, b: any, ...args: any[]) =>
    this.log("debug", a, b, ...args);
  trace: LogFn = (a: any, b: any, ...args: any[]) =>
    this.log("verbose", a, b, ...args);
}

Testing

You can also use AkashicPay with the AkashicChain Testnet & Sepolia (Ethereum) and Shasta (Tron) testnets, useful for local development and staging environments. To do this, no AkashicLink is necessary; you can build an AkashicPay instance as follows, and the SDK will create a "test otk" for you:

import { AkashicPay, Environment } from "@akashicpay/sdk";

// production is the default environment.
// And in production, an otk must be specified
const akashicPay = await AkashicPay.build({
  environment: Environment.Development,
});

// for security reasons, this would throw if the environment was production
// but you can use this in development to record and re-use your otk
const { l2Address, privateKey } = akashicPay.keyBackup;
console.log(
  `my testing L2 address: ${l2Address} and private key: ${privateKey}`
);

Then, to reuse the same otk in later testing (recommended), simply supply the otk during setup:

import { AkashicPay, Environment } from "@akashicpay/sdk";

const privateKey = process.env.akashicKey;
const l2Address = process.env.l2Address;

const akashicPay = await AkashicPay.build({
  privateKey,
  l2Address,
  environment: Environment.Development,
});

You can now create an L1-wallet on a testnet:

const { address } = await akashicPay.getDepositAddress(
  NetworkSymbol.Tron_Shasta,
  "EndUser123"
);

Faucet

During testing and local development, you need cryptocurrency on the testnets to do anything meaningful. Akashic provides a simple faucet where you can request some coins and tokens on Shasta and Sepolia by supplying your L2-address/identity: https://faucet.testnet.akashicchain.com/ If you require further funds, the official Tron Discord provides users with either 5000 TRX or USDT on Shasta every day.

While you won't receive callbacks during testing, you can check to see if your balance has increased with:

const balances = await akashicPay.getBalance();
// -> [{networkSymbol: 'TRX-SHASTA', balance: '5000'}, ...]

Documentation

For more in-depth documentation describing the SDKs functions in detail, explanations of terminology, and guides on how to use AkashicPay.com, see https://akashic-1.gitbook.io/akashicpay/getting-started