-
Notifications
You must be signed in to change notification settings - Fork 52
Question: Key type returned by crypto.keys.unmarshalPublicKey
#184
Comments
Hey @acolytec3 Thanks for reporting this. Two things that we will need to consider here:
You are doing what is expected according to the current API. I recommend that you keep with the If you have any suggestions or feedback, feel free to let us know as we did not spend a big time thinking about this yet. If you would like to help out with the types for |
@vasco-santos Understood, thanks for validating my understanding. I started digging into it a bit, fixed a couple of simple things that would prevent JSDoc from working. That said, |
Happy to help
Yes, that would be better, small iterations to ease development and review. This needs aegir to be updated to its latest version and based on this docs and some example PRs like libp2p/js-libp2p#802 . So, this will generate type definitions on release, but you can look at them by running the build script with aegir. |
Okay, I'll poke around with it. I have started experimenting with upgrading aegir and generating the type definitions but it's going to be a little harder than just updating the existing type definition since I'm not familiar with aegir and all that. If/when I can get something that looks roughly right based on the PRs you linked to, I'll submit a new one. Will close this issue for now as my question is answered. |
In a web app I've recently built, I am generating RSA keys using the
PeerID
library and then using those keys to encrypt and decrypt messages sent vialibp2p.dialProtocol
between two browser-based libp2p nodes. The basic flow for passing the public between the two nodes is as follows:PeerID.create({keyType:'RSA'})
and then start up a node using that generated peerIDlibp2p.peerId.toJSON().pubKey
as a QR codePeerID.createFromPubKey(node1PublicKey)
libp2p-crypto.keys.unmarshalPublicKey(node1.peerId.marshalPubKey())
RsaPublicKey
node1publickey.encrypt(...message...)
RsaPrivateKey
from node 1's peerId usingcrypto.keys.unmarshalPrivateKey
RsaPrivateKey
This all works as outlined here. I can link to the specific code as needed but it's still in pretty rough form in my repo.
The issue I'm finding is that Typescript tells me that
crypto.keys.unmarshalPublicKey
returns a key ofCrypto.PublicKey
which doesn't expose theencrypt
anddecrypt
methods. I can use//@ts-ignore
and everything works as I outlined above but if I usecrypto.keys.supportedKeys.rsa.unmarshalRsaPublicKey
to satisfy Typescript where I usecrypto.keys.unmarshalPublicKey
above, I get this errorError: Cannot read public key. ASN.1 object does not contain an RSAPublicKey.
and the app crashes, even though peerID I'm generating the public key from has theRsaPublicKey
property when I inspect it in the browser console.I'm not sure if there's a better way to get at what I need or if I'm doing something wrong but why would
crypto.keys.unmarshalPublicKey
successfully derive a key and not thecrypto.keys.supportedKeys.rsa.unmarshallRsaPublicKey
method? Alternatively, is there a possible improvement tocrypto.keys.unmarshalPublicKey/PrivateKey
where we could specify the key type as a parameter in the method so that it would return whichever of the supported key types (RSA/ed25519/secp256k1) so library users could make use of the full set of methods supported by each specific key type?The text was updated successfully, but these errors were encountered: