2

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.

2 Answers 2

3

I don't think you can do this with a node, but we have a tiny typescript library to do this:

https://gitlab.com/functori/dev/funkey.ts

3
  • I think Ulrik means the node.js crypto modules for cryptographic functionality?
    – David
    Jan 13, 2023 at 14:04
  • 1
    Typescript or Node.Js is (almost) the same. And he wants to generate keypairs to sign ops with :-)
    – iguerNL
    Jan 13, 2023 at 14:12
  • I posted a solution that I found in the end so it is possible! :) Jan 13, 2023 at 19:39
3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.