Skip to content

Commit 01dc442

Browse files
committed
feat: upload encrypted results to IPFS
1 parent 681488a commit 01dc442

7 files changed

Lines changed: 43 additions & 19 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ __pycache__/
44
*.pyc
55

66
/input/user.json
7-
/output
7+
/output/*
8+
!/output/.gitkeep
89
.env
910

1011
.idea/

output/.gitkeep

Whitespace-only changes.

refiner/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import logging
23
import os
34
import sys
@@ -19,9 +20,12 @@ def run() -> None:
1920
extract_input()
2021

2122
refiner = Refiner()
22-
refiner.transform()
23+
output = refiner.transform()
2324

24-
logging.info("Data transformation complete")
25+
output_path = os.path.join(settings.OUTPUT_DIR, "output.json")
26+
with open(output_path, 'w') as f:
27+
json.dump(output.model_dump(), f, indent=2)
28+
logging.info(f"Data transformation complete: {output}")
2529

2630

2731
def extract_input() -> None:
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from typing import Optional
21
from pydantic import BaseModel
32

4-
class Schema(BaseModel):
3+
class OffChainSchema(BaseModel):
54
name: str
65
version: str
76
description: str

refiner/models/output.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing import Optional
2+
from pydantic import BaseModel
3+
4+
from refiner.models.offchain_schema import OffChainSchema
5+
6+
class Output(BaseModel):
7+
refinement_url: Optional[str] = None
8+
schema: Optional[OffChainSchema] = None

refiner/refine.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@
22
import logging
33
import os
44

5-
from refiner.models.schema import Schema
5+
from refiner.models.offchain_schema import OffChainSchema
6+
from refiner.models.output import Output
67
from refiner.transformer.user_transformer import UserTransformer
78
from refiner.config import settings
8-
from refiner.utils.ipfs import upload_json_to_ipfs
9+
from refiner.utils.encrypt import encrypt_file
10+
from refiner.utils.ipfs import upload_file_to_ipfs, upload_json_to_ipfs
911

1012
class Refiner:
1113
def __init__(self):
1214
self.db_path = os.path.join(settings.OUTPUT_DIR, 'db.libsql')
1315

14-
def transform(self) -> None:
16+
def transform(self) -> Output:
1517
"""Transform all input files into the database."""
1618
logging.info("Starting data transformation")
19+
output = Output()
1720

1821
# Iterate through files and transform data
1922
for input_filename in os.listdir(settings.INPUT_DIR):
@@ -28,20 +31,28 @@ def transform(self) -> None:
2831
transformer.process(input_data)
2932
logging.info(f"Transformed {input_filename}")
3033

31-
# Create a schema file
34+
# Create a schema based on the SQLAlchemy schema
35+
schema = OffChainSchema(
36+
name=settings.SCHEMA_NAME,
37+
version=settings.SCHEMA_VERSION,
38+
description=settings.SCHEMA_DESCRIPTION,
39+
dialect=settings.SCHEMA_DIALECT,
40+
schema=transformer.get_schema()
41+
)
42+
output.schema = schema
43+
44+
# Upload the schema to IPFS
3245
schema_file = os.path.join(settings.OUTPUT_DIR, 'schema.json')
3346
with open(schema_file, 'w') as f:
34-
schema = Schema(
35-
name=settings.SCHEMA_NAME,
36-
version=settings.SCHEMA_VERSION,
37-
description=settings.SCHEMA_DESCRIPTION,
38-
dialect=settings.SCHEMA_DIALECT,
39-
schema=transformer.get_schema()
40-
)
4147
json.dump(schema.model_dump(), f, indent=4)
42-
logging.info(f"Schema saved to {schema_file}")
43-
# ipfs_hash = upload_json_to_ipfs(schema.model_dump())
44-
# logging.info(f"Schema uploaded to IPFS with hash: {ipfs_hash}")
48+
schema_ipfs_hash = upload_json_to_ipfs(schema.model_dump())
49+
logging.info(f"Schema uploaded to IPFS with hash: {schema_ipfs_hash}")
50+
51+
# Encrypt and upload the database to IPFS
52+
encrypted_path = encrypt_file(settings.REFINEMENT_ENCRYPTION_KEY, self.db_path)
53+
ipfs_hash = upload_file_to_ipfs(encrypted_path)
54+
output.refinement_url = f"https://ipfs.vana.org/ipfs/{ipfs_hash}"
4555
continue
4656

4757
logging.info("Data transformation completed successfully")
58+
return output

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pgpy
22
pydantic
33
pydantic_settings
4+
requests
45
sqlalchemy

0 commit comments

Comments
 (0)