noknow-python icon indicating copy to clipboard operation
noknow-python copied to clipboard

ZKSignature.loads doesn't work

Open melmols opened this issue 4 years ago • 1 comments

Heya,

Trying to elaborate on the example. There is no function called "loads" on data.py or core.py. The ZKSignature class also does not have the load function, E.g: ZKData.load(iq.get()) so im wondering if you meant: ZKData.from_json(iq.get())? But this then also brings encoding issues.

Mel

melmols avatar Oct 08 '21 20:10 melmols

Hello! Because I'm using the "dataclass-json" library, it does use .from_json() as you have assumed.

As for the encoding issues, this is a pretty silly bug and something that is totally unnecessary. This is because in the ZK.sign function it uses the converstion function to_str which is entirely unnecessary since it does not need to be a string.

from noknow.core import ZK, ZKParameters, ZKData

SERVER_SECRET = b"ServerSecret!"
CLIENT_SECRET = b"ClientSecurePassword!"

def main():
    server_zk = ZK.new(curve_name="secp256k1", hash_alg="sha3_256")
    client_zk = ZK.new(curve_name="secp256k1", hash_alg="sha3_256")

    server_sig = server_zk.create_signature(SERVER_SECRET)
    client_sig = client_zk.create_signature(CLIENT_SECRET)

    token = server_zk.sign(SERVER_SECRET, client_zk.token())
    token_json = token.to_json()
    token_data = ZKData.from_json(token_json)
    print(token_data == token)  # True

This functions as expected. Note that dumping/loading the ZKData object will result in base64 encoding/decoding to ensure that bytes will always be converted to something that can be represented as a string regardless of if they are valid UTF-8 or not.

Similarly,

signature_json = server_sig.to_json()
signature_data = ZKSignature.from_json(signature_json)

print(signature_json)
# E.g. {"params": {"alg": "sha3_256", "curve": "secp256k1", "salt": "MQr4mP/V+VPD57jtNOHyOg"}, "signature": "AoNeO0aOySszIMAR+IlAFJpUSotCNUMOkQDOYO9GW0feAQ"}
print(signature_data == server_sig)  # => True

GoodiesHQ avatar Jan 31 '22 19:01 GoodiesHQ