forked from chumschat/eversite-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheversite.py
More file actions
62 lines (48 loc) · 2.15 KB
/
eversite.py
File metadata and controls
62 lines (48 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import nekoton as nt
from typing import Optional
class EverSite:
def __init__(self, transport: nt.Transport, address: nt.Address, keypair: nt.KeyPair):
self._address = address
self._transport = transport
self._keypair = keypair
with open("abi/eversite.abi.json") as f:
eversite_abi_text = f.read()
eversite_abi = nt.ContractAbi(eversite_abi_text)
self._upload = eversite_abi.get_function("upload")
assert self._upload is not None
self._getDetails = eversite_abi.get_function("getDetails")
assert self._getDetails is not None
self._owner = eversite_abi.get_function("owner")
assert self._owner is not None
async def _get_account_state(self) -> Optional[nt.AccountState]:
return await self._transport.get_account_state(self._address)
async def get_details(self) -> Optional[nt.ExecutionOutput]:
state = await self._get_account_state()
if state is None:
return None
else:
return self._getDetails.call(state, input={})
async def get_owner(self) -> Optional[nt.PublicKey]:
state = await self._get_account_state()
if state is None:
return None
else:
public_key_int = self._owner.call(state, input={}).output["owner"]
return nt.PublicKey.from_int(public_key_int)
async def build_upload(self, content: nt.Cell, index: int) -> nt.Cell:
input_params = {"index": index, "part": content}
return self._upload.encode_internal_input(input_params)
async def send_chunk(self, content: nt.Cell, index: int) -> nt.Transaction:
signature_id = await self._transport.get_signature_id()
external_message = self._upload.encode_external_message(
self._address,
input={
"index": index,
"part": content
},
public_key=self._keypair.public_key
).sign(self._keypair, signature_id)
tx = await self._transport.send_external_message(external_message)
if tx is None:
raise RuntimeError("Message expired")
return tx