-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction.go
268 lines (232 loc) · 5.94 KB
/
transaction.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package mint
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"strings"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
jsoniter "github.com/json-iterator/go"
log "github.com/sirupsen/logrus"
)
// Transaction transaction
type Transaction struct {
common.VersionedTransaction
Snapshot string `json:"snapshot"`
Hash crypto.Hash `json:"hash,omitempty"`
}
type UTXO struct {
common.Input
common.Output
Lock crypto.Hash `json:"lock,omitempty"`
}
// ReadUTXO read utxo
func ReadUTXO(hash crypto.Hash, index int, node ...string) (*common.UTXOWithLock, error) {
var n = RandomNode()
if len(node) > 0 && node[0] != "" {
n = node[0]
}
data, err := callRPC(n, "getutxo", []interface{}{hash, index})
if err != nil {
return nil, err
}
var o UTXO
if err := jsoniter.Unmarshal(data, &o); err != nil {
return nil, err
}
out := &common.UTXOWithLock{
LockHash: o.Lock,
}
out.UTXO.Input = o.Input
out.UTXO.Output = o.Output
return out, nil
}
func (t Transaction) ReadUTXOKeys(hash crypto.Hash, index uint) (*common.UTXOKeys, error) {
if t.Hash.String() != hash.String() {
return nil, errors.New("hash not matched")
}
if index >= uint(len(t.Outputs)) {
return nil, errors.New("index exceeds output bounds")
}
o := t.Outputs[index]
out := &common.UTXOKeys{}
out.Keys = o.Keys
out.Mask = o.Mask
return out, nil
}
// CheckDepositInput check deposit
func (t Transaction) CheckDepositInput(deposit *common.DepositData, tx crypto.Hash) error {
return nil
}
// ReadLastMintDistribution read last mint distribution
func (t Transaction) ReadLastMintDistribution(group string) (*common.MintDistribution, error) {
return nil, nil
}
// MakeOutTransaction make out transaction
func MakeOutTransaction(t *Transaction, indexs []int, outputAddress string, mask crypto.Key, keys []*crypto.Key, extra string) (*common.Transaction, error) {
if len(indexs) == 0 {
return nil, nil
}
tx := common.NewTransactionV5(t.Asset)
amount := common.NewInteger(0)
var script common.Script
for _, i := range indexs {
if i >= len(t.Outputs) {
return nil, errors.New("index exceeds output bounds")
}
o := t.Outputs[i]
script = o.Script
amount = amount.Add(o.Amount)
tx.AddInput(t.Hash, uint(i))
}
tx.Extra = []byte(extra)
if len(outputAddress) > 0 {
addr, err := common.NewAddressFromString(outputAddress)
if err != nil {
return nil, err
}
tx.AddRandomScriptOutput([]*common.Address{&addr}, script, amount)
} else {
tx.Outputs = []*common.Output{
{
Type: common.OutputTypeScript,
Amount: amount,
Keys: keys,
Script: script,
Mask: mask,
},
}
}
return tx, nil
}
// ReadTransaction read transaction
func ReadTransaction(hash string, node ...string) (*Transaction, error) {
var n = RandomNode()
if len(node) > 0 && node[0] != "" {
n = node[0]
}
data, err := callRPC(n, "gettransaction", []interface{}{hash})
if err != nil {
return nil, err
}
var t struct {
Transaction
Extra string `json:"extra"`
}
if err := jsoniter.Unmarshal(data, &t); err != nil {
return nil, err
}
if bts, err := hex.DecodeString(t.Extra); err == nil {
t.Transaction.Extra = bts
}
if !t.Hash.HasValue() {
return nil, errors.New("null transaction")
}
return &t.Transaction, nil
}
// SendTransaction send transaction
func SendTransaction(raw string, node ...string) (crypto.Hash, error) {
var n = RandomNode()
if len(node) > 0 && node[0] != "" {
n = node[0]
}
data, err := callRPC(n, "sendrawtransaction", []interface{}{raw})
if err != nil {
return crypto.Hash{}, err
}
var resp struct {
Hash crypto.Hash `json:"hash"`
}
if err := jsoniter.Unmarshal(data, &resp); err != nil {
return crypto.Hash{}, err
}
return resp.Hash, nil
}
// DoTransaction do transaction
func DoTransaction(ctx context.Context, rawData string) (*Transaction, error) {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil, errors.New("do transaction not done")
case <-ticker.C:
}
node := RandomNode()
h, err := SendTransaction(rawData, node)
if err != nil {
if strings.HasPrefix(err.Error(), "ERROR invalid output key ") {
return nil, nil
}
if strings.HasPrefix(err.Error(), "ERROR invalid lock ") {
return nil, err
}
log.Errorln("send transaction", err)
continue
}
log.Info("output transaction hash: ", h)
for i := 0; i < 6; i++ {
t, err := ReadTransaction(h.String(), node)
if err != nil {
log.Errorln("read transaction", err)
continue
}
if _, err := crypto.HashFromString(t.Snapshot); err == nil {
return t, nil
}
}
}
}
// WithdrawTransaction withdraw transaction
func WithdrawTransaction(ctx context.Context, t *Transaction, signer Signer, store Store, addr string, mask crypto.Key, keys []*crypto.Key, extra string) (*Transaction, error) {
var rawData = ""
storeKey := fmt.Sprintf("transaction_%s", t.Hash.String())
ensureFunc(func() error {
v, e := store.ReadProperty(ctx, storeKey)
if e == nil {
rawData = v
return nil
}
log.Errorln("read property", storeKey, e)
return e
})
if rawData == "" {
indexs, err := signer.VerifyOutputs(t)
if err != nil || len(indexs) == 0 {
return nil, err
}
var seed = make([]byte, 64)
ensureFunc(func() error {
_, err := rand.Read(seed)
if err == nil {
return nil
}
log.Errorln("rand read", err)
time.Sleep(time.Second)
return err
})
out, err := MakeOutTransaction(t, indexs, addr, mask, keys, extra)
if err != nil {
log.WithError(err).Errorln("make out transaction")
return nil, err
}
signed, err := signer.Sign(out, t)
if err != nil {
log.WithError(err).Errorln("sign transaction")
return nil, err
}
rawData = hex.EncodeToString(signed.Marshal())
ensureFunc(func() error {
e := store.WriteProperty(ctx, storeKey, rawData)
if e == nil {
return nil
}
log.Errorln("write property", storeKey, e)
return e
})
}
return DoTransaction(ctx, rawData)
}