@makkii/app-btc v0.1.2
@makkii/app-btc
Bitcoin-like(such as Bitcoin, Litecoin, etc) application client:
- Support Legacy address, prefix with 1
- Support both mainnet and testnet
- BIP39 Path m/49'/
coinType'/0'/0/indexFor example, bitcoin's 1st account: m/49'/0'/0'/0/1 - Support get account from WIF private key
This library uses some services:
- bitpay insight-api - pass in BtcApiClient constructor, used to get balance, broadcast tranaction, etc.
- Transaction Explorer - bitpay insight-ui
Installation
$ npm install @makkii/app-btcUsage
import {BtcApiClient, BtcKeystoreClient, BtcLocalSigner} from '@makkii/app-btc'
const api_client = new BtcApiClient({ network: 'BTC', insight_api: '***' });
api_client.getBalance('1ADeU4oeRSCTPn8GpZ2g6RXwDKsCuSjSbQ')
.then(console.log)
.catch(error=>console.log(error));
const keystore_client = new BtcKeystoreClient('BTC');
api_client.buildTransaction(
'1ADeU4oeRSCTPn8GpZ2g6RXwDKsCuSjSbQ', // from address
'147SwRQdpCfj5p8PnfsXV2SsVVpVcz3aPq', // to address
0, // amount
{
byte_fee: 10,
}
).then(function(unsignedTx) {
keystore_client.signTransaction(unsignedTx, new BtcLocalSigner(), {
private_key: '***'
}).then(function(signedTx) {
console.log(signedTx);
});
});API
Table of Contents
- BtcLocalSigner
- BtcKeystoreClient
- IBtcConfig
- BtcUnsignedTx
- BtcApiClient
- BtcTxStatus
- BtcTransaction
- BtcPendingTransaction
- BtcKeypair
BtcLocalSigner
Parameters
network
signTransaction
Sign transaction of btc local signer
Parameters
transactionBtcUnsignedTxparams{private_key: string, compressed: boolean} {private_key: string, compressed: boolean}
Returns string btc signed tx
BtcKeystoreClient
Parameters
network("BTC"|"BTCTEST"|"LTC"|"LTCTEST")
ledgerSupport
only btc btcTest support ledger
getCurrentNetwork
Get current network
Returns string network
checkLedgerSupport
Check ledger support
Returns boolean if support ledger
signTransaction
Sign transaction by signer
Parameters
txBtcUnsignedTxsignerT localSigner or hardwaresignerParamany localSigner: {private_key, compressed} hardware:{derivationIndex}unsignedTxunsigned transaction build by buildTransaction
Returns Promise<string> encoded transaction
generateMnemonic
Generate mnemonic by bip39
Returns string 12 length of mnemonic
recoverKeyPairByPrivateKey
Recover key pair by private key
Parameters
priKeystringoptionsany? compressed boolean whether to compress public key
Returns Promise<BtcKeypair>
recoverKeyPairByWIF
Recover key pair by wif
Parameters
WIFstringwifwif string
Returns Promise<BtcKeypair>
validatePrivateKey
Validate private key
not implemented
Parameters
validateAddress
Validate address
Parameters
addressstring
Returns boolean
getAccountFromMnemonic
Get account from mnemonic
Parameters
Returns Promise<BtcKeypair>
getAccountFromHardware
Get account from hardware
Parameters
indexnumber derivation indexhardwareIHardware
IBtcConfig
network
Network name
Type: ("BTC" | "BTCTEST" | "LTC" | "LTCTEST")
insight_api
bitcoin reset api: https://github.com/bitpay/insight-api/tree/v0.4.3
Type: string
broadcast
bitcoin broadcasst tx api
Type: string
explorer
bitcoin explorer url to show tx detail
Type: string
BtcUnsignedTx
BTC unsigned transaction.
- to: Array<{ addr: string; value: number }>;
- from: Array<{ addr: string; value: number }>;
- value: BigNumber;
- utxos: Array<{ hash: string; index: number; script: string; raw: string; amount: number; }>;
- change_address: string;
- to_address: string;
- byte_fee: number;
- network: string;
BtcApiClient
BTC api client that implement IsingleApiClient
Parameters
configIBtcConfig
config
Config of btc api client
Type: IBtcConfig
getNetwork
Get network of btc api client
Returns string network
updateConfiguration
Update configuration of btc api client
Parameters
configIBtcConfig
getBlockByNumber
Get block by number
not implemented
Parameters
blockNumberstring
getBlockNumber
Get current Block height
not implemented
getTransactionStatus
Get transaction status by tx id
Parameters
hashstring transaction id
Returns Promise<BtcTxStatus> transaction status
getTransactionExplorerUrl
Get an explorer url showing transaction details
Parameters
hashstring
Returns string url
getBalance
Get an account's balance
Parameters
addressstring
Returns Promise<BigNumber> balance
getTransactionsByAddress
Get transactions by address
Parameters
Returns Promise<Map<string, BtcTransaction>>
sendTransaction
Send transaction
Parameters
unsignedTxBtcUnsignedTx unsigned transaction build by buildTransactionsignerT localSigner or hardwaresignerParamsany localSigner: {private_key, compressed} hardware:{derivationIndex}
Returns Promise<BtcPendingTransaction>
sameAddress
check if two address is same
Parameters
Returns boolean
sendAll
try to estimate send one address all balance
Parameters
Returns Promise<number> one address all balance after minus fee
buildTransaction
Build transaction
Parameters
Returns Promise<BtcUnsignedTx>
BtcTxStatus
BTC transaction status
- status: boolean;
- blockNumber?: number;
- timestamp?: number;
BtcTransaction
BTC transaction
- hash: string;
- timestamp: number;
- blockNUmber: number;
- status: "CONFIRMED";
- from: Array<{ addr: string; value: number; }>;
- to: Array<{ addr: string; value: number; }>;
- fee: number;
BtcPendingTransaction
BTC pending transaction.
- hash: string;
- status: "PENDING";
- from: Array<{ addr: string; value: number; }>;
- to: Array<{ addr: string; value: number; }>;
- fee: number;
BtcKeypair
BTC key pair.
- private_key: string;
- public_key: string;
- address: string;
- index?: number;
- compressed?: boolean;
- sign?: (hash: any) => Buffer;
- toWIF?: () => string;