Closed ramzesisi closed 5 months ago
`
``` ` package main import ( "bytes" "encoding/json" "fmt" "io" "log" "net/http" "sort" "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" ) type UTXOresponse struct { Txid string `json:"txid"` Vout int `json:"vout"` Status struct { Confirmed bool `json:"confirmed"` BlockHeight int `json:"block_height"` BlockHash string `json:"block_hash"` BlockTime int `json:"block_time"` } `json:"status"` Value int `json:"value"` } type UTXO struct { Txid string Vout int Value int64 } var ( UTXOlist []UTXO ) func main() { privKey := "" senderAddress := "34meJj6KEFN58AFQksTj3Jrbp9WvBQLR1G" destinationAddress := "34meJj6KEFN58AFQksTj3Jrbp9WvBQLR1G" btcAmount := 0.001 transefrBtc(privKey, senderAddress, destinationAddress, float32(btcAmount), int64(20)) } func UTXOrequest() error { client := &http.Client{} req, err := http.NewRequest("GET", "https://mempool.space/api/address/34meJj6KEFN58AFQksTj3Jrbp9WvBQLR1G/utxo", nil) if err != nil { return fmt.Errorf("error creating HTTP request: %v", err) } resp, err := client.Do(req) if err != nil { return fmt.Errorf("error making HTTP request: %v", err) } defer resp.Body.Close() bodyText, err := io.ReadAll(resp.Body) if err != nil { return fmt.Errorf("error reading response body: %v", err) } var transactions []UTXOresponse err = json.Unmarshal(bodyText, &transactions) if err != nil { return fmt.Errorf("error unmarshalling JSON: %v", err) } UTXOlist = make([]UTXO, 0, len(transactions)) for _, tx := range transactions { UTXOlist = append(UTXOlist, UTXO{Txid: tx.Txid, Vout: tx.Vout, Value: int64(tx.Value)}) } return nil } type UTXOByValue []UTXO func (a UTXOByValue) Len() int { return len(a) } func (a UTXOByValue) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a UTXOByValue) Less(i, j int) bool { return a[i].Value > a[j].Value } func estimateFee(txSize int, feePerByte int64) int64 { fmt.Printf("TxSize: %d, FeePerByte: %d", txSize, feePerByte) return int64(txSize) * feePerByte } func transefrBtc(privKey, sender, destination string, amount float32, feePerByte int64) { UTXOrequest() sort.Sort(UTXOByValue(UTXOlist)) params := &chaincfg.MainNetParams tx := wire.NewMsgTx(2) //locktime tx.LockTime = 828008 wif, _ := btcutil.DecodeWIF(privKey) amountSats := int64(amount * 1e8) totalFee := 300 * feePerByte totalAmount := amountSats + totalFee var totalInput int64 for _, utxo := range UTXOlist { prevOutHash, err := chainhash.NewHashFromStr(utxo.Txid) if err != nil { log.Fatal(err) } outpoint := wire.NewOutPoint(prevOutHash, uint32(utxo.Vout)) txIn := wire.NewTxIn(outpoint, nil, nil) txIn.Sequence = 0xFFFFFFFD tx.AddTxIn(txIn) totalInput += utxo.Value if totalInput >= totalAmount { break } } destinationAddress, _ := btcutil.DecodeAddress(destination, params) pkScript_0, _ := txscript.PayToAddrScript(destinationAddress) tx.AddTxOut(wire.NewTxOut(amountSats, pkScript_0)) senderAddress, _ := btcutil.DecodeAddress(sender, params) pkScript_1, _ := txscript.PayToAddrScript(senderAddress) tx.AddTxOut(wire.NewTxOut(totalInput-amountSats-totalFee, pkScript_1)) for i := range tx.TxIn { signature, _ := txscript.RawTxInSignature(tx, i, pkScript_0, txscript.SigHashAll, wif.PrivKey) signatureScript, _ := txscript.NewScriptBuilder().AddData(signature).AddData(wif.PrivKey.PubKey().SerializeCompressed()).Script() tx.TxIn[i].SignatureScript = signatureScript } var buf bytes.Buffer tx.Serialize(&buf) fmt.Printf("Signed transaction: %x\n", buf.Bytes()) } `` ![image](https://github.com/btcsuite/btcd/assets/96226661/69b8b0d3-6ed8-4e13-b000-d0fd894a064e) trying to send small amount from my wallet address to my wallet address trough import signed messagee in electrum (also tried to broadcast trough api) and its not working Any one know how to fix it? I guess that ill have really stupid error
`