@abcpros/minimal-xpi-slp-wallet 中文文档教程
minimal-slp-wallet
这是一款用于前端网络应用程序的极简主义比特币现金 (BCH) 钱包。 它是令牌感知的,与代码从以下分支的钱包不同:minimal-bch-wallet。 它旨在与 gatsby-ipfs-web-wallet 基于网络的钱包一起使用,以及 bch-js JavaScript 库fullstack.cash">FullStack.cash
钱包密钥对的默认推导路径是m/44'/245'/0'/0/0
。 这是 SLP 令牌感知 BCH 钱包的 BIP44 标准。
Examples
examples 目录显示了如何编写使用此库的 node.js JavaScript 应用程序来与 BCH 一起工作:
How to use it?
Import
Add to your HTML scripts
<script src="https://unpkg.com/minimal-slp-wallet"></script>
Node module
npm install minimal-slp-wallet --save
// module import
import BchWallet from "minimal-slp-wallet";
// nodejs modules
const BchWallet = require("minimal-slp-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'/245'/0'/0/0)
console.log(bchWallet.walletInfo.cashAddress);
// legacy address derived from the seed (derivation path: m/44'/245'/0'/0/0)
console.log(bchWallet.walletInfo.legacyAddress);
// private key for the BCH address derived from the seed (derivation path: m/44'/245'/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'/245'/0'/1'"
});
Send transactions
您可以将资金发送到其他 BCH 钱包。 您可以通过简单地扩展接收器数组来将资金分配给 N 个用户。
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);
Send Tokens
您可以用类似的方式发送令牌:
const receiver = {
address: "simpleledger:qpeq7xx5x3a2jfa0x0w8cjqp4v9cm842vgsjqwzvfk",
tokenId: "a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2",
qty: 1.25
}
const txid = await bchWallet.sendTokens(receiver);
// Transaction ID
console.log(txid);
注意目前仅支持单个令牌发送。 即一种令牌类型 每个接收者每个交易。
Get Wallet Balance
获取 BCH 地址的余额(已确认 + 未确认)
// 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");
List Tokens
列出地址持有的 SLP 代币。
// will get token balance for bchWallet.cashAddress
const myBalance = await bchWallet.listTokens();
// will get a balance for any address
const balanceOfOtherAddress = await bchWallet.listTokens("simpleledger:qpeq7xx5x3a2jfa0x0w8cjqp4v9cm842vgsjqwzvfk");
Get Wallet Transaction History
获取涉及此钱包的交易的 TXID 数组。
// 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
在开发 BCH 应用程序时,请记住永远不要将私钥/助记词/助记词发送到您的服务器。
- Your servers can be hacked
- 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
测试
minimal-slp-wallet
This is a minimalist Bitcoin Cash (BCH) wallet for use with front end web apps. It's token aware, unlike the wallet the code is forked from: minimal-bch-wallet. It's intended to be used with the gatsby-ipfs-web-wallet web-based wallet, and the bch-js JavaScript library provided by FullStack.cash
The default derivation path for the wallet keypair is m/44'/245'/0'/0/0
. This is the BIP44 standard for SLP token-aware BCH wallets.
Examples
The examples directory shows how to write node.js JavaScript apps that use this library to work with BCH:
How to use it?
Import
Add to your HTML scripts
<script src="https://unpkg.com/minimal-slp-wallet"></script>
Node module
npm install minimal-slp-wallet --save
// module import
import BchWallet from "minimal-slp-wallet";
// nodejs modules
const BchWallet = require("minimal-slp-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'/245'/0'/0/0)
console.log(bchWallet.walletInfo.cashAddress);
// legacy address derived from the seed (derivation path: m/44'/245'/0'/0/0)
console.log(bchWallet.walletInfo.legacyAddress);
// private key for the BCH address derived from the seed (derivation path: m/44'/245'/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'/245'/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);
Send Tokens
You can send tokens in a similar way:
const receiver = {
address: "simpleledger:qpeq7xx5x3a2jfa0x0w8cjqp4v9cm842vgsjqwzvfk",
tokenId: "a4fb5c2da1aa064e25018a43f9165040071d9e984ba190c222a7f59053af84b2",
qty: 1.25
}
const txid = await bchWallet.sendTokens(receiver);
// Transaction ID
console.log(txid);
Note: Only single token sends are supported at the moment. i.e. One token type per receiver per transaction.
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");
List Tokens
List the SLP tokens held by an address.
// will get token balance for bchWallet.cashAddress
const myBalance = await bchWallet.listTokens();
// will get a balance for any address
const balanceOfOtherAddress = await bchWallet.listTokens("simpleledger:qpeq7xx5x3a2jfa0x0w8cjqp4v9cm842vgsjqwzvfk");
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.
- Your servers can be hacked
- 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
test