Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature suggestion: allow more seed types to be provided to getProgramDerivedAddress() #2312

Open
mikemaccana opened this issue Mar 12, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@mikemaccana
Copy link
Contributor

mikemaccana commented Mar 12, 2024

For

, if I read this correctly, I don't have to do Buffer.from("someString") when running getProgramDerivedAddress() since ProgramDerivedAddressInput includes strings so I can just provide the string directly, avoiding the need to write boilerplate.

What if we allowed Addresses, BigInts etc? If an Address is provided as a seed, encode it as a byte array.

Motivation + Example use case

When working with web3.js 1.x code, I notice a lot of boilerplate encoding code:

Before

const pdaAndBump = PublicKey.findProgramAddressSync(
  [
    Buffer.from("somestring"),
    userKeypair.publicKey.toBuffer(),
    id.toBuffer("le", 8),
  ],
  programKeypair.publicKey
);

So wrote a little wrapper that follows this technique, checking for different types end consistently encoding them:

After

const pdaAndBump = getPDAAndBump(
   ["somestring", userKeypair.publicKey, id],
   programKeypair.publicKey
);

Details

For your own reference, this is what my own getPDAAndBump()looked like, I'm sure you could write something better though:

// A generic function to take strings, publickeys, and BNs
// does whatever work is necessary to turn them into PDAs
export const getPDAAndBump = (
  // @ts-ignore this type exists
  inputs: Array<string | PublicKey | BN>,
  programId: PublicKey
) => {
  const buffers = inputs.map((input) => {
    if (typeof input === "string") {
      return Buffer.from(input);
    }
    // TODO: not sure why instanceof occasionally doesn't work
    // fix it and get rid of the constructor hack
    if (input instanceof PublicKey || input.constructor.name === "PublicKey") {
      return input.toBuffer();
    }
    // @ts-ignore this type exists
    if (input instanceof BN) {
      return input.toBuffer("le", 8);
    }
    throw new Error("getPDAAndBump: unsupported type");
  });
  const programAddressAndBump = PublicKey.findProgramAddressSync(
    buffers,
    programId
  );
  return programAddressAndBump;
};
@mikemaccana mikemaccana added the enhancement New feature or request label Mar 12, 2024
@mikemaccana mikemaccana changed the title Feature suggestion: allow more seed types to be provided getProgramDerivedAddress() Feature suggestion: allow more seed types to be provided to getProgramDerivedAddress() Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant