Say I have a secretKeyString
extracted from a keyPair using keyPair.secretKey.toString();
I know I can restore a keyPair
from the string with:
const secretKeyStringToKeypair = (secretKeyString: string) => {
const secretKeyArray = secretKeyString.split(",").map(Number);
const secretKey = Uint8Array.from(secretKeyArray);
const keyPair = Keypair.fromSecretKey(secretKey);
return keyPair;
};
Which works, I can do:
expect(keyPair2.secretKey.toString()).toEqual(
keyPair.secretKey.toString()
);
But is there a better way of doing this? Mapping the string into an array using string methods seems a bit suboptimal, and I'm surprised there's not something built in to @solana/web3.js
. I had a peek in the Solana Cookbook and couldn't find anything on the topic.