metaplex-foundation / umi

A Solana Framework for JS Clients.
https://umi.typedoc.metaplex.com
MIT License
162 stars 48 forks source link

allow `createKeypairFromSecretKey` to take in bs58 string secret key #87

Closed jim4067 closed 1 year ago

jim4067 commented 1 year ago

Some wallet providers export the secret key as a bs58 string.

I think it would be nice to make the parameter type for createKeypairFromSecretKey a union of string | Uint8Array.

lorisleiva commented 1 year ago

Hi there, the goal of these interfaces is to be as minimal and "to the point" as possible. If we started adding more inputs to these methods, we force all implementations to deal with parsing buffers when all they need to do is create a new keypair. Additionally, if we were to switch to encoded string instead of Uint8Arrays we'd need a systematic and consistent way to know which encodings the string is using (base58, base64, etc).

However we could offer a helper method that uses that interface to provide a more flexible signature such as the one you suggested. Doing so is not really a priority at this point so I'll close this issue but I'll give you the following workaround if you wanted to have this helper in a plugin or on your own apps.

import { Context, createSignerFromKeypair, KeypairSigner } from '@metaplex-foundation/umi';
import { base58 } from '@metaplex-foundation/umi/serializers';

function createSignerFromSecretKey(context: Pick<Context, 'eddsa'>, secretKey: string | Uint8Array): KeypairSigner {
  if (typeof secretKey === 'string') {
    secretKey = base58.deserialize(secretKey)[0];
  }
  return createSignerFromKeypair(context.eddsa.createKeypairFromSecretKey(secretKey));
}

I hope this helps.