@@ -67,8 +67,12 @@ func create(_ *http.Request, args *commentapi.CreateArgs, reply *commentapi.Crea
67
67
68
68
var frequencyCheck = checkFrequency
69
69
70
- if args .Amount != nil {
71
- if args .DryRun {
70
+ isPaidComment := args .Amount != nil || args .PaymentIntentID != nil //this is because odysee android app doesn't pass an amount for dryruns...
71
+ isDryRun := args .DryRun && (args .SupportTxID != nil || args .PaymentIntentID != nil || args .PaymentTxID != nil )
72
+ isActualTransaction := isPaidComment && (args .SupportTxID != nil || args .PaymentIntentID != nil || args .PaymentTxID != nil )
73
+
74
+ if isPaidComment {
75
+ if isDryRun {
72
76
cents := * args .Amount
73
77
if args .SupportTxID != nil {
74
78
lbc , err := btcutil .NewAmount (* args .Amount )
@@ -80,40 +84,15 @@ func create(_ *http.Request, args *commentapi.CreateArgs, reply *commentapi.Crea
80
84
cents *= 100
81
85
}
82
86
request .comment .Amount .SetValid (uint64 (cents ))
83
- } else {
87
+ } else if isActualTransaction {
84
88
err := updateSupportInfo (request )
85
89
if err != nil {
86
90
return err
87
91
}
88
- }
89
- // ignore the frequency if it's a tipped comment
90
- frequencyCheck = ignoreFrequency
91
- }
92
-
93
- if args .SupportTxID != nil || args .PaymentIntentID != nil || args .PaymentTxId != nil {
94
- if args .DryRun {
95
- if args .Amount != nil {
96
- if args .PaymentIntentID != nil {
97
- cents := uint64 (* args .Amount * 100 )
98
- request .comment .Amount .SetValid (cents )
99
- } else if args .SupportTxID != nil {
100
- lbc , err := btcutil .NewAmount (* args .Amount )
101
- if err != nil {
102
- return errors .Err (err )
103
- }
104
- request .comment .Amount .SetValid (uint64 (lbc .ToUnit (btcutil .AmountSatoshi )))
105
- } else if args .PaymentTxId != nil {
106
- //assume cents
107
- request .comment .Amount .SetValid (uint64 (* args .Amount ))
108
- }
109
- }
110
92
} else {
111
- err := updateSupportInfo (request )
112
- if err != nil {
113
- return err
114
- }
93
+ return errors .Err ("you must specify a transaction if it's a paid comment" )
115
94
}
116
- // ignore the frequency if its a tipped comment
95
+ // ignore the frequency if it's a tipped comment
117
96
frequencyCheck = ignoreFrequency
118
97
}
119
98
@@ -737,6 +716,8 @@ func getCounter(key string, expiration time.Duration) (*counter.Counter, error)
737
716
738
717
func updateSupportInfo (request * createRequest ) error {
739
718
triesLeft := 3
719
+ backoff := time .Second
720
+
740
721
for {
741
722
triesLeft --
742
723
err := updateSupportInfoAttempt (request , true )
@@ -746,20 +727,36 @@ func updateSupportInfo(request *createRequest) error {
746
727
if triesLeft == 0 {
747
728
return err
748
729
}
749
- time .Sleep (1 * time .Second )
730
+ time .Sleep (backoff )
731
+ backoff *= 2
750
732
}
751
733
}
752
734
735
+ func checkReplays (txID string ) error {
736
+ existingComment , err := m .Comments (m .CommentWhere .TXID .EQ (null .StringFrom (txID ))).One (db .RO )
737
+ if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
738
+ if ! errors .Is (err , sql .ErrNoRows ) {
739
+ return errors .Err (err )
740
+ }
741
+ }
742
+ if existingComment != nil {
743
+ return errors .Err ("a comment with this transaction id already exists" )
744
+ }
745
+ return nil
746
+ }
747
+
753
748
func updateSupportInfoAttempt (request * createRequest , retry bool ) error {
754
- //TODO: fix replay attacks
755
- //todo: fix stolen tx attacks
756
749
if request .args .PaymentIntentID != nil {
757
750
env := ""
758
751
if request .args .Environment != nil {
759
752
env = * request .args .Environment
760
753
}
761
- paymentintentClient := & paymentintent.Client {B : stripe .GetBackend (stripe .APIBackend ), Key : config .ConnectAPIKey (config .From (env ))}
762
- pi , err := paymentintentClient .Get (* request .args .PaymentIntentID , & stripe.PaymentIntentParams {})
754
+ err := checkReplays (* request .args .PaymentIntentID )
755
+ if err != nil {
756
+ return err
757
+ }
758
+ pic := & paymentintent.Client {B : stripe .GetBackend (stripe .APIBackend ), Key : config .ConnectAPIKey (config .From (env ))}
759
+ pi , err := pic .Get (* request .args .PaymentIntentID , & stripe.PaymentIntentParams {})
763
760
if err != nil {
764
761
if ! retry {
765
762
logrus .Error (errors .Prefix ("could not get payment intent %s" , * request .args .PaymentIntentID ))
@@ -772,9 +769,13 @@ func updateSupportInfoAttempt(request *createRequest, retry bool) error {
772
769
request .comment .Amount .SetValid (uint64 (pi .Amount ))
773
770
request .comment .IsFiat = true
774
771
request .comment .Currency .SetValid (pi .Currency )
772
+ request .comment .TXID .SetValid (* request .args .PaymentIntentID )
775
773
return nil
776
774
} else if request .args .SupportTxID != nil {
777
- request .comment .TXID .SetValid (util .StrFromPtr (request .args .SupportTxID ))
775
+ err := checkReplays (* request .args .SupportTxID )
776
+ if err != nil {
777
+ return err
778
+ }
778
779
txSummary , err := lbry .SDK .GetTx (request .comment .TXID .String )
779
780
if err != nil {
780
781
return errors .Err (err )
@@ -790,35 +791,32 @@ func updateSupportInfoAttempt(request *createRequest, retry bool) error {
790
791
if err != nil {
791
792
return errors .Err (err )
792
793
}
794
+ request .comment .TXID .SetValid (util .StrFromPtr (request .args .SupportTxID ))
793
795
request .comment .Amount .SetValid (amount )
796
+ request .comment .Currency .SetValid ("LBC" )
794
797
return nil
795
- } else if request .args .PaymentTxId != nil {
796
- //check for replays
797
- existingComment , err := m .Comments (m .CommentWhere .TXID .EQ (null .StringFromPtr (request .args .PaymentTxId ))).One (db .RO )
798
- if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
799
- if ! errors .Is (err , sql .ErrNoRows ) {
800
- return errors .Err (err )
801
- }
802
- }
803
- if existingComment != nil {
804
- return errors .Err ("a comment with this transaction id already exists" )
798
+ } else if request .args .PaymentTxID != nil {
799
+ err := checkReplays (* request .args .PaymentTxID )
800
+ if err != nil {
801
+ return err
805
802
}
806
- //query internal apis to verify the transaction
807
- pi , err := lbry .API .GetDetailsForTransaction (* request .args .PaymentTxId )
803
+ pi , err := lbry .API .GetDetailsForTransaction (* request .args .PaymentTxID )
808
804
if err != nil {
809
805
return err
810
806
}
811
807
if pi .Status != "confirmed" {
812
808
return errors .Err ("transaction is not confirmed" )
813
809
}
814
- if pi .ChannelClaimId != request .args .ChannelID {
810
+ if pi .ChannelClaimID != request .args .ChannelID {
815
811
return errors .Err ("channel mismatch for transaction" )
816
812
}
817
813
if time .Since (pi .TippedAt ) > time .Hour {
818
814
return errors .Err ("transaction is too old" )
819
815
}
820
816
request .comment .Amount .SetValid (pi .Amount )
821
817
request .comment .Currency .SetValid (pi .Currency )
818
+ request .comment .TXID .SetValid (* request .args .PaymentTxID )
819
+ request .comment .IsFiat = true
822
820
}
823
821
return nil
824
822
}
0 commit comments