KeyPair PublicKey to bytes
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 returnsKeyPairData, but the documentation is not clear on what is that class and how to retrieve the data.
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.
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.