-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_ordinals.go
More file actions
187 lines (159 loc) · 5.15 KB
/
send_ordinals.go
File metadata and controls
187 lines (159 loc) · 5.15 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package ordinals
import (
"fmt"
"github.com/bitcoin-sv/go-templates/template/ordp2pkh"
"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"
)
// SendOrdinals sends ordinals to the given destinations
func SendOrdinals(config *SendOrdinalsConfig) (*transaction.Transaction, error) {
// Create a new transaction
tx := transaction.NewTransaction()
// Set defaults for optional parameters
feeRate := config.SatsPerKb
if feeRate == 0 {
feeRate = DEFAULT_SAT_PER_KB
}
// Set a default for enforceUniformSend if it's not provided
enforceUniform := config.EnforceUniformSend
// If enforceUniformSend is true, check that the number of destinations matches the number of ordinals
if enforceUniform && (len(config.Destinations) != len(config.Ordinals)) {
return nil, fmt.Errorf("number of destinations must match number of ordinals being sent")
}
// Add ordinal inputs first
for _, ordinalUtxo := range config.Ordinals {
// Verify that ordinals have exactly 1 satoshi
if ordinalUtxo.Satoshis != 1 {
return nil, fmt.Errorf("1Sat Ordinal utxos must have exactly 1 satoshi")
}
unlocker, err := p2pkh.Unlock(config.OrdPk, nil)
if err != nil {
return nil, fmt.Errorf("private key is required to sign the ordinal: %w", err)
}
err = tx.AddInputFrom(
ordinalUtxo.TxID,
ordinalUtxo.Vout,
ordinalUtxo.ScriptPubKey,
ordinalUtxo.Satoshis,
unlocker,
)
if err != nil {
return nil, fmt.Errorf("failed to add ordinal input: %w", err)
}
}
// Add payment inputs
for _, utxo := range config.PaymentUtxos {
unlocker, err := p2pkh.Unlock(config.PaymentPk, nil)
if err != nil {
return nil, fmt.Errorf("private key is required to sign the payment: %w", err)
}
err = tx.AddInputFrom(
utxo.TxID,
utxo.Vout,
utxo.ScriptPubKey,
utxo.Satoshis,
unlocker,
)
if err != nil {
return nil, fmt.Errorf("failed to add payment input: %w", err)
}
}
// Add outputs for each destination
for _, dest := range config.Destinations {
// Create the destination address
dstAddr, err := script.NewAddressFromString(dest.Address)
if err != nil {
return nil, fmt.Errorf("failed to create destination address: %w", err)
}
var lockingScript *script.Script
// Check if we should omit metadata
if dest.OmitMetadata() {
// If omitMetadata is enabled, use a simple P2PKH output
lockingScript, err = p2pkh.Lock(dstAddr)
if err != nil {
return nil, fmt.Errorf("failed to create p2pkh script: %w", err)
}
} else if dest.Inscription != nil {
// Create the ordinal P2PKH script with the inscription
ordP2pkh := &ordp2pkh.OrdP2PKH{
Inscription: dest.Inscription,
Address: dstAddr,
}
// Get the locking script
lockingScript, err = ordP2pkh.Lock()
if err != nil {
return nil, fmt.Errorf("failed to create ordp2pkh locking script: %w", err)
}
} else {
// Just create a regular P2PKH output
lockingScript, err = p2pkh.Lock(dstAddr)
if err != nil {
return nil, fmt.Errorf("failed to create p2pkh script: %w", err)
}
}
// Add the output to the transaction
tx.AddOutput(&transaction.TransactionOutput{
LockingScript: lockingScript,
Satoshis: 1, // 1 sat for ordinals
})
}
// Add additional payments if provided
if config.AdditionalPayments != nil {
for _, payment := range config.AdditionalPayments {
dstAddr, err := script.NewAddressFromString(payment.Address)
if err != nil {
return nil, fmt.Errorf("failed to create payment address: %w", err)
}
lockingScript, err := p2pkh.Lock(dstAddr)
if err != nil {
return nil, fmt.Errorf("failed to create payment script: %w", err)
}
tx.AddOutput(&transaction.TransactionOutput{
LockingScript: lockingScript,
Satoshis: payment.Satoshis,
})
}
}
// Add change output if needed
if config.ChangeAddress == "" && config.PaymentPk == nil {
return nil, fmt.Errorf("either changeAddress or paymentPk is required")
}
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,
})
}
// Calculate total inputs and outputs for funds check
totalIn := uint64(0)
for _, utxo := range config.PaymentUtxos {
totalIn += utxo.Satoshis
}
// Create fee model for computation
feeModel := &fee_model.SatoshisPerKilobyte{
Satoshis: feeRate,
}
err := tx.Fee(feeModel, transaction.ChangeDistributionEqual)
if err != nil {
if err.Error() == "insufficient funds for fee" {
return nil, fmt.Errorf("not enough funds to send ordinals. Total sats in: %d", totalIn)
}
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
}