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; // nodejs modulesconst BchWallet = ;
Create new wallets
const bchWallet = ;await bchWalletwalletInfoPromise // Wait for wallet to be created. // 12 words seed phrase for the walletconsole; // cash address derived from the seed (derivation path: m/44'/145'/0'/0/0)console; // legacy address derived from the seed (derivation path: m/44'/145'/0'/0/0)console; // private key for the BCH address derived from the seed (derivation path: m/44'/145'/0'/0/0)console;
Mnemonic encryption
const bchWallet = null password: "myStrongPassword"; // 12 words seed phrase for the walletconsole; // encrypted mnemonicconsole; const bchWallet2 = bchWalletwalletInfomnemonicEncrypted password: "myStrongPassword"; // decrypted mnemonicconsole;
Initialize wallet with mnemonic
// initialize with 12 words seed phrase for the walletconst bchWallet = "minor bench until split suffer shine series bag avoid cruel orient aunt"; // initialize for specific HD derivation pathconst bchWallet2 = "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 = ; const receivers = address: "bitcoincash:qp2rmj8heytjrksxm2xrjs0hncnvl08xwgkweawu9h" // amount in satoshis, 1 satoshi = 0.00000001 Bitcoin amountSat: 100000 ; const txid = await bchWallet; // Transaction ID// you can then see the transaction in one of the explorers// example: `https://explorer.bitcoin.com/bch/tx/${tx.txid}`;console;
Get Wallet Balance
Gets balance (confirmed + unconfirmed) for an BCH address
// will get a balance for bchWallet.cashAddressconst myBalance = await bchWallet; // will get a balance for any addressconst balanceOfOtherAddress = await bchWallet;
Get Wallet Transaction History
Get an array of TXIDs of the transactions involving this wallet.
// will get transaction history for bchWallet.cashAddressconst myTransactions = await bchWallet; // will get transaction history for any addressconst txHistoryOfOtherAddress = await bchWallet;
Error Handling
try tx = await bchWallet; catch err console; if errmessage && errmessage > -1 return ; return ;
Save keys in the browser
While developing BCH apps, remember to never send the private keys / mnemonic / seed phrase to your servers.
- Your servers can be hacked
- Depending on your jurisdiction you may not have the allowance to manage the funds of your users
const bchWallet1 = ; // save the mnemonic for laterlocalStorage; // retrieve mnemonic to initialize the walletconst bchWallet2 = localStorage;