Skip to content

Commit

Permalink
Add logic to output ofac-sanctioned-digital-currency-addresses.json (#81
Browse files Browse the repository at this point in the history
)

* Add logic to output ofac-sanctioned-digital-currency-addresses.json

* Don't store by chain ID (or even CoinType)

We don't know which chain IDs or coinTypes will be added by OFAC
upstream, and it's difficult given an arbitrary symbol to identify which
chain ID / CoinType it is. So it's easier to just put all the banned
addresses in a flat namespace. There's only 540 so it's not really a
performance hit to check all of them for an Ethereum transaction instead
of just the ETH ones.

* Rename 'bannedAddresses' key to 'addresses'

* Update environment variable name

GITHUB_TOKEN -> API_AUTH_TOKEN_GITHUB

The IT asked for a more descriptive name. Also, apparently you variables
cannot start with the prefix 'GITHUB'.

* Bump version to 1.14.0
  • Loading branch information
nvonpentz authored Oct 17, 2023
1 parent c3dd10e commit ec9e230
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "token-lists",
"version": "1.13.0",
"version": "1.14.0",
"description": "Manages custom token lists for Brave Wallet",
"dependencies": {
"@metamask/contract-metadata": "git+https://[email protected]/MetaMask/contract-metadata.git",
Expand Down
9 changes: 9 additions & 0 deletions scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ async function stageOnRampLists(stagingDir) {
await fsPromises.writeFile(dstOnRampCurrenciesPath, JSON.stringify(onRampCurrencies, null, 2));
}

async function stageOFACLists(stagingDir) {
const ofacLists = await util.fetchGitHubRepoTopLevelFiles('brave-intl', 'ofac-sanctioned-digital-currency-addresses', 'lists');
const dstOfacListsPath = path.join(stagingDir, 'ofac-sanctioned-digital-currency-addresses.json');
await fsPromises.writeFile(dstOfacListsPath, JSON.stringify(ofacLists, null, 2));
}

async function stageTokenPackage() {
const stagingDir = 'build'
if (!fs.existsSync(stagingDir)) {
Expand Down Expand Up @@ -291,6 +297,9 @@ async function stageTokenPackage() {
// Add on ramp JSON files
await stageOnRampLists(stagingDir)

// Add OFAC banned address lists
await stageOFACLists(stagingDir)

stagePackageJson(stagingDir)
stageManifest(stagingDir)
}
Expand Down
55 changes: 54 additions & 1 deletion scripts/util.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,58 @@ const injectCoingeckoIds = (tokensListMap, coingeckoIds) =>
return acc
}, {})

const fetchGitHubFileContent = async (repoOwner, repoName, branch, filePath, githubHeaders) => {
const contentApiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/contents/${filePath}?ref=${branch}`;

const contentResponse = await fetch(contentApiUrl, { headers: githubHeaders });
const contentData = await contentResponse.json();

if (!contentData.content) {
console.error('Failed to fetch content for file:', filePath, contentData.message || 'Unknown error');
return '';
}

return Buffer.from(contentData.content, 'base64').toString('utf-8');
};

const fetchGitHubRepoTopLevelFiles = async (repoOwner, repoName, branch) => {
const githubToken = process.env.API_AUTH_TOKEN_GITHUB;
const githubHeaders = {
"Accept": "application/vnd.github.v3+json",
"Authorization": `token ${githubToken}`
};

const apiUrl = `https://api.github.com/repos/${repoOwner}/${repoName}/git/trees/${branch}?recursive=1`;
const response = await fetch(apiUrl, { headers: githubHeaders });
const data = await response.json();

if (!data.tree) {
console.error('Failed to fetch files:', data.message);
return;
}

const jsonFiles = [];
for (const item of data.tree) {
if (item.type === 'blob' &&
!item.path.includes('/') &&
item.path.endsWith('.json') &&
item.path !== 'README.md') {
jsonFiles.push(item);
}
}

let bannedAddresses = []
for (const file of jsonFiles) {
const content = await fetchGitHubFileContent(repoOwner, repoName, branch, file.path, githubHeaders);
const bannedAddressesForFile = JSON.parse(content);
bannedAddresses = [...bannedAddressesForFile, ...bannedAddresses]
}
bannedAddresses = [...new Set(bannedAddresses)]

return {"addresses": bannedAddresses}
}


module.exports = {
contractReplaceSvgToPng,
contractAddExtraMainnetAssets,
Expand All @@ -685,5 +737,6 @@ module.exports = {
addSupportedSardineCurrencies,
generateCoingeckoIds,
generateChainList,
injectCoingeckoIds
injectCoingeckoIds,
fetchGitHubRepoTopLevelFiles
}

0 comments on commit ec9e230

Please sign in to comment.