Skip to content

Commit 7007cae

Browse files
committed
Update antispam guard to be an app integration
1 parent c241052 commit 7007cae

File tree

7 files changed

+126
-106
lines changed

7 files changed

+126
-106
lines changed

pkg/code/antispam/airdrop.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

pkg/code/antispam/guard.go

Lines changed: 75 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,86 @@
11
package antispam
22

33
import (
4-
"github.com/sirupsen/logrus"
4+
"context"
55

6-
code_data "github.com/code-payments/code-server/pkg/code/data"
6+
"github.com/code-payments/code-server/pkg/code/common"
7+
"github.com/code-payments/code-server/pkg/metrics"
78
)
89

9-
// Guard is an antispam guard that checks whether operations of interest are
10-
// allowed to be performed.
11-
//
12-
// Note: Implementation assumes distributed locking has already occurred for
13-
// all methods.
1410
type Guard struct {
15-
log *logrus.Entry
16-
data code_data.Provider
11+
integration Integration
1712
}
1813

19-
func NewGuard(
20-
data code_data.Provider,
21-
) *Guard {
22-
return &Guard{
23-
log: logrus.StandardLogger().WithField("type", "antispam/guard"),
24-
data: data,
14+
func NewGuard(integration Integration) *Guard {
15+
return &Guard{integration: integration}
16+
}
17+
18+
func (g *Guard) AllowOpenAccounts(ctx context.Context, owner *common.Account) (bool, error) {
19+
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowOpenAccounts")
20+
defer tracer.End()
21+
22+
allow, reason, err := g.integration.AllowOpenAccounts(ctx, owner)
23+
if err != nil {
24+
return false, err
25+
}
26+
if !allow {
27+
recordDenialEvent(ctx, actionOpenAccounts, reason)
28+
}
29+
return allow, nil
30+
}
31+
32+
func (g *Guard) AllowWelcomeBonus(ctx context.Context, owner *common.Account) (bool, error) {
33+
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowWelcomeBonus")
34+
defer tracer.End()
35+
36+
allow, reason, err := g.integration.AllowWelcomeBonus(ctx, owner)
37+
if err != nil {
38+
return false, err
39+
}
40+
if !allow {
41+
recordDenialEvent(ctx, actionWelcomeBonus, reason)
42+
}
43+
return allow, nil
44+
}
45+
46+
func (g *Guard) AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, error) {
47+
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowSendPayment")
48+
defer tracer.End()
49+
50+
allow, reason, err := g.integration.AllowSendPayment(ctx, owner, destination, isPublic)
51+
if err != nil {
52+
return false, err
53+
}
54+
if !allow {
55+
recordDenialEvent(ctx, actionSendPayment, reason)
56+
}
57+
return allow, nil
58+
}
59+
60+
func (g *Guard) AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, error) {
61+
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowReceivePayments")
62+
defer tracer.End()
63+
64+
allow, reason, err := g.integration.AllowReceivePayments(ctx, owner, isPublic)
65+
if err != nil {
66+
return false, err
67+
}
68+
if !allow {
69+
recordDenialEvent(ctx, actionReceivePayments, reason)
70+
}
71+
return allow, nil
72+
}
73+
74+
func (g *Guard) AllowSwap(ctx context.Context, owner *common.Account) (bool, error) {
75+
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "AllowSwap")
76+
defer tracer.End()
77+
78+
allow, reason, err := g.integration.AllowSwap(ctx, owner)
79+
if err != nil {
80+
return false, err
81+
}
82+
if !allow {
83+
recordDenialEvent(ctx, actionSwap, reason)
2584
}
85+
return allow, nil
2686
}

pkg/code/antispam/integration.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package antispam
2+
3+
import (
4+
"context"
5+
6+
"github.com/code-payments/code-server/pkg/code/common"
7+
)
8+
9+
// Integration is an antispam guard integration that apps can implement to check
10+
// whether operations of interest are allowed to be performed.
11+
type Integration interface {
12+
AllowOpenAccounts(ctx context.Context, owner *common.Account) (bool, string, error)
13+
14+
AllowWelcomeBonus(ctx context.Context, owner *common.Account) (bool, string, error)
15+
16+
AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, string, error)
17+
18+
AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error)
19+
20+
AllowSwap(ctx context.Context, owner *common.Account) (bool, string, error)
21+
}
22+
23+
type allowEverythingIntegration struct {
24+
}
25+
26+
// NewAllowEverything returns a default antispam integration that allows everything
27+
func NewAllowEverything() Integration {
28+
return &allowEverythingIntegration{}
29+
}
30+
31+
func (i *allowEverythingIntegration) AllowOpenAccounts(ctx context.Context, owner *common.Account) (bool, string, error) {
32+
return true, "", nil
33+
}
34+
35+
func (i *allowEverythingIntegration) AllowWelcomeBonus(ctx context.Context, owner *common.Account) (bool, string, error) {
36+
return true, "", nil
37+
}
38+
39+
func (i *allowEverythingIntegration) AllowSendPayment(ctx context.Context, owner, destination *common.Account, isPublic bool) (bool, string, error) {
40+
return true, "", nil
41+
}
42+
43+
func (i *allowEverythingIntegration) AllowReceivePayments(ctx context.Context, owner *common.Account, isPublic bool) (bool, string, error) {
44+
return true, "", nil
45+
}
46+
47+
func (i *allowEverythingIntegration) AllowSwap(ctx context.Context, owner *common.Account) (bool, string, error) {
48+
return true, "", nil
49+
}

pkg/code/antispam/intent.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

pkg/code/antispam/metrics.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ const (
1515
actionSendPayment = "SendPayment"
1616
actionReceivePayments = "ReceivePayments"
1717

18-
actionWelcomeBonus = "WelcomeBonus"
19-
actionReferralBonus = "ReferralBonus"
18+
actionWelcomeBonus = "WelcomeBonus"
2019

2120
actionSwap = "Swap"
2221
)

pkg/code/antispam/swap.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

pkg/code/server/transaction/intent_handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ func (h *SendPublicPaymentIntentHandler) AllowCreation(ctx context.Context, inte
381381
return err
382382
}
383383

384-
allow, err := h.antispamGuard.AllowSendPayment(ctx, initiatiorOwnerAccount, true, destination)
384+
allow, err := h.antispamGuard.AllowSendPayment(ctx, initiatiorOwnerAccount, destination, true)
385385
if err != nil {
386386
return err
387387
} else if !allow {

0 commit comments

Comments
 (0)