ä¸ć–‡ć–‡ćˇŁ | English
A comprehensive Go SDK for creating, managing, and signing Bitcoin Partially Signed Bitcoin Transactions (PSBTs). This SDK provides a simple and powerful interface for building complex Bitcoin transactions with support for various UTXO types including Legacy, SegWit, and Taproot.
đźš§ Coming Soon - Comprehensive test cases and examples will be available soon.
go get github.com/kyleo-o/psbt-sdkpackage main
import (
"log"
"github.com/btcsuite/btcd/chaincfg"
"github.com/kyleo-o/psbt-sdk"
)
func main() {
// Set network parameters
netParams := &chaincfg.MainNetParams
// Define inputs (UTXOs to spend)
inputs := []psbt_sdk.Input{
{
OutTxId: "your_tx_id_here",
OutIndex: 0,
},
}
// Define outputs (where to send Bitcoin)
outputs := []psbt_sdk.Output{
{
Address: "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", // Example address
Amount: 100000, // Amount in satoshis
},
}
// Create PSBT builder
builder, err := psbt_sdk.CreatePsbtBuilder(netParams, inputs, outputs)
if err != nil {
log.Fatal(err)
}
// Get PSBT as hex string
psbtHex, err := builder.ToString()
if err != nil {
log.Fatal(err)
}
log.Printf("PSBT: %s", psbtHex)
}// Define signing information
signInputs := []*psbt_sdk.InputSign{
{
UtxoType: psbt_sdk.NonWitness, // or psbt_sdk.Witness, psbt_sdk.Taproot
Index: 0,
Amount: 100000,
SighashType: txscript.SigHashAll,
PriHex: "your_private_key_hex",
},
}
// Sign the PSBT
err = builder.UpdateAndSignInput(signInputs)
if err != nil {
log.Fatal(err)
}
// Extract final transaction
txHex, err := builder.ExtractPsbtTransaction()
if err != nil {
log.Fatal(err)
}
log.Printf("Final Transaction: %s", txHex)type Input struct {
OutTxId string `json:"out_tx_id"` // Previous transaction ID
OutIndex uint32 `json:"out_index"` // Output index in previous transaction
}type Output struct {
Address string `json:"address"` // Bitcoin address
Amount uint64 `json:"amount"` // Amount in satoshis
Script string `json:"script"` // Optional: custom script
}type InputSign struct {
UtxoType UtxoType `json:"utxo_type"`
Index int `json:"index"`
OutRaw string `json:"out_raw"`
PkScript string `json:"pk_script"`
RedeemScript string `json:"redeem_script"`
ControlBlockWitness string `json:"control_block_witness"`
Amount uint64 `json:"amount"`
SighashType txscript.SigHashType `json:"sighash_type"`
PriHex string `json:"pri_hex"`
MultiSigScript string `json:"multi_sig_script"`
PreSigScript string `json:"pre_sig_script"`
}Creates a new PSBT builder with inputs and outputs.
func CreatePsbtBuilder(netParams *chaincfg.Params, ins []Input, outs []Output) (*PsbtBuilder, error)Creates a PSBT builder from an existing PSBT hex string.
func NewPsbtBuilder(netParams *chaincfg.Params, psbtHex string) (*PsbtBuilder, error)UpdateAndSignInput(signIns []*InputSign) error- Sign inputs (Legacy/SegWit)UpdateAndSignTaprootInput(signIns []*InputSign) error- Sign Taproot inputsUpdateAndSignInputNoFinalize(signIns []*InputSign) error- Sign without finalizingUpdateAndMultiSignInput(signIns []*InputSign) error- Multi-signature signing
AddInput(in Input, signIn *InputSign) error- Add input to transactionAddOutput(outs []Output) error- Add outputs to transactionAddInputOnly(in Input) error- Add input without signing info
ToString() (string, error)- Get PSBT as hex stringExtractPsbtTransaction() (string, error)- Extract final transactionIsComplete() bool- Check if PSBT is completeCalculateFee(feeRate int64, extraSize int64) (int64, error)- Calculate feesCalTxSize() (int64, error)- Calculate transaction size
// Create inputs and outputs
inputs := []psbt_sdk.Input{
{
OutTxId: "previous_tx_id",
OutIndex: 0,
},
}
outputs := []psbt_sdk.Output{
{
Address: "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
Amount: 50000,
},
}
// Create and sign
builder, _ := psbt_sdk.CreatePsbtBuilder(netParams, inputs, outputs)
signInputs := []*psbt_sdk.InputSign{
{
UtxoType: psbt_sdk.NonWitness,
Index: 0,
Amount: 100000,
OutRaw: "previous_tx_raw",
SighashType: txscript.SigHashAll,
PriHex: "private_key_hex",
},
}
builder.UpdateAndSignInput(signInputs)
txHex, _ := builder.ExtractPsbtTransaction()signInputs := []*psbt_sdk.InputSign{
{
UtxoType: psbt_sdk.Witness,
Index: 0,
Amount: 100000,
SighashType: txscript.SigHashAll,
PriHex: "private_key_hex",
},
}
builder.UpdateAndSignInput(signInputs)signInputs := []*psbt_sdk.InputSign{
{
UtxoType: psbt_sdk.Witness,
Index: 0,
PkScript: "pk_script",
RedeemScript: "redeem_script",
Amount: 100000,
SighashType: txscript.SigHashAll,
PriHex: "private_key_hex",
},
}
builder.UpdateAndSignInput(signInputs)signInputs := []*psbt_sdk.InputSign{
{
UtxoType: psbt_sdk.Taproot,
Index: 0,
PkScript: "pk_script",
Amount: 100000,
SighashType: txscript.SigHashDefault,
PriHex: "private_key_hex",
},
}
builder.UpdateAndSignTaprootInput(signInputs)The SDK supports three types of UTXOs:
NonWitness(1) - Legacy P2PKH/P2SH transactionsWitness(2) - SegWit P2WPKH/P2WSH transactionsTaproot(3) - Taproot P2TR transactions
- Bitcoin Mainnet
- Bitcoin Testnet3
- Bitcoin Signet
- Bitcoin RegTest
github.com/btcsuite/btcd- Bitcoin protocol implementationgithub.com/btcsuite/btcd/btcutil- Bitcoin utilitiesgithub.com/btcsuite/btcd/btcutil/psbt- PSBT implementation
This project is licensed under the MIT License - see the LICENSE file for details.
For questions and support, please open an issue on GitHub.