ucanto icon indicating copy to clipboard operation
ucanto copied to clipboard

Tag ed25519 public key bytes with `0xed` and decode into Principal

Open DougAnderson444 opened this issue 2 years ago • 3 comments

Hi all,

I've been looking for a way to generate a Principal from ed25519 Public Key bytes. I can't find it in the code (and the readme is out of date for Principals) and think this was originally intended to encode ed25519 public key bytes with 0xed, but it does not do what the comment implies it should do:

https://github.com/web3-storage/ucanto/blob/582c4c504d2e75feee2c5d298ea08b2f01ff1c5e/packages/principal/src/ed25519/verifier.js#L61-L66

Am I correct with creating a Principal like this, the code seems to work:

import { tagWith } from './multiformat.js'

const taggedMF = tagWith(Verifier.code /* `0xed` */, new Uint8Array(publicKey))
const principal = Verifier.decode(taggedMF);
const audienceDid = principal.did() 

If this is elsewhere in the codebase I must have missed it, but if not, please consider this issue is raised to add this functionality to create tagged, encoded Principals from raw bytes. Thanks!

DougAnderson444 avatar Jan 17 '23 14:01 DougAnderson444

Am I correct with creating a Principal like this, the code seems to work:

Yeah that seems reasonable!

We do not have a function to do exactly what you're looking for because we store tagged keys so that when importing them we don't need to have additional context about what kind of key it is.

I suppose we could add such a function, but I'm bit hesitant because lib is trying to abstract actual key algorithms with a universal interface. Importing raw keys obviously not going to work universally, but that's not so say we can't add a function to linked module to do just that.

Gozala avatar Jan 20 '23 22:01 Gozala

Thanks for your feedback 😀

For context, my use case is essentially: "Here is my public key, can you please send me a UCAN?" which is coming from someone who generated their keypair outside the library (so it wouldn't be tagged yet).

DougAnderson444 avatar Jan 20 '23 23:01 DougAnderson444