-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
988 lines (859 loc) · 28.9 KB
/
main.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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
package main
import (
"bufio"
"bytes"
"encoding/csv"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"math/big"
"net/http"
"os"
"strconv"
"strings"
"time"
"unicode"
)
var (
seed string
authToken string
apiToken string
accessToken string
cleanedOrders []OrderRecord
openSeaAPIKey string
)
type OrderRecord struct {
OrderNo string
Email string
ShippingPhone string
Amount float64
}
type OrderVerificationResponse struct {
Shipments []struct {
DestinationAddress struct {
ContactEmail string `json:"contact_email"`
ContactPhone string `json:"contact_phone"`
} `json:"destination_address"`
OrderData struct {
PlatformOrderNumber string `json:"platform_order_number"`
} `json:"order_data"`
} `json:"shipments"`
}
type FundAccountRequest struct {
Seed string `json:"seed"`
Amount *big.Int `json:"amount,omitempty"`
To string `json:"to"`
}
type FundAccountResponse struct {
Account string `json:"account"`
Amount big.Int `json:"amount"`
}
type FundAccountErrorResponse struct {
Message string `json:"message"`
Description string `json:"description"`
}
type IndiegogoResponse struct {
Response []struct {
Email string `json:"email"`
Order struct {
ID int64 `json:"id"`
Shipping struct {
PhoneNumber string `json:"phone_number"`
} `json:"shipping"`
} `json:"order"`
} `json:"response"`
}
type BalanceRequest struct {
Account string `json:"account"`
}
type BalanceResponse struct {
Balance big.Int `json:"balance"`
}
type BalanceErrorResponse struct {
Message string `json:"message"`
Description string `json:"description"`
}
// EmailRequest represents the JSON payload structure for the Brevo API request
type EmailRequest struct {
Sender Sender `json:"sender"`
To []ToEmail `json:"to"`
Subject string `json:"subject"`
HtmlContent string `json:"htmlContent"`
}
// Sender represents the "sender" part of the payload
type Sender struct {
Name string `json:"name"`
Email string `json:"email"`
}
// ToEmail represents each recipient in the "to" array
type ToEmail struct {
Email string `json:"email"`
Name string `json:"name"` // This can be an empty string if the name is not used
}
type OpenSeaResponse struct {
NFTs []struct {
Contract string `json:"contract"`
} `json:"nfts"`
}
const (
easyshipAPIURL = "https://api.easyship.com/2023-01/shipments?per_page=1&platform_order_number="
fundAPIURL = "https://api.node3.functionyard.fula.network/account/set_balance"
balanceAPIURL = "https://api.node3.functionyard.fula.network/account/balance"
userDetailFile = "userDetails.txt"
openSeaNFTId = "functional-elephants-club"
contractAddress = "0xe44d2ce514fd50ffa3a296ee6ce01bb1ddb5b6d6"
chain = "matic" // Assuming the NFT is on Polygon
streamrFile = "streamr.txt"
)
var fundingAmount *big.Int
func (f FundAccountRequest) MarshalJSON() ([]byte, error) {
type Alias FundAccountRequest // Create an alias to avoid infinite recursion
return json.Marshal(&struct {
Amount json.Number `json:"amount"` // Use json.Number for the amount
*Alias
}{
Amount: json.Number(f.Amount.String()), // Convert big.Int to json.Number
Alias: (*Alias)(&f),
})
}
func checkAccountBalance(accountID string) (string, error) {
client := &http.Client{}
balanceRequest := BalanceRequest{
Account: accountID,
}
requestBody, err := json.Marshal(balanceRequest)
if err != nil {
log.Println("Error marshaling balance request:", err)
return "0", err
}
resp, err := client.Post(balanceAPIURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
log.Println("Error sending balance request:", err)
return "0", err
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Error reading balance response body:", err)
return "0", err
}
if resp.StatusCode != http.StatusOK {
log.Println("Balance check response body:", string(bodyBytes))
return "0", fmt.Errorf("balance check failed with status code: %d", resp.StatusCode)
}
var balanceResp BalanceResponse
err = json.Unmarshal(bodyBytes, &balanceResp)
if err != nil {
log.Println("Error decoding balance response:", err)
return "0", err
}
return balanceResp.Balance.String(), nil
}
func preprocessCSVLine(line string) string {
// Replace all improperly quoted fields
// For example, if the pattern is ="", replace it with the correct format
// This is a simple example and might need to be adjusted to handle more complex cases correctly
return strings.ReplaceAll(line, "=\"\"\"", "\"")
}
// Reads the CSV file and returns a slice of OrderRecords
func readCSVOrders(filePath string) ([]OrderRecord, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
var orders []OrderRecord
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Preprocess the line to fix any quoting issues
processedLine := preprocessCSVLine(line)
// Convert the processed line into a reader so it can be used by csv.NewReader
reader := csv.NewReader(strings.NewReader(processedLine))
record, err := reader.Read()
if err != nil {
if err == io.EOF {
break
}
return nil, err // Handle the error as appropriate
}
cleanedAmount := strings.Replace(strings.Trim(record[2], " $"), ",", "", -1)
amount, _ := strconv.ParseFloat(cleanedAmount, 64) // Assuming the Amount is in the 11th column (index 10)
orders = append(orders, OrderRecord{
OrderNo: strings.TrimSpace(record[0]),
Email: strings.TrimSpace(record[1]),
ShippingPhone: strings.TrimSpace(record[3]),
Amount: amount,
})
}
return orders, nil
}
func readTokensFromFile(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
if scanner.Scan() {
seed = scanner.Text()
}
if scanner.Scan() {
authToken = scanner.Text()
}
if scanner.Scan() {
apiToken = scanner.Text()
}
if scanner.Scan() {
accessToken = scanner.Text()
}
return scanner.Err()
}
func verifyNFTHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var data struct {
Address string `json:"address"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Verify NFT ownership using Polygon API (you'll need to implement this)
hasNFT := verifyNFTOwnership(data.Address)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]bool{"hasNFT": hasNFT})
}
func init() {
var err error
cleanedOrders, err = readCSVOrders("contributions-masked.csv")
if err != nil {
log.Fatalf("Error loading orders: %v", err)
}
// Log the first 3 orders
log.Println("First 3 orders:")
for i := 0; i < 3 && i < len(cleanedOrders); i++ {
log.Printf("%+v", cleanedOrders[i])
}
// Log the last 3 orders
log.Println("Last 3 orders:")
lastIndex := len(cleanedOrders) - 1
for i := lastIndex; i > lastIndex-3 && i >= 0; i-- {
log.Printf("%+v", cleanedOrders[i])
}
}
func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
// Parse command-line flags
flag.StringVar(&openSeaAPIKey, "opensea-api", "", "OpenSea API key")
flag.Parse()
// Check if the OpenSea API key is provided
if openSeaAPIKey == "" {
log.Fatal("OpenSea API key is required. Please provide it using the --opensea-api flag.")
}
log.Print("Server Started")
fundingAmount, _ = new(big.Int).SetString("999999999999999999999999999999", 10)
err := readTokensFromFile(".tokens")
if err != nil {
log.Fatalf("Error reading tokens: %v", err)
}
http.HandleFunc("/streamr", streamrHandler)
http.HandleFunc("/register", registerHandler)
http.HandleFunc("/verify-nft", verifyNFTHandler)
http.HandleFunc("/verify-nft-and-fund", verifyNFTAndFundHandler)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", registerHandler)
log.Fatal(http.ListenAndServe(":9090", nil))
}
func readAPIKey(filePath string) (string, error) {
data, err := os.ReadFile(filePath)
if err != nil {
return "", fmt.Errorf("failed to read API key from file: %v", err)
}
return string(data), nil
}
func sendEmailDetails(toEmail string, orderID string, phoneNumber string, orderAmount float64) error {
apiKey, err := readAPIKey("/home/ubuntu/testnet-server/brevo.key")
if err != nil {
log.Fatal(err)
}
apiURL := "https://api.brevo.com/v3/smtp/email"
// Split the email address at "@" and use the first part as the name
emailParts := strings.Split(toEmail, "@")
namePart := emailParts[0] // The part before "@"
// Construct the HTML content
htmlContent := fmt.Sprintf(`
<html><head></head><body>
<p>Hello,</p>
<p>Thank you for your request to join our network. Here are the details of your order in the system:</p>
<ul>
<li>Order ID: %s</li>
<li>Phone Number: %s</li>
<li>Order Amount: %.2f</li>
</ul>
<p>Please double check what you entered on the join request.</p>
</body></html>
`, orderID, phoneNumber, orderAmount)
// Prepare the request payload
emailRequest := EmailRequest{
Sender: Sender{
Name: "Functionyard", // Updated sender name
Email: "[email protected]", // Updated sender email
},
To: []ToEmail{
{
Email: toEmail,
Name: namePart,
},
},
Subject: "Your Join Network Request", // Updated subject
HtmlContent: htmlContent,
}
// Marshal the payload to JSON
payloadBytes, err := json.Marshal(emailRequest)
if err != nil {
return fmt.Errorf("error marshaling payload to JSON: %v", err)
}
// Create a new HTTP POST request
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(payloadBytes))
if err != nil {
return fmt.Errorf("error creating request: %v", err)
}
// Set the necessary headers
req.Header.Set("Content-Type", "application/json")
req.Header.Set("accept", "application/json")
req.Header.Set("api-key", apiKey)
// Make the request using the default HTTP client
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("error sending request to email API: %v", err)
}
defer resp.Body.Close()
// Check the response
body, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("error reading response body: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("API responded with non-OK status: %d, body: %s", resp.StatusCode, string(body))
}
log.Println("Email sent successfully")
return nil
}
func registerHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
http.ServeFile(w, r, "static/register.html")
case "POST":
email := r.FormValue("email")
orderID := r.FormValue("orderId")
phoneNumber := r.FormValue("phoneNumber")
tokenAccountID := r.FormValue("tokenAccountId")
appId := r.FormValue("appId")
// Validate appId against the allowed list
allowedAppIds := []string{"land.fx.fotos", "land.fx.blox", "main"}
appIdValid := false
for _, a := range allowedAppIds {
if appId == a {
appIdValid = true
break
}
}
if !appIdValid {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "Invalid appId provided"})
return
}
// Skip verifyOrder and isOrderFunded checks if appId is "land.fx.fotos"
if appId == "land.fx.fotos" {
email = fmt.Sprintf("random_%[email protected]", time.Now().Unix())
orderID = fmt.Sprintf("order_%d", time.Now().Unix())
phoneNumber = fmt.Sprintf("555-1234-%d", time.Now().Unix()%10000)
} else {
w.Header().Set("Content-Type", "application/json")
if appId == "main" {
// Check if the order has already funded 6 accounts
fundedAccounts := getFundedAccountsCount(orderID)
if fundedAccounts >= 6 {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "This order has already funded the maximum number of accounts."})
return
}
}
orderFound, emailFound, foundOrderNo, foundShippingPhone, foundOrderAmount := verifyOrder(email, orderID, phoneNumber)
if !orderFound {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "Your order could not be found automatically or does not match what we have in our system. If your email is in the system you will shortly receive an email with registered order details. You can also contact [email protected]"})
if emailFound {
err := sendEmailDetails(email, foundOrderNo, foundShippingPhone, foundOrderAmount)
log.Println("Email sending result")
log.Println(err)
}
return
}
if isOrderFunded(tokenAccountID, appId) {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "The account is already registered. If you think this is a mistake please contact [email protected]"})
return
}
}
success, errMsg := fundAccount(tokenAccountID)
if !success {
balance, err := checkAccountBalance(tokenAccountID)
if err == nil && balance != "0" {
log.Println("Account has a positive balance, considering funding successful")
} else {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": errMsg})
return
}
}
w.WriteHeader(http.StatusOK)
saveUserDetails(orderID, tokenAccountID, appId)
response := map[string]string{"status": "success", "message": "Account is funded successfully"}
json.NewEncoder(w).Encode(response)
default:
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "Invalid request method"})
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func getFundedAccountsCount(orderID string) int {
file, err := os.Open(userDetailFile)
if err != nil {
log.Println("Error opening file:", err)
return 0
}
defer file.Close()
count := 0
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
parts := strings.Split(line, ", ")
if len(parts) >= 2 && parts[1] == orderID {
count++
}
}
return count
}
//lint:ignore U1000 will be used in future
func verifyOrderEasyShip(email, orderID, phoneNumber string) bool {
client := &http.Client{}
req, err := http.NewRequest("GET", easyshipAPIURL+orderID, nil)
if err != nil {
log.Println("Error creating request:", err)
return false
}
req.Header.Add("accept", "application/json")
req.Header.Add("authorization", authToken)
resp, err := client.Do(req)
if err != nil {
log.Println("Error sending request to API:", err)
return false
}
defer resp.Body.Close()
var orderResponse OrderVerificationResponse
if err := json.NewDecoder(resp.Body).Decode(&orderResponse); err != nil {
log.Println("Error decoding response:", err)
return false
}
for _, shipment := range orderResponse.Shipments {
if shipment.DestinationAddress.ContactEmail == email &&
shipment.DestinationAddress.ContactPhone == phoneNumber &&
shipment.OrderData.PlatformOrderNumber == orderID {
return true
}
}
return false
}
//lint:ignore U1000 will be used in future
func verifyOrderIgg(email, orderID, phoneNumber string) bool {
// Prepare the Indiegogo API request
client := &http.Client{}
indiegogoAPIURL := "https://api.indiegogo.com/2/campaigns/28885449/contributions.json"
req, err := http.NewRequest("GET", indiegogoAPIURL, nil)
if err != nil {
log.Println("Error creating request:", err)
return false
}
// Add the required query parameters
q := req.URL.Query()
q.Add("api_token", apiToken)
q.Add("access_token", accessToken)
q.Add("email", email) // This will filter the results by the provided email
req.URL.RawQuery = q.Encode()
req.Header.Add("accept", "application/json")
// Perform the API request
resp, err := client.Do(req)
if err != nil {
log.Println("Error sending request to API:", err)
return false
}
defer resp.Body.Close()
// Parse the JSON response
var indiegogoResponse IndiegogoResponse
if err := json.NewDecoder(resp.Body).Decode(&indiegogoResponse); err != nil {
log.Println("Error decoding response:", err)
return false
}
// Check if any order matches the provided details
for _, contribution := range indiegogoResponse.Response {
if contribution.Email == email &&
fmt.Sprintf("%d", contribution.Order.ID) == orderID &&
contribution.Order.Shipping.PhoneNumber == phoneNumber {
return true
}
}
return false
}
func sanitizeInput(input string) string {
// Remove any non-printable characters except '@' for email
return strings.Map(func(r rune) rune {
if unicode.IsPrint(r) || r == '@' {
return r
}
return -1
}, strings.TrimSpace(input))
}
// Verifies the order by matching the user input against the parsed CSV records
func verifyOrder(email, orderID, phoneNumber string) (bool, bool, string, string, float64) {
sanitizedOrderID := sanitizeInput(orderID)
sanitizedEmail := sanitizeInput(email)
sanitizedPhone := sanitizeInput(phoneNumber)
if len(sanitizedPhone) < 4 {
// Handle error or adjust logic as necessary
return false, false, "", "", 0.0
}
sanitizedPhoneLast4 := sanitizedPhone[len(sanitizedPhone)-4:]
emailFound := false
foundOrderNo := ""
foundShippingPhone := ""
foundOrderAmount, _ := strconv.ParseFloat("0", 64)
log.Printf("verifyOrder called. sanitizedOrderID: %s, sanitizedEmail: %s, sanitizedPhoneLast4: %s", sanitizedOrderID, sanitizedEmail, sanitizedPhoneLast4)
for _, order := range cleanedOrders {
sanitizedOrderEmail := sanitizeInput(order.Email)
if strings.EqualFold(sanitizedOrderEmail, sanitizedEmail) {
log.Print("email found")
emailFound = true // Email matches.
foundOrderNo = order.OrderNo
foundShippingPhone = order.ShippingPhone
foundOrderAmount = order.Amount
if len(order.ShippingPhone) < 4 {
continue
}
sanitizedOrderNo := sanitizeInput(order.OrderNo)
sanitizedOrderPhone := sanitizeInput(order.ShippingPhone)
orderPhoneLast4 := order.ShippingPhone[len(sanitizedOrderPhone)-4:]
if strings.EqualFold(sanitizedOrderNo, sanitizedOrderID) &&
strings.EqualFold(orderPhoneLast4, sanitizedPhoneLast4) &&
order.Amount > 1 {
return true, true, foundOrderNo, foundShippingPhone, foundOrderAmount // Full match.
}
}
}
return false, emailFound, foundOrderNo, foundShippingPhone, foundOrderAmount // Full match not found, return status of email match.
}
func fundAccount(tokenAccountID string) (bool, string) {
client := &http.Client{}
fundRequest := FundAccountRequest{
Seed: seed,
Amount: fundingAmount,
To: tokenAccountID,
}
requestBody, err := json.Marshal(fundRequest)
if err != nil {
log.Fatalf("Failed to marshal request: %v", err)
}
log.Println("Request body jsonData:", string(requestBody))
resp, err := client.Post(fundAPIURL, "application/json", bytes.NewBuffer(requestBody))
if err != nil {
log.Println("Error sending request to funding API:", err)
return false, fmt.Sprintf("Error sending request to funding API: %s", err.Error())
}
defer resp.Body.Close()
// Read the response body into a byte slice so it can be reused
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
log.Println("Error reading response body:", err)
return false, fmt.Sprintf("Error reading response body: %s", err.Error())
}
var errorResp FundAccountErrorResponse
if resp.StatusCode != http.StatusOK {
log.Printf("Server responded with non-OK status: %d\n", resp.StatusCode)
log.Println("Response body:", string(bodyBytes))
errorErr := json.Unmarshal(bodyBytes, &errorResp)
if errorErr == nil {
// If there is no error, then it was an error response
log.Printf("Error response from funding API: %+v\n", errorResp)
return false, fmt.Sprintf("Error response from funding API: %+v", errorResp)
} else {
// If both decodes failed, there is an issue with the response format
log.Printf("Error decoding funding response: %v\n", errorErr)
return false, fmt.Sprintf("Error decoding funding response: %v", errorErr.Error())
}
}
// Print the full response body for debugging
log.Println("Full response body:", string(bodyBytes))
// Attempt to decode the response into the success structure
var fundResponse FundAccountResponse
successErr := json.Unmarshal(bodyBytes, &fundResponse)
if successErr == nil {
// If there is no error, then it was a success response
log.Printf("Funding successful: %+v\n", fundResponse)
return fundResponse.Account == tokenAccountID && fundResponse.Amount.String() == fundingAmount.String(), ""
}
// Attempt to decode the response into the error structure
errorErr := json.Unmarshal(bodyBytes, &errorResp)
if errorErr == nil {
// If there is no error, then it was an error response
log.Printf("Error response from funding API: %+v\n", errorResp)
return false, fmt.Sprintf("Error response from funding API: %+v", errorResp)
} else {
// If both decodes failed, there is an issue with the response format
log.Printf("Error decoding funding response: %v\n", errorErr)
return false, fmt.Sprintf("Error decoding funding response: %v", errorErr.Error())
}
}
func saveUserDetails(orderID, tokenAccountID, appId string) {
timestamp := time.Now().Format(time.RFC3339) // Get current date/time
// Include appId in the record format
record := fmt.Sprintf("%s, %s, %s, %s\n", timestamp, orderID, tokenAccountID, appId)
file, err := os.OpenFile(userDetailFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println("Error opening file:", err)
return
}
defer file.Close()
if _, err := file.WriteString(record); err != nil {
log.Println("Error writing to file:", err)
}
}
func isOrderFunded(tokenAccountID, appId string) bool {
file, err := os.Open(userDetailFile)
if err != nil {
log.Println("Error opening file:", err)
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Update parts length check for new format: "timestamp, orderID, tokenAccountID, appId"
parts := strings.Split(line, ", ")
if len(parts) >= 3 && parts[2] == tokenAccountID && parts[3] == appId {
return true
}
}
return false
}
func verifyNFTOwnership(address string) bool {
url := fmt.Sprintf("https://api.opensea.io/api/v2/chain/%s/account/%s/nfts?collection=%s", chain, address, openSeaNFTId)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error creating request:", err)
return false
}
req.Header.Add("accept", "application/json")
req.Header.Add("x-api-key", openSeaAPIKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error sending request:", err)
return false
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error reading response:", err)
return false
}
if resp.StatusCode != http.StatusOK {
fmt.Printf("API request failed with status code: %d\n", resp.StatusCode)
fmt.Println("Response body:", string(body))
return false
}
var openSeaResp OpenSeaResponse
err = json.Unmarshal(body, &openSeaResp)
if err != nil {
fmt.Println("Error parsing JSON:", err)
return false
}
// Check if the user owns any NFTs from the specified contract
for _, nft := range openSeaResp.NFTs {
if nft.Contract == contractAddress {
return true
}
}
return false
}
func verifyNFTAndFundHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var data struct {
Address string `json:"address"`
TokenAccountID string `json:"tokenAccountId"`
AppID string `json:"appId"`
}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Verify NFT ownership
hasNFT := verifyNFTOwnership(data.Address)
if !hasNFT {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "NFT verification failed. You do not own the required NFT."})
return
}
// Check if the order (address in this case) is already funded
if isOrderFunded(data.Address, data.AppID) {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "The order is already registered. If you think this is a mistake please contact [email protected]"})
return
}
// Fund the account
success, errMsg := fundAccount(data.TokenAccountID)
if !success {
balance, err := checkAccountBalance(data.TokenAccountID)
if err == nil && balance != "0" {
log.Println("Account has a positive balance, considering funding successful")
} else {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": errMsg})
return
}
}
// Save user details
saveUserDetails(data.Address, data.TokenAccountID, data.AppID)
// Send success response
w.WriteHeader(http.StatusOK)
response := map[string]string{"status": "success", "message": "Account is funded successfully"}
json.NewEncoder(w).Encode(response)
}
func accountExists(streamrAccount string) bool {
file, err := os.Open(streamrFile)
if err != nil {
return false
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Contains(scanner.Text(), streamrAccount) {
return true
}
}
return false
}
func sendStreamrEmail(email, orderID, phoneNumber, streamrAccount string) error {
apiKey, err := readAPIKey("/home/ubuntu/testnet-server/brevo.key")
if err != nil {
return err
}
apiURL := "https://api.brevo.com/v3/smtp/email"
htmlContent := fmt.Sprintf(`
<html><head></head><body>
<p>New Streamr node request:</p>
<ul>
<li>Email: %s</li>
<li>Order ID: %s</li>
<li>Phone Number: %s</li>
<li>Streamr Account: %s</li>
</ul>
</body></html>
`, email, orderID, phoneNumber, streamrAccount)
emailRequest := EmailRequest{
Sender: Sender{
Name: "Functionyard",
Email: "[email protected]",
},
To: []ToEmail{
{
Email: "[email protected]",
Name: "FX Land",
},
},
Subject: "New Streamr node request",
HtmlContent: htmlContent,
}
payloadBytes, err := json.Marshal(emailRequest)
if err != nil {
return err
}
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(payloadBytes))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("accept", "application/json")
req.Header.Set("api-key", apiKey)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("API responded with non-OK status: %d", resp.StatusCode)
}
return nil
}
func saveStreamrAccount(streamrAccount, orderID string) error {
file, err := os.OpenFile(streamrFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer file.Close()
_, err = file.WriteString(fmt.Sprintf("%s,%s\n", streamrAccount, orderID))
return err
}
func streamrHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.ServeFile(w, r, "static/streamr.html")
return
}
email := r.FormValue("email")
orderID := r.FormValue("orderId")
phoneNumber := r.FormValue("phoneNumber")
streamrAccount := r.FormValue("streamrAccount")
orderFound, emailFound, foundOrderNo, foundShippingPhone, foundOrderAmount := verifyOrder(email, orderID, phoneNumber)
if !orderFound {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "Your order could not be found automatically or does not match what we have in our system. If your email is in the system you will shortly receive an email with registered order details. You can also contact [email protected]"})
if emailFound {
err := sendEmailDetails(email, foundOrderNo, foundShippingPhone, foundOrderAmount)
log.Println("Email sending result")
log.Println(err)
}
return
}
// Check if streamrAccount already exists in streamr.txt
if accountExists(streamrAccount) {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "This Streamr account is already registered."})
return
}
// Send email to [email protected]
err := sendStreamrEmail(email, orderID, phoneNumber, streamrAccount)
if err != nil {
log.Println("Error sending Streamr email:", err)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "Error processing your request. Please try again later."})
return
}
// Save streamrAccount and orderID to streamr.txt
err = saveStreamrAccount(streamrAccount, orderID)
if err != nil {
log.Println("Error saving Streamr account:", err)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]string{"status": "error", "message": "Error processing your request. Please try again later."})
return
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(map[string]string{"status": "success", "message": "Your Streamr node request has been submitted successfully."})
}