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

@xchainjs/xchain-client

Package Overview
Dependencies
Maintainers
4
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@xchainjs/xchain-client

A specification for a generalised interface for crypto wallets clients, to be used by XChainJS implementations. The client should not have any functionality to generate a key, instead, the `asgardex-crypto` library should be used to ensure cross-chain com

  • 0.0.7
  • npm
  • Socket score

Version published
Weekly downloads
96
decreased by-65.09%
Maintainers
4
Weekly downloads
 
Created
Source

XChainJS Wallet Client Interface

A specification for a generalised interface for crypto wallets clients, to be used by XChainJS implementations. The client should not have any functionality to generate a key, instead, the asgardex-crypto library should be used to ensure cross-chain compatible keystores are handled. The client is only ever passed a master BIP39 phrase, from which a temporary key and address is decoded.

Configuration

Initialise and set up the client to connect to its necessary third-party services to fulfil basic functionality. The third-party services used must be at a minimum to fulfil the wallet functionality, such as displaying balances and sending transactions.

During configuration, the following can be passed in:

  • Network choice (default is MAINNET)
  • Phrase (mandatory)
  • Service Keys (optional, if null, client will use config defaults or free service limits.)

Querying

Querying the client for balances and transaction history. Transaction history is optional.

Optional blockchain-specific queries can be added here, such as Binance Chain market information.

Transactions

Making transfers.

Optional blockchain-specific transactions can be added here, such as Binance Chain freeze/unfreeze.


Class Variables

Client

Public variable that returns the current client

public client: XChainClient 

Network

Public variable that returns the current network

public network: Network 

PrivateKey

Private variable that retains the private key that was extracted from the phrase during initialisation. The phrase should not be retained.

private privkey: PrivKey | null = null

Address

Public variable that returns the address decoded from the private key during initialisation.

public address: Address

Config and Setup

Set Network

Used to set a type of NETWORK, which is either MAINNET or TESTNET.

enum Network {
  TEST = 'testnet',
  MAIN = 'mainnet',
}
setNetwork(net: Network): XChainClient

Returns the client.

Set Phrase

Used to set the master BIP39 phrase, from which the private key is extracted and the address decoded.

setPhrase(phrase: string): address

The function should store the private key and address, then return the address generated by the phrase.

Set Service Keys

The client is expected to know which services it needs, handle errors and have hard-coded sensible defaults if errors. The client should export a type allowing devs to know which services it needs, as well as documentation to explain how to get keys.

If no key is set, either the service has no API key or the client is expected to stay within node rate limits.

Example:

  1. Blockchair (for querying and broadcasting)
  2. Ethplorer (for getting address balances)
  3. EthGasStation (for getting gas prices)
  4. Binance Chain node for Binance queries
  • etc
export type ServiceKeys = {
  [key: string]: { // name of the service (ie `blockchair`)
    serviceKey: string // Optional API Key for the service
  }
}
setServiceKeys(keys: ServiceKeys)

Querying

Get Explorer URL

Returns the correctly formatted url string with paths for:

  • Addresses
  • Transactions

The default Explorer URL can be hard-coded, or passed in as a service. The object to be cast into the explorer link should be passed in as a string, like a transaction ID or an address.

enum Path {
  address = 'address',
  transaction = 'transaction',
}
getExplorerUrl(type: Path, param: string): string

The function should return the correctly formatted url string.

Example

https://blockchair.com/bitcoin/transaction/d11ff3352c50b1f5c8e2030711702a2071ca0e65457b40e6e0bcbea99e5dc82e
https://blockchair.com/bitcoin/address/19iqYbeATe4RxghQZJnYVFU4mjUUu76EA6

https://explorer.binance.org/tx/94F3A6257337052B04F9CC09F657966BFBD88546CA5C23F47AB0A601D29D8979
https://explorer.binance.org/address/bnb1z35wusfv8twfele77vddclka9z84ugywug48gn

https://etherscan.io/tx/0x87a4fa498cc48874631eaa776e84a49d28f42f01e22c51ff7cdfe1f2f6772f67
https://etherscan.io/address/0x8eb68e8f207be3dd1ec4baedf0b5c22245cda463

Get Balance

Returns the balance of an address.

  • If address is not passed, gets the balance of the current client address.
  • Optional asset can be passed, in which the query will be specific to that asset, such as ERC-20 token.
  • Returns an array of assets and amounts, with assets in chain notation CHAIN.SYMBOL-ID
  • Converts to 8 decimal places for conformity
getBalance(address?: Address, asset?: string): Promise<Balances>

Example of third-party service queries to get balances:

https://api.blockchair.com/bitcoin/addresses/balances?addresses=34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo
https://api.ethplorer.io/getAddressInfo/0xb00E81207bcDA63c9E290E0b748252418818c869?apiKey=freekey
https://dex.binance.org/api/v1/account/bnb1jxfh2g85q3v0tdq56fnevx6xcxtcnhtsmcu64m

