forked from libsv/go-bt
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinput.go
More file actions
155 lines (134 loc) · 4.3 KB
/
Copy pathinput.go
File metadata and controls
155 lines (134 loc) · 4.3 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
package bt
import (
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"github.com/libsv/go-bt/v2/bscript"
"github.com/pkg/errors"
)
/*
Field Description Size
--------------------------------------------------------------------------------------------------------
Previous Transaction hash doubled SHA256-hashed of a (previous) to-be-used transaction 32 bytes
Previous Txout-index non-negative integer indexing an output of the to-be-used 4 bytes
transaction
Txin-script length non-negative integer VI = VarInt 1-9 bytes
Txin-script / scriptSig Script <in-script length>-many bytes
sequence_no normally 0xFFFFFFFF; irrelevant unless transaction's 4 bytes
lock_time is > 0
*/
// DefaultSequenceNumber is the default starting sequence number
const DefaultSequenceNumber uint32 = 0xFFFFFFFF
// Input is a representation of a transaction input
//
// DO NOT CHANGE ORDER - Optimised for memory via maligned
//
type Input struct {
previousTxID []byte
PreviousTxSatoshis uint64
PreviousTxScript *bscript.Script
UnlockingScript *bscript.Script
PreviousTxOutIndex uint32
SequenceNumber uint32
}
// ReadFrom reads from the `io.Reader` into the `bt.Input`.
func (i *Input) ReadFrom(r io.Reader) (int64, error) {
*i = Input{}
var bytesRead int64
previousTxID := make([]byte, 32)
n, err := io.ReadFull(r, previousTxID)
bytesRead += int64(n)
if err != nil {
return bytesRead, errors.Wrapf(err, "previousTxID(32): got %d bytes", n)
}
prevIndex := make([]byte, 4)
n, err = io.ReadFull(r, prevIndex)
bytesRead += int64(n)
if err != nil {
return bytesRead, errors.Wrapf(err, "previousTxID(4): got %d bytes", n)
}
var l VarInt
n64, err := l.ReadFrom(r)
bytesRead += n64
if err != nil {
return bytesRead, err
}
script := make([]byte, l)
n, err = io.ReadFull(r, script)
bytesRead += int64(n)
if err != nil {
return bytesRead, errors.Wrapf(err, "script(%d): got %d bytes", l, n)
}
sequence := make([]byte, 4)
n, err = io.ReadFull(r, sequence)
bytesRead += int64(n)
if err != nil {
return bytesRead, errors.Wrapf(err, "sequence(4): got %d bytes", n)
}
i.previousTxID = ReverseBytes(previousTxID)
i.PreviousTxOutIndex = binary.LittleEndian.Uint32(prevIndex)
i.UnlockingScript = bscript.NewFromBytes(script)
i.SequenceNumber = binary.LittleEndian.Uint32(sequence)
return bytesRead, nil
}
// PreviousTxIDAdd will add the supplied txID bytes to the Input,
// if it isn't a valid transaction id an ErrInvalidTxID error will be returned.
func (i *Input) PreviousTxIDAdd(txID []byte) error {
if !IsValidTxID(txID) {
return ErrInvalidTxID
}
i.previousTxID = txID
return nil
}
// PreviousTxIDAddStr will validate and add the supplied txID string to the Input,
// if it isn't a valid transaction id an ErrInvalidTxID error will be returned.
func (i *Input) PreviousTxIDAddStr(txID string) error {
bb, err := hex.DecodeString(txID)
if err != nil {
return err
}
return i.PreviousTxIDAdd(bb)
}
// PreviousTxID will return the PreviousTxID if set.
func (i *Input) PreviousTxID() []byte {
return i.previousTxID
}
// PreviousTxIDStr returns the Previous TxID as a hex string.
func (i *Input) PreviousTxIDStr() string {
return hex.EncodeToString(i.previousTxID)
}
// String implements the Stringer interface and returns a string
// representation of a transaction input.
func (i *Input) String() string {
return fmt.Sprintf(
`prevTxHash: %s
prevOutIndex: %d
scriptLen: %d
script: %s
sequence: %x
`,
hex.EncodeToString(i.previousTxID),
i.PreviousTxOutIndex,
len(*i.UnlockingScript),
i.UnlockingScript,
i.SequenceNumber,
)
}
// Bytes encodes the Input into a hex byte array.
func (i *Input) Bytes(clear bool) []byte {
h := make([]byte, 0)
h = append(h, ReverseBytes(i.previousTxID)...)
h = append(h, LittleEndianBytes(i.PreviousTxOutIndex, 4)...)
if clear {
h = append(h, 0x00)
} else {
if i.UnlockingScript == nil {
h = append(h, VarInt(0).Bytes()...)
} else {
h = append(h, VarInt(uint64(len(*i.UnlockingScript))).Bytes()...)
h = append(h, *i.UnlockingScript...)
}
}
return append(h, LittleEndianBytes(i.SequenceNumber, 4)...)
}