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

minimal-bch-wallet

v1.3.3

Published

A minimalist Bitcoin Cash (BCH) wallet npm library, for use in a web apps.

Downloads

32

Readme

minimal-bch-wallet

This is a minalist Bitcoin Cash (BCH) wallet for use with front end web apps. It's forked from the Simple Bitcoin Wallet developed by Honest.cash. It's intended to be used with the gatsby-ipfs-web-wallet and bch-js provided by FullStack.cash

The default derivation path for the wallet keypair is m/44'/145'/0'/0/0. This is the BIP44 standard for BCH wallets.

How to use it?

Import

Add to your HTML scripts

<script src="https://unpkg.com/minimal-bch-wallet"></script>

Node module

npm install minimal-bch-wallet --save
// module import
import BchWallet from "minimal-bch-wallet";

// nodejs modules
const BchWallet = require("minimal-bch-wallet");

Create new wallets

const bchWallet = new BchWallet();
await bchWallet.walletInfoPromise // Wait for wallet to be created.

// 12 words seed phrase for the wallet
console.log(bchWallet.walletInfo.mnemonic);

// cash address derived from the seed (derivation path: m/44'/145'/0'/0/0)
console.log(bchWallet.walletInfo.cashAddress);

// legacy address derived from the seed (derivation path: m/44'/145'/0'/0/0)
console.log(bchWallet.walletInfo.legacyAddress);

// private key for the BCH address derived from the seed (derivation path: m/44'/145'/0'/0/0)
console.log(bchWallet.walletInfo.privateKey);

Mnemonic encryption

const bchWallet = new BchWallet(null, {
  password: "myStrongPassword"
});

// 12 words seed phrase for the wallet
console.log(bchWallet.walletInfo.mnemonic);

// encrypted mnemonic
console.log(bchWallet.walletInfo.mnemonicEncrypted);

const bchWallet2 = new BchWallet(bchWallet.walletInfo.mnemonicEncrypted, {
  password: "myStrongPassword"
});

// decrypted mnemonic
console.log(bchWallet2.walletInfo.mnemonic);

Initialize wallet with mnemonic

// initialize with 12 words seed phrase for the wallet
const bchWallet = new BchWallet("minor bench until split suffer shine series bag avoid cruel orient aunt");

// initialize for specific HD derivation path
const bchWallet2 = new BchWallet("minor bench until split suffer shine series bag avoid cruel orient aunt", {
    HdPath: "m/44'/145'/0'/1'"
});

Send transactions

You can send funds to other BCH wallets. You can distribute funds to N users by simply extending the receiver array.

const bchWallet = new BchWallet();

const receivers = [
    {
        address: "bitcoincash:qp2rmj8heytjrksxm2xrjs0hncnvl08xwgkweawu9h",
        // amount in satoshis, 1 satoshi = 0.00000001 Bitcoin
        amountSat: 100000
    }
];

const txid = await bchWallet.send(receivers);

// Transaction ID
// you can then see the transaction in one of the explorers
// example: `https://explorer.bitcoin.com/bch/tx/${tx.txid}`;
console.log(txid);

Get Wallet Balance

Gets balance (confirmed + unconfirmed) for an BCH address

// will get a balance for bchWallet.cashAddress
const myBalance = await bchWallet.getBalance();

// will get a balance for any address
const balanceOfOtherAddress = await bchWallet.getBalance("bitcoincash:qp2rmj8heytjrksxm2xrjs0hncnvl08xwgkweawu9h");

Get Wallet Transaction History

Get an array of TXIDs of the transactions involving this wallet.

// will get transaction history for bchWallet.cashAddress
const myTransactions = await bchWallet.getTransactions();

// will get transaction history for any address
const txHistoryOfOtherAddress = await bchWallet.getTransactions("bitcoincash:qp2rmj8heytjrksxm2xrjs0hncnvl08xwgkweawu9h");

Error Handling

try {
    tx = await bchWallet.send([
        { address: "bitcoincash:qrlhkg4d9z3y88j246a6482xzregxaxnfsagmd2kh3", amountSat: 1000 }
    ]);
} catch (err) {
    console.error(err);

    if (err.message && err.message.indexOf("Insufficient") > -1) {
        return alert("Insufficient balance on your BCH account.");
    }

    return alert("Error. Try again later.");
}

Save keys in the browser

While developing BCH apps, remember to never send the private keys / mnemonic / seed phrase to your servers.

  1. Your servers can be hacked
  2. Depending on your jurisdiction you may not have the allowance to manage the funds of your users
const bchWallet1 = new BchWallet();

// save the mnemonic for later
localStorage.setItem("BCH_MNEMONIC", bchWallet1.walletInfo.mnemonic);

// retrieve mnemonic to initialize the wallet
const bchWallet2 = new BchWallet(localStorage.getItem("BCH_MNEMONIC"));

Licence

MIT