Giter Club home page Giter Club logo

bchaddrjs's Introduction

BchAddr.js: Bitcoin Cash general purpose address translation for Node.js and web browsers.

NPM

JavaScript Style Guide

Plug & Play JavaScript library for all Bitcoin Cash address format translation needs. Easy-to-use, thoroughly tested, and feature complete.

Support for the new Bitcoin Cash address format which improves upon BIP 173, as well as the Bitpay and Legacy formats.

Test out a demo address translator powered by BchAddr.js here.

Installation

Using NPM

$ npm install --save bchaddrjs-slp

Using Bower

$ bower install --save bchaddrjs-slp

Usage

In Node.js

import * as bchaddr from "bchaddrjs-slp";
var bchaddr = require('bchaddrjs-slp');

Browser

Script Tag

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

<html>
  <head>
    ...
    <script src="https://unpkg.com/[email protected]/dist/bchaddrjs-slp-0.2.14.min.js" crossorigin="anonymous"></script>
  </head>
  ...
</html>

Code Examples

Supported formats, networks and address types.

var Format = bchaddr.Format; // Legacy, Bitpay or Cashaddr.
var Network = bchaddr.Network; // Mainnet or Testnet.
var Type = bchaddr.Type; // P2PKH or P2SH.

Test for address format.

var isLegacyAddress = bchaddr.isLegacyAddress;
var isBitpayAddress = bchaddr.isBitpayAddress;
var isCashAddress = bchaddr.isCashAddress;

isLegacyAddress('1B9UNtBfkkpgt8kVbwLN9ktE62QKnMbDzR') // true
isLegacyAddress('qph5kuz78czq00e3t85ugpgd7xmer5kr7c5f6jdpwk') // false
isBitpayAddress('CScMwvXjdooDnGevHgfHjGWFi9cjk75Aaj') // true
isBitpayAddress('1B9UNtBfkkpgt8kVbwLN9ktE62QKnMbDzR') // false
isCashAddress('qph5kuz78czq00e3t85ugpgd7xmer5kr7c5f6jdpwk') // true
isCashAddress('CScMwvXjdooDnGevHgfHjGWFi9cjk75Aaj') // false

Test for address network.

var isMainnetAddress = bchaddr.isMainnetAddress;
var isTestnetAddress = bchaddr.isTestnetAddress;

isMainnetAddress('1P238gziZdeS5Wj9nqLhQHSBK2Lz6zPSke') // true
isMainnetAddress('mnbGP2FeRsbgdQCzDT35zPWDcYSKm4wrcg') // false
isTestnetAddress('qqdcsl6c879esyxyacmz7g6vtzwjjwtznsggspc457') // true
isTestnetAddress('CeUvhjLnSgcxyedaUafcyo4Cw9ZPwGq9JJ') // false

Test for address type.

var isP2PKHAddress = bchaddr.isP2PKHAddress;
var isP2SHAddress = bchaddr.isP2SHAddress;

isP2PKHAddress('1Mdob5JY1yuwoj6y76Vf3AQpoqUH5Aft8z') // true
isP2PKHAddress('2NFGG7yRBizUANU48b4dASrnNftqsNwzSM1') // false
isP2SHAddress('H92i9XpREZiBscxGu6Vx3M8jNGBKqscBBB') // true
isP2SHAddress('CeUvhjLnSgcxyedaUafcyo4Cw9ZPwGq9JJ') // false

Detect address format.

var detectAddressFormat = bchaddr.detectAddressFormat;

detectAddressFormat('qqdcsl6c879esyxyacmz7g6vtzwjjwtznsggspc457') // Format.Cashaddr
detectAddressFormat('CScMwvXjdooDnGevHgfHjGWFi9cjk75Aaj') // Format.Bitpay

Detect address network.

var detectAddressNetwork = bchaddr.detectAddressNetwork;

