-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathofac.go
95 lines (83 loc) · 1.83 KB
/
ofac.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package chain
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
)
const (
DefaultOfacListURL = "https://raw.githubusercontent.com/InjectiveLabs/injective-lists/refs/heads/master/json/wallets/ofacAndRestricted.json"
)
var (
OfacListPath = "injective_data"
OfacListFilename = "ofac.json"
)
type OfacChecker struct {
ofacListPath string
ofacList map[string]bool
}
func NewOfacChecker() (*OfacChecker, error) {
checker := &OfacChecker{
ofacListPath: GetOfacListPath(),
}
if _, err := os.Stat(checker.ofacListPath); os.IsNotExist(err) {
if err := DownloadOfacList(); err != nil {
return nil, err
}
}
if err := checker.loadOfacList(); err != nil {
return nil, err
}
return checker, nil
}
func GetOfacListPath() string {
return path.Join(OfacListPath, OfacListFilename)
}
func DownloadOfacList() error {
resp, err := http.Get(DefaultOfacListURL)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download OFAC list, status code: %d", resp.StatusCode)
}
if err := os.MkdirAll(OfacListPath, 0755); err != nil { // nolint:gocritic // 0755 is the correct permission
return err
}
outFile, err := os.Create(GetOfacListPath())
if err != nil {
return err
}
defer outFile.Close()
_, err = io.Copy(outFile, resp.Body)
if err != nil {
return err
}
_, err = outFile.WriteString("\n")
if err != nil {
return err
}
return nil
}
func (oc *OfacChecker) loadOfacList() error {
file, err := os.ReadFile(oc.ofacListPath)
if err != nil {
return err
}
var list []string
err = json.Unmarshal(file, &list)
if err != nil {
return err
}
oc.ofacList = make(map[string]bool)
for _, item := range list {
oc.ofacList[item] = true
}
return nil
}
func (oc *OfacChecker) IsBlacklisted(address string) bool {
return oc.ofacList[address]
}