-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_utxos.go
More file actions
95 lines (79 loc) · 2.31 KB
/
send_utxos.go
File metadata and controls
95 lines (79 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package ordinals
import (
"fmt"
"github.com/bsv-blockchain/go-sdk/script"
"github.com/bsv-blockchain/go-sdk/transaction"
fee_model "github.com/bsv-blockchain/go-sdk/transaction/fee_model"
"github.com/bsv-blockchain/go-sdk/transaction/template/p2pkh"
)
// SendUtxos sends utxos to the given destinations
func SendUtxos(config *SendUtxosConfig) (*transaction.Transaction, error) {
// Create a new transaction
tx := transaction.NewTransaction()
// Add inputs
for _, utxo := range config.Utxos {
unlocker, err := p2pkh.Unlock(config.PaymentPk, nil)
if err != nil {
return nil, fmt.Errorf("failed to create unlocker: %w", err)
}
err = tx.AddInputFrom(
utxo.TxID,
utxo.Vout,
utxo.ScriptPubKey,
utxo.Satoshis,
unlocker,
)
if err != nil {
return nil, fmt.Errorf("failed to add input: %w", err)
}
}
// Add payment outputs
for _, payment := range config.Payments {
dstAddr, err := script.NewAddressFromString(payment.Address)
if err != nil {
return nil, fmt.Errorf("failed to create destination address: %w", err)
}
lockingScript, err := p2pkh.Lock(dstAddr)
if err != nil {
return nil, fmt.Errorf("failed to create p2pkh script: %w", err)
}
tx.AddOutput(&transaction.TransactionOutput{
LockingScript: lockingScript,
Satoshis: payment.Satoshis,
})
}
// Add change output if needed
if config.ChangeAddress != "" {
changeAddr, err := script.NewAddressFromString(config.ChangeAddress)
if err != nil {
return nil, fmt.Errorf("failed to create change address: %w", err)
}
changeScript, err := p2pkh.Lock(changeAddr)
if err != nil {
return nil, fmt.Errorf("failed to create change script: %w", err)
}
tx.AddOutput(&transaction.TransactionOutput{
LockingScript: changeScript,
Change: true,
})
}
// Set fee rate using SatsPerKb if provided, otherwise use the default value
feeRate := config.SatsPerKb
if feeRate == 0 {
feeRate = DEFAULT_SAT_PER_KB
}
// Create fee model for computation
feeModel := &fee_model.SatoshisPerKilobyte{
Satoshis: feeRate,
}
err := tx.Fee(feeModel, transaction.ChangeDistributionEqual)
if err != nil {
return nil, fmt.Errorf("failed to calculate fee: %w", err)
}
// Sign the transaction
err = tx.Sign()
if err != nil {
return nil, fmt.Errorf("failed to sign transaction: %w", err)
}
return tx, nil
}