Skip to content

Commit

Permalink
Merge branch 'master' of git+ssh://github.com/brave-intl/bat-go into …
Browse files Browse the repository at this point in the history
…pay-with-bat-skus
  • Loading branch information
husobee committed Jul 24, 2023
2 parents 52fc187 + 265158b commit 240ffcf
Show file tree
Hide file tree
Showing 11 changed files with 378 additions and 31 deletions.
65 changes: 53 additions & 12 deletions libs/clients/gemini/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ var (
},
[]string{"country_code", "status"},
)

documentTypePrecedence = []string{
"passport",
"drivers_license",
"national_identity_card",
"passport_card",
"tax_id",
"residence_permit",
"work_permit",
"voter_id",
"visa",
"national_insurance_card",
"indigenous_card",
}
)

func init() {
Expand Down Expand Up @@ -450,8 +464,15 @@ func (v *ValidateAccountReq) GenerateQueryString() (url.Values, error) {

// ValidateAccountRes - request structure for inputs to validate account client call
type ValidateAccountRes struct {
ID string `json:"id"`
CountryCode string `json:"countryCode"`
ID string `json:"id"`
CountryCode string `json:"countryCode"`
ValidDocuments []ValidDocument `json:"validDocuments"`
}

// ValidDocument represent a valid proof of identity document type.
type ValidDocument struct {
Type string `json:"type"`
IssuingCountry string `json:"issuingCountry"`
}

// ValidateAccount - given a verificationToken validate the token is authentic and get the unique account id
Expand All @@ -476,46 +497,52 @@ func (c *HTTPClient) ValidateAccount(ctx context.Context, verificationToken, rec
return "", res.CountryCode, err
}

if len(res.ValidDocuments) <= 0 {
return "", "", errors.New("error no valid documents in response")
}

issuingCountry := strings.ToUpper(res.ValidDocuments[0].IssuingCountry)
if dcountry := countryForDocByPrecendence(documentTypePrecedence, res.ValidDocuments); dcountry != "" {
issuingCountry = strings.ToUpper(dcountry)
}

// feature flag for using new custodian regions
if useCustodianRegions, ok := ctx.Value(appctx.UseCustodianRegionsCTXKey).(bool); ok && useCustodianRegions {
// get the uphold custodian supported regions
if custodianRegions, ok := ctx.Value(appctx.CustodianRegionsCTXKey).(*custodian.Regions); ok {
allowed := custodianRegions.Gemini.Verdict(
res.CountryCode,
)

allowed := custodianRegions.Gemini.Verdict(issuingCountry)
if !allowed {
countGeminiWalletAccountValidation.With(prometheus.Labels{
"country_code": res.CountryCode,
"status": "failure",
}).Inc()
return res.ID, res.CountryCode, errorutils.ErrInvalidCountry
return res.ID, issuingCountry, errorutils.ErrInvalidCountry
}
}
} else { // use default blacklist functionality
if blacklist, ok := ctx.Value(appctx.BlacklistedCountryCodesCTXKey).([]string); ok {
// check country code
for _, v := range blacklist {
if strings.EqualFold(res.CountryCode, v) {
if res.CountryCode != "" {
if issuingCountry != "" {
countGeminiWalletAccountValidation.With(prometheus.Labels{
"country_code": res.CountryCode,
"country_code": issuingCountry,
"status": "failure",
}).Inc()
}
return res.ID, res.CountryCode, errorutils.ErrInvalidCountry
return res.ID, issuingCountry, errorutils.ErrInvalidCountry
}
}
}
}
if res.CountryCode != "" {
countGeminiWalletAccountValidation.With(prometheus.Labels{
"country_code": res.CountryCode,
"country_code": issuingCountry,
"status": "success",
}).Inc()
}

return res.ID, res.CountryCode, nil
return res.ID, issuingCountry, nil
}

// FetchAccountList fetches the list of accounts associated with the given api key
Expand Down Expand Up @@ -565,3 +592,17 @@ func (c *HTTPClient) FetchBalances(
}
return &body, err
}

func countryForDocByPrecendence(prec []string, docs []ValidDocument) string {
var result string

for _, pdoc := range prec {
for _, vdoc := range docs {
if strings.EqualFold(pdoc, vdoc.Type) {
return vdoc.IssuingCountry
}
}
}

return result
}
73 changes: 73 additions & 0 deletions libs/clients/gemini/clientx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package gemini

import (
"testing"

should "github.com/stretchr/testify/assert"
)

func TestCountryForDocByPrecendence(t *testing.T) {
type testCase struct {
name string
given []ValidDocument
exp string
}

tests := []testCase{
{
name: "empty",
},

{
name: "one_passport",
given: []ValidDocument{
{
Type: "passport",
IssuingCountry: "US",
},
},
exp: "US",
},

{
name: "two_docs",
given: []ValidDocument{
{
Type: "passport",
IssuingCountry: "US",
},

{
Type: "drivers_license",
IssuingCountry: "CA",
},
},
exp: "US",
},

{
name: "two_docs_reverse",
given: []ValidDocument{
{
Type: "drivers_license",
IssuingCountry: "CA",
},

{
Type: "passport",
IssuingCountry: "US",
},
},
exp: "US",
},
}

for i := range tests {
tc := tests[i]

t.Run(tc.name, func(t *testing.T) {
act := countryForDocByPrecendence(documentTypePrecedence, tc.given)
should.Equal(t, tc.exp, act)
})
}
}
2 changes: 2 additions & 0 deletions nitro-shim/scripts/sleep.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#!/bin/bash

echo --- Monitoring enclave $(date) ---
set -eux

while true
do
# check every so often that the enclave is running
sleep 480
date

EID=$(nitro-cli describe-enclaves | jq -r .[].EnclaveID)
if [ "${EID}" == "" ]; then
Expand Down
10 changes: 10 additions & 0 deletions services/wallet/controllers_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,13 @@ func UpdateWalletV4(s *Service) func(w http.ResponseWriter, r *http.Request) *ha
return handlers.RenderContent(r.Context(), nil, w, http.StatusOK)
}
}

