You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
, 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:
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 PDAsexportconstgetPDAAndBump=(// @ts-ignore this type existsinputs: Array<string|PublicKey|BN>,programId: PublicKey)=>{constbuffers=inputs.map((input)=>{if(typeofinput==="string"){returnBuffer.from(input);}// TODO: not sure why instanceof occasionally doesn't work// fix it and get rid of the constructor hackif(inputinstanceofPublicKey||input.constructor.name==="PublicKey"){returninput.toBuffer();}// @ts-ignore this type existsif(inputinstanceofBN){returninput.toBuffer("le",8);}thrownewError("getPDAAndBump: unsupported type");});constprogramAddressAndBump=PublicKey.findProgramAddressSync(buffers,programId);returnprogramAddressAndBump;};
The text was updated successfully, but these errors were encountered:
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
For
solana-web3.js/packages/addresses/src/program-derived-address.ts
Line 80 in 273bc9e
Buffer.from("someString")
when runninggetProgramDerivedAddress()
sinceProgramDerivedAddressInput
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
So wrote a little wrapper that follows this technique, checking for different types end consistently encoding them:
After
Details
For your own reference, this is what my own
getPDAAndBump()
looked like, I'm sure you could write something better though:The text was updated successfully, but these errors were encountered: