-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
submit_transactions.go
247 lines (205 loc) · 6.82 KB
/
submit_transactions.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
package minercraft
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/tonicpow/go-minercraft/v2/apis/arc"
"github.com/tonicpow/go-minercraft/v2/apis/mapi"
)
type (
// RawSubmitTransactionsResponse is the response returned from mapi where payload is a string.
RawSubmitTransactionsResponse struct {
Encoding string `json:"encoding"`
MimeType string `json:"mimetype"`
Payload string `json:"payload"`
PublicKey string `json:"publicKey"`
Signature string `json:"signature"`
}
// SubmitTransactionsResponse is the formatted response which converts payload string to payloads.
SubmitTransactionsResponse struct {
Encoding string `json:"encoding"`
MimeType string `json:"mimetype"`
Payload UnifiedTxsPayload `json:"payload"`
PublicKey string `json:"publicKey"`
Signature string `json:"signature"`
}
// UnifiedTxsPayload is the structure of the json payload string in the MapiResponse.
UnifiedTxsPayload struct {
APIVersion string `json:"apiVersion"`
CurrentHighestBlockHash string `json:"currentHighestBlockHash"`
CurrentHighestBlockHeight int `json:"currentHighestBlockHeight"`
FailureCount int `json:"failureCount"`
MinerID string `json:"minerId"`
Timestamp time.Time `json:"timestamp"`
Txs []UnifiedTx `json:"txs"`
TxSecondMempoolExpiry int `json:"txSecondMempoolExpiry"`
}
// UnifiedTx is the transaction format in the mapi txs response.
UnifiedTx struct {
// mAPI specific fields
ConflictedWith []mapi.ConflictedWith `json:"conflictedWith,omitempty"`
ResultDescription string `json:"resultDescription"`
ReturnResult string `json:"returnResult"`
TxID string `json:"txid"`
// FailureRetryable if true indicates the tx can be resubmitted to mAPI.
FailureRetryable bool `json:"failureRetryable"`
// Arc specific fields
BlockHash string `json:"blockHash,omitempty"`
BlockHeight int64 `json:"blockHeight,omitempty"`
ExtraInfo string `json:"extraInfo,omitempty"`
Status int `json:"status,omitempty"`
Timestamp time.Time `json:"timestamp,omitempty"`
Title string `json:"title,omitempty"`
TxStatus arc.TxStatus `json:"txStatus,omitempty"`
}
)
// SubmitTransactions is used for submitting batched transactions
//
// Reference: https://github.com/bitcoin-sv-specs/brfc-merchantapi#5-submit-multiple-transactions
func (c *Client) SubmitTransactions(ctx context.Context, miner *Miner, txs []Transaction) (*SubmitTransactionsResponse, error) {
if miner == nil {
return nil, errors.New("miner was nil")
}
if len(txs) <= 0 {
return nil, errors.New("no transactions")
}
response, err := submitTransactions(ctx, c, miner, txs)
if err != nil {
return nil, err
}
switch c.apiType {
case MAPI:
var raw RawSubmitTransactionsResponse
if err = json.Unmarshal(response.BodyContents, &raw); err != nil {
return nil, err
}
return parseRawSubmitTransactionsResponse(raw)
case Arc:
return processArcSubmitTransactionsResponse(response)
default:
return nil, fmt.Errorf("unknown API type: %s", c.apiType)
}
}
// submitTransactions submits the transactions to the miner.
func submitTransactions(ctx context.Context, client *Client, miner *Miner, txs []Transaction) (*RequestResponse, error) {
api, err := client.MinerAPIByMinerID(miner.MinerID, client.apiType)
if err != nil {
return nil, err
}
var route string
if route, err = ActionRouteByAPIType(SubmitTxs, client.apiType); err != nil {
return nil, err
}
submitURL := api.URL + route
payload := &httpPayload{
Method: http.MethodPost,
URL: submitURL,
Token: api.Token,
Headers: make(map[string]string),
}
switch client.apiType {
case MAPI:
if err = proceedMapiSubmitTxs(
txs, payload,
); err != nil {
return nil, err
}
case Arc:
if err = proceedArcSubmitTxs(
txs, payload,
); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("unknown API type: %s", client.apiType)
}
response := httpRequest(ctx, client, payload)
return response, nil
}
func proceedArcSubmitTxs(txs []Transaction, httpPayload *httpPayload) error {
var rawTxs = make([]string, 0, len(txs))
for _, tx := range txs {
rawTxs = append(rawTxs, tx.RawTx)
}
body := map[string]interface{}{
"rawTx": rawTxs,
}
data, err := json.Marshal(body)
if err != nil {
return fmt.Errorf("failed to marshal JSON when submitting transactions: %w", err)
}
httpPayload.Data = data
if txs[0].MerkleProof {
httpPayload.Headers["X-MerkleProof"] = "true"
}
if txs[0].CallBackURL != "" {
httpPayload.Headers["X-CallbackUrl"] = txs[0].CallBackURL
}
if txs[0].CallBackToken != "" {
httpPayload.Headers["X-CallbackToken"] = txs[0].CallBackToken
}
if statusCode, ok := arc.MapTxStatusToInt(txs[0].WaitForStatus); ok {
httpPayload.Headers["X-WaitForStatus"] = strconv.Itoa(statusCode)
}
return nil
}
// proceedMapiSubmitTxs prepares the payload for MAPI.
func proceedMapiSubmitTxs(txs []Transaction, httpPayload *httpPayload) error {
data, err := json.Marshal(txs)
if err != nil {
return err
}
httpPayload.Data = data
return nil
}
// parseRawSubmitTransactionsResponse parses the raw response for MAPI.
func parseRawSubmitTransactionsResponse(raw RawSubmitTransactionsResponse) (*SubmitTransactionsResponse, error) {
result := &SubmitTransactionsResponse{
Signature: raw.Signature,
PublicKey: raw.PublicKey,
Encoding: raw.Encoding,
MimeType: raw.MimeType,
}
var payload UnifiedTxsPayload
if err := json.Unmarshal(
[]byte(raw.Payload), &payload,
); err != nil {
return nil, err
}
result.Payload = payload
return result, nil
}
// convertArcSubmitTxModelToUnifiedTx converts Arc's SubmitTxModel to UnifiedTx.
func convertArcSubmitTxModelToUnifiedTx(arcTxModel arc.SubmitTxModel) UnifiedTx {
return UnifiedTx{
BlockHash: arcTxModel.BlockHash,
BlockHeight: arcTxModel.BlockHeight,
ExtraInfo: arcTxModel.ExtraInfo,
Status: arcTxModel.Status,
Timestamp: arcTxModel.Timestamp,
Title: arcTxModel.Title,
TxStatus: arcTxModel.TxStatus,
TxID: arcTxModel.TxID,
}
}
// processArcSubmitTransactionsResponse processes the response for Arc.
func processArcSubmitTransactionsResponse(response *RequestResponse) (*SubmitTransactionsResponse, error) {
var arcResponse []arc.SubmitTxModel
if err := json.Unmarshal(response.BodyContents, &arcResponse); err != nil {
return nil, err
}
unifiedTxs := make([]UnifiedTx, len(arcResponse))
for i, arcTxModel := range arcResponse {
unifiedTxs[i] = convertArcSubmitTxModelToUnifiedTx(arcTxModel)
}
result := &SubmitTransactionsResponse{
Payload: UnifiedTxsPayload{
Txs: unifiedTxs,
},
}
return result, nil
}