xDeFi bitcoin sdk
Getting started
npm install @xdefi/bitcoin
Initialize
import XDEFIBitcoin from "@xdefi/bitcoin";
let bitcoinClient;
if (window.xfi.bitcoin) {
bitcoinClient = new XDEFIBitcoin(window.xfi.bitcoin);
}
Get accounts
bitcoinClient.getAccounts()
.then((accounts) => {
})
.catch((error) => {
})
Get Unspent UTXOs (Get Balance)
bitcoinClient.getUnspentUTXOs("btcAddress")
.then((utxos) => {
})
.catch((error) => {
})
Transfer
bitcoinClient.transfer(
"btcAddressFrom",
"btcAddressTo",
"1044",
"100"
)
.then((txHash) => {
})
.catch((error) => {
})
Sign Bitcoin Transaction
import * as Bitcoin from "bitcoinjs-lib";
const valueOut = 1;
const returnedUTXOS = await bitcoinClient.getUnspentUTXOs("btcAddress")
const psbt = new Bitcoin.Psbt({ network: Bitcoin.networks.testnet });
returnedUTXOS.forEach((UTXO) => {
let formattedWitnessUtxo = {
script: Buffer.from(UTXO.witnessUtxo.script),
value: UTXO.witnessUtxo.value,
};
psbt.addInput({
hash: UTXO.hash,
index: UTXO.index,
witnessUtxo: formattedWitnessUtxo,
});
});
psbt.addOutput({ address: "otherAddress", value: valueOut });
const hexPbst = psbt.toHex();
console.log("pbsthex", hexPbst)
bitcoinClient.signTransaction("btcAddressFrom", hexPbst)
.then((resultSignature) => {
console.log(resultSignature);
this.resultSignature = resultSignature;
})
.catch(console.error);
NPM scripts
npm t
: Run test suitenpm start
: Run npm run build
in watch modenpm run test:watch
: Run test suite in interactive watch modenpm run test:prod
: Run linting and generate coveragenpm run build
: Generate bundles and typings, create docsnpm run lint
: Lints codenpm run commit
: Commit using conventional commit style (husky will tell you to use it if you haven't :wink:)
Excluding peerDependencies
On library development, one might want to set some peer dependencies, and thus remove those from the final bundle. You can see in Rollup docs how to do that.
Good news: the setup is here for you, you must only include the dependency name in external
property within rollup.config.js
. For example, if you want to exclude lodash
, just write there external: ['lodash']
.