Skip to content

GridPlus/chains-lib

 
 

Repository files navigation

Chainslib

This TypeScript library is designed to provide a unified interface for working with multiple blockchain networks, allowing developers to interact with various blockchain chains seamlessly. It supports a wide range of features and blockchain networks, making it a versatile tool for blockchain development.

Features

This library offers the following key features:

Hardware Wallet Support: It provides integration with Ledger and Trezor hardware wallets for enhanced security in blockchain transactions.

Wallet Types: You can work with both seed phrase and private key wallets for managing your blockchain assets.

Standardized RPC Interface: The library offers a consistent RPC interface for fetching blockchain information and broadcasting transactions across different supported chains.

Supported Chains

This library currently supports the following blockchain networks: // example for fallback data source // example with custom provider

Chain Provider Datasources Signers Custom chain
Bitcoin Bitcoin Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor No
Ethereum EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
BNB Smart Chain EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Polygon EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Avalanche EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Fantom EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Arbitrum EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Aurora EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Canto EVM EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Optimism EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Klaytn EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Cronos EVM Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Binance Binance Indexer SeedPhrase, PrivateKey, Ledger, Trezor No
BitcoinCash BitcoinCash Indexer SeedPhrase, PrivateKey, Ledger, Trezor No
Cosmos Hub Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Osmosis Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Axelar Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Juno Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Crescent Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Kava Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Stargaze Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Akash Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Cronos Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Kujira Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Stride Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Mars Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Terra Cosmos Indexer, Chain SeedPhrase, PrivateKey, Ledger, Trezor Yes
Dogecoin Dogecoin Indexer SeedPhrase, PrivateKey, Ledger, Trezor No
Litecoin Litecoin Indexer SeedPhrase, PrivateKey, Ledger, Trezor No
Solana Solana Indexer, Chain SeedPhrase, PrivateKey, Ledger No
ThorChain ThorChain Indexer, Chain SeedPhrase, Ledger No
MayaChain ThorChain Indexer, Chain SeedPhrase, Ledger No
Tron Tron Indexer, Chain SeedPhrase, PrivateKey, Ledger No

Installation

To use this library in your TypeScript project, you can install it via npm or yarn:

npm install @xdefi-tech/chains
# or
yarn add @xdefi-tech/chains

Manifest

Each provider may have different manifests, but they share common fields. For more details, please refer to the README of the respective provider.

  • name: The name of the blockchain network. Uses only for display name to user
  • description: A brief description or additional information about the blockchain network.
  • rpcURL: The URL endpoint for the Remote Procedure Call (RPC) interface of the blockchain network.
  • chainSymbol: The symbol representing the blockchain network.
  • blockExplorerURL: The URL of a block explorer service specific to the blockchain. Block explorers allow users to view details about blocks, transactions, addresses, and other blockchain-related data.
  • chainId: The unique identifier of the blockchain network.
  • chain: The name of the blockchain network. If you are using IndexerDataSource, it must be obtained from the registry.
  • decimals: The number of decimal places used by the native currency of the blockchain network.
  • feeGasStep: An object containing gas step values for different fee levels (high, medium, low) used in transactions.

Usage

Here's a basic example of how to use this library in your TypeScript application:

1. Import Required Modules

import { BitcoinProvider } from './chain.provider';
import LedgerSigner from './ledger.signer';
import { MsgBody, Msg } from '../msg';

2. Initialize Bitcoin Provider

Initialize the Bitcoin provider with the necessary configurations:

const provider = new BitcoinProvider(new IndexerDataSource(BITCOIN_MANIFEST));

3. Create Transaction Input

Define the transaction input data, including the sender (from), recipient (to), and the amount to send.

const txInput: MsgBody = {
  from: 'FROM ADDRESS',
  to: 'TO ADDRESS',
  amount: 0.000001,
};

4. Create a Transaction Message

Create a transaction message using the provider:

const message: Msg = provider.createMsg(txInput);

5. Sign the Transaction with Ledger

Use the LedgerSigner to sign the transaction. Provide the message and the derivation path:

const transport = await Transport.create();
const signer = new LedgerSigner(transport);
const derivationPath = "m/84'/0'/0'/0/0";

await signer.sign(message, derivationPath);
// finally close
transport.close();

6. Broadcast the Transaction

Now that the transaction is signed, you can broadcast it to the Bitcoin network using the BitcoinProvider. This step assumes that the transaction is already signed within the message.

await provider.broadcast([message]);

Fallback datasource

  • If an error occurs while fetching data from the primary data source, the FallbackDataSource will automatically switch to one of the alternative data sources configured in the constructor.
  • The number of retry attempts and the order in which the alternative data sources are used can be customized based on the requirements and preferences of the application.
import { FallbackDataSource } from '@xdefi-tech/chains-core';
import { EvmProvider, EVM_MANIFESTS } from '@xdefi-tech/chains-evm';

const provider = new EvmProvider(
  new FallbackDataSource(
    EVM_MANIFEST.ethereum,
    {
      attempts: 5,
    },
    new EvmProvider.dataSourceList.IndexerDataSource(EVM_MANIFEST.ethereum),
    new EvmProvider.dataSourceList.ChainDataSource(EVM_MANIFEST.ethereum)
  )
);

// The same as any other provider
const response = await provider.getBalance(
  '0x1234567890123456789012345678901234567890'
);
const data = await response.getData();

Retrieving a Transaction

1. Get Transaction Hash

If you have a transaction hash, you can retrieve the transaction details. Use the getTransaction method of the BitcoinProvider:

const txHash = 'TX HAS';
const txData = await provider.getTransaction(txHash);

The txData object will contain transaction details, including the transaction hash.

Disclaimer

This library is provided as-is, and we make no warranties or guarantees regarding its functionality or security. Always exercise caution and proper security practices when working with blockchain assets.

Happy blockchain development! 🚀🔗

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.7%
  • Other 1.3%