forked from coinbase/mesh-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
construction.go
335 lines (298 loc) · 10.9 KB
/
construction.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2020 Coinbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fetcher
import (
"context"
"fmt"
"github.com/coinbase/rosetta-sdk-go/asserter"
"github.com/coinbase/rosetta-sdk-go/types"
)
// ConstructionCombine creates a network-specific transaction from an unsigned
// transaction and an array of provided signatures.
//
// The signed transaction returned from this method will be sent to the
// `/construction/submit` endpoint by the caller.
func (f *Fetcher) ConstructionCombine(
ctx context.Context,
network *types.NetworkIdentifier,
unsignedTransaction string,
signatures []*types.Signature,
) (string, *Error) {
if err := f.connectionSemaphore.Acquire(ctx, semaphoreRequestWeight); err != nil {
return "", &Error{
Err: fmt.Errorf("%w: %s", ErrCouldNotAcquireSemaphore, err.Error()),
}
}
defer f.connectionSemaphore.Release(semaphoreRequestWeight)
response, clientErr, err := f.rosettaClient.ConstructionAPI.ConstructionCombine(ctx,
&types.ConstructionCombineRequest{
NetworkIdentifier: network,
UnsignedTransaction: unsignedTransaction,
Signatures: signatures,
},
)
if err != nil {
return "", f.RequestFailedError(clientErr, err, "/construction/combine")
}
if err := asserter.ConstructionCombineResponse(response); err != nil {
fetcherErr := &Error{
Err: fmt.Errorf("%w: /construction/combine", err),
}
return "", fetcherErr
}
return response.SignedTransaction, nil
}
// ConstructionDerive returns the network-specific address associated with a
// public key.
//
// Blockchains that require an on-chain action to create an
// account should not implement this method.
func (f *Fetcher) ConstructionDerive(
ctx context.Context,
network *types.NetworkIdentifier,
publicKey *types.PublicKey,
metadata map[string]interface{},
) (*types.AccountIdentifier, map[string]interface{}, *Error) {
if err := f.connectionSemaphore.Acquire(ctx, semaphoreRequestWeight); err != nil {
return nil, nil, &Error{
Err: fmt.Errorf("%w: %s", ErrCouldNotAcquireSemaphore, err.Error()),
}
}
defer f.connectionSemaphore.Release(semaphoreRequestWeight)
response, clientErr, err := f.rosettaClient.ConstructionAPI.ConstructionDerive(ctx,
&types.ConstructionDeriveRequest{
NetworkIdentifier: network,
PublicKey: publicKey,
Metadata: metadata,
},
)
if err != nil {
return nil, nil, f.RequestFailedError(clientErr, err, "/construction/derive")
}
if err := asserter.ConstructionDeriveResponse(response); err != nil {
fetcherErr := &Error{
Err: fmt.Errorf("%w: /construction/derive", err),
}
return nil, nil, fetcherErr
}
return response.AccountIdentifier, response.Metadata, nil
}
// ConstructionHash returns the network-specific transaction hash for
// a signed transaction.
func (f *Fetcher) ConstructionHash(
ctx context.Context,
network *types.NetworkIdentifier,
signedTransaction string,
) (*types.TransactionIdentifier, *Error) {
if err := f.connectionSemaphore.Acquire(ctx, semaphoreRequestWeight); err != nil {
return nil, &Error{
Err: fmt.Errorf("%w: %s", ErrCouldNotAcquireSemaphore, err.Error()),
}
}
defer f.connectionSemaphore.Release(semaphoreRequestWeight)
response, clientErr, err := f.rosettaClient.ConstructionAPI.ConstructionHash(ctx,
&types.ConstructionHashRequest{
NetworkIdentifier: network,
SignedTransaction: signedTransaction,
},
)
if err != nil {
return nil, f.RequestFailedError(clientErr, err, "/construction/hash")
}
if err := asserter.TransactionIdentifierResponse(response); err != nil {
fetcherErr := &Error{
Err: fmt.Errorf("%w: /construction/hash", err),
}
return nil, fetcherErr
}
return response.TransactionIdentifier, nil
}
// ConstructionMetadata returns the validated response
// from the ConstructionMetadata method.
func (f *Fetcher) ConstructionMetadata(
ctx context.Context,
network *types.NetworkIdentifier,
options map[string]interface{},
publicKeys []*types.PublicKey,
) (map[string]interface{}, []*types.Amount, *Error) {
if err := f.connectionSemaphore.Acquire(ctx, semaphoreRequestWeight); err != nil {
return nil, nil, &Error{
Err: fmt.Errorf("%w: %s", ErrCouldNotAcquireSemaphore, err.Error()),
}
}
defer f.connectionSemaphore.Release(semaphoreRequestWeight)
metadata, clientErr, err := f.rosettaClient.ConstructionAPI.ConstructionMetadata(ctx,
&types.ConstructionMetadataRequest{
NetworkIdentifier: network,
Options: options,
PublicKeys: publicKeys,
},
)
if err != nil {
return nil, nil, f.RequestFailedError(clientErr, err, "/construction/metadata")
}
if err := asserter.ConstructionMetadataResponse(metadata); err != nil {
fetcherErr := &Error{
Err: fmt.Errorf("%w: /construction/metadata", err),
}
return nil, nil, fetcherErr
}
return metadata.Metadata, metadata.SuggestedFee, nil
}
// ConstructionParse is called on both unsigned and signed transactions to
// understand the intent of the formulated transaction.
//
// This is run as a sanity check before signing (after `/construction/payloads`)
// and before broadcast (after `/construction/combine`).
func (f *Fetcher) ConstructionParse(
ctx context.Context,
network *types.NetworkIdentifier,
signed bool,
transaction string,
) ([]*types.Operation, []*types.AccountIdentifier, map[string]interface{}, *Error) {
if err := f.connectionSemaphore.Acquire(ctx, semaphoreRequestWeight); err != nil {
return nil, nil, nil, &Error{
Err: fmt.Errorf("%w: %s", ErrCouldNotAcquireSemaphore, err.Error()),
}
}
defer f.connectionSemaphore.Release(semaphoreRequestWeight)
response, clientErr, err := f.rosettaClient.ConstructionAPI.ConstructionParse(ctx,
&types.ConstructionParseRequest{
NetworkIdentifier: network,
Signed: signed,
Transaction: transaction,
},
)
if err != nil {
return nil, nil, nil, f.RequestFailedError(clientErr, err, "/construction/parse")
}
if err := f.Asserter.ConstructionParseResponse(response, signed); err != nil {
fetcherErr := &Error{
Err: fmt.Errorf("%w: /construction/parse", err),
}
return nil, nil, nil, fetcherErr
}
return response.Operations, response.AccountIdentifierSigners, response.Metadata, nil
}
// ConstructionPayloads is called with an array of operations
// and the response from `/construction/metadata`. It returns an
// unsigned transaction blob and a collection of payloads that must
// be signed by particular addresses using a certain SignatureType.
//
// The array of operations provided in transaction construction often times
// can not specify all "effects" of a transaction (consider invoked transactions
// in Ethereum). However, they can deterministically specify the "intent"
// of the transaction, which is sufficient for construction. For this reason,
// parsing the corresponding transaction in the Data API (when it lands on chain)
// will contain a superset of whatever operations were provided during construction.
func (f *Fetcher) ConstructionPayloads(
ctx context.Context,
network *types.NetworkIdentifier,
operations []*types.Operation,
metadata map[string]interface{},
publicKeys []*types.PublicKey,
) (string, []*types.SigningPayload, *Error) {
if err := f.connectionSemaphore.Acquire(ctx, semaphoreRequestWeight); err != nil {
return "", nil, &Error{
Err: fmt.Errorf("%w: %s", ErrCouldNotAcquireSemaphore, err.Error()),
}
}
defer f.connectionSemaphore.Release(semaphoreRequestWeight)
response, clientErr, err := f.rosettaClient.ConstructionAPI.ConstructionPayloads(ctx,
&types.ConstructionPayloadsRequest{
NetworkIdentifier: network,
Operations: operations,
Metadata: metadata,
PublicKeys: publicKeys,
},
)
if err != nil {
return "", nil, f.RequestFailedError(clientErr, err, "/construction/payloads")
}
if err := asserter.ConstructionPayloadsResponse(response); err != nil {
fetcherErr := &Error{
Err: fmt.Errorf("%w: /construction/payloads", err),
}
return "", nil, fetcherErr
}
return response.UnsignedTransaction, response.Payloads, nil
}
// ConstructionPreprocess is called prior to `/construction/payloads` to construct a
// request for any metadata that is needed for transaction construction
// given (i.e. account nonce).
//
// The request returned from this method will be used by the caller (in a
// different execution environment) to call the `/construction/metadata`
// endpoint.
func (f *Fetcher) ConstructionPreprocess(
ctx context.Context,
network *types.NetworkIdentifier,
operations []*types.Operation,
metadata map[string]interface{},
) (map[string]interface{}, []*types.AccountIdentifier, *Error) {
if err := f.connectionSemaphore.Acquire(ctx, semaphoreRequestWeight); err != nil {
return nil, nil, &Error{
Err: fmt.Errorf("%w: %s", ErrCouldNotAcquireSemaphore, err.Error()),
}
}
defer f.connectionSemaphore.Release(semaphoreRequestWeight)
response, clientErr, err := f.rosettaClient.ConstructionAPI.ConstructionPreprocess(ctx,
&types.ConstructionPreprocessRequest{
NetworkIdentifier: network,
Operations: operations,
Metadata: metadata,
},
)
if err != nil {
return nil, nil, f.RequestFailedError(clientErr, err, "/construction/preprocess")
}
if err := asserter.ConstructionPreprocessResponse(response); err != nil {
fetcherErr := &Error{
Err: fmt.Errorf("%w: /construction/preprocess", err),
}
return nil, nil, fetcherErr
}
return response.Options, response.RequiredPublicKeys, nil
}
// ConstructionSubmit returns the validated response
// from the ConstructionSubmit method.
func (f *Fetcher) ConstructionSubmit(
ctx context.Context,
network *types.NetworkIdentifier,
signedTransaction string,
) (*types.TransactionIdentifier, map[string]interface{}, *Error) {
if err := f.connectionSemaphore.Acquire(ctx, semaphoreRequestWeight); err != nil {
return nil, nil, &Error{
Err: fmt.Errorf("%w: %s", ErrCouldNotAcquireSemaphore, err.Error()),
}
}
defer f.connectionSemaphore.Release(semaphoreRequestWeight)
submitResponse, clientErr, err := f.rosettaClient.ConstructionAPI.ConstructionSubmit(
ctx,
&types.ConstructionSubmitRequest{
NetworkIdentifier: network,
SignedTransaction: signedTransaction,
},
)
if err != nil {
return nil, nil, f.RequestFailedError(clientErr, err, "/construction/submit")
}
if err := asserter.TransactionIdentifierResponse(submitResponse); err != nil {
fetcherErr := &Error{
Err: fmt.Errorf("%w: /construction/submit", err),
}
return nil, nil, fetcherErr
}
return submitResponse.TransactionIdentifier, submitResponse.Metadata, nil
}