-
Notifications
You must be signed in to change notification settings - Fork 13
/
mixinnet_transaction.go
281 lines (238 loc) · 6.35 KB
/
mixinnet_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
269
270
271
272
273
274
275
276
277
278
279
280
281
package mixin
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"github.com/fox-one/msgpack"
"github.com/shopspring/decimal"
)
const (
TxVersion = 0x02
)
type (
MintData struct {
Group string `json:"group"`
Batch uint64 `json:"batch"`
Amount Integer `json:"amount"`
}
DepositData struct {
Chain Hash `json:"chain"`
AssetKey string `json:"asset"`
TransactionHash string `json:"transaction"`
OutputIndex uint64 `json:"index"`
Amount Integer `json:"amount"`
}
WithdrawalData struct {
Chain Hash `json:"chain"`
AssetKey string `json:"asset"`
Address string `json:"address"`
Tag string `json:"tag"`
}
Input struct {
Hash *Hash `json:"hash,omitempty"`
Index int `json:"index,omitempty"`
Genesis []byte `json:"genesis,omitempty"`
Deposit *DepositData `json:"deposit,omitempty"`
Mint *MintData `json:"mint,omitempty"`
}
Output struct {
Type uint8 `json:"type"`
Amount Integer `json:"amount"`
Keys []Key `json:"keys,omitempty"`
Withdrawal *WithdrawalData `json:"withdrawal,omitempty" msgpack:",omitempty"`
Script Script `json:"script"`
Mask Key `json:"mask,omitempty"`
}
AggregatedSignature struct {
Signers []int `json:"signers"`
Signature *Signature `json:"signature"`
}
Transaction struct {
Hash *Hash `json:"hash,omitempty" msgpack:"-"`
Snapshot *Hash `json:"snapshot,omitempty" msgpack:"-"`
Signatures []map[uint16]*Signature `json:"signatures,omitempty" msgpack:"-"`
AggregatedSignature *AggregatedSignature `json:"aggregated_signature,omitempty" msgpack:"-"`
Version uint8 `json:"version"`
Asset Hash `json:"asset"`
Inputs []*Input `json:"inputs"`
Outputs []*Output `json:"outputs"`
Extra TransactionExtra `json:"extra,omitempty"`
}
TransactionOutput struct {
Receivers []string
Threshold uint8
Amount decimal.Decimal
}
TransactionInput struct {
Memo string
Inputs []*MultisigUTXO
Outputs []TransactionOutput
Hint string
}
)
func (t *Transaction) DumpTransactionData() ([]byte, error) {
switch t.Version {
case 0, 1:
tx := TransactionV1{
Transaction: *t,
}
if len(t.Signatures) > 0 {
tx.Signatures = make([][]*Signature, len(t.Signatures))
for i, sigs := range t.Signatures {
tx.Signatures[i] = make([]*Signature, len(sigs))
for k, sig := range sigs {
tx.Signatures[i][k] = sig
}
}
}
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf).UseCompactEncoding(true)
err := enc.Encode(tx)
if err != nil {
return nil, err
}
return buf.Bytes(), nil
case 2:
return NewEncoder().EncodeTransaction(t), nil
default:
return nil, errors.New("unknown tx version")
}
}
func (t *Transaction) DumpTransaction() (string, error) {
bts, err := t.DumpTransactionData()
if err != nil {
return "", err
}
return hex.EncodeToString(bts), nil
}
func (t Transaction) DumpTransactionPayload() ([]byte, error) {
t.Signatures = nil
t.AggregatedSignature = nil
return t.DumpTransactionData()
}
func (t *Transaction) TransactionHash() (Hash, error) {
if t.Hash == nil {
raw, err := t.DumpTransactionPayload()
if err != nil {
return Hash{}, err
}
h := NewHash(raw)
t.Hash = &h
}
return *t.Hash, nil
}
func (i *TransactionInput) AppendUTXO(utxo *MultisigUTXO) {
i.Inputs = append(i.Inputs, utxo)
}
func (i *TransactionInput) AppendOutput(receivers []string, threshold uint8, amount decimal.Decimal) {
i.Outputs = append(i.Outputs, TransactionOutput{
Receivers: receivers,
Threshold: threshold,
Amount: amount,
})
}
func (i *TransactionInput) Asset() Hash {
if len(i.Inputs) == 0 {
return Hash{}
}
return i.Inputs[0].Asset()
}
func (i *TransactionInput) TotalInputAmount() decimal.Decimal {
var total decimal.Decimal
for _, input := range i.Inputs {
total = total.Add(input.Amount)
}
return total
}
func (i *TransactionInput) Validate() error {
if len(i.Inputs) == 0 {
return errors.New("no input utxo")
}
var (
members = map[string]bool{}
total = i.TotalInputAmount()
asset = i.Asset()
)
for _, input := range i.Inputs {
if asset != input.Asset() {
return errors.New("invalid input utxo, asset not matched")
}
if len(members) == 0 {
for _, u := range input.Members {
members[u] = true
}
continue
}
if len(members) != len(input.Members) {
return errors.New("invalid input utxo, member not matched")
}
for _, m := range input.Members {
if _, f := members[m]; !f {
return errors.New("invalid input utxo, member not matched")
}
}
}
for _, output := range i.Outputs {
if output.Threshold == 0 || int(output.Threshold) > len(output.Receivers) {
return fmt.Errorf("invalid output threshold: %d", output.Threshold)
}
if !output.Amount.IsPositive() {
return fmt.Errorf("invalid output amount: %v", output.Amount)
}
if total = total.Sub(output.Amount); total.IsNegative() {
return errors.New("invalid output: amount exceed")
}
}
return nil
}
func (c *Client) MakeMultisigTransaction(ctx context.Context, input *TransactionInput) (*Transaction, error) {
if err := input.Validate(); err != nil {
return nil, err
}
var tx = Transaction{
Version: TxVersion,
Asset: input.Asset(),
Extra: []byte(input.Memo),
}
// add inputs
for _, input := range input.Inputs {
tx.Inputs = append(tx.Inputs, &Input{
Hash: &input.TransactionHash,
Index: input.OutputIndex,
})
}
outputs := input.Outputs
// refund the change
{
change := input.TotalInputAmount()
for _, output := range input.Outputs {
change = change.Sub(output.Amount)
}
if change.IsPositive() {
outputs = append(outputs, TransactionOutput{
Receivers: input.Inputs[0].Members,
Threshold: input.Inputs[0].Threshold,
Amount: change,
})
}
}
ghostInputs := make([]*GhostInput, 0, len(outputs))
for idx, output := range outputs {
ghostInputs = append(ghostInputs, &GhostInput{
Receivers: output.Receivers,
Index: idx,
Hint: input.Hint,
})
}
ghosts, err := c.BatchReadGhostKeys(ctx, ghostInputs)
if err != nil {
return nil, err
}
for idx, output := range outputs {
ghost := ghosts[idx]
tx.Outputs = append(tx.Outputs, ghost.DumpOutput(output.Threshold, output.Amount))
}
return &tx, nil
}