hippius 0.1.13__py3-none-any.whl → 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hippius_sdk/utils.py ADDED
@@ -0,0 +1,152 @@
1
+ """
2
+ Utility functions for the Hippius SDK.
3
+
4
+ This module provides common utility functions used across the SDK.
5
+ """
6
+
7
+ import binascii
8
+ from typing import Optional
9
+
10
+
11
+ def format_size(size_bytes: int) -> str:
12
+ """
13
+ Format a size in bytes to a human-readable string.
14
+
15
+ Args:
16
+ size_bytes: Size in bytes
17
+
18
+ Returns:
19
+ str: Human-readable size string (e.g., '1.23 MB', '456.78 KB')
20
+ """
21
+ if size_bytes >= 1024 * 1024 * 1024:
22
+ return f"{size_bytes / (1024 * 1024 * 1024):.2f} GB"
23
+ elif size_bytes >= 1024 * 1024:
24
+ return f"{size_bytes / (1024 * 1024):.2f} MB"
25
+ elif size_bytes >= 1024:
26
+ return f"{size_bytes / 1024:.2f} KB"
27
+ else:
28
+ return f"{size_bytes} bytes"
29
+
30
+
31
+ def is_valid_cid(cid: str) -> bool:
32
+ """
33
+ Check if a string is likely a valid IPFS CID.
34
+
35
+ Args:
36
+ cid: String to check
37
+
38
+ Returns:
39
+ bool: True if it appears to be a valid CID
40
+ """
41
+ return (
42
+ cid
43
+ and isinstance(cid, str)
44
+ and cid.startswith(("Qm", "bafy", "bafk", "bafyb", "bafzb", "b"))
45
+ )
46
+
47
+
48
+ def normalize_hex_string(hex_string: str) -> str:
49
+ """
50
+ Normalize a hex string by removing '0x' prefix if present.
51
+
52
+ Args:
53
+ hex_string: Hex string to normalize
54
+
55
+ Returns:
56
+ str: Normalized hex string without '0x' prefix
57
+ """
58
+ if isinstance(hex_string, str) and hex_string.startswith("0x"):
59
+ return hex_string[2:]
60
+ return hex_string
61
+
62
+
63
+ def is_valid_hex(hex_string: str) -> bool:
64
+ """
65
+ Check if a string contains only valid hexadecimal characters.
66
+
67
+ Args:
68
+ hex_string: String to check
69
+
70
+ Returns:
71
+ bool: True if the string contains only valid hex characters
72
+ """
73
+ return all(c in "0123456789abcdefABCDEF" for c in hex_string)
74
+
75
+
76
+ def hex_to_ipfs_cid(hex_string: str) -> str:
77
+ """
78
+ Convert a hex-encoded IPFS CID to a regular IPFS CID.
79
+
80
+ This function handles multiple formats:
81
+ 1. Proper CIDs directly (starting with Qm, bafy, etc.)
82
+ 2. Hex strings that, when decoded, represent ASCII CIDs
83
+ 3. Binary CIDv0 hex representations
84
+ 4. Other hex formats
85
+
86
+ Args:
87
+ hex_string: Hex string representation of an IPFS CID
88
+
89
+ Returns:
90
+ str: Regular IPFS CID
91
+ """
92
+ # If it's already a proper CID, return it directly
93
+ if is_valid_cid(hex_string):
94
+ return hex_string
95
+
96
+ # Normalize by removing 0x prefix if present
97
+ norm_hex = normalize_hex_string(hex_string)
98
+
99
+ # If not valid hex after normalization, it might be a direct CID
100
+ if not is_valid_hex(norm_hex):
101
+ if is_valid_cid(norm_hex):
102
+ return norm_hex
103
+ # Return original if we can't process it
104
+ return hex_string
105
+
106
+ # First, try to decode as ASCII if it's a hex representation of ASCII characters
107
+ try:
108
+ bytes_data = bytes.fromhex(norm_hex)
109
+ ascii_str = bytes_data.decode("ascii")
110
+
111
+ # If the decoded string starts with a valid CID prefix, return it
112
+ if is_valid_cid(ascii_str):
113
+ return ascii_str
114
+ except Exception:
115
+ # If ASCII decoding fails, continue with other methods
116
+ pass
117
+
118
+ # Try to decode as a binary CID
119
+ try:
120
+ import base58
121
+
122
+ binary_data = bytes.fromhex(norm_hex)
123
+
124
+ # Check if it matches CIDv0 pattern (starts with 0x12, 0x20)
125
+ if len(binary_data) > 2 and binary_data[0] == 0x12 and binary_data[1] == 0x20:
126
+ # CIDv0 (Qm...)
127
+ return base58.b58encode(binary_data).decode("utf-8")
128
+
129
+ # If it doesn't match CIDv0, for CIDv1 just return the hex without 0x prefix
130
+ return norm_hex
131
+ except ImportError:
132
+ # If base58 is not available
133
+ print("Warning: base58 module not available for proper CID conversion")
134
+ return norm_hex
135
+ except Exception as e:
136
+ print(f"Error converting hex to CID: {e}")
137
+ return hex_string
138
+
139
+
140
+ def format_cid(cid: str) -> str:
141
+ """
142
+ Format a CID for display.
143
+
144
+ This is a wrapper around hex_to_ipfs_cid for backward compatibility.
145
+
146
+ Args:
147
+ cid: Content Identifier (CID) to format
148
+
149
+ Returns:
150
+ str: Formatted CID string
151
+ """
152
+ return hex_to_ipfs_cid(cid)
@@ -1,10 +0,0 @@
1
- hippius_sdk/__init__.py,sha256=pEVObrzsEpX0iJhtAEmzY832jVEr7wVPqoxaSwsE3BM,1261
2
- hippius_sdk/cli.py,sha256=thTRIjKNoCiKaxH1P2UukVvH8r3zR6PDaYEk9ROHiAA,74073
3
- hippius_sdk/client.py,sha256=dIrjaXl2J71_ljhu_nFS-gTOmKDRPzJDXeviBSOAeis,14894
4
- hippius_sdk/config.py,sha256=nLDmZft5hR_mD_NrmnnfMwfWRCsp2uQi2f1mTWu-fC4,22870
5
- hippius_sdk/ipfs.py,sha256=kMqmtyvdjzo1JlXTdLYA74zAmZocDAOsW9cmzwG6ejg,63959
6
- hippius_sdk/substrate.py,sha256=hyPCjLCTuBTWqHvU8eMowpThsIJYUrvX4yepIs3NcZk,33037
7
- hippius-0.1.13.dist-info/METADATA,sha256=Brw4f-3IFqK85YePwvAWWEbKmgxd3RcOOrGpsJcE8I4,28001
8
- hippius-0.1.13.dist-info/WHEEL,sha256=Zb28QaM1gQi8f4VCBhsUklF61CTlNYfs9YAZn-TOGFk,88
9
- hippius-0.1.13.dist-info/entry_points.txt,sha256=b1lo60zRXmv1ud-c5BC-cJcAfGE5FD4qM_nia6XeQtM,98
10
- hippius-0.1.13.dist-info/RECORD,,