dev@locazia: make deploy blockchain route

This commit is contained in:
user committed 2024-03-08 00:35:01 +03:00
1 parent 44abce2aae
commit 9859121c40
7 files changed
+218 -13

No files matched your search

+84 -8
View File
@@ -1,6 +1,15 @@
from app.core._blockchain.ton.connect import TonConnect, unpack_wallet_info
from app.core.content.utils import create_metadata_for_item
from app.core.models.node_storage import StoredContent
from sanic import response
from datetime import datetime, timedelta
from app.core._blockchain.ton.platform import platform
from app.core._config import PROJECT_HOST
from base64 import b64encode, b64decode
from base58 import b58decode, b58encode
from app.core._utils.resolve_content import resolve_content
from tonsdk.boc import begin_cell, Cell, begin_dict
from tonsdk.utils import Address
def valid_royalty_params(royalty_params):
@@ -37,17 +46,84 @@ async def s_api_v1_blockchain_send_new_content_message(request):
await ton_connect.restore_connection()
assert ton_connect.connected, "No connected wallet"
encrypted_content_cid, err = resolve_content(request.json['content'])
assert not err, f"Invalid content CID"
encrypted_content = request.ctx.db_session.query(StoredContent).filter(
StoredContent.hash == encrypted_content_cid.content_hash_b58
).first()
assert encrypted_content, "No content locally found"
assert encrypted_content.type == "local/content_bin", "Invalid content type"
image_content_cid = resolve_content(request.json['image'])
assert not err, f"Invalid image CID"
image_content = request.ctx.db_session.query(StoredContent).filter(
StoredContent.hash == image_content_cid.content_hash_b58
).first()
assert image_content, "No image locally found"
# await ton_connect._sdk_client.send_transaction({
# 'valid_until': int(datetime.now().timestamp()),
# 'messages': [
# {
# 'address':
# }
# ]
# })
metadata_content = await create_metadata_for_item(
request.ctx.db_session,
title=request.json['title'],
cover_url=request.json['image'],
authors=request.json['authors']
)
royalties_dict = begin_dict(8)
i = 0
for royalty_param in request.json['royaltyParams']:
royalties_dict.store_ref(
i, begin_cell()
.store_address(Address(royalty_param['address']))
.store_uint(royalty_param['value'], 16)
.end_cell()
)
i += 1
await ton_connect._sdk_client.send_transaction({
'valid_until': int(datetime.now().timestamp() + 120),
'messages': [
{
'address': platform.address.to_string(1, 1, 1),
'amount': str(int(0.15 * 10 ** 9)),
'payload': b64encode(
begin_cell()
.store_uint(0x5491d08c, 32)
.store_uint(int(encrypted_content_cid.content_hash.hex()[2:], 16), 256)
.store_ref(
begin_cell()
.store_ref(
begin_cell()
.store_coins(int(request.json['price']))
.store_coins(int(1000000 * 10 ** 9))
.store_coins(int(5000000 * 10 ** 9))
.end_cell()
)
.store_maybe_ref(royalties_dict.end_dict())
.store_uint(0, 1)
.end_cell()
)
.store_ref(
begin_cell()
.store_ref(
begin_cell()
.store_uint(1, 8)
.store_bytes(f"{PROJECT_HOST}/api/v1/storage/{metadata_content.hash}".encode())
.end_cell()
)
.store_ref(
begin_cell()
.store_ref(begin_cell().store_bytes(f"{encrypted_content_cid.serialize_v1()}".encode()).end_cell())
.store_ref(begin_cell().store_bytes(f"{image_content_cid.serialize_v1()}".encode()).end_cell())
.store_ref(begin_cell().store_bytes(f"{metadata_content.serialize_v1()}".encode()).end_cell())
.end_cell()
)
.end_cell()
)
.end_cell().to_boc(False)
).decode()
}
]
})
return response.json({"message": "Transaction requested"})
+10 -3
View File
@@ -12,8 +12,7 @@ import aiofiles
async def s_api_v1_storage_post(request):
assert request.ctx.user, "No authorized"
if not request.files and not request.json:
if not request.files:
return response.json({"error": "No file provided"}, status=400)
file_param = list(request.files.values())[0][0] if request.files else None
@@ -34,7 +33,8 @@ async def s_api_v1_storage_post(request):
file_meta["extension_encoding"] = file_encoding
try:
file_hash = b58encode(hashlib.sha256(file_content).digest()).decode()
file_hash_bin = hashlib.sha256(file_content).digest()
file_hash = b58encode(file_hash_bin).decode()
stored_content = request.ctx.db_session.query(StoredContent).filter(StoredContent.hash == file_hash).first()
if stored_content:
stored_cid = stored_content.cid.serialize_v1()
@@ -44,6 +44,13 @@ async def s_api_v1_storage_post(request):
"content_url": f"dmy://storage?cid={stored_cid}",
})
if request.ctx.user:
pass
elif request.ctx.verified_hash:
assert request.ctx.verified_hash == file_hash_bin, "Invalid service request hash"
else:
return response.json({"error": "Unauthorized"}, status=401)
new_content = StoredContent(
type="local/content_bin",
user_id=request.ctx.user.id,