-
Notifications
You must be signed in to change notification settings - Fork 13
/
collectible.go
176 lines (150 loc) · 5.4 KB
/
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
package mixin
import (
"context"
"errors"
"fmt"
"time"
"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"`
TransactionHash Hash `json:"transaction_hash,omitempty"`
OutputIndex int `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 Hash `json:"transaction_hash,omitempty"`
RawTransaction string `json:"raw_transaction,omitempty"`
}
// ReadCollectibleOutputs return a list of collectibles outputs
func (c *Client) ReadCollectibleOutputs(ctx context.Context, members []string, threshold uint8, 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"] = fmt.Sprint(limit)
}
if len(members) > 0 {
if threshold < 1 || int(threshold) > len(members) {
return nil, errors.New("invalid members")
}
params["members"] = HashMembers(members)
params["threshold"] = fmt.Sprint(threshold)
}
var outputs []*CollectibleOutput
if err := c.Get(ctx, "/collectibles/outputs", params, &outputs); err != nil {
return nil, err
}
return outputs, nil
}
func (c *Client) MakeCollectibleTransaction(
ctx context.Context,
output *CollectibleOutput,
token *CollectibleToken,
receivers []string,
threshold uint8,
) (*Transaction, error) {
tx := &Transaction{
Version: TxVersion,
Asset: token.MixinID,
Extra: token.NFO,
Inputs: []*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 = []*Output{ghostInputs[0].DumpOutput(threshold, output.Amount)}
return tx, nil
}
// 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
}
// SignCollectibleRequest sign a collectibles request
func (c *Client) SignCollectibleRequest(ctx context.Context, reqID, pin string) (*CollectibleRequest, error) {
uri := "/collectibles/requests/" + reqID + "/sign"
params := map[string]string{
"pin": c.EncryptPin(pin),
}
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
}
// UnlockCollectibleRequest unlock a collectibles request
func (c *Client) UnlockCollectibleRequest(ctx context.Context, reqID, pin string) error {
var (
uri = "/collectibles/requests/" + reqID + "/unlock"
params = map[string]string{
"pin": c.EncryptPin(pin),
}
)
if err := c.Post(ctx, uri, params, nil); err != nil {
return err
}
return nil
}