Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use urllib.request instead of requests #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions check-btc-supply-chain.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,51 @@
"""

import glob
import json
import requests
import hashlib
import json
import os
import urllib.request
from urllib.parse import urlparse

from packaging import version

json_files = []
json_files += glob.glob("db/wallets/*.json")
json_files += glob.glob("db/full-nodes/*.json")


for i in json_files:
wallet = json.load(open(i))
versions = wallet["versions"]

last_version = sorted([v['version_number'] for v in versions], key=lambda x: version.parse(x))[-1]

for v in versions:
# we only CI the last added version
if v['version_number'] != last_version: continue

for source in v['sources']:
a = urlparse(source)
fname = os.path.basename(a.path)
local_file = "target/"+fname
if not os.path.exists(local_file):
r = requests.get(source, allow_redirects=True)
if r.status_code == 404:
raise Exception("HTTP 404 on "+source)
open(local_file, 'wb').write(r.content)

# now we have the file locally
# computing the sha256
sha256 = hashlib.sha256(open(local_file, 'rb').read())

if sha256.hexdigest() != v['sha256']:
raise Exception("Under attack! SHA256 does not match " + source)

print(source + ": verified")



last_version = sorted(
[v["version_number"] for v in versions], key=lambda x: version.parse(x)
)[-1]

for v in versions:
# we only CI the last added version
if v["version_number"] != last_version:
continue

for source in v["sources"]:
a = urlparse(source)
fname = os.path.basename(a.path)
local_file = "target/" + fname
if not os.path.exists(local_file):

# Identify this user-agent so server does not reject
opener = urllib.request.build_opener()
opener.addheaders = [("User-Agent", "CheckBTC/1.0")]
urllib.request.install_opener(opener)
r = urllib.request.urlopen(source)
if r.getcode() == 404:
raise Exception("HTTP 404 on " + source)
open(local_file, "wb").write(r.read())

# now we have the file locally
# computing the sha256
sha256 = hashlib.sha256(open(local_file, "rb").read())

if sha256.hexdigest() != v["sha256"]:
raise Exception("Under attack! SHA256 does not match " + source)

print(source + ": verified")
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
packaging
requests