-
Notifications
You must be signed in to change notification settings - Fork 26
/
txoutput.go
214 lines (178 loc) · 5.66 KB
/
txoutput.go
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package bt
import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/libsv/go-bk/bip32"
"github.com/libsv/go-bk/crypto"
"github.com/libsv/go-bt/v2/bscript"
"github.com/pkg/errors"
)
// newOutputFromBytes returns a transaction Output from the bytes provided
func newOutputFromBytes(bytes []byte) (*Output, int, error) {
if len(bytes) < 8 {
return nil, 0, fmt.Errorf("%w < 8", ErrOutputTooShort)
}
offset := 8
l, size := NewVarIntFromBytes(bytes[offset:])
offset += size
totalLength := offset + int(l)
if len(bytes) < totalLength {
return nil, 0, fmt.Errorf("%w < 8 + script", ErrInputTooShort)
}
s := bscript.Script(bytes[offset:totalLength])
return &Output{
Satoshis: binary.LittleEndian.Uint64(bytes[0:8]),
LockingScript: &s,
}, totalLength, nil
}
// TotalOutputSatoshis returns the total Satoshis outputted from the transaction.
func (tx *Tx) TotalOutputSatoshis() (total uint64) {
for _, o := range tx.Outputs {
total += o.Satoshis
}
return
}
// AddP2PKHOutputFromPubKeyHashStr makes an output to a PKH with a value.
func (tx *Tx) AddP2PKHOutputFromPubKeyHashStr(publicKeyHash string, satoshis uint64) error {
s, err := bscript.NewP2PKHFromPubKeyHashStr(publicKeyHash)
if err != nil {
return err
}
tx.AddOutput(&Output{
Satoshis: satoshis,
LockingScript: s,
})
return nil
}
// AddP2PKHOutputFromPubKeyBytes makes an output to a PKH with a value.
func (tx *Tx) AddP2PKHOutputFromPubKeyBytes(publicKeyBytes []byte, satoshis uint64) error {
s, err := bscript.NewP2PKHFromPubKeyBytes(publicKeyBytes)
if err != nil {
return err
}
tx.AddOutput(&Output{
Satoshis: satoshis,
LockingScript: s,
})
return nil
}
// AddP2PKHOutputFromPubKeyStr makes an output to a PKH with a value.
func (tx *Tx) AddP2PKHOutputFromPubKeyStr(publicKey string, satoshis uint64) error {
s, err := bscript.NewP2PKHFromPubKeyStr(publicKey)
if err != nil {
return err
}
tx.AddOutput(&Output{
Satoshis: satoshis,
LockingScript: s,
})
return nil
}
// AddP2PKHOutputFromAddress makes an output to a PKH with a value.
func (tx *Tx) AddP2PKHOutputFromAddress(addr string, satoshis uint64) error {
s, err := bscript.NewP2PKHFromAddress(addr)
if err != nil {
return err
}
tx.AddOutput(&Output{
Satoshis: satoshis,
LockingScript: s,
})
return nil
}
// AddP2PKHOutputFromScript makes an output to a P2PKH script paid to the provided locking script with a value.
func (tx *Tx) AddP2PKHOutputFromScript(script *bscript.Script, satoshis uint64) error {
if !script.IsP2PKH() {
return errors.Wrapf(ErrInvalidScriptType, "'%s' is not a valid P2PKH script", script.ScriptType())
}
tx.AddOutput(&Output{
Satoshis: satoshis,
LockingScript: script,
})
return nil
}
// AddP2PKHOutputFromBip32ExtKey generated a random P2PKH output script from a provided *bip32.ExtendedKey,
// and add it to the receiving tx. The derviation path used is returned.
func (tx *Tx) AddP2PKHOutputFromBip32ExtKey(privKey *bip32.ExtendedKey, satoshis uint64) (string, error) {
script, derivationPath, err := bscript.NewP2PKHFromBip32ExtKey(privKey)
if err != nil {
return "", err
}
tx.AddOutput(&Output{
LockingScript: script,
Satoshis: satoshis,
})
return derivationPath, nil
}
// AddHashPuzzleOutput makes an output to a hash puzzle + PKH with a value.
func (tx *Tx) AddHashPuzzleOutput(secret, publicKeyHash string, satoshis uint64) error {
publicKeyHashBytes, err := hex.DecodeString(publicKeyHash)
if err != nil {
return err
}
s := &bscript.Script{}
_ = s.AppendOpcodes(bscript.OpHASH160)
secretBytesHash := crypto.Hash160([]byte(secret))
if err = s.AppendPushData(secretBytesHash); err != nil {
return err
}
_ = s.AppendOpcodes(bscript.OpEQUALVERIFY, bscript.OpDUP, bscript.OpHASH160)
if err = s.AppendPushData(publicKeyHashBytes); err != nil {
return err
}
_ = s.AppendOpcodes(bscript.OpEQUALVERIFY, bscript.OpCHECKSIG)
tx.AddOutput(&Output{
Satoshis: satoshis,
LockingScript: s,
})
return nil
}
// AddOpReturnOutput creates a new Output with OP_FALSE OP_RETURN and then the data
// passed in encoded as hex.
func (tx *Tx) AddOpReturnOutput(data []byte) error {
o, err := CreateOpReturnOutput([][]byte{data})
if err != nil {
return err
}
tx.AddOutput(o)
return nil
}
// AddOpReturnPartsOutput creates a new Output with OP_FALSE OP_RETURN and then
// uses OP_PUSHDATA format to encode the multiple byte arrays passed in.
func (tx *Tx) AddOpReturnPartsOutput(data [][]byte) error {
o, err := CreateOpReturnOutput(data)
if err != nil {
return err
}
tx.AddOutput(o)
return nil
}
// CreateOpReturnOutput creates a new Output with OP_FALSE OP_RETURN and then
// uses OP_PUSHDATA format to encode the multiple byte arrays passed in.
func CreateOpReturnOutput(data [][]byte) (*Output, error) {
s := &bscript.Script{}
_ = s.AppendOpcodes(bscript.OpFALSE, bscript.OpRETURN)
if err := s.AppendPushDataArray(data); err != nil {
return nil, err
}
return &Output{LockingScript: s}, nil
}
// OutputCount returns the number of transaction Inputs.
func (tx *Tx) OutputCount() int {
return len(tx.Outputs)
}
// AddOutput adds a new output to the transaction.
func (tx *Tx) AddOutput(output *Output) {
tx.Outputs = append(tx.Outputs, output)
}
// PayTo creates a new P2PKH output from a BitCoin address (base58)
// and the satoshis amount and adds that to the transaction.
func (tx *Tx) PayTo(script *bscript.Script, satoshis uint64) error {
return tx.AddP2PKHOutputFromScript(script, satoshis)
}
// PayToAddress creates a new P2PKH output from a BitCoin address (base58)
// and the satoshis amount and adds that to the transaction.
func (tx *Tx) PayToAddress(addr string, satoshis uint64) error {
return tx.AddP2PKHOutputFromAddress(addr, satoshis)
}