opentensor/async-substrate-interface

Decoded addresses returned from parent and child key storage calls

Opened this issue · 2 comments

The following two should return the exact same output:

from substrateinterface import SubstrateInterface, Keypair

def sync_():
    substrate = SubstrateInterface(url="wss://archive.chain.opentensor.ai:443/")

    parent = "5F2CsUDVbRbVMXTh9fAzF9GacjVX7UapvRxidrxe7z8BYckQ"
    netuid = 2
    result = substrate.query('SubtensorModule', 'ParentKeys', [parent, netuid])
    print(result.value)

import asyncio
from async_substrate_interface import AsyncSubstrateInterface

async def async_():

    substrate = AsyncSubstrateInterface(url="wss://archive.chain.opentensor.ai:443/")
    await substrate.initialize()

    parent = "5F2CsUDVbRbVMXTh9fAzF9GacjVX7UapvRxidrxe7z8BYckQ"
    netuid = 2
    result = await substrate.query('SubtensorModule', 'ParentKeys', [parent, netuid])
    print(result.value)

if __name__ == "__main__":

    sync_()
    asyncio.run(async_())

Instead the output looks like this:

[(18446744073709551615, '5HK5tp6t2S59DywmHRWPBVJeJ86T61KjurYqeooqj8sREpeN'), (18446744073709551615, '5F27Eqz2PhyMtGMEce898x31DokNqRVxkm5AhDDe6rDGNvoY'), (9218420833201746944, '5Fq5v71D4LX8Db1xsmRSy6udQThcZ8sFDqxQFwnUZ1BuqY5A'), (18446744073709551615, '5F2NCTUugqBHMPp5UyGjGMV9a94a2Eh6JDPBUFzWL2Zabjz8'), (18446744073709551615, '5FFM6Nvvm78GqyMratgXXvjbqZPi7SHgSQ81nyS96jBuUWgt'), (18446744073709551615, '5DvTpiniW9s3APmHRYn8FroUWyfnLtrsid5Mtn5EwMXHN2ed')]
[(18446744073709551615, ((232, 36, 201, 53, 148, 3, 87, 175, 115, 201, 97, 189, 215, 56, 126, 26, 184, 33, 236, 41, 57, 236, 209, 157, 170, 254, 96, 129, 174, 154, 230, 116),)), (18446744073709551615, ((130, 185, 173, 25, 121, 157, 219, 27, 240, 194, 55, 238, 95, 12, 71, 38, 213, 194, 173, 39, 215, 29, 44, 39, 110, 226, 13, 49, 181, 57, 28, 55),)), (9218420833201746944, ((166, 141, 193, 147, 47, 41, 181, 229, 235, 66, 133, 113, 61, 217, 166, 206, 76, 159, 60, 87, 91, 102, 24, 75, 21, 131, 81, 81, 20, 246, 123, 30),)), (18446744073709551615, ((130, 236, 8, 54, 7, 57, 212, 107, 131, 34, 126, 201, 137, 26, 199, 200, 94, 213, 126, 30, 159, 109, 28, 54, 163, 131, 124, 73, 95, 233, 8, 103),)), (18446744073709551615, ((140, 210, 128, 212, 62, 76, 246, 80, 26, 227, 180, 37, 88, 57, 117, 255, 99, 206, 77, 116, 112, 207, 81, 128, 116, 181, 144, 63, 243, 117, 96, 14),)), (18446744073709551615, ((82, 47, 67, 47, 81, 40, 125, 245, 119, 17, 163, 160, 15, 108, 122, 51, 75, 12, 129, 63, 241, 120, 194, 38, 73, 240, 21, 102, 239, 74, 93, 72),))]

So for the async version of the substrate interface, the ss58 addresses are either encoded or decoded, rather than returned in the desired format.
py-substrateinterface: '5HK5tp6t2S59DywmHRWPBVJeJ86T61KjurYqeooqj8sREpeN'
async-substrate-interface: ((232, 36, 201, 53, 148, 3, 87, 175, 115, 201, 97, 189, 215, 56, 126, 26, 184, 33, 236, 41, 57, 236, 209, 157, 170, 254, 96, 129, 174, 154, 230, 116), )

Any help would be great in getting the addresses either back into the correct format or fixing the bug.

Thanks

Looking into this. Until then you can decode the ss58 with the function we have in bittensor:

def decode_account_id(account_id_bytes: Union[bytes, str]) -> str:
    """
    Decodes an AccountId from bytes to a Base64 string using SS58 encoding.

    Args:
        account_id_bytes (bytes): The AccountId in bytes that needs to be decoded.

    Returns:
        str: The decoded AccountId as a Base64 string.
    """
    if isinstance(account_id_bytes, tuple) and isinstance(account_id_bytes[0], tuple):
        account_id_bytes = account_id_bytes[0]

    # Convert the AccountId bytes to a Base64 string
    return ss58_encode(bytes(account_id_bytes).hex(), SS58_FORMAT)