Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert functions for IPFS CID v0 to 32 byte hex string and back #229

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions algosdk/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from . import encoding
import decimal
import base64
import base58
import binascii
from nacl.signing import SigningKey, VerifyKey
from nacl.exceptions import BadSignatureError
from typing import Dict, Any
Expand Down Expand Up @@ -32,6 +34,39 @@ def algos_to_microalgos(algos):
"""
return round(algos * constants.microalgos_to_algos_ratio)

def ipfscidv0_to_byte32(cid):
"""
Convert ipfscidv0 to 32 bytes hex string.

Args:
cid (string): IPFS CID Version 0

Returns:
str: 32 Bytes long string
"""
"""bytes32 is converted back into Ipfs hash format."""

decoded = base58.b58decode(cid)
sliced_decoded = decoded[2:]
return binascii.b2a_hex(sliced_decoded).decode("utf-8")


def byte32_to_ipfscidv0(hexstr):
"""
Convert 32 bytes hex string to ipfscidv0.

Args:
hexstr (string): 32 Bytes long string

Returns:
str: IPFS CID Version 0
"""

binary_str = binascii.a2b_hex(hexstr)
completed_binary_str = b'\x12 ' + binary_str
return base58.b58encode(completed_binary_str).decode("utf-8")



def sign_bytes(to_sign, private_key):
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
project_urls={
"Source": "https://github.com/algorand/py-algorand-sdk",
},
install_requires=["pynacl", "pycryptodomex>=3.6.0", "msgpack"],
install_requires=["pynacl", "pycryptodomex>=3.6.0", "msgpack", "base58>=2.1.0"],
packages=setuptools.find_packages(),
python_requires=">=3.5",
package_data={"": ["data/langspec.json"]},
Expand Down
12 changes: 12 additions & 0 deletions test/steps/steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,18 @@ def check_microalgos(context, microalgos):
assert int(microalgos) == context.microalgos


@when("I convert {ipfscid} ipfscid to bytes32 hex string and back")
def convert_ipfscid(context, ipfscid):
context.ipfscid = util.byte32_to_ipfscidv0(
util.ipfscidv0_to_byte32(ipfscid)
)


@then("it should still be the same ipfscid {ipfscid}")
def check_ipfscid(context, ipfscid):
assert ipfscid == context.ipfscid


@then("I get transactions by address and round")
def txns_by_addr_round(context):
txns = context.acl.transactions_by_address(
Expand Down