forked from NickP005/mochimo-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstruction_handler.go
More file actions
697 lines (577 loc) · 23.8 KB
/
construction_handler.go
File metadata and controls
697 lines (577 loc) · 23.8 KB
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
package main
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/NickP005/go_mcminterface"
)
type PublicKey struct {
HexBytes string `json:"hex_bytes"`
CurveType string `json:"curve_type"`
}
// ConstructionDeriveRequest is used to derive an account identifier from a public key.
type ConstructionDeriveRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
PublicKey PublicKey `json:"public_key"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
// ConstructionDeriveResponse is returned by the `/construction/derive` endpoint.
type ConstructionDeriveResponse struct {
AccountIdentifier AccountIdentifier `json:"account_identifier"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
// constructionDeriveHandler is the HTTP handler for the `/construction/derive` endpoint.
func constructionDeriveHandler(w http.ResponseWriter, r *http.Request) {
var req ConstructionDeriveRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bconstructionDeriveHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInternalError)
return
}
// Validate the network identifier
if req.NetworkIdentifier.Blockchain != Constants.NetworkIdentifier.Blockchain || req.NetworkIdentifier.Network != Constants.NetworkIdentifier.Network {
mlog(3, "§bconstructionDeriveHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork)
return
}
// Validate the curve type
if req.PublicKey.CurveType != "wotsp" {
mlog(3, "§bconstructionDeriveHandler(): §4Wrong curve type")
giveError(w, ErrWrongCurveType)
return
}
/*
var wots_address go_mcminterface.WotsAddress
if len(req.PublicKey.HexBytes) == 2144*2+2 {
wots_address = go_mcminterface.WotsAddressFromHex(req.PublicKey.HexBytes[2:])
} else if len(req.PublicKey.HexBytes) == 2144*2 {
wots_address = go_mcminterface.WotsAddressFromHex(req.PublicKey.HexBytes)
} else {
giveError(w, ErrInvalidAccountFormat)
return
}
// Create the account identifier
accountIdentifier := getAccountFromAddress(wots_address)*/
// read from metadata the tag
if _, ok := req.Metadata["tag"]; !ok {
mlog(3, "§bconstructionDeriveHandler(): §4Tag not found")
giveError(w, ErrInvalidRequest)
return
}
// Create the account identifier
accountIdentifier := AccountIdentifier{
Address: req.Metadata["tag"].(string),
}
// Construct the response
response := ConstructionDeriveResponse{
AccountIdentifier: accountIdentifier,
Metadata: map[string]interface{}{}, // Add any additional metadata if necessary
}
// Encode the response as JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
type ConstructionPreprocessRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
Operations []Operation `json:"operations"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
// ConstructionPreprocessResponse represents the output of the `/construction/preprocess` endpoint.
type ConstructionPreprocessResponse struct {
Options map[string]interface{} `json:"options"`
RequiredPublicKeys []AccountIdentifier `json:"required_public_keys,omitempty"`
}
// constructionPreprocessHandler is the HTTP handler for the `/construction/preprocess` endpoint.
func constructionPreprocessHandler(w http.ResponseWriter, r *http.Request) {
var req ConstructionPreprocessRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bconstructionPreprocessHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
// Validate the network identifier
if req.NetworkIdentifier.Blockchain != Constants.NetworkIdentifier.Blockchain || req.NetworkIdentifier.Network != Constants.NetworkIdentifier.Network {
mlog(3, "§bconstructionPreprocessHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork)
return
}
// Get from metadata the block_to_live
options := make(map[string]interface{})
requiredPublicKeys := []AccountIdentifier{}
// At least SOURCE_TRANSFER, DESTINATION_TRANSFER, FEE
operationTypes := make(map[string]int)
for _, op := range req.Operations {
operationTypes[op.Type]++
}
if n, ok := operationTypes["SOURCE_TRANSFER"]; !ok || n != 1 {
mlog(3, "§bconstructionPreprocessHandler(): §4SOURCE_TRANSFER not found or more than one")
giveError(w, ErrInvalidRequest)
return
}
if n, ok := operationTypes["DESTINATION_TRANSFER"]; !ok || n > 255 {
mlog(3, "§bconstructionPreprocessHandler(): §4DESTINATION_TRANSFER not found or more than 255")
giveError(w, ErrInvalidRequest)
return
}
if n, ok := operationTypes["FEE"]; !ok || n != 1 {
mlog(3, "§bconstructionPreprocessHandler(): §4FEE not found or more than one")
giveError(w, ErrInvalidRequest)
return
}
var source_operation Operation
for _, op := range req.Operations {
if op.Type == "SOURCE_TRANSFER" {
source_operation = op
break
}
}
// add to required public keys the address of the source
requiredPublicKeys = append(requiredPublicKeys, source_operation.Account)
// add to options the source address
options["source_addr"] = source_operation.Account.Address
// Get from metadata the block_to_live
if _, ok := req.Metadata["block_to_live"]; !ok {
fmt.Println("Block to live not found")
giveError(w, ErrInvalidRequest)
return
}
options["block_to_live"] = req.Metadata["block_to_live"]
// Get from metadata the change_pk
if _, ok := req.Metadata["change_pk"]; !ok {
mlog(3, "§bconstructionPreprocessHandler(): §4Change pk not found")
giveError(w, ErrInvalidRequest)
return
}
if len(req.Metadata["change_pk"].(string)) == 2144*2+2 {
mlog(5, "§bconstructionPreprocessHandler(): §7Change pk is a full WOTS+ address")
wotsAddr := go_mcminterface.WotsAddressFromHex(req.Metadata["change_pk"].(string)[2:])
options["change_pk"] = "0x" + hex.EncodeToString(wotsAddr.Address[:20])
} else if len(req.Metadata["change_pk"].(string)) == 20*2+2 {
mlog(5, "§bconstructionPreprocessHandler(): §7Change pk is a WOTS+ hashed address")
options["change_pk"] = "0x" + req.Metadata["change_pk"].(string)[2:]
} else {
mlog(3, "§bconstructionPreprocessHandler(): §4Invalid change pk format")
giveError(w, ErrInvalidRequest)
return
}
// Construct the response
response := ConstructionPreprocessResponse{
Options: options,
RequiredPublicKeys: requiredPublicKeys,
}
// Encode the response as JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// ConstructionMetadataRequest is used to get information required to construct a transaction.
type ConstructionMetadataRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
Options map[string]interface{} `json:"options,omitempty"`
PublicKeys []PublicKey `json:"public_keys,omitempty"`
}
// ConstructionMetadataResponse is returned by the `/construction/metadata` endpoint.
type ConstructionMetadataResponse struct {
Metadata map[string]interface{} `json:"metadata"`
SuggestedFee []Amount `json:"suggested_fee,omitempty"`
}
func constructionMetadataHandler(w http.ResponseWriter, r *http.Request) {
var req ConstructionMetadataRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bconstructionMetadataHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
// Validate the network identifier
if req.NetworkIdentifier.Blockchain != Constants.NetworkIdentifier.Blockchain || req.NetworkIdentifier.Network != Constants.NetworkIdentifier.Network {
mlog(3, "§bconstructionMetadataHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork)
return
}
// determine the source balance. If source_addr is not in options give error
if source_addr, ok := req.Options["source_addr"]; !ok || len(source_addr.(string)) != 20*2+2 {
mlog(3, "§bconstructionMetadataHandler(): §4Source address not provided or invalid")
giveError(w, ErrInvalidRequest)
return
}
//source_balance, err := go_mcminterface.QueryBalance(req.Options["source_addr"].(string)[2:])
source_wots, err := go_mcminterface.QueryTagResolveHex(req.Options["source_addr"].(string)[2:])
if err != nil {
mlog(3, "§bconstructionMetadataHandler(): §4Source balance not found: §c%s", err)
giveError(w, ErrAccountNotFound)
return
}
source_balance := source_wots.GetAmount()
metadata := make(map[string]interface{})
metadata["source_balance"] = fmt.Sprintf("%d", source_balance)
// Set the change_pk from options
if change_pk, ok := req.Options["change_pk"]; !ok || len(change_pk.(string)) != 20*2+2 {
mlog(3, "§bconstructionMetadataHandler(): §4Change pk not provided or invalid")
giveError(w, ErrInvalidRequest)
return
}
metadata["change_pk"] = req.Options["change_pk"]
if _, ok := req.Options["block_to_live"]; !ok {
mlog(3, "§bconstructionMetadataHandler(): §4Block to live not provided")
giveError(w, ErrInvalidRequest)
return
}
metadata["block_to_live"] = req.Options["block_to_live"]
response := ConstructionMetadataResponse{
Metadata: metadata,
SuggestedFee: []Amount{
{
Value: strconv.FormatUint(Globals.SuggestedFee, 10),
Currency: MCMCurrency,
},
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// ConstructionPayloadsRequest is the input to the `/construction/payloads` endpoint.
type ConstructionPayloadsRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
Operations []Operation `json:"operations"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
PublicKeys []PublicKey `json:"public_keys,omitempty"`
}
// ConstructionPayloadsResponse is returned by the `/construction/payloads` endpoint.
type ConstructionPayloadsResponse struct {
UnsignedTransaction string `json:"unsigned_transaction"`
Payloads []SigningPayload `json:"payloads"`
}
// SigningPayload represents the payload to be signed.
type SigningPayload struct {
AccountIdentifier AccountIdentifier `json:"account_identifier"`
HexBytes string `json:"hex_bytes"`
SignatureType string `json:"signature_type"`
}
func constructionPayloadsHandler(w http.ResponseWriter, r *http.Request) {
var req ConstructionPayloadsRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bconstructionPayloadsHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
// Validate the network identifier
if req.NetworkIdentifier.Blockchain != Constants.NetworkIdentifier.Blockchain || req.NetworkIdentifier.Network != Constants.NetworkIdentifier.Network {
mlog(3, "§bconstructionPayloadsHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork)
return
}
// Validate the minimum operations
operationTypes := make(map[string]int)
for _, op := range req.Operations {
operationTypes[op.Type]++
}
if n, ok := operationTypes["SOURCE_TRANSFER"]; !ok || n != 1 {
mlog(3, "§bconstructionPayloadsHandler(): §4SOURCE_TRANSFER not found or more than one")
giveError(w, ErrInvalidRequest)
return
}
if n, ok := operationTypes["DESTINATION_TRANSFER"]; !ok || n > 255 {
mlog(3, "§bconstructionPayloadsHandler(): §4DESTINATION_TRANSFER not found or more than 255")
giveError(w, ErrInvalidRequest)
return
}
if n, ok := operationTypes["FEE"]; !ok || n != 1 {
mlog(3, "§bconstructionPayloadsHandler(): §4FEE not found or more than one")
giveError(w, ErrInvalidRequest)
return
}
// Check if there are public keys - TO MOVE TO PAYLOADS
if len(req.PublicKeys) != 1 {
mlog(3, "§bconstructionPayloadsHandler(): §4Invalid number of public keys")
giveError(w, ErrInvalidRequest)
return
}
// Read from the WOTS+ full address informations for signature
pk_bytes, _ := hex.DecodeString(req.PublicKeys[0].HexBytes)
if len(pk_bytes) != 2144 {
mlog(3, "§bconstructionPayloadsHandler(): §4Invalid public key length")
giveError(w, ErrInvalidRequest)
return
}
//source_addr := pk_bytes[len(pk_bytes)-32:]
//source_public_seed := pk_bytes[len(pk_bytes)-64 : len(pk_bytes)-32]
pk_hash := go_mcminterface.AddrHashGenerate(pk_bytes[:2144])
// Create a TXENTRY
var txentry go_mcminterface.TXENTRY = go_mcminterface.NewTXENTRY()
txentry.SetSignatureScheme("wotsp")
var send_total uint64 = 0
var change_total uint64 = 0
//var source_total uint64 = req.Metadata["source_balance"].(uint64)
source_total, _ := strconv.ParseUint(req.Metadata["source_balance"].(string), 10, 64)
// For every operation
for _, op := range req.Operations {
if op.Type == "DESTINATION_TRANSFER" {
amount, _ := strconv.ParseUint(op.Amount.Value, 10, 64)
DST := go_mcminterface.NewDSTFromString(op.Account.Address[2:], op.Metadata["memo"].(string), amount)
txentry.AddDestination(DST)
send_total += amount
} else if op.Type == "SOURCE_TRANSFER" {
var source_address go_mcminterface.WotsAddress
tagBytes, err := hex.DecodeString(op.Account.Address[2:])
if err != nil {
mlog(3, "§bconstructionPayloadsHandler(): §4Error decoding source address: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
source_address.SetTAG(tagBytes)
source_address.SetAddress(pk_hash)
txentry.SetSourceAddress(source_address)
var change_address go_mcminterface.WotsAddress
change_pk, err := hex.DecodeString(req.Metadata["change_pk"].(string)[2:])
if err != nil {
mlog(3, "§bconstructionPayloadsHandler(): §4Error decoding change address: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
change_address.SetTAG(tagBytes)
change_address.SetAddress(change_pk)
txentry.SetChangeAddress(change_address)
} else if op.Type == "FEE" {
amount, _ := strconv.ParseUint(op.Amount.Value, 10, 64)
txentry.SetFee(amount)
}
}
txentry.SetSendTotal(send_total)
change_total = source_total - (send_total + txentry.GetFee())
txentry.SetChangeTotal(change_total)
// Set block to live
//block_to_live := req.Metadata["block_to_live"].(uint64)
block_to_live, _ := strconv.ParseUint(req.Metadata["block_to_live"].(string), 10, 64)
txentry.SetBlockToLive(block_to_live)
//var pubSeedArray [32]byte
//copy(pubSeedArray[:], source_public_seed)
//txentry.SetWotsSigPubSeed(pubSeedArray)
//txentry.SetWotsSigAddresses(source_addr)
var unsignedTransactionBytes []byte
unsignedTransactionBytes = append(unsignedTransactionBytes, txentry.Hdr.Bytes()...)
unsignedTransactionBytes = append(unsignedTransactionBytes, txentry.Dat.Bytes()...)
unsignedTransaction := hex.EncodeToString(unsignedTransactionBytes)
var payloads []SigningPayload
// add one for the source
payloads = append(payloads, SigningPayload{
AccountIdentifier: req.Operations[0].Account,
HexBytes: unsignedTransaction,
SignatureType: "wotsp",
})
// Construct the response
response := ConstructionPayloadsResponse{
UnsignedTransaction: unsignedTransaction,
Payloads: payloads,
}
// Encode the response as JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// ConstructionCombineRequest is the input to the `/construction/combine` endpoint.
type ConstructionCombineRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
UnsignedTransaction string `json:"unsigned_transaction"`
Signatures []Signature `json:"signatures"`
}
type Signature struct {
SigningPayload SigningPayload `json:"signing_payload"`
PublicKey PublicKey `json:"public_key"`
SignatureType string `json:"signature_type"`
HexBytes string `json:"hex_bytes"`
}
type ConstructionCombineResponse struct {
SignedTransaction string `json:"signed_transaction"`
}
func constructionCombineHandler(w http.ResponseWriter, r *http.Request) {
var req ConstructionCombineRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bconstructionCombineHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
// Validate the network identifier
if req.NetworkIdentifier.Blockchain != Constants.NetworkIdentifier.Blockchain || req.NetworkIdentifier.Network != Constants.NetworkIdentifier.Network {
mlog(3, "§bconstructionCombineHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork)
return
}
// Validate the unsigned transaction
// TODO LATER
// Validate the number of signatures
if len(req.Signatures) != 1 {
mlog(3, "§bconstructionCombineHandler(): §4Invalid number of signatures")
giveError(w, ErrInvalidRequest)
return
}
// Validate the signature
if req.Signatures[0].SigningPayload.HexBytes != req.UnsignedTransaction {
mlog(3, "§bconstructionCombineHandler(): §4Invalid signing payload")
giveError(w, ErrInvalidRequest)
return
}
if len(req.Signatures[0].HexBytes) != 2208*2 {
mlog(3, "§bconstructionCombineHandler(): §4Invalid signature length")
giveError(w, ErrInvalidRequest)
return
}
// TO DO CHECK THAT SIGNATURE IS VALID - Will in the futuredelegate to node
// Construct the signed transaction
signedTransaction := req.UnsignedTransaction + req.Signatures[0].HexBytes
signedTransactionBytes, _ := hex.DecodeString(signedTransaction)
// Append void nonce and hash (8 bytes + 32 bytes)
signedTransactionBytes = append(signedTransactionBytes, make([]byte, 8+32)...)
txentry := go_mcminterface.TransactionFromBytes(signedTransactionBytes)
// Set the nonce to current block
txentry.SetNonce(Globals.LatestBlockNum)
// Compute the hash
copy(txentry.Tlr.ID[:], txentry.Hash())
signedTransaction = hex.EncodeToString(txentry.Bytes())
// Construct the response
response := ConstructionCombineResponse{
SignedTransaction: signedTransaction,
}
// Encode the response as JSON
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
type ConstructionParseRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
Signed bool `json:"signed"`
Transaction string `json:"transaction"`
}
type ConstructionParseResponse struct {
Operations []Operation `json:"operations"`
AccountIdentifierSigners []AccountIdentifier `json:"account_identifier_signers,omitempty"` // Replacing deprecated signers
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
func constructionParseHandler(w http.ResponseWriter, r *http.Request) {
var req ConstructionParseRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bconstructionParseHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
// Validate the network identifier
if req.NetworkIdentifier.Blockchain != Constants.NetworkIdentifier.Blockchain || req.NetworkIdentifier.Network != Constants.NetworkIdentifier.Network {
mlog(3, "§bconstructionParseHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork)
return
}
// Validate the transaction - TODO LATER
transaction_bytes, err := hex.DecodeString(req.Transaction)
if err != nil {
mlog(3, "§bconstructionParseHandler(): §4Error decoding transaction: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
// if signed==false, add void signature, nonce and hash
if !req.Signed {
transaction_bytes = append(transaction_bytes, make([]byte, (2144+32+32)+8+32)...)
}
var tx_entry go_mcminterface.TXENTRY = go_mcminterface.TransactionFromBytes(transaction_bytes)
var tx_entries []go_mcminterface.TXENTRY = []go_mcminterface.TXENTRY{tx_entry}
transactions := getTransactionsFromBlockBody(tx_entries, go_mcminterface.WotsAddress{}, false)
// Construct the operations
operations := transactions[0].Operations
// Construct metadata
metadata := make(map[string]interface{})
metadata["block_to_live"] = strconv.FormatUint(tx_entries[0].GetBlockToLive(), 10)
// Construct the signers by finding the source address
var signers []AccountIdentifier
for _, op := range operations {
if op.Type == "SOURCE_TRANSFER" {
signers = append(signers, op.Account)
break
}
}
response := ConstructionParseResponse{
Operations: operations,
AccountIdentifierSigners: signers,
Metadata: metadata,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
type ConstructionHashRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
SignedTransaction string `json:"signed_transaction"`
}
type TransactionIdentifierResponse struct {
TransactionIdentifier TransactionIdentifier `json:"transaction_identifier"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
func constructionHashHandler(w http.ResponseWriter, r *http.Request) {
var req ConstructionHashRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bconstructionHashHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
// Validate the network identifier
if req.NetworkIdentifier.Blockchain != Constants.NetworkIdentifier.Blockchain || req.NetworkIdentifier.Network != Constants.NetworkIdentifier.Network {
mlog(3, "§bconstructionHashHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork)
return
}
// Validate the signed transaction - TO DO LATER
// Convert hex to bytes
transaction_bytes, _ := hex.DecodeString(req.SignedTransaction[:len(req.SignedTransaction)-32*2])
hash := sha256.Sum256(transaction_bytes)
// Construct the response
response := TransactionIdentifierResponse{
TransactionIdentifier: TransactionIdentifier{
Hash: hex.EncodeToString(hash[:]),
},
Metadata: map[string]interface{}{}, // Add any additional metadata if necessary
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
type ConstructionSubmitRequest struct {
NetworkIdentifier NetworkIdentifier `json:"network_identifier"`
SignedTransaction string `json:"signed_transaction"`
}
type ConstructionSubmitResponse struct {
TransactionIdentifier TransactionIdentifier `json:"transaction_identifier"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
func constructionSubmitHandler(w http.ResponseWriter, r *http.Request) {
var req ConstructionSubmitRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
mlog(3, "§bconstructionSubmitHandler(): §4Error decoding request: §c%s", err)
giveError(w, ErrInvalidRequest)
return
}
// Validate the network identifier
if req.NetworkIdentifier.Blockchain != Constants.NetworkIdentifier.Blockchain || req.NetworkIdentifier.Network != Constants.NetworkIdentifier.Network {
mlog(3, "§bconstructionSubmitHandler(): §4Wrong network identifier")
giveError(w, ErrWrongNetwork)
return
}
// Validate the signed transaction - TODO LATER
// Submit the transaction to the Mochimo blockchain
transaction := go_mcminterface.TransactionFromHex(req.SignedTransaction)
mlog(5, "§bconstructionSubmitHandler(): §7Submitting transaction with hash §60x%s", hex.EncodeToString(transaction.Hash()))
err := go_mcminterface.SubmitTransaction(transaction)
if err != nil {
mlog(3, "§bconstructionSubmitHandler(): §4Error submitting transaction: §c%s", err)
giveError(w, ErrInternalError)
return
}
// set nonce to 0
transaction.SetNonce(0)
// mlog nonce and hash
mlog(5, "§bconstructionSubmitHandler(): §7Nonce: §60x%x", transaction.GetNonce())
mlog(5, "§bconstructionSubmitHandler(): §7Hash: §60x%x", transaction.Hash())
// Construct the response
response := ConstructionSubmitResponse{
TransactionIdentifier: TransactionIdentifier{
Hash: hex.EncodeToString(transaction.Hash()),
},
Metadata: map[string]interface{}{}, // Add any additional metadata if necessary
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}