tx.go
TEXT FILE3.0 KB · Public source code for developers, search crawlers and AI agents
1package tx
2
3import (
4 "bytes"
5 "crypto/sha256"
6 "encoding/binary"
7 "encoding/hex"
8 "errors"
9)
10
11const MaxMoney int64 = 21000000 * 100000000
12
13type OutPoint struct {
14 Hash [32]byte
15 Index uint32
16}
17type Input struct {
18 Previous OutPoint
19 Script []byte
20 Sequence uint32
21 Witness [][]byte
22}
23type Output struct {
24 Value int64
25 Script []byte
26}
27type Transaction struct {
28 Version int32
29 Inputs []Input
30 Outputs []Output
31 LockTime uint32
32}
33
34func compact(b *bytes.Buffer, n uint64) {
35 switch {
36 case n < 253:
37 b.WriteByte(byte(n))
38 case n <= 65535:
39 b.WriteByte(253)
40 binary.Write(b, binary.LittleEndian, uint16(n))
41 case n <= 0xffffffff:
42 b.WriteByte(254)
43 binary.Write(b, binary.LittleEndian, uint32(n))
44 default:
45 b.WriteByte(255)
46 binary.Write(b, binary.LittleEndian, n)
47 }
48}
49func blob(b *bytes.Buffer, data []byte) { compact(b, uint64(len(data))); b.Write(data) }
50
51func (t Transaction) Serialize(witness bool) []byte {
52 var b bytes.Buffer
53 put := func(v any) { binary.Write(&b, binary.LittleEndian, v) }
54 put(t.Version)
55 has := false
56 for _, in := range t.Inputs {
57 if len(in.Witness) > 0 {
58 has = true
59 }
60 }
61 has = has && witness
62 if has {
63 b.Write([]byte{0, 1})
64 }
65 compact(&b, uint64(len(t.Inputs)))
66 for _, in := range t.Inputs {
67 b.Write(in.Previous.Hash[:])
68 put(in.Previous.Index)
69 blob(&b, in.Script)
70 put(in.Sequence)
71 }
72 compact(&b, uint64(len(t.Outputs)))
73 for _, out := range t.Outputs {
74 put(out.Value)
75 blob(&b, out.Script)
76 }
77 if has {
78 for _, in := range t.Inputs {
79 compact(&b, uint64(len(in.Witness)))
80 for _, item := range in.Witness {
81 blob(&b, item)
82 }
83 }
84 }
85 put(t.LockTime)
86 return b.Bytes()
87}
88
89func displayHash(data []byte) string {
90 h := sha256.Sum256(data)
91 h = sha256.Sum256(h[:])
92 for i := 0; i < 16; i++ {
93 h[i], h[31-i] = h[31-i], h[i]
94 }
95 return hex.EncodeToString(h[:])
96}
97func (t Transaction) TXID() string { return displayHash(t.Serialize(false)) }
98func (t Transaction) WTXID() string { return displayHash(t.Serialize(true)) }
99func (t Transaction) Weight() int { return 3*len(t.Serialize(false)) + len(t.Serialize(true)) }
100func (t Transaction) VSize() int { return (t.Weight() + 3) / 4 }
101
102// Fee checks a supplied UTXO snapshot, not chain state or scripts.
103func (t Transaction) Fee(coins map[OutPoint]Output) (int64, error) {
104 if len(t.Inputs) == 0 || len(t.Outputs) == 0 {
105 return 0, errors.New("empty transaction")
106 }
107 var totalIn, totalOut int64
108 seen := make(map[OutPoint]bool)
109 for _, in := range t.Inputs {
110 if seen[in.Previous] {
111 return 0, errors.New("duplicate input")
112 }
113 seen[in.Previous] = true
114 coin, ok := coins[in.Previous]
115 if !ok {
116 return 0, errors.New("missing prevout")
117 }
118 if coin.Value < 0 || coin.Value > MaxMoney-totalIn {
119 return 0, errors.New("input amount out of range")
120 }
121 totalIn += coin.Value
122 }
123 for _, out := range t.Outputs {
124 if out.Value < 0 || out.Value > MaxMoney-totalOut {
125 return 0, errors.New("output amount out of range")
126 }
127 totalOut += out.Value
128 }
129 if totalOut > totalIn {
130 return 0, errors.New("insufficient input value")
131 }
132 return totalIn - totalOut, nil
133}Read-only source · Select a line number to link directly · Raw download above
