-
Notifications
You must be signed in to change notification settings - Fork 46
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 #357 from aergoio/topic/blacklist
add blacklist
- Loading branch information
Showing
5 changed files
with
108 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package blacklist | ||
|
||
import ( | ||
"github.com/aergoio/aergo/v2/types" | ||
"github.com/aergoio/aergo/v2/internal/common" | ||
"github.com/aergoio/aergo/v2/internal/enc/hex" | ||
) | ||
|
||
type Blacklist struct { | ||
sourcelist []string // account address (b58 encoded like Am...) or id (32 bytes in hex = 64 bytes) | ||
blocked map[string]bool // all above converted to account id (32 bytes) | ||
} | ||
|
||
var globalBlacklist *Blacklist | ||
|
||
// Initialize sets up the blacklist with the given addresses. | ||
// This function should be called only once at the start. | ||
func Initialize(addresses []string) { | ||
conf := &Blacklist{} | ||
conf.sourcelist = make([]string, len(addresses)) | ||
copy(conf.sourcelist, addresses) | ||
conf.blocked = make(map[string]bool) | ||
for _, v := range addresses { | ||
key, err := toKey(v) | ||
if err == nil { | ||
conf.blocked[key] = true | ||
} else { | ||
// Handle invalid address, log or take other actions as needed | ||
} | ||
} | ||
globalBlacklist = conf | ||
} | ||
|
||
func Check(address string) bool { | ||
if globalBlacklist == nil { | ||
return false | ||
} | ||
key, err := toKey(address) | ||
if err != nil { | ||
return false | ||
} | ||
return globalBlacklist.blocked[key] | ||
} | ||
|
||
func toKey(address string) (string, error) { | ||
var key []byte | ||
var err error | ||
if len(address) == 64 { | ||
key, err = hex.Decode(address) | ||
} else { | ||
var addr []byte | ||
addr, err = types.DecodeAddress(address) | ||
if err != nil { | ||
return "", err | ||
} | ||
key = common.Hasher(addr) | ||
} | ||
return string(key), err | ||
} |
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