forked from libsv/go-bt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
125 lines (107 loc) · 3.59 KB
/
input.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
package bt
import (
"encoding/binary"
"encoding/hex"
"fmt"
"github.com/libsv/go-bt/bscript"
)
/*
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
*/
// Input is a representation of a transaction input
//
// DO NOT CHANGE ORDER - Optimized for memory via maligned
//
type Input struct {
PreviousTxID string
PreviousTxSatoshis uint64
PreviousTxScript *bscript.Script
UnlockingScript *bscript.Script
PreviousTxOutIndex uint32
SequenceNumber uint32
}
// DefaultSequenceNumber is the default starting sequence number
const DefaultSequenceNumber uint32 = 0xFFFFFFFF
// NewInput creates a new empty Input object with a finalized sequence number.
func NewInput() *Input {
return &Input{
UnlockingScript: bscript.NewFromBytes(make([]byte, 0)),
SequenceNumber: DefaultSequenceNumber,
}
}
// NewInputFromBytes returns a transaction input from the bytes provided.
func NewInputFromBytes(bytes []byte) (*Input, int, error) {
if len(bytes) < 36 {
return nil, 0, fmt.Errorf("input length too short < 36")
}
offset := 36
l, size := DecodeVarInt(bytes[offset:])
offset += size
totalLength := offset + int(l) + 4 // 4 bytes for nSeq
if len(bytes) < totalLength {
return nil, 0, fmt.Errorf("input length too short < 36 + script + 4")
}
return &Input{
PreviousTxID: hex.EncodeToString(ReverseBytes(bytes[0:32])),
PreviousTxOutIndex: binary.LittleEndian.Uint32(bytes[32:36]),
SequenceNumber: binary.LittleEndian.Uint32(bytes[offset+int(l):]),
UnlockingScript: bscript.NewFromBytes(bytes[offset : offset+int(l)]),
}, totalLength, nil
}
// NewInputFromUTXO returns a transaction input from the UTXO fields provided.
func NewInputFromUTXO(prevTxID string, prevTxIndex uint32, prevTxSats uint64,
prevTxScript string, nSeq uint32) (*Input, error) {
pts, err := bscript.NewFromHexString(prevTxScript)
if err != nil {
return nil, err
}
return &Input{
PreviousTxID: prevTxID,
PreviousTxOutIndex: prevTxIndex,
PreviousTxSatoshis: prevTxSats,
PreviousTxScript: pts,
SequenceNumber: nSeq,
}, nil
}
func (i *Input) String() string {
return fmt.Sprintf(
`prevTxHash: %x
prevOutIndex: %d
scriptLen: %d
script: %x
sequence: %x
`,
i.PreviousTxID,
i.PreviousTxOutIndex,
len(*i.UnlockingScript),
i.UnlockingScript,
i.SequenceNumber,
)
}
// ToBytes encodes the Input into a hex byte array.
func (i *Input) ToBytes(clear bool) []byte {
h := make([]byte, 0)
// todo: not checking error
pid, _ := hex.DecodeString(i.PreviousTxID)
h = append(h, ReverseBytes(pid)...)
h = append(h, GetLittleEndianBytes(i.PreviousTxOutIndex, 4)...)
if clear {
h = append(h, 0x00)
} else {
if i.UnlockingScript == nil {
h = append(h, VarInt(0)...)
} else {
h = append(h, VarInt(uint64(len(*i.UnlockingScript)))...)
h = append(h, *i.UnlockingScript...)
}
}
return append(h, GetLittleEndianBytes(i.SequenceNumber, 4)...)
}