-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
212 additions
and
2 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,85 @@ | ||
package chain | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
const ( | ||
defaultOfacListURL = "https://raw.githubusercontent.com/InjectiveLabs/injective-lists/master/wallets/ofac.json" | ||
defaultofacListFilename = "ofac.json" | ||
) | ||
|
||
type OfacChecker struct { | ||
ofacListPath string | ||
ofacList map[string]interface{} | ||
} | ||
|
||
func NewOfacChecker() (*OfacChecker, error) { | ||
checker := &OfacChecker{ | ||
ofacListPath: getOfacListPath(), | ||
} | ||
if _, err := os.Stat(checker.ofacListPath); os.IsNotExist(err) { | ||
if err := checker.downloadOfacList(); err != nil { | ||
return nil, err | ||
} | ||
} | ||
if err := checker.loadOfacList(); err != nil { | ||
return nil, err | ||
} | ||
return checker, nil | ||
} | ||
|
||
func getOfacListPath() string { | ||
currentDirectory, _ := os.Getwd() | ||
for !strings.HasSuffix(currentDirectory, "sdk-go") { | ||
currentDirectory = filepath.Dir(currentDirectory) | ||
} | ||
return filepath.Join(currentDirectory, defaultofacListFilename) | ||
} | ||
|
||
func (oc *OfacChecker) downloadOfacList() error { | ||
resp, err := http.Get(defaultOfacListURL) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return errors.New("failed to download OFAC list") | ||
} | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := os.WriteFile(oc.ofacListPath, body, 0644); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (oc *OfacChecker) loadOfacList() error { | ||
file, err := os.ReadFile(oc.ofacListPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = json.Unmarshal(file, &oc.ofacList) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (oc *OfacChecker) IsBlacklisted(address string) bool { | ||
_, exists := oc.ofacList[address] | ||
return exists | ||
} |
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