Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KeyPair PublicKey to bytes #53

Open
apoleo88 opened this issue Apr 2, 2021 · 2 comments
Open

KeyPair PublicKey to bytes #53

apoleo88 opened this issue Apr 2, 2021 · 2 comments

Comments

@apoleo88
Copy link

apoleo88 commented Apr 2, 2021

From version 2 I can't find a way to extract the bytes from the public and private keys generated with Keypair:

KeyPair keyPair = await X25519().newKeyPair();

In v1.4.1 I was using:

keyPair.privateKey.extractSync()
keyPair.publicKey.bytes

But now in v2:

  • PublicKey has no longer the getter .bytes

  • Private key is no longer extractable(?). There is the method extract() that returns KeyPairData, but the documentation is not clear on what is that class and how to retrieve the data.

@maxfornacon
Copy link

maxfornacon commented Apr 10, 2021

I guess I figured it out.

// First generate KeyPair
KeyPair keyPair = await X25519().newKeyPair();

// To get the public key bytes you have to use SimplePublicKey
SimplePublicKey publicKey = await keyPair.extractPublicKey();
List<int> publicKeyBytes = publicKey.bytes; // now this works


// To extract the private key do the following:
// extract SimpleKeyPairData
SimpleKeyPairData simpleKeyPairData = await keyPair.extract();

// then you can extract the private key bytes
List<int> privateKeyBytes = await simpleKeyPairData.extractPrivateKeyBytes();

But I agree with you that this is not very clearly documented and not very intuitive to use either.
I hope this helps you anyways.

Update:

It appears that after updating the min-sdk version to 2.12 this doesn't work anymore.

@maxfornacon
Copy link

For sdk: ">=2.12.0-0 <3.0.0" you need to add as SimplePublicKey.
Like this: SimplePublicKey publicKey = await keyPair.extractPublicKey() as SimplePublicKey;
Same goes for SimpleKeyPairData.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants