Is it possible to generate keys with node's crypto
library and import them in the InMemorySigner
?
I want to generate keys that I can use to sign transactions for a proof of concept.
Tezos Stack Exchange is a question and answer site for users, experts, and developers of the Tezos blockchain project. It only takes a minute to sign up.
Sign up to join this communityI don't think you can do this with a node, but we have a tiny typescript library to do this:
So what I did in the end is something like this:
// Signer factory example from the docs
const signerFactory = async (rpcUrl, pk) => {
const Tezos = new Taquito.TezosToolkit(rpcUrl);
await Tezos.setProvider({
signer: await Signer.InMemorySigner.fromSecretKey(pk),
});
return Tezos;
};
// Hashing function to hash the key
const toB58Hash = (payload, prefix) => {
const blakeHash = blake.blake2b(payload, undefined, 32);
const tmp = new Uint8Array(prefix.length + blakeHash.length);
tmp.set(prefix);
tmp.set(blakeHash, prefix.length);
const b58 = bs58check.encode(Buffer.from(tmp));
return b58;
};
export const get_signer = async () => {
// Generate a key pair
const key_pair = crypto.generateKeyPair("ed25519");
// Export in pem format
const pem = key_pair.privateKey.export({
type: "pkcs8",
format: "pem",
});
// Take the actual key part from the pem formatted string
// and encode it with the b58 function above
const rawPrivateKey = pem.split("-----")[2].trim();
const encodedKey = toB58Hash(rawPrivateKey, prefix.edsk2);
const signer = await signerFactory(
"https://mainnet.tezos.marigold.dev",
encodedKey
);
};
Note that this should most likely not be used in a production environment with a bunch of money.