dev@locazia: make deploy blockchain route
This commit is contained in:
1 parent
44abce2aae
commit
9859121c40
7 files changed
+218
-13
No files matched your search
@@ -1,7 +1,7 @@
|
||||
from app.core.content.content_id import ContentId
|
||||
|
||||
|
||||
def resolve_content(content_id): # -> [content, error]
|
||||
def resolve_content(content_id) -> ContentId: # -> [content, error]
|
||||
try:
|
||||
return ContentId.deserialize(content_id), None
|
||||
except BaseException as e:
|
||||
|
||||
@@ -17,6 +17,10 @@ class ContentId:
|
||||
|
||||
self.accept_type = accept_type
|
||||
|
||||
@property
|
||||
def content_hash_b58(self) -> str:
|
||||
return b58encode(self.content_hash).decode()
|
||||
|
||||
def serialize_v1(self) -> str:
|
||||
at_bin = string_to_bytes_fixed_size(self.accept_type, 15)
|
||||
assert len(self.content_hash) == 32, "Invalid hash length"
|
||||
|
||||
@@ -1,6 +1,64 @@
|
||||
from app.core.models.node_storage import StoredContent
|
||||
from app.core._crypto.signer import Signer
|
||||
from app.core._secrets import hot_seed
|
||||
from hashlib import sha256
|
||||
from base58 import b58encode, b58decode
|
||||
|
||||
from app.core._config import PROJECT_HOST
|
||||
from httpx import AsyncClient
|
||||
import json
|
||||
|
||||
|
||||
async def create_metadata_for_item(**kwargs):
|
||||
async def create_metadata_for_item(
|
||||
db_session,
|
||||
title: str = None,
|
||||
cover_url: str = None,
|
||||
authors: list = None
|
||||
) -> StoredContent:
|
||||
assert title, "No title provided"
|
||||
assert cover_url, "No cover_url provided"
|
||||
assert len(title) > 3, "Title too short"
|
||||
title = title[:100].strip()
|
||||
|
||||
item_metadata = {
|
||||
'name': title,
|
||||
'description': '@MY Content Ownership Proof NFT',
|
||||
'attributes': [
|
||||
# {
|
||||
# 'trait_type': 'Artist',
|
||||
# 'value': 'Unknown'
|
||||
# },
|
||||
]
|
||||
}
|
||||
if cover_url:
|
||||
item_metadata['image'] = cover_url
|
||||
|
||||
item_metadata['authors'] = [
|
||||
''.join([_a_ch for _a_ch in _a if len(_a_ch.encode()) == 1]) for _a in (authors or [])[:500]
|
||||
]
|
||||
|
||||
signer = Signer(hot_seed)
|
||||
|
||||
# Upload file
|
||||
metadata_bin = json.dumps(item_metadata).encode()
|
||||
metadata_hash = sha256(metadata_bin).digest()
|
||||
metadata_hash_b58 = b58encode(metadata_hash).decode()
|
||||
|
||||
async with AsyncClient() as client:
|
||||
response = await client.post(
|
||||
f"{PROJECT_HOST}/api/v1/storage",
|
||||
files={"file": ('metadata.json', metadata_bin, 'application/json')},
|
||||
headers={
|
||||
'X-Service-Signature': signer.sign(metadata_bin),
|
||||
'X-Message-Hash': metadata_hash_b58,
|
||||
}
|
||||
)
|
||||
assert response.status_code == 200
|
||||
response_json = response.json()
|
||||
metadata_sha256 = response_json['content_sha256']
|
||||
|
||||
metadata_content = db_session.query(StoredContent).filter(StoredContent.hash == metadata_sha256).first()
|
||||
if metadata_content:
|
||||
return metadata_content
|
||||
|
||||
raise Exception("Metadata not created")
|
||||
Reference in new issue
Block a user