Bitcoin wallt version 2 for golang programming language
check out avaiable methods in pacakge
install using go get -u github.com/ranjbar-dev/[email protected]
// configure package
bitcoinwallet.SetConfig(&bitcoinwallet.Config{
Timeout: 30, // http client timeout in second
Explorer: explorer.NewTrezorExplorer(), // exporer to get blockchain info, check ./exporer folder for other options
PriceCrawler: pricecrawler.NewBinanceCraweler(), // price crawler to get current price data, check ./pricecrawler folder for other options
FeeCrawler: feecrawler.NewBlockstreamCrawler(), // fee crawler to get current fee data, check ./feecrawler folder for other options
Chaincfg: &chaincfg.MainNetParams, // Bitcoin chain config
})
privateKey, _ := bitcoinwallet.GeneratePrivateKey()
fmt.Println("*ecdsa.PrivateKey:", privateKey)
hex, _ := bitcoinwallet.PrivateKeyToHex(privateKey)
fmt.Println("private key hex:", hex)
bytes, _ := bitcoinwallet.PrivateKeyToBytes(privateKey)
fmt.Println("private key bytes:", bytes)
privateKey, _ := bitcoinwallet.BytesToPrivateKey(bytes)
fmt.Println("*ecdsa.PrivateKey:", bytes)
privateKey, _ := bitcoinwallet.HexToPrivateKey(hex)
fmt.Println("*ecdsa.PrivateKey:", bytes)
address, _ := bitcoinwallet.PrivateKeyToAddress(privateKey)
fmt.Println("Address:", address)
retrieve address balacne from explorer configured in package
balance, _ := bitcoinwallet.FetchAddressBalance(address)
fmt.Println("Balance in satoshi:", balance)
retrieve address utxo from explorer configured in package
utxos, _ := bitcoinwallet.FetchAddressUTXOs(address)
fmt.Println("UTXO records:", utxos)
retrieve blockchain high, mid and low fee in sat/vbyte using fee crawler configured in package
low, mid, high, err := bitcoinwallet.FetchEstimateFee()
retrieve BTC price in USD using price crawler configured in package
price, err := bitcoinwallet.FetchPrice()
In the Bitcoin blockchain, a block is a collection of transactions that have been confirmed and recorded on the blockchain.
fetch current block number using explorer configured in package
blockNumber, _ := bitcoinwallet.FetchCurrentBlockNumber()
fmt.Println("blockNumber:", blockNumber)
fetch current block hash using explorer configured in package
blockHash, _ := bitcoinwallet.FetchCurrentBlockHash()
fmt.Println("blockHash:", blockHash)
fetch block data by block number using explorer configured in package
blockData, _ := bitcoinwallet.FetchBlockByNumber(blockNumber)
fmt.Println("blockData:", blockData)
var inputs models.TransactionInput
// add transaction inputs
inputs[0] = models.NewTransactionInput(privateKey, utxoValue, utxoIndex, utxoTxId)
var outputs models.TransactionOutput
// add transaction outputs
outputs[0] = models.NewTransactionOutput(toAddressBytes, valueInSatoshi)
// create a new transaction
transaction := bitcoinwallet.NewTransaction(inputs, outputs)
// get transaction size in bytes
size := transaction.Size()
fmt.Println("size of transaction in bytes: ", size)
// get transaction inputs
fmt.Println("transaction inputs: ", transaction.Inputs())
// get transaction outputs
fmt.Println("transaction outputs: ", transaction.Outputs())
// calculate transaction fee based on inputs and outputs
fmt.Println("transaction fee: ", transaction.Fee())
// sign transaction
err := transaction.SignAndSerialize()
if err != nil {
fmt.Println("error on sign and serialize transaction: ", err)
return
}
// broadcast transaction into blockchain
txID, err := transaction.Broadcast()
if err != nil {
fmt.Println("error on broadcast transaction: ", err)
return
}
fmt.Println("transaction broadcasted successfully with txID: ", txID)
-
add tests
-
replace int and int64 with big.Int for balance and satoshi transfer usages