-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlegacy_collectible.go
224 lines (192 loc) · 7.38 KB
/
legacy_collectible.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
package mixin
import (
"context"
"errors"
"strconv"
"time"
"github.com/fox-one/mixin-sdk-go/v2/mixinnet"
"github.com/shopspring/decimal"
)
const (
// CollectibleOutputState
CollectibleOutputStateUnspent = "unspent"
CollectibleOutputStateSigned = "signed"
CollectibleOutputStateSpent = "spent"
// CollectibleRequestAction
CollectibleRequestActionSign = "sign"
CollectibleRequestActionUnlock = "unlock"
// CollectibleRequestState
CollectibleRequestStateInitial = "initial"
CollectibleRequestStateSigned = "signed"
)
type CollectibleOutput struct {
Type string `json:"type,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
UserID string `json:"user_id,omitempty"`
OutputID string `json:"output_id,omitempty"`
TokenID string `json:"token_id,omitempty"`
Extra string `json:"extra,omitempty"`
TransactionHash mixinnet.Hash `json:"transaction_hash,omitempty"`
OutputIndex uint8 `json:"output_index,omitempty"`
Amount decimal.Decimal `json:"amount,omitempty"`
Senders []string `json:"senders,omitempty"`
SendersThreshold uint8 `json:"senders_threshold,omitempty"`
Receivers []string `json:"receivers,omitempty"`
ReceiversThreshold uint8 `json:"receivers_threshold,omitempty"`
State string `json:"state,omitempty"`
SignedBy string `json:"signed_by,omitempty"`
SignedTx string `json:"signed_tx,omitempty"`
}
type CollectibleRequest struct {
Type string `json:"type,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
RequestID string `json:"request_id,omitempty"`
UserID string `json:"user_id,omitempty"`
TokenID string `json:"token_id,omitempty"`
Amount decimal.Decimal `json:"amount,omitempty"`
Senders []string `json:"senders,omitempty"`
SendersThreshold uint8 `json:"senders_threshold,omitempty"`
Receivers []string `json:"receivers,omitempty"`
ReceiversThreshold uint8 `json:"receivers_threshold,omitempty"`
Signers []string `json:"signers,omitempty"`
Action string `json:"action,omitempty"`
State string `json:"state,omitempty"`
TransactionHash mixinnet.Hash `json:"transaction_hash,omitempty"`
RawTransaction string `json:"raw_transaction,omitempty"`
CodeID string `json:"code_id"`
}
// ReadCollectibleOutputs return a list of collectibles outputs
func (c *Client) ReadCollectibleOutputs(ctx context.Context, members []string, threshold uint8, state string, offset time.Time, limit int) ([]*CollectibleOutput, error) {
params := make(map[string]string)
if !offset.IsZero() {
params["offset"] = offset.UTC().Format(time.RFC3339Nano)
}
if limit > 0 {
params["limit"] = strconv.Itoa(limit)
}
if state != "" {
params["state"] = state
}
if len(members) > 0 {
if threshold < 1 || int(threshold) > len(members) {
return nil, errors.New("invalid members")
}
params["members"] = mixinnet.HashMembers(members)
params["threshold"] = strconv.Itoa(int(threshold))
}
var outputs []*CollectibleOutput
if err := c.Get(ctx, "/collectibles/outputs", params, &outputs); err != nil {
return nil, err
}
return outputs, nil
}
// ReadCollectibleOutputs request with accessToken and returns a list of collectibles outputs
func ReadCollectibleOutputs(ctx context.Context, accessToken string, members []string, threshold uint8, state string, offset time.Time, limit int) ([]*CollectibleOutput, error) {
return NewFromAccessToken(accessToken).ReadCollectibleOutputs(ctx, members, threshold, state, offset, limit)
}
func (c *Client) MakeCollectibleTransaction(
ctx context.Context,
txVer uint8,
output *CollectibleOutput,
token *CollectibleToken,
receivers []string,
threshold uint8,
) (*mixinnet.Transaction, error) {
tx := &mixinnet.Transaction{
Version: txVer,
Asset: token.MixinID,
Extra: token.NFO,
Inputs: []*mixinnet.Input{{
Hash: &output.TransactionHash,
Index: output.OutputIndex,
}},
}
ghostInputs, err := c.BatchReadGhostKeys(ctx, []*GhostInput{{
Receivers: receivers,
Index: 0,
Hint: output.OutputID,
}})
if err != nil {
return nil, err
}
tx.Outputs = []*mixinnet.Output{
{
Keys: ghostInputs[0].Keys,
Mask: ghostInputs[0].Mask,
Amount: mixinnet.IntegerFromDecimal(output.Amount),
Script: mixinnet.NewThresholdScript(threshold),
},
}
return tx, nil
}
// MakeCollectibleTransaction make collectible transaction with accessToken
func MakeCollectibleTransaction(
ctx context.Context,
accessToken string,
txVer uint8,
output *CollectibleOutput,
token *CollectibleToken,
receivers []string,
threshold uint8,
) (*mixinnet.Transaction, error) {
return NewFromAccessToken(accessToken).MakeCollectibleTransaction(ctx, txVer, output, token, receivers, threshold)
}
// CreateCollectibleRequest create a collectibles request
func (c *Client) CreateCollectibleRequest(ctx context.Context, action, raw string) (*CollectibleRequest, error) {
params := map[string]string{
"action": action,
"raw": raw,
}
var req CollectibleRequest
if err := c.Post(ctx, "/collectibles/requests", params, &req); err != nil {
return nil, err
}
return &req, nil
}
// CreateCollectibleRequest create a collectibles request with accessToken
func CreateCollectibleRequest(ctx context.Context, accessToken, action, raw string) (*CollectibleRequest, error) {
return NewFromAccessToken(accessToken).CreateCollectibleRequest(ctx, action, raw)
}
// SignCollectibleRequest sign a collectibles request
func (c *Client) SignCollectibleRequest(ctx context.Context, reqID, pin string) (*CollectibleRequest, error) {
params := map[string]string{}
if key, err := mixinnet.KeyFromString(pin); err == nil {
params["pin_base64"] = c.EncryptTipPin(key, TIPCollectibleRequestSign, reqID)
} else {
params["pin"] = c.EncryptPin(pin)
}
uri := "/collectibles/requests/" + reqID + "/sign"
var req CollectibleRequest
if err := c.Post(ctx, uri, params, &req); err != nil {
return nil, err
}
return &req, nil
}
// CancelCollectible cancel a collectibles request
func (c *Client) CancelCollectibleRequest(ctx context.Context, reqID string) error {
uri := "/collectibles/requests/" + reqID + "/cancel"
if err := c.Post(ctx, uri, nil, nil); err != nil {
return err
}
return nil
}
// CancelCollectible cancel a collectibles request with accessToken
func CancelCollectibleRequest(ctx context.Context, accessToken, reqID string) error {
return NewFromAccessToken(accessToken).CancelCollectibleRequest(ctx, reqID)
}
// UnlockCollectibleRequest unlock a collectibles request
func (c *Client) UnlockCollectibleRequest(ctx context.Context, reqID, pin string) error {
params := map[string]string{}
if key, err := mixinnet.KeyFromString(pin); err == nil {
params["pin_base64"] = c.EncryptTipPin(key, TIPCollectibleRequestUnlock, reqID)
} else {
params["pin"] = c.EncryptPin(pin)
}
var uri = "/collectibles/requests/" + reqID + "/unlock"
if err := c.Post(ctx, uri, params, nil); err != nil {
return err
}
return nil
}