Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@crypto-connect/common

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@crypto-connect/common

CryptoConnect common files

  • 0.0.2-alpha.55
  • canary
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

CryptoConnect Common Files

This package is primarily aimed at providing most of the boilerplate needed to build integrations.

In general, it's expected for integrations to be created by extending BaseConnectionSecure with a custom or existing auth method.

Implementing Custom Connections

import { BaseConnectionSecure, CryptoBalance } from "@crypto-connect/common";
import { ExampleApiKeys } from "./ExampleApiKeys";
import { ExampleBalances } from "./example-types";

/**
 * Example API Base URL
 */
const BASE_URL = "https://api.example.com";

/**
 * Example Request Endpoints
 */
const ENDPOINTS = {
  balances: `${BASE_URL}/balances`,
};

/**
 * Example Auth Methods
 */
type ExampleAuth = ExampleApiKeys;

/**
 * Example API Client
 */
class ExampleConnectionSecure extends BaseConnectionSecure<ExampleAuth> {
  /**
   * Supply auth in constructor
   */
  constructor(protected auth: ExampleAuth) {
    super();
  }

  /**
   * Get normalized list of Example balances
   */
  async getBalances(): Promise<CryptoBalance[]> {
    const endpoint = ENDPOINTS.accounts;
    const result = await this.auth.request<ExampleBalances>(endpoint);

    // convert api result to balances
    // ...

    return balances;
  }
}

export { ExampleConnectionSecure as Example };

Implementing Custom Auth Methods

OAuth

Integrating a service that uses OAuth is very simple - define the endpoints and the error type.

import { OAuth } from "@crypto-connect/common";

/**
 * Example OAuth Base URL
 */
const BASE_URL = "https://api.example.com";

/**
 * Example OAuth Endpoints
 */
const ENDPOINTS = {
  authorizeUrl: `${BASE_URL}/oauth/authorize`,
  tokenUrl: `${BASE_URL}/oauth/token`,
};

/**
 * Service Error Response
 */
type ExampleError = { error: string };

/**
 * Make authenticated requests with OAuth
 */
export class ExampleOAuth extends OAuth<ExampleError> {
  endpoints = ENDPOINTS;
}

API Keys

Services that use API keys are varied in their approach, so require more effort. A signRequest method must be defined that takes the request parameters and credentials, and returns signed request parameters as needed.

import { ApiKeys, RequestUrl, RequestOptions } from "@crypto-connect/common";

/**
 * Make authenticated requests with Api Keys
 */
export class ExampleAPIKeys extends ApiKeys<
  // service credentials
  {
    apiKey: string;
    apiSecret: string;
  },
  // service error response
  {
    error: string;
  }
> {
  /**
   * Sign requests
   */
  signRequest(
    url: RequestUrl,
    { method = "GET", headers = {}, body = "" }: RequestOptions = {},
  ): [RequestUrl, RequestOptions] {
    const { apiKey, apiSecret } = this.credentials;

    // Generate a signature and sign the request
    // ...

    return [url, { method, headers, body }];
  }
}

FAQs

Package last updated on 04 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc