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

feat: Add compatibility for publicKeys #5895

Closed
wants to merge 2 commits into from

Conversation

Gitmazter
Copy link

Ran into an issue where I was passing web3.publicKeys to createMintToInstruction with 3 multiSigners but received an anonymous.toString() is undefined error while trying to use serializeMessage() for offline signing.

The error was that the docs allowed a (signer | publicKey) [ ] but the function only works with a signer[ ], my fix was to move my web3.publicKeys into an object. {publicKey: myKey} which allowed me to serialize the instruction.

What I assume happened was that the logic
pubkey: signer instanceof PublicKey ? signer : signer.publicKey
did not work as intended and saw my publicKeys as signers and thus tried publicKey.publicKey.toString()

I believe the changes to the logic in this file will resolve the issue for anyone encountering it in the future.

I'm not entirely familiar with the SPL library yet so I'd be glad for suggestions for how to improve my PR or just have a discussion if anything else might have caused it

Ran into an issue where I was passing web3.publicKeys to createMintToInstruction but received an anonymous.toString() is undefined error. 

The error was that the docs allowed a (signer | publicKey) [ ] but the function only works with a signer[ ], my fix was to move my publicKeys into an object. {publicKey: myKey}

I believe the changes to the logic in this file will resolve the issue for anyone encountering it in the future
@mergify mergify bot added the community Community contribution label Nov 27, 2023
Copy link
Contributor

@buffalojoec buffalojoec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for reporting this and submitting a fix!

I wonder if the issue had something to do with a dual-package error. TL/DR, if two different versions of @solana/web3.js are somehow being added to the dependency tree, then instanceof is going to fail. Since the conditional defaults to a signer anytime instanceof PublicKey fails, that seems to make sense to me.

It looks like your fix will work, but I suggested a way to write this without using instanceof at all.

isSigner: true,
isWritable: false,
});
if (signer instanceof PublicKey || signer instanceof Keypair) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All Keypair are Signer but not all Signer are Keypair.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gitmazter if we're going to proceed with changes, instanceof should also be revised here.

token/js/src/instructions/internal.ts Outdated Show resolved Hide resolved
@Gitmazter
Copy link
Author

Will add comment this here as well outside of the oudated conversation:

This is a better/simpler solution, will commit it! Thank you for paying attention to my PR :) Still new to cooperating over Github so it's definetly easing my social anxiety lol.

After reflecting on your comments the two versions of web3.js argument sounds the most likely to me, i think it applies to wallet adapters as well since I'm using the phantom adapter as part of my project which isnt named PublicKey but is named after a 2 byte hash e.g. ve{}|ey{} etc. Although I did try to convert these before submitting my pr but the problem remained.

@buffalojoec
Copy link
Contributor

This is a better/simpler solution, will commit it! Thank you for paying attention to my PR :) Still new to cooperating over Github so it's definetly easing my social anxiety lol.

You are welcome to contribute here, mate.

After reflecting on your comments the two versions of web3.js argument sounds the most likely to me, i think it applies to wallet adapters as well since I'm using the phantom adapter as part of my project which isnt named PublicKey but is named after a 2 byte hash e.g. ve{}|ey{} etc. Although I did try to convert these before submitting my pr but the problem remained.

Is this issue related to the fix you've PR'ed here?

isSigner: true,
isWritable: false,
});
if (signer instanceof PublicKey || signer instanceof Keypair) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Gitmazter if we're going to proceed with changes, instanceof should also be revised here.

Comment on lines +21 to +33
else if (signer.toString()) {
try {
const compatiblePubkey = new PublicKey(signer.toString())
keys.push({
pubkey: compatiblePubkey,
isSigner: true,
isWritable: false,
});
}
catch (e) {
// not a pubkey
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a try-catch here with no catch?

@github-actions github-actions bot added the stale [bot only] Added to stale content; will be closed soon label Dec 18, 2023
@github-actions github-actions bot closed this Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Community contribution stale [bot only] Added to stale content; will be closed soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants