ecashaddrjs

eCash cashaddr address format support for Node.js and web browsers.

Usage no npm install needed!

<script type="module">
  import ecashaddrjs from 'https://cdn.skypack.dev/ecashaddrjs';
</script>

README

eCashAddr.js: The eCash address format for Node.js and web browsers.

NPM

JavaScript implementation for CashAddr address format for eCash.

Compliant with the original CashAddr specification which improves upon BIP 173.

Note: This is a JavaScript implementation of the CashAddr format specification. If you are looking for a general purpose Bitcoin Cash address translation library, check out the easy-to-use and well-tested BchAddr.js.

Installation

Using NPM

$ npm install --save ecashaddrjs

Manually

You may also download the distribution file manually and place it within your third-party scripts directory: dist/cashaddrjs-1.0.7.min.js.

Usage

Convert a bitcoincash: prefixed address to an ecash: prefixed address

In Node.js

const ecashaddr = require('ecashaddrjs');
const bitcoincashAddress = 'bitcoincash:qpadrekpz6gjd8w0zfedmtqyld0r2j4qmuj6vnmhp6'
const { prefix, type, hash } = ecashaddr.decode(bitcoincashAddress);
console.log(prefix); // 'bitcoincash'
console.log(type); // 'P2PKH'
console.log(hash); // Uint8Array [ 118, 160, ..., 115 ]
console.log(ecashaddr.encode('ecash', type, hash)); 
// 'ecash:qpadrekpz6gjd8w0zfedmtqyld0r2j4qmuthccqd8d'

React

import cashaddr from 'ecashaddrjs';

function convertBitcoincashToEcash(bitcoincashAddress) {    
  /* NOTE 
  This function assumes input parameter 'bitcoincashAddress' is a valid bitcoincash: address
  cashaddr.decode() will throw an error if 'bitcoincashAddress' lacks a prefix 
  */
  const { prefix, type, hash,  } = cashaddr.decode(
          bitcoincashAddress,
      );
  const ecashAddress = cashaddr.encode('ecash', type, hash);
  return ecashAddress;
}   

Browser

<html>
  <head>    
    <script src="https://unpkg.com/ecashaddrjs@1.0.7/dist/cashaddrjs-1.0.7.min.js"></script>
  </head>
  <body>
  <script>
  function convertBitcoincashToEcash(bitcoincashAddress) {    
    /* NOTE 
    This function assumes input parameter 'bitcoincashAddress' is a valid bitcoincash: address
    cashaddr.decode() will throw an error if 'bitcoincashAddress' lacks a prefix 
    */
    const { prefix, type, hash,  } = cashaddr.decode(
            bitcoincashAddress,
        );
    const ecashAddress = cashaddr.encode('ecash', type, hash);
    return ecashAddress;
  }
  const eCashAddr = convertBitcoincashToEcash('bitcoincash:qpadrekpz6gjd8w0zfedmtqyld0r2j4qmuj6vnmhp6')
  console.log(eCashAddr)
  // ecash:qpadrekpz6gjd8w0zfedmtqyld0r2j4qmuthccqd8d
  </script>
  </body>
</html>

Script Tag

You may include a script tag in your HTML and the ecashaddr module will be defined globally on subsequent scripts.

<html>
  <head>
    ...
    <script src="https://unpkg.com/ecashaddrjs@1.0.7/dist/cashaddrjs-1.0.7.min.js"></script>
  </head>
  ...
</html>

jsFiddle

https://jsfiddle.net/zghd6c2y/

Change Log

Aug 4, 2021 - Added support for ectest: prefix