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

bch-js-ext

v0.1.2

Published

Extensions to @psf/bch-js library

Downloads

22

Readme

bch-js-ext

Extensions to PSF bch-js JavaScript library.

bch-js is a great JavaScript library for working with Bitcoin Cash. The easiest way is to use it with the software stack provided by FullStack.cash. The current library provides some extensions to bch-js like:

  • Some useful methods for getting detailed UTXO information (Ext module)
  • Easier transactions building (TXBuilder module)
  • Working with groups of NFTs (NFT module)

Usage

  • Install library: npm install bch-js-ext
  • Instantiate the library in your code:
const MAINNET_API = 'https://api.fullstack.cash/v3/'
const SLPDB_API = 'https://slpdb.fountainhead.cash/'

const BCHJSEXT = require('bch-js-ext')
const bchjs = new BCHJSEXT({
  restURL: MAINNET_API,
  slpdbURL: SLPDB_API,
  apiToken: process.env.BCHJSTOKEN
})

For more detailed usage see the examples directory code.

Library methods

Ext module

These methods are used also in the other modules.

  • validAddress(address) - check if given BCH or SLP address is valid

    • address: String. example: 'bitcoincash:eedddd...' or 'simpleledger:....'
    • return: true/false
  • findBiggestUtxo(utxos) - find UTXO with biggest value in an Array of UTXOs

    • utxos: Array of UTXO objects
    • return: UTXO object
 const data = await bchjs.Electrumx.utxo('bitcoincash:dsffff...')
 const utxos = data.utxos
 const utxo = await bchjs.Ext.findBiggestUtxo(utxos)
  • findPaymentUtxo(address, amount = 0) - using findBiggestUtxo() method above to find the UTXO with enough value in satoshis
    • address: String. example: `'bitcoincash:eedddd...'
    • amount: Integer (optional). If provided will check if UTXO have such amount of satoshis. If not provided will not check UTXO value at all
const paymentUtxo = await this.bchjs.Ext.findPaymentUtxo('bitcoincash:dsffff...')
  • getUtxoDetails(address) - detailed information about UTXOs (hydreted UTXOs)
    • address: String. example: `'bitcoincash:eedddd...'
    • return: Array of hydrated UTXOs
