hashberg-io / multiformats

Python implementation of multiformat protocols.
https://multiformats.readthedocs.io
MIT License
22 stars 6 forks source link

Why specificing both the base and the prefix ? #22

Closed ticapix closed 1 month ago

ticapix commented 2 months ago

Hi,

I'm trying to transform a JWK into a did:key

The JWK is defined as

{
    "crv": "Ed25519",
    "d": "zXt8CIQcpTe5AqrSj7jbCmsT7Hk_iSsa7OugpuETy00",
    "kty": "OKP",
    "x": "1IFOwqNRAXr7bxkzsaUP4c5TsfODswA3L8EZYoaWP08"
}

To do so, I need to decode the x parameter which is encoded in base64url.

I'm expecting to either be able to use the procedural method via multibase.decode() because I would prefix the string with the proper base64url flag u, or use the object-oriented style via multibase.get("base64url").decode(), without having to prefix the string.

multibase.decode("u" + "1IFOwqNRAXr7bxkzsaUP4c5TsfODswA3L8EZYoaWP08") # works
multibase.get("base64url").decode("u" + "1IFOwqNRAXr7bxkzsaUP4c5TsfODswA3L8EZYoaWP08") # works
multibase.get("base64url").decode("1IFOwqNRAXr7bxkzsaUP4c5TsfODswA3L8EZYoaWP08") # fails
> multiformats.multibase.err.MultibaseKeyError: 'No known multibase code is a prefix of the given string.'

What is the point of getting the Multibase object if we still have to prefix the string ?

Is that the expected behavior ?

Best regards

ticapix commented 2 months ago

Solved.

I can directly use raw_decoder()

sg495 commented 1 month ago

Thank you for this, and for your pull request (which has now been merged)!