sonic-868 commited on
Commit
9468e63
·
1 Parent(s): 0562ced

Upload 5 files

Browse files
Files changed (5) hide show
  1. Dockerfile +21 -0
  2. Readme.md +21 -0
  3. app.py +52 -0
  4. requirements.txt +3 -0
  5. static/index.html +33 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python base image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory inside the container
5
+ WORKDIR /app
6
+
7
+ # Copy the requirements file to the container
8
+ COPY requirements.txt .
9
+
10
+ # Install the project dependencies
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Copy the application files to the container
14
+ COPY app.py .
15
+ COPY static static
16
+
17
+ # Expose the port on which the application will run
18
+ EXPOSE 8000
19
+
20
+ # Set the entry point command to run the FastAPI application
21
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--workers", "1"]
Readme.md ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .
2
+
3
+ ## Get Started
4
+
5
+ If you want to run a "f4_address_converter" docker project, please follow the steps below
6
+
7
+ ## Build Image
8
+
9
+ ###
10
+
11
+ ```shell
12
+ docker build -t f4_address_converter .
13
+ ```
14
+
15
+ ## Start a instance
16
+
17
+ ```shell
18
+ docker run -p 8000:8000 f4_address_converter
19
+ ```
20
+
21
+ Open the web page at http://0.0.0.0:8000
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+
3
+ import uvicorn
4
+
5
+ from fastapi.staticfiles import StaticFiles
6
+ from fastapi import FastAPI
7
+ from fastapi.responses import FileResponse
8
+
9
+ app = FastAPI()
10
+
11
+
12
+ def add_base32_padding(encoded_text):
13
+ padding_needed = 8 - (len(encoded_text) % 8)
14
+ return encoded_text + '=' * padding_needed
15
+
16
+
17
+ def remove_f4_prefix(encoded_text):
18
+ prefix = "f410f"
19
+ if encoded_text.startswith(prefix):
20
+ return encoded_text[len(prefix):]
21
+ else:
22
+ return None
23
+
24
+
25
+ @app.get("/", response_class=FileResponse)
26
+ def read_root():
27
+ return "./static/index.html"
28
+
29
+
30
+ @app.get("/convert_address_f4_0X")
31
+ def convert_address_f4_0X(f4Address: str):
32
+ print(f4Address)
33
+ if f4Address is not None and len(f4Address) == 44:
34
+ encoded_text = remove_f4_prefix(f4Address)
35
+ encoded_text_with_padding = add_base32_padding(encoded_text.upper())
36
+ decoded_bytes = base64.b32decode(encoded_text_with_padding)
37
+
38
+ # Discard the last 4 bytes
39
+ decoded_bytes = decoded_bytes[:-4]
40
+
41
+ hex_string = "0x" + decoded_bytes.hex()
42
+
43
+ return {"fevm address": hex_string}
44
+ else:
45
+ return {"error": "Incorrect f4 address format"}
46
+
47
+
48
+
49
+ app.mount("/", StaticFiles(directory="static"), name="static")
50
+
51
+ if __name__ == "__main__":
52
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ fastapi==0.74.*
2
+ requests==2.27.*
3
+ uvicorn[standard]==0.17.*
static/index.html ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>F4 to FEVM Converter</title>
5
+ <script>
6
+ function convertAddress() {
7
+ var f4Address = document.getElementById("f4Address").value;
8
+
9
+ // Make a GET request to the FastAPI endpoint
10
+ fetch("/convert_address_f4_0X?f4Address=" + f4Address)
11
+ .then(response => response.json())
12
+ .then(data => {
13
+ if (data.error) {
14
+ document.getElementById("fevmAddress").innerHTML = "Error: " + data.error;
15
+ } else {
16
+ document.getElementById("fevmAddress").innerHTML = "FEVM Address: " + data["fevm address"];
17
+ }
18
+ })
19
+ .catch(error => {
20
+ document.getElementById("fevmAddress").innerHTML = "Error: " + error;
21
+ });
22
+ }
23
+ </script>
24
+ </head>
25
+ <body>
26
+ <h1>F4 to FEVM Converter</h1>
27
+ <label for="f4Address">Enter F4 Address:</label>
28
+ <input type="text" id="f4Address" name="f4Address">
29
+ <button onclick="convertAddress()">Convert</button>
30
+ <br>
31
+ <div id="fevmAddress"></div>
32
+ </body>
33
+ </html>