const tokenUtxos = await bchjs.Ext.getUtxoDetails('bitcoincash:dsffff...')
  • Get UTXOs with detailed information about particular token (specified by ID or ticker)

    • getTokenUtxos(address, tokenId, types = {})
    • getTickerUtxos(address, ticker, types = {})
      • address: String. example: `'bitcoincash:eedddd...'
      • tokenId: String. example: 'b34esadd...'
      • ticker: String. example: 'NFT_TOKEN'
      • return: Array of hydrated UTXOs
  • filterTokenUtxos(utxos, prefix) - filter Array of token UTXOs for ones, which ticker starts with prefix

    • utxos: Array of hydrated UTXOs
    • return: Array of hydrated UTXOs
const tokenUtxos = await bchjs.Ext.getUtxoDetails('bitcoincash:dsffff...')
const nameServiceUTXOs = await bchjs.Ext. filterTokenUtxos(tokenUtxos, '_ns.')

TXBuilder module

These methods are used also in the NFT module.

Attaching input and output UTXOs during transaction building contain a lot of boilerplate code. Specially signing ot input adds a lot of similar code. This module trying to make transaction buildeing easier. TODO: maybe replace with CashScript in the future

  • addSignedInputs(wif, inputs) - add Array of input UTXOs and sign them

    • wif: String. Private key (WIF) for signing. example: 'L4.....'
    • inputs: Array of { utxo: <UTXO object>, value: Integer } objects. value is optional. if not provided - using UTXO value
  • addOutputs (outputs) - add array of output UTXOs (payment, remainder etc.)

    • outputs: Array of { out: <address_or_script>, value: Integer } objects. value is optional. if not provided - using UTXO value

Example (NFT group creation)

const paymentUtxo = await this.bchjs.Ext.findPaymentUtxo(account.address)
const remainder = paymentUtxo.value - (2 * DUST) - TX_FEE
const txBuilder = new this.bchjs.TXBuilder(this.bchjs)
const script = this.bchjs.SLP.NFT1.newNFTGroupOpReturn(mintConfig)
const outputs = [
  { out: script }, // OUT#0 OP_RETURN
  { out: legacyAddress, value: DUST }, // OUT#1 token
  { out: legacyAddress, value: DUST }, // OUT#2 minting baton
  { out: bchAddress, value: remainder } // OUT#3 remainder
]
const inputs = [{ utxo: paymentUtxo }] // IN#0 pay
txBuilder.addSignedInputs(wif, inputs)

See NFT examples in the example directory for more detailed usage information.

NFT module

Methods for easy Group/Children NFT non-fungible tokens operations - create, transfer, burn etc.

All of the methods have optional last parameter send:

  • true: send the TX to the blockchain
  • false: just create transaction HEX for inspection (default)

account parameter used as a first parameter in most of the calls:

config = {
  address: 'bitcoincash: bdddfd....',
  wif: 'L3.....'
}
  • createGroup(account, config, send) - create NFT group - can be used to create NFT children in the same group
    • account: Object with address and private key.
    • config: Object with token parameters. examples: { name: 'NFT NS', prefix: '_ns' }, { name: 'TLD', ticker: '_ns.dom' }
const groupNFT = {
  name: 'Rare Items',
  ticker: '_rare.items'
}
const groupId = await bchjs.NFT.createGroup(account, groupNFT, true)

For more information: examples/nft_group.js

  • createChild(account, config, send) - create NFT child inside given NFT group
    • account: Object with address and private key.
    • config: Object with token parameters. examples: { group: 'b3sssdd...', name: 'CHLD1', prefix: '_uns' }, { group: 'b3sssdd...', name: 'CHLD1', ticker: '_yns.bbb.aaaa' }
const childNFT = {
  group: 'dwgfdd...' // for example groupId in the example above
  name: 'Very Rare Item #1',
  ticker: '_item.tresure.one'
}
const itemId = await bchjs.NFT.createChild(paymentAccount, childNFT, true)

For more information: examples/nft_child.js

  • transferChild(account, config, toSlpAddress, send) - transfer ownership of token to another account (simple token sending to SLP address)
    • account: Object with address and private key.
    • config: Object with token information. example: { id: 'b3sssdd...' }, { ticker: '_uns.bbb.aaa' }. Can even reuse NFT child creation config above.
    • toSlpAddress: String. SLP address to send token to. example: 'simpleledger:ddsvs...'
const config = { id: 'dsfsdf...' } // for example tokenId from the example above
await bchjs.NFT.transferChild(account, config, 'simpleledger:dfgdfggd...', true)

For more information: examples/nft_transfer.js

  • removeChild(account, config, send) - burn child NFT token
    • account: Object with address and private key.
    • config: Object with token information. example: { id: 'b3sssdd...' }, { ticker: '_uns.bbb.aaa' }. Can even reuse NFT child creation config above.
const config = { id: 'dsfsdf...' } // for example tokenId from the example above
await bchjs.NFT.removeChild(account, config, true)
  • findChildren(groupId) - find all non-burned NFT children in a given group (created from the same group NFT token)
    • groupId: String. token ID of the NFT group - for example groupId in the examples above
    • return: Array of objects with token information (SLPDB response format)
const children = await bchjs.NFT.findChildren('dhdfhf...')
  • tokenAddresses(tokenId) - return the SLP address, owning given NFT child token
    • tokenId: String. token ID of the NFT child
    • return: String - SLP address
const itemAddr = await bchjs.NFT.tokenAddresses('dhdfhf...')