@okxweb3/coin-bitcoin
v1.0.27
Published
Bitcoin SDK is used to interact with the Bitcoin Mainnet or Testnet, it contains various functions can be used to web3 wallet. The SDK not only support Bitcoin, it also supports following chains:
Downloads
2,098
Keywords
Readme
@okxweb3/coin-bitcoin
Bitcoin SDK is used to interact with the Bitcoin Mainnet or Testnet, it contains various functions can be used to web3 wallet. The SDK not only support Bitcoin, it also supports following chains:
- BTC
- BSV
- DOGE
- LTC
- TBTC
Getting Started
Installing BitCoin SDK
npm install @okxweb3/coin-bitcoin
What Can BitCoin SDK Do
- getMnemonic
- getRandomPrivateKey
- getDerivedPrivateKey
- getNewAddress
- getAddressByPublicKey
- validAddress
- calcTxHash
- signTransaction
- validSignedTransaction
- signMessage
- verifyMessage
- genPsbtOfBIP322Simple
- signMessageOfBIP322Simple
Using BitCoin SDK
Get Private Key
get random private key
import { BtcWallet } from "@okxweb3/coin-bitcoin";
let wallet = new BtcWallet()
let privateKey = await wallet.getRandomPrivateKey();
get derived private key
import { BtcWallet } from "@okxweb3/coin-bitcoin";
let wallet = new BtcWallet()
let mnemonic = await getMnemonic(128);
let param = {
mnemonic: mnemonic,
hdPath: "m/44'/0'/0'/0/0"
};
let privateKey = await wallet.getDerivedPrivateKey(param);
Get New Address
get new address from private key
import { BtcWallet } from "@okxweb3/coin-bitcoin";
let wallet = new BtcWallet()
// legacy address
let params: NewAddressParams = {
privateKey: "L22jGDH5pKE4WHb2m9r2MdiWTtGarDhTYRqMrntsjD5uCq5z9ahY"
};
let address = await wallet.getNewAddress(params);
// native segwit address
let params2: NewAddressParams = {
privateKey: "L22jGDH5pKE4WHb2m9r2MdiWTtGarDhTYRqMrntsjD5uCq5z9ahY",
addressType: "segwit_native",
};
let address2 = await wallet.getNewAddress(params2);
// nested segwit address
let params3: NewAddressParams = {
privateKey: "L22jGDH5pKE4WHb2m9r2MdiWTtGarDhTYRqMrntsjD5uCq5z9ahY",
addressType: "segwit_nested",
};
let address3 = await wallet.getNewAddress(params3);
// taproot segwit address
let params4: NewAddressParams = {
privateKey: "L22jGDH5pKE4WHb2m9r2MdiWTtGarDhTYRqMrntsjD5uCq5z9ahY",
addressType: "segwit_taproot",
};
let address4 = await wallet.getNewAddress(params4);
get new address from public key
import { BtcWallet } from "@okxweb3/coin-bitcoin";
let wallet = new BtcWallet()
let params5 = {
publicKey: "03052b16e71e4413f24f8504c3b188b7edebf97b424582877e4993ef9b23d0f045",
addressType: "segwit_taproot",
}
let address5 = await wallet.getAddressByPublicKey(params5);
SignMessage
let wallet = new BtcWallet()
let privateKey = await wallet.getRandomPrivateKey();
let address = await wallet.getNewAddress({privateKey:privateKey})
let signParams: SignTxParams = {
privateKey: privateKey,
data: {
type: 0,
address: address.address,
message: "hello world!",
}
};
let res = await wallet.signMessage(signParams)
genPsbtOfBIP322Simple
Generate psbt transaction to be signed, currently only segwit_taproot and segwit_taproot address types are supported.
let btc = new TBtcWallet();
let pri = await btc.getRandomPrivateKey();
let addr = await btc.getNewAddress({privateKey:pri,addressType:"segwit_taproot"});
console.log(addr.address)
let tx = btc.genPsbtOfBIP322Simple({message:"hello", address:addr.address});
signMessageOfBIP322Simple
Sign the psbt transaction to be signed
let tx = btc.genPsbtOfBIP322Simple({message:"hello", address:addr.address});
let sig = await btc.signMessageOfBIP322Simple(tx,pri);
let ok = await btc.verifyMessage({
signature: sig,
data: {
address: addr.address,
message: "hello",
publicKey: addr.compressedPublicKey,
}})
expect(ok).toEqual(true);
Sign Transaction
sign transaction
import { BtcWallet } from "@okxweb3/coin-bitcoin";
let wallet = new BtcWallet()
let btcTxParams = {
inputs: [
{
txId: "a7edebed3f2e51a2ed99a3625fb408bd9db2ce61b1794880b3f214b26bf7a023",
vOut: 0,
amount: 250000
},
],
outputs: [
{
address: "tb1qtsq9c4fje6qsmheql8gajwtrrdrs38kdzeersc",
amount: 150000
},
{
address: "mouQtmBWDS7JnT65Grj2tPzdSmGKJgRMhE",
amount: 50000
},
],
address: "2NF33rckfiQTiE5Guk5ufUdwms8PgmtnEdc",
feePerB: 2
};
let signParams: SignTxParams = {
privateKey: "L22jGDH5pKE4WHb2m9r2MdiWTtGarDhTYRqMrntsjD5uCq5z9ahY",
data: btcTxParams
};
let tx = await wallet.signTransaction(signParams);
sign legacy transaction
import { BtcWallet } from "@okxweb3/coin-bitcoin";
let wallet = new BtcWallet()
let btcTxParams = {
inputs: [
{
txId: "1e0f92720ef34ab75eefc5d691b551fb2f783eac61503a69cdf63eb7305d2306",
vOut: 0,
amount: 2500000,
address: "1GhLyRg4zzFixW3ZY5ViFzT4W5zTT9h7Pc",
},
{
txId: "6a8187bcd23b820804312077d5bcfaae534bc2cf21a2e3854e558f099fa0401f",
vOut: 1,
amount: 2019431,
address: "1GhLyRg4zzFixW3ZY5ViFzT4W5zTT9h7Pc",
}
],
outputs: [
{
address: "1GhLyRg4zzFixW3ZY5ViFzT4W5zTT9h7Pc",
amount: 2500000
}
],
address: "1GhLyRg4zzFixW3ZY5ViFzT4W5zTT9h7Pc",
feePerB: 2
};
let signParams: SignTxParams = {
privateKey: "L22jGDH5pKE4WHb2m9r2MdiWTtGarDhTYRqMrntsjD5uCq5z9ahY",
data: btcTxParams
};
let tx = await wallet.signTransaction(signParams);
Doginals inscribe
import { DogeWallet } from "@okxweb3/coin-bitcoin";
let wallet = new DogeWallet()
let privateKey = "QV3XGHS28fExYMnEsoXrzRr7bjQbCH1qRPfPCMLBKhniWF4uFEcs"
const commitTxPrevOutputList: PrevOutput[] = [];
commitTxPrevOutputList.push({
txId: "adc5edd2a536c92fed35b3d75cbdbc9f11212fe3aa6b55c0ac88c289ba7c4fae",
vOut: 2,
amount: 317250000,
address: "DFuDR3Vn22KMnrnVCxh6YavMAJP8TCPeA2",
privateKey: privateKey,
});
const inscriptionData: InscriptionData = {
contentType: "text/plain;charset=utf8",
body: base.fromHex(base.toHex(Buffer.from('{"p":"drc-20","op":"mint","tick":"tril","amt":"100"}'))),
revealAddr: "DFuDR3Vn22KMnrnVCxh6YavMAJP8TCPeA2",
};
const request = {
type: 1,
commitTxPrevOutputList,
commitFeeRate: 100000,
revealFeeRate: 100000,
revealOutValue: 100000,
inscriptionData,
changeAddress: "DFuDR3Vn22KMnrnVCxh6YavMAJP8TCPeA2",
};
let result = await wallet.signTransaction({privateKey: privateKey, data: request})
console.log(result);
SRC20 inscribe
you need replace TBtcWallet with BtcWallet when you run on mainnet not testnet.
import { TBtcWallet } from "@okxweb3/coin-bitcoin";
let wallet = new TBtcWallet()
let network = bitcoin.networks.testnet;
let privateKey = "cPnvkvUYyHcSSS26iD1dkrJdV7k1RoUqJLhn3CYxpo398PdLVE22"
const commitTxPrevOutputList: PrevOutput[] = [];
commitTxPrevOutputList.push({
txId: "c865cd4dc206ccdaf1cff0fad4f0272f2075af5c975c670debbf8d56045391ad",
vOut: 3,
amount: 202000,
address: "2NF33rckfiQTiE5Guk5ufUdwms8PgmtnEdc",
privateKey: privateKey,
});
const inscriptionData: InscriptionData = {
contentType: "stamp:",
body: '{"p":"src-20","op":"deploy","tick":"coder","max":"21000000","lim":"1000","dec":"8"}',
revealAddr: "2NF33rckfiQTiE5Guk5ufUdwms8PgmtnEdc",
}
const request = {
commitTxPrevOutputList,
commitFeeRate: 100,
revealOutValue: 790,
inscriptionData,
changeAddress: "2NF33rckfiQTiE5Guk5ufUdwms8PgmtnEdc",
type: 101,
};
const txs: InscribeTxs =await wallet.signTransaction({privateKey: privateKey, data: request});
console.log(JSON.stringify(txs));
BRC20 inscribe
test("inscribe", async () => {
let network = bitcoin.networks.testnet;
let privateKey = "cPnvkvUYyHcSSS26iD1dkrJdV7k1RoUqJLhn3CYxpo398PdLVE22"
const commitTxPrevOutputList: PrevOutput[] = [];
commitTxPrevOutputList.push({
txId: "36cdb491d2b02c1668d02e42edd80af339e1195df4d58927ab9db9e4893509a5",
vOut: 4,
amount: 1145068,
address: "2NF33rckfiQTiE5Guk5ufUdwms8PgmtnEdc",
privateKey: privateKey,
});
commitTxPrevOutputList.push({
txId: "3d79592cd151427d2d3e55aaf09749c8417d24889c20edf68bd936adc427412a",
vOut: 0,
amount: 546,
address: "tb1qtsq9c4fje6qsmheql8gajwtrrdrs38kdzeersc",
privateKey: privateKey,
});
commitTxPrevOutputList.push({
txId: "83f5768abfd8b95dbfd9191a94042a06a2c3639394fd50f40a00296cb551be8d",
vOut: 0,
amount: 546,
address: "mouQtmBWDS7JnT65Grj2tPzdSmGKJgRMhE",
privateKey: privateKey,
});
commitTxPrevOutputList.push({
txId: "8583f92bfc087549f6f20eb2d1604b69d5625a9fe60df72e61e9138884f57c41",
vOut: 0,
amount: 546,
address: "tb1pklh8lqax5l7m2ycypptv2emc4gata2dy28svnwcp9u32wlkenvsspcvhsr",
privateKey: privateKey,
});
const inscriptionDataList: InscriptionData[] = [];
inscriptionDataList.push({
contentType: "text/plain;charset=utf-8",
body: `{"p":"brc-20","op":"mint","tick":"xcvb","amt":"100"}`,
revealAddr: "tb1pklh8lqax5l7m2ycypptv2emc4gata2dy28svnwcp9u32wlkenvsspcvhsr",
});
inscriptionDataList.push({
contentType: "text/plain;charset=utf-8",
body: `{"p":"brc-20","op":"mint","tick":"xcvb","amt":"10"}`,
revealAddr: "mouQtmBWDS7JnT65Grj2tPzdSmGKJgRMhE",
});
inscriptionDataList.push({
contentType: "text/plain;charset=utf-8",
body: `{"p":"brc-20","op":"mint","tick":"xcvb","amt":"10000"}`,
revealAddr: "tb1qtsq9c4fje6qsmheql8gajwtrrdrs38kdzeersc",
});
inscriptionDataList.push({
contentType: "text/plain;charset=utf-8",
body: `{"p":"brc-20","op":"mint","tick":"xcvb","amt":"1"}`,
revealAddr: "2NF33rckfiQTiE5Guk5ufUdwms8PgmtnEdc",
});
const request: InscriptionRequest = {
commitTxPrevOutputList,
commitFeeRate: 2,
revealFeeRate: 2,
revealOutValue: 546,
inscriptionDataList,
changeAddress: "tb1pklh8lqax5l7m2ycypptv2emc4gata2dy28svnwcp9u32wlkenvsspcvhsr",
};
const txs: InscribeTxs = inscribe(network, request);
console.log(txs);
expect(txs.commitTxFee).toEqual(1180)
});