-
Notifications
You must be signed in to change notification settings - Fork 4
/
tx_request.go
170 lines (154 loc) · 3.71 KB
/
tx_request.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
package gen
import (
"bytes"
"fmt"
"github.com/memocash/index/ref/bitcoin/memo"
"github.com/memocash/index/ref/bitcoin/tx/script"
"github.com/memocash/index/ref/bitcoin/wallet"
)
type InputGetterOld interface {
SetPkHashesToUse([][]byte)
GetUTXOsOld(*memo.UTXORequest) ([]memo.UTXO, error)
}
type InputGetterWrapper struct {
Old InputGetterOld
PkHashes [][]byte
UTXOs []memo.UTXO
Used []memo.UTXO
pksToUse [][]byte
reset bool
}
func GetWrapper(getter InputGetterOld, pkHash []byte) InputGetter {
return &InputGetterWrapper{
Old: getter,
PkHashes: [][]byte{pkHash},
}
}
func GetWrapperMultiKey(getter InputGetterOld, pkHashes [][]byte) InputGetter {
return &InputGetterWrapper{
Old: getter,
PkHashes: pkHashes,
}
}
func (w InputGetterWrapper) GetPkHashes() [][]byte {
var pkHashes = w.PkHashes
if len(w.pksToUse) > 0 {
loop:
for i := 0; i < len(pkHashes); i++ {
for _, pkToUse := range w.pksToUse {
if bytes.Equal(pkToUse, pkHashes[i]) {
continue loop
}
}
pkHashes = append(pkHashes[:i], pkHashes[i+1:]...)
i--
}
}
return pkHashes
}
func (w *InputGetterWrapper) GetUTXOs(request *memo.UTXORequest) ([]memo.UTXO, error) {
if request == nil {
request = new(memo.UTXORequest)
}
pkHashes := w.GetPkHashes()
var usableUtxos []memo.UTXO
utxoLoop:
for _, utxo := range w.UTXOs {
for _, pkHash := range pkHashes {
if bytes.Equal(pkHash, utxo.Input.PkHash) {
usableUtxos = append(usableUtxos, utxo)
continue utxoLoop
}
}
}
if w.reset {
w.reset = false
if len(usableUtxos) > 0 {
return usableUtxos, nil
}
}
utxos, err := w.Old.GetUTXOsOld(request)
if err != nil {
return nil, fmt.Errorf("error getting UTXOs using wrapper; %w", err)
}
for i := 0; i < len(utxos); i++ {
for _, used := range w.Used {
if bytes.Equal(used.Input.PrevOutHash, utxos[i].Input.PrevOutHash) &&
used.Input.PrevOutIndex == utxos[i].Input.PrevOutIndex {
utxos = append(utxos[:i], utxos[i+1:]...)
i--
break
}
}
}
w.UTXOs = append(w.UTXOs, utxos...)
return utxos, nil
}
func (w *InputGetterWrapper) MarkUTXOsUsed(utxos []memo.UTXO) {
w.Used = append(w.Used, utxos...)
loop:
for i := 0; i < len(w.UTXOs); i++ {
for _, utxo := range utxos {
if utxo.IsEqual(w.UTXOs[i]) {
w.UTXOs = append(w.UTXOs[:i], w.UTXOs[i+1:]...)
i--
continue loop
}
}
}
}
func (w *InputGetterWrapper) SetPkHashesToUse(pkHashes [][]byte) {
w.pksToUse = pkHashes
w.Old.SetPkHashesToUse(pkHashes)
}
func (w *InputGetterWrapper) AddChangeUTXO(utxo memo.UTXO) {
for _, pkHash := range w.PkHashes {
if bytes.Equal(pkHash, utxo.Input.PkHash) {
utxo.AncestorsNC++
w.UTXOs = append([]memo.UTXO{utxo}, w.UTXOs...)
return
}
}
}
func (w *InputGetterWrapper) NewTx() {
w.reset = true
}
type InputGetter interface {
SetPkHashesToUse([][]byte)
GetUTXOs(*memo.UTXORequest) ([]memo.UTXO, error)
MarkUTXOsUsed([]memo.UTXO)
AddChangeUTXO(memo.UTXO)
NewTx()
}
type FaucetSaver interface {
Save(userPkHash []byte, faucetPkHash []byte, fundTxHash []byte, memoTxHash []byte) error
IsFreeTx([]*memo.Output) bool
GetKey() wallet.PrivateKey
}
type TxRequest struct {
Getter InputGetter
InputsToUse []memo.UTXO
Outputs []*memo.Output
Change wallet.Change
KeyRing wallet.KeyRing
}
func (r TxRequest) GetTokenSendOutput() *script.TokenSend {
for _, spendOutput := range r.Outputs {
spendOutputScript, ok := spendOutput.Script.(*script.TokenSend)
if ok {
return spendOutputScript
}
}
return nil
}
func (r TxRequest) GetTokenHash() []byte {
for _, spendOutput := range r.Outputs {
switch v := spendOutput.Script.(type) {
case *script.TokenSend:
return v.TokenHash
case *script.TokenMint:
return v.TokenHash
}
}
return nil
}