Skip to content

Commit 8918bee

Browse files
authored
Merge pull request #404 from brave-intl/master
2020-05-21 RC
2 parents 65689d0 + 96f61a1 commit 8918bee

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

payment/credentials.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,21 @@ type CredentialBinding struct {
5555
Signature string `json:"signature" valid:"base64"`
5656
}
5757

58+
// DeduplicateCredentialBindings - given a list of tokens return a deduplicated list
59+
func DeduplicateCredentialBindings(tokens ...CredentialBinding) []CredentialBinding {
60+
var (
61+
seen = map[string]bool{}
62+
result = []CredentialBinding{}
63+
)
64+
for _, t := range tokens {
65+
if !seen[t.TokenPreimage] {
66+
seen[t.TokenPreimage] = true
67+
result = append(result, t)
68+
}
69+
}
70+
return result
71+
}
72+
5873
// Issuer includes information about a particular credential issuer
5974
type Issuer struct {
6075
ID uuid.UUID `json:"id" db:"id"`
@@ -177,6 +192,9 @@ func (service *Service) SignOrderCreds(ctx context.Context, orderID uuid.UUID, i
177192

178193
// generateCredentialRedemptions - helper to create credential redemptions from cred bindings
179194
var generateCredentialRedemptions = func(ctx context.Context, cb []CredentialBinding) ([]cbr.CredentialRedemption, error) {
195+
// deduplicate credential bindings
196+
cb = DeduplicateCredentialBindings(cb...)
197+
180198
var (
181199
requestCredentials = make([]cbr.CredentialRedemption, len(cb))
182200
issuers = make(map[string]*Issuer)

payment/credentials_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,39 @@ import (
55
"testing"
66
)
77

8+
func TestDeduplicateCredentialBindings(t *testing.T) {
9+
10+
var tokens = []CredentialBinding{
11+
{
12+
TokenPreimage: "totally_random",
13+
},
14+
{
15+
TokenPreimage: "totally_random_1",
16+
},
17+
{
18+
TokenPreimage: "totally_random",
19+
},
20+
{
21+
TokenPreimage: "totally_random_2",
22+
},
23+
}
24+
var seen = []CredentialBinding{}
25+
26+
var result = DeduplicateCredentialBindings(tokens...)
27+
if len(result) > len(tokens) {
28+
t.Error("result should be less than number of tokens")
29+
}
30+
31+
for _, v := range result {
32+
for _, vv := range seen {
33+
if v == vv {
34+
t.Error("Deduplication of tokens didn't work")
35+
}
36+
seen = append(seen, v)
37+
}
38+
}
39+
}
40+
841
func TestIssuerID(t *testing.T) {
942

1043
cases := []struct {

promotion/suggestions.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ type CredentialBinding struct {
3030
Signature string `json:"signature" valid:"base64"`
3131
}
3232

33+
// DeduplicateCredentialBindings - given a list of tokens return a deduplicated list
34+
func DeduplicateCredentialBindings(tokens ...CredentialBinding) []CredentialBinding {
35+
var (
36+
seen = map[string]bool{}
37+
result = []CredentialBinding{}
38+
)
39+
for _, t := range tokens {
40+
if !seen[t.TokenPreimage] {
41+
seen[t.TokenPreimage] = true
42+
result = append(result, t)
43+
}
44+
}
45+
return result
46+
}
47+
3348
// Suggestion encapsulates information from the user about where /how they want to contribute
3449
type Suggestion struct {
3550
Type string `json:"type" valid:"in(auto-contribute|oneoff-tip|recurring-tip|payment)"`
@@ -124,6 +139,10 @@ type SuggestionWorker interface {
124139

125140
// GetCredentialRedemptions as well as total and funding sources from a list of credential bindings
126141
func (service *Service) GetCredentialRedemptions(ctx context.Context, credentials []CredentialBinding) (total decimal.Decimal, requestCredentials []cbr.CredentialRedemption, fundingSources map[string]FundingSource, promotions map[string]*Promotion, err error) {
142+
143+
// deduplicate the bindings before anything
144+
credentials = DeduplicateCredentialBindings(credentials...)
145+
127146
total = decimal.Zero
128147
requestCredentials = make([]cbr.CredentialRedemption, len(credentials))
129148
fundingSources = make(map[string]FundingSource)

promotion/suggestions_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,39 @@ import (
77
"github.com/stretchr/testify/assert"
88
)
99

10+
func TestDeduplicateCredentialBindings(t *testing.T) {
11+
12+
var tokens = []CredentialBinding{
13+
{
14+
TokenPreimage: "totally_random",
15+
},
16+
{
17+
TokenPreimage: "totally_random_1",
18+
},
19+
{
20+
TokenPreimage: "totally_random",
21+
},
22+
{
23+
TokenPreimage: "totally_random_2",
24+
},
25+
}
26+
var seen = []CredentialBinding{}
27+
28+
var result = DeduplicateCredentialBindings(tokens...)
29+
if len(result) > len(tokens) {
30+
t.Error("result should be less than number of tokens")
31+
}
32+
33+
for _, v := range result {
34+
for _, vv := range seen {
35+
if v == vv {
36+
t.Error("Deduplication of tokens didn't work")
37+
}
38+
seen = append(seen, v)
39+
}
40+
}
41+
}
42+
1043
func TestUnmarshalText(t *testing.T) {
1144
encoded := "eyJ0eXBlIjogImF1dG8tY29udHJpYnV0ZSIsICJjaGFubmVsIjogImJyYXZlLmNvbSJ9"
1245
var expected, d Suggestion

0 commit comments

Comments
 (0)