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