npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

multichain-tools

v2.0.16

Published

MultiChain Tools is a TypeScript package that provides a set of utilities and functions for interacting with various blockchain networks, including Ethereum (EVM-based chains) and Bitcoin. It offers an easy way to sign and send transactions, fetch derived

Downloads

725

Readme

MultiChain Tools

MultiChain Tools is a TypeScript package that provides a set of utilities and functions for interacting with various blockchain networks, including Ethereum (EVM-based chains) and Bitcoin. It offers an easy way to sign and send transactions, fetch derived addresses, and estimate transaction fees using a single NEAR account that controls the keys for the other chains.

Supported Chains

  • BTC
  • EVM

Installation

To install MultiChain Tools, use npm:

npm install multichain-tools

Usage

To use MultiChain Tools in your project, import the required functions and classes:

import {
  signAndSendEVMTransaction,
  signAndSendBTCTransaction,
  fetchDerivedEVMAddress,
  fetchBTCFeeProperties,
  fetchDerivedBTCAddress,
  fetchEstimatedEVMFee,
  fetchEVMFeeProperties,
  fetchDerivedBTCAddressAndPublicKey,
} from 'multichain-tools'

Derived Path

In this repository, we frequently utilize derived paths, which enable the generation of new keys using the combination of a Root Key and a String, resulting in a Child Key.

For more detailed information, please refer to the NEAR Documentation on Chain Signatures.

To ensure consistency and predictability in providing the key path, we recommend using JSON Canonical Serialization. This standardizes the format of the path, making it easier to understand and use. But, you can also provide a plain string.

Here's an example of how to provide a derived path using Canonical Serialization:

import canonicalize from 'canonicalize'

const derivedPath = canonicalize({
  chain: 'BTC',
  domain: 'example.com',
  meta: {
    prop1: 'prop1',
  },
})

or

const derivedPath = 'myderivedpath,btc'

In the example above:

  • chain: Specifies the chain for which you are requesting the signature, such as BTC (Bitcoin), ETH (Ethereum), etc.
  • domain: Represents the domain of the dApp (e.g., www.example.com).
  • meta: Allows you to include any additional information you want to incorporate into the key path.

By following this approach, you can create standardized and predictable derived paths for generating child keys based on a root key and a specific string combination.

Key Pair

In this repository, we will also utilize NEAR account key pairs, which are essentially the private and public keys of an account used to sign transactions.

To create a key pair, you can use the following code:

import { KeyPair } from 'near-api-js'

const nearAccountKeyPair = KeyPair.fromString(
  process.env.NEXT_PUBLIC_NEAR_PRIVATE_KEY
)

Examples

Signing and Sending an EVM Transaction

const evmRequest: EVMRequest = {
  transaction: {
    to: '0x742d35Cc6634C0532925a3b844Bc454e4438f44e',
    value: '1000000000000000000', // In wei
    derivedPath,
  },
  chainConfig: {
    providerUrl: 'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID',
    contract: 'v2.multichain-mpc.testnet',
  },
  nearAuthentication: {
    networkId: 'testnet',
    keypair: nearAccountKeyPair,
    accountId: 'signer.near',
  },
}

const response: Response = await signAndSendEVMTransaction(evmRequest)

Signing and Sending a Bitcoin Transaction

const btcRequest: BitcoinRequest = {
  chainConfig: {
    providerUrl: 'https://btc.example.com/api/',
    contract: 'v2.multichain-mpc.testnet',
    networkType: 'testnet',
  },
  transaction: {
    to: '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2',
    value: '1000000', // In satoshis
    derivedPath,
  },
  nearAuthentication: {
    networkId: 'mainnet',
    keypair: nearAccountKeyPair,
    accountId: 'signer.near',
  },
}

const response: Response = await signAndSendBTCTransaction(btcRequest)

Fetching Derived Addresses

const evmAddress: string = await fetchDerivedEVMAddress(
  'signer.near',
  derivedPath,
  'testnet',
  'v2.multichain-mpc.testnet'
)

import * as bitcoinlib from 'bitcoinjs-lib'

const { address: btcAddress, publicKey: btcPublicKey } =
  await fetchDerivedBTCAddressAndPublicKey(
    'signer.near',
    derivedPath,
    bitcoinlib.networks.testnet,
    'testnet',
    'v2.multichain-mpc.testnet'
  )

Fetching Transaction Fee Properties

const evmFeeProperties = await fetchEVMFeeProperties(
  'https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID',
  {
    to: '0x0987654321098765432109876543210987654321',
    value: ethers.parseEther('1'),
  }
)

const btcFeeProperties = await fetchBTCFeeProperties(
  'https://btc.example.com/api/',
  '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2',
  [{ address: '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2', value: 1000000 }]
)