Beignet
:warning: This is pre-alpha software and not suitable for production apps yet.
Description
An instant, self-custodial Bitcoin wallet for JS devs.
This Typescript library offers JS developers a way to incorporate an on-chain, self-custodial Bitcoin wallet into their projects.
Table of Contents
- Getting Started
- Running Tests & Examples
- Implementation
- Advanced Usage
- Documentation
- Support
Getting Started
yarn add beignet
npm i -S beignet
Running Tests & Examples
Clone the Repository
git clone git@github.com:synonymdev/beignet.git && cd beignet
Install Dependencies & Build
npm i && npm run build
Run tests:
npm run test
Run example project:
npm run example
Implementation
import { Wallet, generateMnemonic } from 'beignet';
const mnemonic = generateMnemonic();
const createWalletRes = await Wallet.create({ mnemonic });
if (createWalletRes.isErr()) return;
const wallet = createWalletRes.value;
const walletData = wallet.data;
const address = await wallet.getAddress();
const addressBalanceRes = await wallet.getAddressBalance(address);
if (addressBalance.isErr()) return;
const addressBalance = addressBalanceRes.value;
const walletBalance = wallet.getBalance();
const walletRefresh = await wallet.refreshWallet();
const feeInfo = wallet.getFeeInfo();
const sendRes = await wallet.send({ address: 'address to send sats to', amount: 1000, satPerByte: 2 });
const sendMaxRes = await wallet.sendMax({ address: 'address to send sats to', satPerByte: 2 });
Advanced Usage
import { Wallet, generateMnemonic } from 'beignet';
import net from 'net'
import tls from 'tls'
import { TStorage } from './wallet';
const mnemonic = generateMnemonic();
const passphrase = 'passphrase';
const servers: TServer = {
host: '35.233.47.252',
ssl: 18484,
tcp: 18483,
protocol: EProtocol.ssl,
};
const network = ENetworks.mainnet;
const addressType = EAddressType.p2tr;
const addressTypesToMonitor = [EAddressType.p2tr, EAddressType.p2wpkh];
const onMessage: TOnMessage = (id, data) => {
console.log(id);
console.dir(data, { depth: null });
}
const disableMessagesOnCreate = true;
const storage: TStorage = {
async getData<K extends keyof IWalletData>(
key: string
): Promise<Result<IWalletData[K]>> {
},
async setData<K extends keyof IWalletData>(
key: string,
value: IWalletData[K]
): Promise<Result<boolean>> {
}
};
const createWalletRes = await Wallet.create({
mnemonic,
passphrase,
electrumOptions: {
servers,
net,
tls
},
network,
onMessage,
storage,
addressType,
addressTypesToMonitor,
disableMessagesOnCreate
});
if (createWalletRes.isErr()) return;
const wallet = createWalletRes.value;
const utxos = wallet.listUtxos();
const txs = [
{ address: 'address1', amount: 1000 },
{ address: 'address2', amount: 2000 },
{ address: 'address3', amount: 3000 },
];
const sendManyRes = await wallet.sendMany({ txs });
const sweepPrivateKeyRes = await wallet.sweepPrivateKey({
privateKey: 'privateKey',
toAddress: 'toAddress',
satsPerByte: 5,
broadcast: false
});
const history = await wallet.getAddressHistory('address');
const txDetails = await wallet.getTransactionDetails('txid');
React Native
You can use react-native-tcp-socket
as a drop in replacement for net
& tls
in a react-native environment. In package.json
:
"react-native": {
"net": "react-native-tcp-socket",
"tls": "react-native-tcp-socket"
}
Documentation
Support
If you are experiencing any problems, please open an issue or reach out to us on Telegram.