-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #528 from adamdecaf/search-crypto-addresses
feat: add /crypto address and extract digital currency addresses from OFAC
- Loading branch information
Showing
9 changed files
with
284 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2022 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
|
||
moovhttp "github.com/moov-io/base/http" | ||
"github.com/moov-io/base/log" | ||
"github.com/moov-io/watchman/pkg/ofac" | ||
) | ||
|
||
type cryptoAddressSearchResult struct { | ||
OFAC []SDNWithDigitalCurrencyAddress `json:"ofac"` | ||
} | ||
|
||
func searchByCryptoAddress(logger log.Logger, searcher *searcher) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
cryptoAddress := strings.TrimSpace(r.URL.Query().Get("address")) | ||
cryptoName := strings.TrimSpace(r.URL.Query().Get("name")) | ||
if cryptoAddress == "" { | ||
moovhttp.Problem(w, errNoSearchParams) | ||
return | ||
} | ||
|
||
limit := extractSearchLimit(r) | ||
|
||
// Find SDNs with a crypto address that exactly matches | ||
resp := cryptoAddressSearchResult{ | ||
OFAC: searcher.FindSDNCryptoAddresses(limit, cryptoName, cryptoAddress), | ||
} | ||
|
||
w.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(resp) | ||
} | ||
} | ||
|
||
type SDNWithDigitalCurrencyAddress struct { | ||
SDN *ofac.SDN `json:"sdn"` | ||
|
||
DigitalCurrencyAddresses []ofac.DigitalCurrencyAddress `json:"digitalCurrencyAddresses"` | ||
} | ||
|
||
func (s *searcher) FindSDNCryptoAddresses(limit int, name, needle string) []SDNWithDigitalCurrencyAddress { | ||
s.RLock() | ||
defer s.RUnlock() | ||
|
||
var out []SDNWithDigitalCurrencyAddress | ||
for i := range s.SDNComments { | ||
addresses := s.SDNComments[i].DigitalCurrencyAddresses | ||
for j := range addresses { | ||
// Skip addresses of a different coin | ||
if name != "" && addresses[j].Currency != name { | ||
continue | ||
} | ||
if addresses[j].Address == needle { | ||
// Find SDN | ||
sdn := s.findSDNWithoutLock(s.SDNComments[i].EntityID) | ||
if sdn != nil { | ||
out = append(out, SDNWithDigitalCurrencyAddress{ | ||
SDN: sdn.SDN, | ||
DigitalCurrencyAddresses: addresses, | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// Copyright 2022 The Moov Authors | ||
// Use of this source code is governed by an Apache License | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/moov-io/base/log" | ||
"github.com/moov-io/watchman/pkg/ofac" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
cryptoSearcher = newSearcher(log.NewNopLogger(), noLogPipeliner, 1) | ||
) | ||
|
||
func init() { | ||
// Set SDN Comments | ||
ofacResults, err := ofac.Read(filepath.Join("..", "..", "test", "testdata", "sdn_comments.csv")) | ||
if err != nil { | ||
panic(fmt.Sprintf("ERROR reading sdn_comments.csv: %v", err)) | ||
} | ||
|
||
cryptoSearcher.SDNComments = ofacResults.SDNComments | ||
cryptoSearcher.SDNs = precomputeSDNs([]*ofac.SDN{ | ||
{ | ||
EntityID: "39796", // matches TestSearchCrypto | ||
SDNName: "Person A", | ||
SDNType: "individual", | ||
Title: "Guy or Girl doing crypto stuff", | ||
}, | ||
}, nil, noLogPipeliner) | ||
} | ||
|
||
func TestSearchCryptoSetup(t *testing.T) { | ||
require.Len(t, cryptoSearcher.SDNComments, 13) | ||
require.Len(t, cryptoSearcher.SDNs, 1) | ||
} | ||
|
||
type expectedCryptoAddressSearchResult struct { | ||
OFAC []SDNWithDigitalCurrencyAddress `json:"ofac"` | ||
} | ||
|
||
func TestSearchCrypto(t *testing.T) { | ||
router := mux.NewRouter() | ||
addSearchRoutes(log.NewNopLogger(), router, cryptoSearcher) | ||
|
||
w := httptest.NewRecorder() | ||
req := httptest.NewRequest("GET", "/crypto?address=0x242654336ca2205714071898f67E254EB49ACdCe", nil) | ||
router.ServeHTTP(w, req) | ||
w.Flush() | ||
require.Equal(t, http.StatusOK, w.Code) | ||
|
||
var response expectedCryptoAddressSearchResult | ||
err := json.NewDecoder(w.Body).Decode(&response) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, response.OFAC, 1) | ||
require.Equal(t, "39796", response.OFAC[0].SDN.EntityID) | ||
|
||
// Now with cryptocurrency name specified | ||
req = httptest.NewRequest("GET", "/crypto?name=ETH&address=0x242654336ca2205714071898f67E254EB49ACdCe", nil) | ||
router.ServeHTTP(w, req) | ||
w.Flush() | ||
require.Equal(t, http.StatusOK, w.Code) | ||
|
||
err = json.NewDecoder(w.Body).Decode(&response) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, response.OFAC, 1) | ||
require.Equal(t, "39796", response.OFAC[0].SDN.EntityID) | ||
|
||
// With wrong cryptocurrency name | ||
req = httptest.NewRequest("GET", "/crypto?name=QRR&address=0x242654336ca2205714071898f67E254EB49ACdCe", nil) | ||
router.ServeHTTP(w, req) | ||
w.Flush() | ||
require.Equal(t, http.StatusOK, w.Code) | ||
|
||
err = json.NewDecoder(w.Body).Decode(&response) | ||
require.NoError(t, err) | ||
|
||
require.Len(t, response.OFAC, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.