detectAddressNetwork('1P238gziZdeS5Wj9nqLhQHSBK2Lz6zPSke') // Network.Mainnet
detectAddressNetwork('qqdcsl6c879esyxyacmz7g6vtzwjjwtznsggspc457') // Network.Testnet

Detect address type.

var detectAddressType = bchaddr.detectAddressType;

detectAddressType('1P238gziZdeS5Wj9nqLhQHSBK2Lz6zPSke') // Type.P2PKH
detectAddressType('3NKpWcnyZtEKttoQECAFTnmkxMkzgbT4WX') // Type.P2SH

Translate address from any address format into a specific format.

var toLegacyAddress = bchaddr.toLegacyAddress;
var toBitpayAddress = bchaddr.toBitpayAddress;
var toCashAddress = bchaddr.toCashAddress;

toLegacyAddress('qph5kuz78czq00e3t85ugpgd7xmer5kr7c5f6jdpwk') // 1B9UNtBfkkpgt8kVbwLN9ktE62QKnMbDzR
toBitpayAddress('1B9UNtBfkkpgt8kVbwLN9ktE62QKnMbDzR') // CScMwvXjdooDnGevHgfHjGWFi9cjk75Aaj
toCashAddress('1B9UNtBfkkpgt8kVbwLN9ktE62QKnMbDzR') // bitcoincash:qph5kuz78czq00e3t85ugpgd7xmer5kr7c5f6jdpwk

Documentation

Generate and Browse Locally

$ npm run docs

Online

Browse automatically generated jsdocs online.

bchaddrjs's People

Contributors

ealmansi avatar jcramer avatar joshmh avatar knoxcard avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

bchaddrjs's Issues

Typings updates

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/bchaddrjs-slp/src/bchaddr.d.ts b/node_modules/bchaddrjs-slp/src/bchaddr.d.ts
index 37a8b4b..a1a7bb1 100644
--- a/node_modules/bchaddrjs-slp/src/bchaddr.d.ts
+++ b/node_modules/bchaddrjs-slp/src/bchaddr.d.ts
@@ -1,11 +1,22 @@
-export type Format = string;
-
-export type Network = string;
-
-export type Type = string;
+export enum Format {
+    Legacy = "legacy",
+    Bitpay = "bitpay",
+    Cashaddr = "cashaddr",
+    Slpaddr = "slpaddr"
+};
+
+export enum Network {
+    Mainnet = "mainnet",
+    Testnet = "testnet"
+};
+
+export enum Type {
+    P2PKH = "p2pkh",
+    P2SH = "p2sh"
+};
 
 export interface decoded {
-    hash: Uint8Array;
+    hash: Array<number>;
     format: Format;
     network: Network;
     type: Type;

This issue body was partially generated by patch-package.

Add enums to .d.ts

Hi! ๐Ÿ‘‹

Firstly, thanks for your work on this project! ๐Ÿ™‚

Today I used patch-package to patch [email protected] for the project I'm working on.

Here is the diff that solved my problem:

diff --git a/node_modules/bchaddrjs-slp/src/bchaddr.d.ts b/node_modules/bchaddrjs-slp/src/bchaddr.d.ts
index 37a8b4b..abce6f4 100644
--- a/node_modules/bchaddrjs-slp/src/bchaddr.d.ts
+++ b/node_modules/bchaddrjs-slp/src/bchaddr.d.ts
@@ -1,8 +1,19 @@
-export type Format = string;
-
-export type Network = string;
-
-export type Type = string;
+export enum Format {
+    Legacy = "legacy",
+    Bitpay = "bitpay",
+    Cashaddr = "cashaddr",
+    Slpaddr = "slpaddr"
+};
+
+export enum Network {
+    Mainnet = "mainnet",
+    Testnet = "testnet"
+};
+
+export enum Type {
+    P2PKH = "p2pkh",
+    P2SH = "p2sh"
+};
 
 export interface decoded {
     hash: Uint8Array;

This issue body was partially generated by patch-package.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.