Skip to content

Commit

Permalink
Update and modify files for the project
Browse files Browse the repository at this point in the history
- Updated files:
  - src/bee_py/bee.py
  - src/bee_py/chunk/soc.py
  - src/bee_py/feed/feed.py
  - src/bee_py/feed/identifiers.py
  - src/bee_py/feed/retrievable.py
  - src/bee_py/modules/bytes.py
  - src/bee_py/modules/bzz.py
  - src/bee_py/modules/chunk.py
  - src/bee_py/modules/debug/balance.py
  - src/bee_py/modules/debug/chequebook.py
  - src/bee_py/modules/debug/connectivity.py
  - src/bee_py/modules/debug/settlements.py
  - src/bee_py/modules/debug/stake.py
  - src/bee_py/modules/debug/stamps.py
  - src/bee_py/modules/debug/states.py
  - src/bee_py/modules/debug/status.py
  - src/bee_py/modules/debug/tag.py
  - src/bee_py/modules/debug/transactions.py
  - src/bee_py/modules/feed.py
  - src/bee_py/modules/pinning.py
  - src/bee_py/modules/pss.py
  - src/bee_py/modules/soc.py
  - src/bee_py/modules/status.py
  - src/bee_py/modules/stewardship.py
  - src/bee_py/modules/tag.py
  - src/bee_py/types/type.py
  - src/bee_py/utils/bytes.py
  - src/bee_py/utils/type.py
  - tests/unit/utils/test_eth.py
  • Loading branch information
Aviksaikat committed Jan 5, 2024
1 parent 0ba3f51 commit 353f9f7
Show file tree
Hide file tree
Showing 29 changed files with 296 additions and 186 deletions.
94 changes: 60 additions & 34 deletions src/bee_py/bee.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Optional, Union

