BITCOIN GO Package
a comprehensive and versatile Go library for all your Bitcoin transaction needs. offers robust support for various Bitcoin transaction types, including spending transactions, Bitcoin address management, Bitcoin Schnorr signatures, BIP-39 mnemonic phrase generation, hierarchical deterministic (HD) wallet derivation, and Web3 Secret Storage Definition.
This package was inspired by the python-bitcoin-utils package and turned into GO
Features
Transaction Types
This comprehensive package provides robust support for a wide array of Bitcoin transaction types, encompassing the full spectrum of Bitcoin transaction capabilities. Whether you need to execute standard payments, facilitate complex multi-signature wallets, leverage Segregated Witness (SegWit) transactions for lower fees and enhanced scalability, or embrace the privacy and flexibility of Pay-to-Taproot (P2TR) transactions, this package has you covered. Additionally, it empowers users to engage in legacy transactions, create time-locked transactions, and harness the security of multisignature (multisig) transactions. With this package, you can seamlessly navigate the diverse landscape of Bitcoin transactions, ensuring your interactions with the Bitcoin network are secure, efficient, and tailored to your specific needs.
P2PKH (Pay-to-Public-Key-Hash): The most common transaction type, it sends funds to a recipient's public key hash. Provides security and anonymity.
P2SH (Pay-to-Script-Hash): Allows more complex scripts to be used, enhancing Bitcoin's capabilities by enabling features like multisignature wallets.
P2WPKH (Pay-to-Witness-Public-Key-Hash): A Segregated Witness (SegWit) transaction type, it offers reduced fees and improved scalability while maintaining compatibility.
P2WSH (Pay-to-Witness-Script-Hash): Another SegWit transaction, it extends the benefits of SegWit to more complex script scenarios, reducing transaction size and fees.
P2TR (Pay-to-Taproot): An upgrade aiming to improve privacy and flexibility, allowing users to choose between various scripts and enhance transaction efficiency.
Legacy Transactions: Refers to older transaction types used before SegWit, with higher fees and less scalability.
Multisignature (Multisig) Transactions: Involves multiple signatures to authorize a Bitcoin transaction, commonly used for security purposes in shared wallets.
SegWit Transactions: A collective term for P2WPKH and P2WSH transactions, leveraging segregated witness data to reduce transaction size and fees.
Time-Locked Transactions: These transactions have a predetermined time or block height before they can be spent, adding security and functionality to Bitcoin smart contracts.
Coinbase Transactions: The first transaction in each block, generating new Bitcoins as a block reward for miners. It includes the miner's payout address.
Create Transaction
Using this package, you can create a Bitcoin transaction in two ways: either through the BtcTransaction struct or the BitcoinTransactionBuilder struct
BtcTransaction: To use the BtcTransaction struct, you should have a general understanding of how Bitcoin transactions work, including knowledge of UTXOs, scripts, various types of scripts, Bitcoin addresses, signatures, and more. We created examples and tests to enhance your understanding. An example of this transaction type is explained below, and you can also find numerous examples in the test folder.
BitcoinTransactionBuilder: Even with limited prior knowledge, you can utilize this class to send various types of transactions. Below, I've provided an example in which a transaction features 8 distinct input addresses with different types and private keys, as well as 10 different output addresses. Furthermore, additional examples have been prepared, which you can find in the example folder.
Addresses
P2PKH A P2PKH (Pay-to-Public-Key-Hash) address in Bitcoin represents ownership of a cryptocurrency wallet by encoding a hashed public key
P2WPKH: A P2WPKH (Pay-to-Witness-Public-Key-Hash) address in Bitcoin is a Segregated Witness (SegWit) address that enables more efficient and secure transactions by segregating witness data, enhancing network scalability and security.
P2WSH: A P2WSH (Pay-to-Witness-Script-Hash) address in Bitcoin is a Segregated Witness (SegWit) address that allows users to spend bitcoins based on the conditions specified in a witness script, offering improved security and flexibility for complex transaction types.
P2TR: A P2TR (Pay-to-Taproot) address in Bitcoin is a type of address that allows users to send and receive bitcoins using the Taproot smart contract, offering enhanced privacy and scalability features.
P2SH: A P2SH (Pay-to-Script-Hash) address in Bitcoin is an address type that enables the use of more complex scripting, often associated with multi-signature transactions or other advanced smart contract functionality, enhancing flexibility and security.
P2SH(SEGWIT): A P2SH (Pay-to-Script-Hash) Segregated Witness (SegWit) address in Bitcoin combines the benefits of P2SH and SegWit technologies, allowing for enhanced transaction security, reduced fees, and improved scalability.
Sign
BIP-39
- Generate BIP39 mnemonics, providing a secure and standardized way to manage keys and seed phrases
HD Wallet
- Implement hierarchical deterministic (HD) wallet derivation
Web3 Secret Storage Definition
JSON Format: Private keys are stored in a JSON (JavaScript Object Notation) format, making it easy to work with in various programming languages.
Encryption: The private key is encrypted using the user's chosen password. This ensures that even if the JSON file is compromised, an attacker cannot access the private key without the password.
Key Derivation: The user's password is typically used to derive an encryption key using a key derivation function (KDF). This derived key is then used to encrypt the private key.
Scrypt Algorithm: The Scrypt algorithm is commonly used for key derivation, as it is computationally intensive and resistant to brute-force attacks.
Checksum: A checksum is often included in the JSON file to help detect errors in the password.
Initialization Vector (IV): An IV is typically used to add an extra layer of security to the encryption process.
Versioning: The JSON file may include a version field to indicate which version of the encryption and storage format is being used.
Metadata: Additional metadata, such as the address associated with the private key, may be included in the JSON file.
Node Provider
We have added two APIs (Mempool and BlockCypher) to the plugin for network access. You can easily use these two APIs to obtain information such as unspent transactions (UTXO), network fees, sending transactions, receiving transaction information, and retrieving account transactions.
EXAMPLES
Key and addresses
Private key
// Create an EC private key instance from a WIF (Wallet Import Format) encoded string.
privateKey, _ := keypair.NewECPrivateFromWIF("cT33CWKwcV8afBs5NYzeSzeSoGETtAB8izjDjMEuGqyqPoF7fbQR")
// Retrieve the corresponding public key from the private key.
publicKey := privateKey.GetPublic()
// Sign an input using the private key.
signSegwitV0OrLagacy := privateKey.SignInput()
// Sign a Taproot transaction using the private key.
signSegwitV1TapprotTransaction := privateKey.SignTaprootTransaction()
// Convert the private key to a WIF (Wallet Import Format) encoded string.
// The boolean argument specifies whether to use the compressed format.
toWif := privateKey.ToWIF(true, &address.MainnetNetwork)
// Convert the private key to its hexadecimal representation.
toHex := privateKey.ToHex()
Public key
// Create an instance of an EC public key from a hexadecimal representation.
publicKey, _ := keypair.NewECPPublicFromHex()
// Generate a Pay-to-Public-Key-Hash (P2PKH) address from the public key.
p2pkh := publicKey.ToAddress()
// Generate a Pay-to-Witness-Public-Key-Hash (P2WPKH) Segregated Witness (SegWit) address from the public key.
p2wpkh := publicKey.ToSegwitAddress()
// Generate a Pay-to-Witness-Script-Hash (P2WSH) Segregated Witness (SegWit) address from the public key.
p2wsh := publicKey.ToP2WSHAddress()
// Generate a Taproot address from the public key.
p2tr := publicKey.ToTaprootAddress()
// Generate a Pay-to-Public-Key-Hash (P2PKH) inside Pay-to-Script-Hash (P2SH) address from the public key.
p2pkhInP2sh := publicKey.ToP2PKHInP2SH()
// Generate a Pay-to-Witness-Public-Key-Hash (P2WPKH) inside Pay-to-Script-Hash (P2SH) address from the public key.
p2wpkhInP2sh := publicKey.ToP2WPKHInP2SH()
// Generate a Pay-to-Witness-Script-Hash (P2WSH) inside Pay-to-Script-Hash (P2SH) address from the public key.
p2wshInP2sh := publicKey.ToP2WSHInP2SH()
// Generate a Pay-to-Public-Key (P2PK) inside Pay-to-Script-Hash (P2SH) address from the public key.
p2pkInP2sh := publicKey.ToP2PKInP2SH()
// Get the compressed bytes representation of the public key.
compressedBytes := publicKey.ToCompressedBytes()
// Get the uncompressed bytes representation of the public key.
unCompressedBytes := publicKey.ToUnCompressedBytes(true)
// extracts and returns the x-coordinate (first 32 bytes) of the ECPublic key
// as a hexadecimal string.
onlyX := publicKey.ToXOnlyHex()
// CalculateTweak computes and returns the TapTweak value based on the ECPublic key
// and an optional script. It uses the key's x-coordinate and the Merkle root of the script
// (if provided) to calculate the tweak.
tweak, _ := publicKey.CalculateTweak(scripts.Script{})
// computes and returns the Taproot commitment point's x-coordinate
// derived from the ECPublic key and an optional script, represented as a hexadecimal string.
taproot, _ := publicKey.ToTapRotHex([]interface{}{})
// Verify verifies a signature against a message
verify := publicKey.Verify()
Addresses
// The `...FromAddress` methods only check the address and return an error if it does not belong to Bitcoin.
// If you also want to verify that the address belongs to a specific network,
// please select the desired network using the parameters.
// Generate a Pay-to-Public-Key-Hash (P2PKH) address from the public key.
p2pkh, _ := address.P2PKHAddressFromAddress("1Q5odQtVCc4PDmP5ncrp7DSuVbh2ML4Gnb", network)
// Generate a Pay-to-Witness-Public-Key-Hash (P2WPKH) Segregated Witness (SegWit) address from the public key.
p2wpkh, _ := address.P2WPKHAddresssFromAddress("bc1ql5eh45als8sgdkt2drsl344q55g03sj2u9enzz")
// Generate a Pay-to-Witness-Script-Hash (P2WSH) Segregated Witness (SegWit) address from the public key.
p2wsh, _ := address.P2WSHAddresssFromAddress("bc1qf90kcg2ktg0wm983cyvhy0jsrj2fmqz26ugf5jz3uw68mtnr8ljsnf8pqe")
// Generate a Taproot address from the public key.
p2tr, _ := address.P2TRAddressFromAddress("bc1pmelvn3xz2n3dmcsvk2k99na7kc55ry77zmhg4z39upry05myjthq37f6jk")
// Generate a Pay-to-Public-Key-Hash (P2PKH) inside Pay-to-Script-Hash (P2SH) address from the public key.
p2pkhInP2sh, _ := address.P2SHAddressFromAddress("3HDtvvRMu3yKGFXYFSubTspbhbLagpdKJ7")
// Generate a Pay-to-Witness-Public-Key-Hash (P2WPKH) inside Pay-to-Script-Hash (P2SH) address from the public key.
p2wpkhInP2sh, _ := address.P2SHAddressFromAddress("36Dq32LRMW8EJyD3T2usHaxeMBmUpsXhq2")
// Generate a Pay-to-Witness-Script-Hash (P2WSH) inside Pay-to-Script-Hash (P2SH) address from the public key.
p2wshInP2sh, _ := address.P2SHAddressFromAddress("3PPL49fMytbEKJsjjPnkfWh3iWzrZxQZAg")
// Generate a Pay-to-Public-Key (P2PK) inside Pay-to-Script-Hash (P2SH) address from the public key.
p2pkInP2sh, _ := address.P2SHAddressFromAddress("3NCe6AGzjz2jSyRKCc8o3Bg5MG6pUM92bg")
// You can create any type of Bitcoin address with scripts.
// Create an address with scripts for P2WSH multisig 3-of-5.
newScript := scripts.NewScript("OP_3", publicKey.ToHex(true), publicKey.ToHex(true), publicKey.ToHex(true), publicKey.ToHex(true), publicKey.ToHex(true), "OP_5", "OP_CHECKMULTISIG")
// Generate a P2WSH 3-of-5 address.
p2wsh3of5Address, _ := address.P2WSHAddresssFromScript(newScript)
// Generate a P2SH 3-of-5 address from the P2WSH address.
p2sh3Of5, _ := address.P2SHAddressFromScript(p2wsh3of5Address.ToScriptPubKey(), address.P2WPKHInP2SH)
// The method calculates the address checksum and returns the Base58-encoded
// Bitcoin legacy address or the Bech32 format for SegWit addresses.
p2sh3Of5.Show(network)
// Return the scriptPubKey that corresponds to this address.
p2sh3Of5.ToScriptPubKey()
// Access the legacy or SegWit program of the address.
p2sh3Of5.Program()
Transaction