Source

Face.ts

import { Env, Network, NetworkChainIdMap, unsupportedChainError } from '@haechi-labs/face-types';

import { Auth } from './Auth';
import { Bora } from './blockchain/Bora';
// import { Near, Solana } from './blockchain';
import { Internal } from './Internal';
import { Provider } from './Provider';
import { isSupportedNetwork } from './utils/network';
import { Wallet } from './Wallet';

export { Network };

export interface FaceConfig {
  apiKey: string;
  network: Network | number;
  scheme: string;
}

/**
 * Face SDK
 *
 * @category API
 *
 * @class
 * @name Face
 * @param {object} config - Config.
 * @param {string} config.apiKey - Api key.
 * @param {string} config.network - Blockchain Network such as Goerli, Mumbai, Ethereum etc.
 */
export class Face {
  private config: any;
  private network: Network;
  private readonly internal: Internal;

  public auth: Auth;
  public wallet: Wallet;
  public bora: Bora;

  // public wc: WalletConnect;
  // public solana: Solana;
  // public near: Near;

  constructor(config: FaceConfig) {
    this.config = config;
    const { apiKey, network, scheme, ...rest } = config;
    const _network: Network =
      typeof network === 'number' ? (NetworkChainIdMap[network] as Network) : network!;

    if (!isSupportedNetwork(_network)) {
      throw unsupportedChainError();
    }

    this.network = _network;
    this.internal = new Internal({
      apiKey,
      network: _network,
      scheme,
      env: (rest as { env?: Env }).env,
      iframeUrl: (rest as { iframeUrl?: string }).iframeUrl,
      face: this,
    });

    /** @member {Auth} */
    this.auth = new Auth(this.internal);
    /** @member {Wallet} */
    this.wallet = new Wallet(this.internal);
    this.bora = new Bora(this.internal, this.auth);

    // this.wc = new WalletConnect(this.internal);
    // this.near = new Near(this.internal);
    // this.solana = new Solana(this.internal);
  }

  /**
   * Return provider for the EVM blockchain provider
   *
   * @method
   */
  getEthLikeProvider(): Provider {
    return new Provider(this.internal);
  }

  /**
   * Return the wallet address array
   *
   * @method
   * @returns {Promise<string[]>}
   */
  getAddresses = async (): Promise<string[]> => {
    return await this.internal.getAddresses();
  };

  setNetwork = (network: Network) => {
    this.network = network;
  };

  /**
   * Return the set network
   *
   * @method
   * @returns {Network}
   */
  getNetwork = (): Network => {
    return this.network;
  };

  /**
   * Return the chain id of current network
   *
   * @method
   * @returns {Promise<number>}
   */
  getChainId = async (): Promise<number> => {
    return Number(await this.internal.sendRpc({ method: 'eth_chainId', params: [] }));
  };

  /**
   * Switch network
   *
   * @method
   * @returns {Promise<number>}
   */
  async switchNetwork(network: Network | number) {
    const _network: Network =
      typeof network === 'number' ? (NetworkChainIdMap[network] as Network) : network!;

    if (!isSupportedNetwork(_network)) {
      throw unsupportedChainError();
    }

    return this.internal.switchNetwork(_network);
  }
}