import websockets
from ape.managers.accounts import AccountAPI
from ape.types import AddressType
from eth_pydantic_types import HexBytes
from requests import HTTPError
Expand Down Expand Up @@ -160,21 +161,21 @@ def __get_request_options_for_call(
"""
if options:
if isinstance(options, (JsonFeedOptions, BeeRequestOptions, AllTagsOptions)):
options = options.model_dump()
options = options.model_dump() # type: ignore
if isinstance(
self.request_options,
(JsonFeedOptions, BeeRequestOptions, AllTagsOptions),
):
self.request_options = self.request_options.model_dump()
return {**self.request_options, **options}
self.request_options = self.request_options.model_dump() # type: ignore
return {**self.request_options, **options} # type: ignore
else:
return self.request_options

def __make_feed_reader(
self,
feed_type: Union[FeedType, str],
topic: Union[bytes, str],
owner: Union[AddressType, str],
topic: Union[bytes, str, Topic],
owner: Union[AddressType, str, bytes],
options: Optional[BeeRequestOptions] = None,
) -> FeedReader:
"""
Expand All @@ -194,7 +195,10 @@ def __make_feed_reader(
assert_request_options(options)

canonical_topic = make_topic(topic)
canonical_owner = make_hex_eth_address(owner).hex()
canonical_owner = make_hex_eth_address(owner)

if isinstance(canonical_owner, HexBytes):
canonical_owner = canonical_owner.hex()

return _make_feed_reader(
self.__get_request_options_for_call(options),
Expand All @@ -203,7 +207,7 @@ def __make_feed_reader(
canonical_owner,
)

def __resolve_signer(self, signer: Optional[Union[Signer, bytes, str]] = None) -> Signer:
def __resolve_signer(self, signer: Optional[Union[Signer, bytes, str]] = None) -> Union[Signer, AccountAPI]:
"""
Resolves the signer to be used.
Expand All @@ -226,7 +230,7 @@ def __resolve_signer(self, signer: Optional[Union[Signer, bytes, str]] = None) -
msg = "Signer Error"
raise BeeArgumentError(msg, signer)

def make_feed_topic(self, topic: str) -> Topic:
def make_feed_topic(self, topic: Union[Topic, bytes, str]) -> Topic:
"""
Make a new feed topic from a string
Expand All @@ -238,6 +242,10 @@ def make_feed_topic(self, topic: str) -> Topic:
Returns:
hashed topic data
"""
if isinstance(topic, Topic):
topic = topic.value
if isinstance(topic, bytes):
topic = topic.decode()

return make_topic_from_string(topic)

Expand Down Expand Up @@ -470,7 +478,7 @@ def download_readable_file(
self,
reference: ReferenceCidOrENS,
path: str = "",
options: BeeRequestOptions = None,
options: Optional[Union[BeeRequestOptions, dict]] = None,
) -> FileData:
"""
Downloads a single file as a readable stream.
Expand All @@ -492,7 +500,7 @@ def download_readable_file(
assert_reference_or_ens(reference)
reference = make_reference_or_ens(reference, ReferenceType.MANIFEST)

return bzz_api.download_file_readable(self.__get_request_options_for_call(options), reference, path)
return bzz_api.download_file_readable(self.__get_request_options_for_call(options), reference, path) # type: ignore # noqa: 501

def upload_files(
self,
Expand Down Expand Up @@ -526,7 +534,7 @@ def upload_files(
assert_collection_upload_options(options)
assert_file_upload_options(options)

data = make_collection_from_file_list(file_list)
data = make_collection_from_file_list(file_list) # type: ignore

upload_result = bzz_api.upload_collection(
self.__get_request_options_for_call(request_options),
Expand Down Expand Up @@ -636,7 +644,7 @@ def create_tag(self, options: Optional[BeeRequestOptions] = None) -> Tag:

return tag_api.create_tag(self.__get_request_options_for_call(options))

def get_all_tags(self, options: Optional[AllTagsOptions] = None) -> list[Tag]:
def get_all_tags(self, options: Optional[Union[AllTagsOptions, dict]] = None) -> list[Tag]:
"""
Fetches all tags from the Bee node.
Expand All @@ -663,17 +671,17 @@ def get_all_tags(self, options: Optional[AllTagsOptions] = None) -> list[Tag]:
if isinstance(options, dict):
options = AllTagsOptions.model_validate(options, strict=True)

if options.offset and options.limit:
if options.offset and options.limit: # type: ignore
return tag_api.get_all_tags(
self.__get_request_options_for_call(options),
options.offset,
options.limit,
self.__get_request_options_for_call(options), # type: ignore
options.offset, # type: ignore
options.limit, # type: ignore
)
elif options.offset:
return tag_api.get_all_tags(self.__get_request_options_for_call(options), options.offset)
elif options.limit:
return tag_api.get_all_tags(self.__get_request_options_for_call(options), options.limit)
return tag_api.get_all_tags(self.__get_request_options_for_call(options))
elif options.offset: # type: ignore
return tag_api.get_all_tags(self.__get_request_options_for_call(options), options.offset) # type: ignore
elif options.limit: # type: ignore
return tag_api.get_all_tags(self.__get_request_options_for_call(options), options.limit) # type: ignore
return tag_api.get_all_tags(self.__get_request_options_for_call(options)) # type: ignore

def retrieve_tag(self, tag_uid: Union[int, Tag], options: Optional[BeeRequestOptions] = None):
"""
Expand Down Expand Up @@ -762,7 +770,7 @@ def update_tag(

tag_uid = make_tag_uid(uid)

return tag_api.update_tag(self.__get_request_options_for_call(request_options), tag_uid)
return tag_api.update_tag(self.__get_request_options_for_call(request_options), tag_uid, reference)

def pin(
self,
Expand Down Expand Up @@ -972,6 +980,7 @@ def is_feed_retrievable(
raise BeeError(msg)

return are_all_sequential_feeds_update_retrievable(
self,
canonical_owner,
canonical_topic,
index,
Expand Down Expand Up @@ -1205,7 +1214,10 @@ def create_feed_manifest(
assert_batch_id(postage_batch_id)

canonical_topic = make_topic(topic)
canonical_owner = make_hex_eth_address(owner).hex()
canonical_owner = make_hex_eth_address(owner)

if isinstance(canonical_owner, HexBytes):
canonical_owner = canonical_owner.hex()

reference = _create_feed_manifest(
self.__get_request_options_for_call(options),
Expand Down Expand Up @@ -1247,13 +1259,15 @@ def make_feed_reader(
assert_request_options(options)

canonical_topic = make_topic(topic).value
canonical_signer = self.__resolve_signer(signer).address
canonical_signer = self.__resolve_signer(signer)

if isinstance(canonical_topic, Signer):
canonical_signer = Signer.signers # type: ignore
else:
canonical_signer = canonical_signer.address # type: ignore

return _make_feed_reader(
self.__get_request_options_for_call(options),
feed_type,
canonical_topic,
canonical_signer,
self.__get_request_options_for_call(options), feed_type, canonical_topic, canonical_signer # type: ignore
)

def make_feed_writer(
Expand Down Expand Up @@ -1286,6 +1300,9 @@ def make_feed_writer(
canonical_topic = make_topic(topic)
canonical_signer = self.__resolve_signer(signer)

if isinstance(canonical_topic, Signer):
canonical_signer = Signer.signers

return _make_feed_writer(
self.__get_request_options_for_call(options),
feed_type,
Expand Down Expand Up @@ -1339,7 +1356,7 @@ def get_json_feed(
self,
topic: Union[Topic, bytes, str],
options: Optional[Union[JsonFeedOptions, dict]] = None,
) -> dict:
) -> str:
"""
High-level function that allows you to easily get data from feed.
Returned data are parsed using json.loads().
Expand Down Expand Up @@ -1390,17 +1407,21 @@ def get_json_feed(
address = make_eth_address(options.address)
else:
try:
address = self.__resolve_signer(options.signer).address
signer = self.__resolve_signer(options.signer)
if isinstance(signer, Signer):
address = signer.signer.address
else:
address = signer.address
except BeeError as e:
msg = "Either address, signer or default signer has to be specified!"
raise BeeError(msg) from e
reader = self.make_feed_reader(feed_type, hashed_topic, address, options)
reader = self.make_feed_reader(feed_type, hashed_topic, address, options) # type: ignore

return json_api.get_json_data(self, reader)

def make_soc_reader(
self,
owner_address: [AddressType, str, bytes],
owner_address: Union[AddressType, str, bytes],
options: Optional[BeeRequestOptions] = None,
) -> SOCReader:
"""
Expand All @@ -1425,7 +1446,12 @@ def __download(identifier: Identifier):
self.__get_request_options_for_call(options), canonical_owner, identifier
)

return SOCReader(owner=make_hex_eth_address(canonical_owner).hex(), download=__download)
owner = make_hex_eth_address(canonical_owner)

if isinstance(owner, HexBytes):
owner = owner.hex()

return SOCReader(owner=owner, download=__download)

def make_soc_writer(
self,
Expand All @@ -1449,7 +1475,7 @@ def make_soc_writer(

canonical_signer = self.__resolve_signer(signer)
if isinstance(canonical_signer, Signer):
canonical_signer = Signer.singer # type: ignore[attr-defined]
canonical_signer = Signer.signer # type: ignore

reader = self.make_soc_reader(canonical_signer.address, options) # type: ignore[attr-defined]

Expand Down
32 changes: 22 additions & 10 deletions src/bee_py/chunk/soc.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import NewType, Optional, Union

from ape.managers.accounts import AccountAPI
from eth_account import Account
from eth_typing import ChecksumAddress as AddressType
from pydantic import BaseModel, Field

Expand All @@ -18,7 +17,16 @@
from bee_py.chunk.span import SPAN_SIZE
from bee_py.modules.chunk import download
from bee_py.modules.soc import upload
from bee_py.types.type import BatchId, BeeRequestOptions, Data, Reference, Signer, UploadOptions, assert_address
from bee_py.types.type import (
BatchId,
BeeRequestOptions,
Data,
FeedUpdateOptions,
Reference,
Signer,
UploadOptions,
assert_address,
)
from bee_py.utils.bytes import bytes_at_offset, bytes_equal, flex_bytes_at_offset
from bee_py.utils.error import BeeError
from bee_py.utils.hash import keccak256_hash
Expand Down Expand Up @@ -93,7 +101,9 @@ def recover_chunk_owner(data: bytes) -> AddressType:
return owner_address


def make_single_owner_chunk_from_data(data: Union[Data, bytes], address: AddressType) -> SingleOwnerChunk:
def make_single_owner_chunk_from_data(
data: Union[Data, bytes], address: Union[AddressType, bytes, str]
) -> SingleOwnerChunk:
"""
Verifies if the data is a valid single owner chunk.
Expand Down Expand Up @@ -142,15 +152,15 @@ def make_soc_address(identifier: Union[Identifier, bytes], address: Union[Addres
def make_single_owner_chunk(
chunk: Chunk,
identifier: Union[Identifier, bytearray],
signer: Union[AccountAPI, Account],
signer: Union[AccountAPI, Signer],
) -> SingleOwnerChunk:
"""
Creates a single owner chunk object.
Args:
chunk: A chunk object used for the span and payload.
identifier|bytearray: The identifier of the chunk.
signer: The singer interface for signing the chunk.
signer: The signer interface for signing the chunk.
signer can be a ape account API or a eth_account object.
Returns:
Expand All @@ -165,6 +175,8 @@ def make_single_owner_chunk(
assert_valid_chunk_data(chunk.data, chunk_address)

digest = keccak256_hash(identifier, chunk_address)
if isinstance(signer, Signer):
signer = signer.signer

signature = sign(data=digest, account=signer)

Expand Down Expand Up @@ -224,18 +236,18 @@ def upload_single_owner_chunk(

def upload_single_owner_chunk_data(
request_options: BeeRequestOptions,
signer: Signer,
signer: Union[Signer, AccountAPI],
postage_batch_id: BatchId,
identifier: Identifier,
identifier: Union[Identifier, bytes],
data: bytes,
options: Optional[dict] = None,
options: Optional[Union[FeedUpdateOptions, BeeRequestOptions, dict]] = None,
) -> Reference:
"""
Helper function to create and upload SOC.
Args:
request_options: Ky Options for making requests.
signer: The singer interface for signing the chunk.
signer: The signer interface for signing the chunk.
postage_batch_id: Postage BatchId that will be assigned to uploaded data.
identifier: The identifier of the chunk.
data: The chunk data.
Expand All @@ -253,7 +265,7 @@ def upload_single_owner_chunk_data(

def download_single_owner_chunk(
request_options: BeeRequestOptions,
owner_address: AddressType,
owner_address: Union[AddressType, bytes],
identifier: Identifier,
) -> SingleOwnerChunk:
"""Downloads a Single Owner Chunk (SOC) from the Bee network.
Expand Down
Loading

0 comments on commit 353f9f7

Please sign in to comment.