Example of returned array:

[
  { 
    "asset" : "BTC.BTC"
    "amount" : 100000000
  }
]

Get Transactions

Gets a simplied array of recent transactions for an address.

export type TxHistoryParams = {
  address: Address // Address to get history for
  offset: number // Optional Offset
  limit: string // Optional Limit of transactions
  startTime: string // Optional start time
}
getTransactions(params?: TxHistoryParams): Promise<TxPage>

Example of third party services to help:

// get UTXOS for address
https://api.blockchair.com/bitcoin/outputs?recipient=34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo
// get tx details for each UTXO
https://api.blockchair.com/bitcoin/dashboards/transactions/ff0bd969cce99b8d8086e452d7b63167fc178680fee796fc742cb14a9a6ef929

https://api.ethplorer.io/getAddressTransactions/0xb297cacf0f91c86dd9d2fb47c6d12783121ab780?apiKey=freekey
https://dex.binance.org/api/v1/transactions?address=bnb1jxfh2g85q3v0tdq56fnevx6xcxtcnhtsmcu64m

Example of return:

[
  { 
    "hash" : "980D9519CCB39DC02F8B0208A4D181125EE8A2678B280AF70666288B62957DAE",
    "from" : "34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo",
    "to" : 34vRoCGJym3xR7yCVPFHoCNxv4Twseoxp4,
    "amount": 100000000,
    "asset" : "BTC.BTC",
    "fee" : 2500,
    "memo" : "transfer"
    "date" : "2020-10-04T06:24:36.548Z"
   },
   { 
    "hash" : "0D9519CCB39DC02F8B0208A4D181125EE8A2678B280AF70666288B62957DAE98",
    "from" : "34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo",
    "to" : 34vRoCGJym3xR7yCVPFHoCNxv4Twseoxp4,
    "amount": 200000000,
    "asset" : "BTC.BTC",
    "fee" : 2500,
    "memo" : "transfer"
    "date" : "2020-10-04T06:24:36.548Z"
   },
]

Due to the complexity of this function and dependence of third-party services, this function can be omitted in early versions of the client.


Transactions

Get Fees

This function calculates and returns the fee object in a generalised way for a simple transfer function. ASGARDEX userbase has high time preferences, so high fee rates are preferred.

Since this depends on optional third-party services, sensible defaults should be hardcoded if there are errors.

The fastest fee rate should be guaranteed next block (1.5x Fast), fast should be 1-2 blocks (1x next block fee rate), average should be 2-3 blocks (0.5x Fast). Don't over-complicate this. PoW blockchains have no guarantees.

  • Type should specify the units to display, or if flat fees, simply "flat". The client should interpret and display this, such as showing the user the fee rates and their units.
  • Fastest (target of next block)
  • Fast (target of 1-2 blocks)
  • Average (target of 2-3 blocks)

Third party services: Bitcoin - returns next block feeRate (fast). Use multiples of this to extrapolate to Fastest/Average. https://api.blockchair.com/bitcoin/stats

Ethereum - returns fastest/fast/average https://ethgasstation.info/api/ethgasAPI.json?api-key=XXAPI_Key_HereXXX

getFees(): Promise<Fees>

Examples

// Bitcoin (sats/byte)
{
  "type" : "sats/byte"
  "fastest" : 100
  "fast" : 50
  "average" : 20
}
// Ethereum (gwei)
{
  "type" : "gwei"
  "fastest" : 70
  "fast" : 50
  "average" : 40
}
// Binance Chain (flat rate)
{
  "type" : "flat"
  "average" : 37500
}

Transfer

General transfer function that should be signed and broadcast using a third party service. The fee should always be rate, which is units per transaction size. The size should be calculated on the fly or hardcoded:

  • Bitcoin: 250 bytes is typical, so feeRate of 10 is 10 sats per byte, eg, 2500 sats
  • Ethereum: gwei is standard, so a feeRate of 20 would be interpreted as 20 GWEI
  • Binance Chain: fixed size, so the feeRate is ignored.

Broadcast URLs

https://api.blockchair.com/{:chain}/push/transaction
https://dex.binance.org/api/v1/broadcast
export type TxParams = {
  asset: string // BTC.BTC
  amount: number // in base format (10**8)
  recipient: address // address
  feeRate: number // optional feeRate 
  memo: string // optional memo to pass
}

transfer(params: TxParams): Promise<TransferResult>

The function should return the hash of the finalised transaction.

Chain Specific

Chain-specific transactions can be added as well, such as Ethereum approve() or Binance Chain freeze()/unfreeze()

export type ApproveParams = {
  asset: string // BTC.BTC
  amount: number // in base format (10**8)
  spender: address // address
}
approve(params: ApproveParams): Promise<TransferResult>

The function should return the hash of the finalised transaction.

Purge

When a wallet is "locked" the private key should be purged in each client by setting it back to null.

purgeClient()

FAQs

Package last updated on 09 Nov 2020

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