-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ofac * ofac * adds log * imports * space * decode * fix test * refactor * tests work * ofac * ethereum side * decode message * parachain side * finish off relayers ofac * revert testing change * fixes * return err * return early * add log
- Loading branch information
1 parent
6d11768
commit e87ddb2
Showing
15 changed files
with
608 additions
and
10 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,33 @@ | ||
package parachain | ||
|
||
import ( | ||
"encoding/hex" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/decred/base58" | ||
"golang.org/x/crypto/blake2b" | ||
) | ||
|
||
func SS58Encode(pubKeyHex string, ss58Prefix uint8) (string, error) { | ||
if strings.HasPrefix(pubKeyHex, "0x") { | ||
pubKeyHex = pubKeyHex[2:] | ||
} | ||
|
||
pubKey, err := hex.DecodeString(pubKeyHex) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to decode hex: %w", err) | ||
} | ||
|
||
address := append([]byte{ss58Prefix}, pubKey...) | ||
|
||
hashInput := append([]byte("SS58PRE"), address...) | ||
|
||
hash := blake2b.Sum512(hashInput) | ||
checksum := hash[:2] | ||
|
||
fullAddress := append(address, checksum...) | ||
|
||
ss58Addr := base58.Encode(fullAddress) | ||
return ss58Addr, nil | ||
} |
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,14 @@ | ||
package parachain | ||
|
||
import ( | ||
assert "github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestSS58Prefix(t *testing.T) { | ||
address := "0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48" | ||
|
||
ss58Address, err := SS58Encode(address, 1) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "A1k3praCLftTgBTb6aVavh3UNKwXN599Fqov17MkEy6bwCU", ss58Address) | ||
} |
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,33 @@ | ||
package parachain | ||
|
||
import ( | ||
"testing" | ||
|
||
gethCommon "github.com/ethereum/go-ethereum/common" | ||
assert "github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetDestination(t *testing.T) { | ||
registerTokenPayload := "00a736aa000000000000774667629726ec1fabebcec0d9139bd1c8f72a2300e87648170000000000000000000000" | ||
decodePayloadAndCompareDestinationAddress(t, registerTokenPayload, "") // register token does not have a destination | ||
|
||
sendTokenPayload := "00a736aa000000000001774667629726ec1fabebcec0d9139bd1c8f72a23008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a4800c16ff2862300000000000000000000e87648170000000000000000000000" | ||
bobAddress := "0x8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48" | ||
decodePayloadAndCompareDestinationAddress(t, sendTokenPayload, bobAddress) | ||
|
||
sendTokenToPayload := "00a736aa000000000001774667629726ec1fabebcec0d9139bd1c8f72a2301d00700001cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c00286bee000000000000000000000000000064a7b3b6e00d000000000000000000e87648170000000000000000000000" | ||
ferdieAddress := "0x1cbd2d43530a44705ad088af313e18f80b53ef16b36177cd4b77b846f2a5f07c" | ||
decodePayloadAndCompareDestinationAddress(t, sendTokenToPayload, ferdieAddress) | ||
|
||
sendNativeTokenPayload := "00a736aa0000000000022121cfe35065c0c33465fbada265f08e9613428a4b9eb4bb717cd7db2abf622e008eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48065cd1d00000000000000000000000000e87648170000000000000000000000" | ||
decodePayloadAndCompareDestinationAddress(t, sendNativeTokenPayload, bobAddress) | ||
} | ||
|
||
func decodePayloadAndCompareDestinationAddress(t *testing.T, payload, expectedAddress string) { | ||
data := gethCommon.Hex2Bytes(payload) | ||
|
||
destination, err := GetDestination(data) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, expectedAddress, destination) | ||
} |
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,89 @@ | ||
package ofac | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type OFAC struct { | ||
enabled bool | ||
apiKey string | ||
} | ||
|
||
type Response struct { | ||
Identifications []struct { | ||
Category string `json:"category"` | ||
Name string `json:"name"` | ||
Description string `json:"description"` | ||
URL string `json:"url"` | ||
} `json:"identifications"` | ||
} | ||
|
||
func New(enabled bool, apiKey string) *OFAC { | ||
return &OFAC{enabled, apiKey} | ||
} | ||
|
||
func (o OFAC) IsBanned(source, destination string) (bool, error) { | ||
if !o.enabled { | ||
return false, nil | ||
} | ||
|
||
if source != "" { | ||
isSourcedBanned, err := o.isOFACListed(source) | ||
if err != nil { | ||
return true, err | ||
} | ||
if isSourcedBanned { | ||
log.WithField("source", source).Warn("found ofac banned source address") | ||
return true, nil | ||
} | ||
} | ||
|
||
if destination != "" { | ||
isDestinationBanned, err := o.isOFACListed(destination) | ||
if err != nil { | ||
return true, err | ||
} | ||
if isDestinationBanned { | ||
log.WithField("destination", destination).Warn("found ofac banned destination address") | ||
return true, nil | ||
} | ||
} | ||
|
||
return false, nil | ||
} | ||
|
||
func (o OFAC) isOFACListed(address string) (bool, error) { | ||
client := &http.Client{} | ||
|
||
req, err := http.NewRequest("GET", fmt.Sprintf("https://public.chainalysis.com/api/v1/address/%s", address), nil) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
req.Header.Add("Accept", "application/json") | ||
req.Header.Add("X-API-Key", o.apiKey) | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
return true, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
var response Response | ||
err = json.Unmarshal(body, &response) | ||
if err != nil { | ||
return true, err | ||
} | ||
|
||
return len(response.Identifications) > 0, nil | ||
} |
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.