Skip to content

Commit 366b181

Browse files
Box name decode with utf-8 should be optional (#153)
* feat: making decode of a box name with utf-8 fallback in case it's not correctly encoded
1 parent a41fcef commit 366b181

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

docs/markdown/autoapi/algokit_utils/applications/app_manager/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ Get the local state for an account in an application.
133133

134134
Get names of all boxes for an application.
135135

136+
If the box name can’t be decoded from UTF-8, the string representation of the bytes is returned.
137+
136138
* **Parameters:**
137139
**app_id** – The application ID
138140
* **Returns:**

docs/markdown/autoapi/algokit_utils/models/state/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ The name of the box
2222

2323
#### name *: str*
2424

25-
The name of the box as a string
25+
The name of the box as a string.
26+
If the name can’t be decoded from UTF-8, the string representation of the bytes is returned instead.
2627

2728
#### name_raw *: bytes*
2829

src/algokit_utils/applications/app_manager.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ def get_local_state(self, app_id: int, address: str) -> dict[str, AppState]:
261261
def get_box_names(self, app_id: int) -> list[BoxName]:
262262
"""Get names of all boxes for an application.
263263
264+
If the box name can't be decoded from UTF-8, the string representation of the bytes is returned.
265+
264266
:param app_id: The application ID
265267
:return: List of box names
266268
@@ -270,13 +272,20 @@ def get_box_names(self, app_id: int) -> list[BoxName]:
270272
>>> box_names = app_manager.get_box_names(app_id)
271273
"""
272274

275+
def utf8_decode_or_string_cast(b: bytes) -> str:
276+
"""Return the UTF-8 encoding or return the string representation of the bytes."""
277+
try:
278+
return b.decode("utf-8")
279+
except UnicodeDecodeError:
280+
return str(b)
281+
273282
box_result = self._algod.application_boxes(app_id)
274283
assert isinstance(box_result, dict)
275284
return [
276285
BoxName(
277286
name_raw=base64.b64decode(b["name"]),
278287
name_base64=b["name"],
279-
name=base64.b64decode(b["name"]).decode("utf-8"),
288+
name=utf8_decode_or_string_cast(base64.b64decode(b["name"])),
280289
)
281290
for b in box_result["boxes"]
282291
]

src/algokit_utils/models/state.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ class BoxName:
2222
"""The name of the box"""
2323

2424
name: str
25-
"""The name of the box as a string"""
25+
"""The name of the box as a string.
26+
If the name can't be decoded from UTF-8, the string representation of the bytes is returned instead."""
2627
name_raw: bytes
2728
"""The name of the box as raw bytes"""
2829
name_base64: str

0 commit comments

Comments
 (0)