// GetWalletV4 is the same as get wallet v3, but we are now requiring http signatures for get wallet requests
func GetWalletV4(w http.ResponseWriter, r *http.Request) *handlers.AppError {
return GetWalletV3(w, r)
}

// GetUpholdWalletBalanceV4 produces an http handler for the service s which handles balance inquiries of uphold wallets
func GetUpholdWalletBalanceV4(w http.ResponseWriter, r *http.Request) *handlers.AppError {
return GetUpholdWalletBalanceV3(w, r)
}
18 changes: 7 additions & 11 deletions services/wallet/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ func (service *Service) getCustodianRegions() custodian.Regions {

// RegisterRoutes - register the wallet api routes given a chi.Mux
func RegisterRoutes(ctx context.Context, s *Service, r *chi.Mux) *chi.Mux {
disableDisconnect, _ := ctx.Value(appctx.DisableDisconnectCTXKey).(bool) // defaults false
// setup our wallet routes
r.Route("/v3/wallet", func(r chi.Router) {
// rate limited to 2 per minute...
Expand All @@ -277,11 +276,6 @@ func RegisterRoutes(ctx context.Context, s *Service, r *chi.Mux) *chi.Mux {
"LinkGeminiDepositAccount", LinkGeminiDepositAccountV3(s))).ServeHTTP)
r.Post("/xyzabc/{paymentID}/claim", middleware.HTTPSignedOnly(s)(middleware.InstrumentHandlerFunc(
"LinkXyzAbcDepositAccount", LinkXyzAbcDepositAccountV3(s))).ServeHTTP)
// disconnect verified custodial wallet
if !disableDisconnect { // if disable-disconnect is false then add this route
r.Delete("/{custodian}/{paymentID}/claim", middleware.HTTPSignedOnly(s)(middleware.InstrumentHandlerFunc(
"DisconnectCustodianLinkV3", DisconnectCustodianLinkV3(s))).ServeHTTP)
}

// create wallet connect routes for our wallet providers
r.Post("/uphold/{paymentID}/connect", middleware.InstrumentHandlerFunc(
Expand All @@ -292,11 +286,6 @@ func RegisterRoutes(ctx context.Context, s *Service, r *chi.Mux) *chi.Mux {
"LinkGeminiDepositAccount", LinkGeminiDepositAccountV3(s))).ServeHTTP)
r.Post("/xyzabc/{paymentID}/connect", middleware.HTTPSignedOnly(s)(middleware.InstrumentHandlerFunc(
"LinkXyzAbcDepositAccount", LinkXyzAbcDepositAccountV3(s))).ServeHTTP)
// disconnect verified custodial wallet
if !disableDisconnect { // if disable-disconnect is false then add this route
r.Delete("/{custodian}/{paymentID}/connect", middleware.HTTPSignedOnly(s)(middleware.InstrumentHandlerFunc(
"DisconnectCustodianLinkV3", DisconnectCustodianLinkV3(s))).ServeHTTP)
}
}

r.Get("/linking-info", middleware.SimpleTokenAuthorizedOnly(
Expand All @@ -318,6 +307,13 @@ func RegisterRoutes(ctx context.Context, s *Service, r *chi.Mux) *chi.Mux {
r.Post("/", middleware.InstrumentHandlerFunc("CreateWalletV4", CreateWalletV4(s)))
r.Patch("/{paymentID}", middleware.HTTPSignedOnly(s)(middleware.InstrumentHandlerFunc(
"UpdateWalletV4", UpdateWalletV4(s))).ServeHTTP)
r.Get("/{paymentID}",
middleware.HTTPSignedOnly(s)(middleware.InstrumentHandlerFunc(
"GetWalletV4", GetWalletV4)).ServeHTTP)
// get wallet balance routes
r.Get("/uphold/{paymentID}",
middleware.HTTPSignedOnly(s)(middleware.InstrumentHandlerFunc(
"GetUpholdWalletBalanceV4", GetUpholdWalletBalanceV4)).ServeHTTP)
})

return r
Expand Down
16 changes: 13 additions & 3 deletions tools/payments/cmd/authorize/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ The flags are:
The redis cluster password
-ru
The redis cluster user
-p
The payout id
*/
package main

Expand All @@ -32,9 +34,9 @@ import (
"flag"
"log"
"os"
"strings"

"github.com/brave-intl/bat-go/tools/payments"
uuid "github.com/satori/go.uuid"
)

func main() {
Expand Down Expand Up @@ -65,6 +67,10 @@ func main() {
"v", false,
"view verbose logging")

payoutID := flag.String(
"p", "",
"payout id")

flag.Parse()

// get the list of report files for prepare
Expand All @@ -84,8 +90,12 @@ func main() {
log.Fatalf("failed to create settlement client: %v\n", err)
}

if payoutID == nil || strings.TrimSpace(*payoutID) == "" {
log.Fatal("failed payout id cannot be nil or empty\n")
}

wc := &payments.WorkerConfig{
PayoutID: uuid.NewV4().String(),
PayoutID: *payoutID,
ConsumerGroup: payments.SubmitStream + "-cg",
Stream: payments.SubmitStream,
Count: 0,
Expand Down Expand Up @@ -128,6 +138,6 @@ func main() {

if *verbose {
log.Printf("submit transactions loaded for %+v\n", wc)
log.Println("completed report submission")
log.Println("authorize command complete")
}
}
14 changes: 11 additions & 3 deletions tools/payments/cmd/prepare/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import (
"flag"
"log"
"os"
"strings"

"github.com/brave-intl/bat-go/tools/payments"
uuid "github.com/satori/go.uuid"
)

func main() {
Expand All @@ -59,6 +59,10 @@ func main() {
"ru", "",
"redis cluster username")

payoutID := flag.String(
"p", "",
"payout id")

flag.Parse()

// get the list of report files for prepare
Expand All @@ -77,8 +81,12 @@ func main() {
log.Fatalf("failed to create settlement client: %v\n", err)
}

if payoutID == nil || strings.TrimSpace(*payoutID) == "" {
log.Fatal("failed payout id cannot be nil or empty\n")
}

wc := &payments.WorkerConfig{
PayoutID: uuid.NewV4().String(),
PayoutID: *payoutID,
ConsumerGroup: payments.PrepareStream + "-cg",
Stream: payments.PrepareStream,
Count: 0,
Expand Down Expand Up @@ -112,6 +120,6 @@ func main() {

if *verbose {
log.Printf("prepare transactions loaded for %+v\n", wc)
log.Println("completed report preparation")
log.Println("prepare command complete")
}
}
4 changes: 4 additions & 0 deletions tools/payments/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ require (
github.com/google/uuid v1.3.0
github.com/redis/go-redis/v9 v9.0.2
github.com/shopspring/decimal v1.3.1
github.com/stretchr/testify v1.8.4
github.com/veracruz-project/go-nitro-enclave-attestation-document v0.0.0-20230315135749-6fc97d770084
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/btcsuite/btcutil v1.0.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fxamacker/cbor/v2 v2.4.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
Expand All @@ -31,4 +34,5 @@ require (
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/sys v0.4.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 240ffcf

Please sign in to comment.