magland / sortingview

Web app for viewing results of ephys spike sorting
Apache License 2.0
25 stars 8 forks source link

Setup issue #234

Open GrimmSnark opened 2 months ago

GrimmSnark commented 2 months ago

Hello,

So I am trying to get sortingview working for spikeinterface. However when I run the kachery-cloud-init I get the following error. Any advice would be greatly appreciated.

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\ProgramData\anaconda3\envs\si_env2\Scripts\kachery-cloud-init.exe\__main__.py", line 7, in <module>
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\click\core.py", line 1157, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\kachery_cloud\cli.py", line 13, in init
    kc.init()
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\kachery_cloud\init.py", line 62, in init
    client_info = get_client_info()
                  ^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\kachery_cloud\init.py", line 52, in get_client_info
    response = _kachery_gateway_request(payload)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\kachery_cloud\_kachery_gateway_request.py", line 20, in _kachery_gateway_request
    'signature': _sign_message_as_client(payload)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\kachery_cloud\_client_keys.py", line 57, in _sign_message_as_client
    return _sign_message(msg, public_key_hex, private_key_hex)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\envs\si_env2\Lib\site-packages\kachery_cloud\_client_keys.py", line 110, in _sign_message
    pubk.verify(bytes.fromhex(signature), msg_bytes)
cryptography.exceptions.InvalidSignature
magland commented 2 months ago

Hi @GrimmSnark

I haven't personally tested on windows, so this might be OS-specific.

Please try running the following script. On my Linux machine it succeeds and prints "Success". I'm curious on whether it works for you on windows. We can take it from there.

def _generate_keypair():
    from cryptography.hazmat.primitives import serialization
    from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
    privk = Ed25519PrivateKey.generate()
    pubk = privk.public_key()
    private_key_hex = privk.private_bytes(
        encoding=serialization.Encoding.Raw,
        format=serialization.PrivateFormat.Raw,
        encryption_algorithm=serialization.NoEncryption()
    ).hex()
    public_key_hex = pubk.public_bytes(
        encoding=serialization.Encoding.Raw,
        format=serialization.PublicFormat.Raw
    ).hex()
    test_msg = b'123'
    test_signature = _sign_message(test_msg, public_key_hex, private_key_hex)
    assert _verify_signature(test_msg, public_key_hex, test_signature)
    return public_key_hex, private_key_hex

def _verify_signature(msg: bytes, public_key_hex: str, signature: str):
    from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PublicKey
    pubk = Ed25519PublicKey.from_public_bytes(bytes.fromhex(public_key_hex))
    try:
        pubk.verify(bytes.fromhex(signature), msg)
    except Exception:
        return False
    return True

def _sign_message(msg: bytes, public_key_hex: str, private_key_hex: str) -> str:
    from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey, Ed25519PublicKey
    privk = Ed25519PrivateKey.from_private_bytes(bytes.fromhex(private_key_hex))
    pubk = Ed25519PublicKey.from_public_bytes(bytes.fromhex(public_key_hex))
    signature = privk.sign(msg).hex()
    pubk.verify(bytes.fromhex(signature), msg)
    return signature

public_key_hex, private_key_hex = _generate_keypair()
_sign_message(b'123', public_key_hex, private_key_hex)
print('Success')