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

Signing transaction bug when required signatures is greater than one. #84

Closed
dreson4 opened this issue Jul 6, 2022 · 2 comments
Closed

Comments

@dreson4
Copy link

dreson4 commented Jul 6, 2022

I'm trying to make a transaction where I pay fee for another user's transfer.

txBuilder := solana.NewTransactionBuilder()
transferInstruction := token.NewTransferCheckedInstruction(100, 6, userMintAccountPublicKey, mint, destinationMintAccountPublicKey, userPublicKey, nil).Build()

sendTransaction, err := txBuilder.
		AddInstruction(closeInstruction).
		SetFeePayer(feePayerWallet.PublicKey()).
		SetRecentBlockHash(blockHash.Value.Blockhash).
		Build()

In this case because the fee payer and the user are actually different wallets, the transaction needs two signatures.
So I sign the transaction using both the feePayer and the owner of the account I'm transferring from as follows

	sendTransaction.Sign(func(key solana.PublicKey) *solana.PrivateKey {
		return &feePayerWallet
	})
	
	sendTransaction.Sign(func(key solana.PublicKey) *solana.PrivateKey {
		return &userPrivateKey
	})

When I check the transaction signatures, there are four signatures provided instead of just two, the signatures provided are actually just repeating twice. So I checked the Sign function, turns out

func (tx *Transaction) Sign(getter privateKeyGetter) (out []Signature, err error) {
	messageContent, err := tx.Message.MarshalBinary()
	if err != nil {
		return nil, fmt.Errorf("unable to encode message for signing: %w", err)
	}

	signerKeys := tx.Message.signerKeys()

	for _, key := range signerKeys {
		privateKey := getter(key)
		if privateKey == nil {
			return nil, fmt.Errorf("signer key %q not found. Ensure all the signer keys are in the vault", key.String())
		}

		s, err := privateKey.Sign(messageContent)
		if err != nil {
			return nil, fmt.Errorf("failed to signed with key %q: %w", key.String(), err)
		}

		tx.Signatures = append(tx.Signatures, s)
	}
	return tx.Signatures, nil
}

That's the reason why there are double the number of unique signatures.
So the transaction verification fails.

When I manually remove the repeating signatures and send the transaction it broadcasts just fine and it's a valid transaction.

Wondering if this is a bug or my mistake maybe for this kind of transaction I need to sign the transaction differently

@gagliardetto
Copy link
Owner

From your code, I understand that you're calling the Sign method twice; is that correct?

How about trying something like this:

	signers := []solana.PrivateKey{feePayerWallet, userPrivateKey}

	_, err = tx.Sign(func(key solana.PublicKey) *solana.PrivateKey {
		for _, candidate := range signers {
			if candidate.PublicKey().Equals(key) {
				return &candidate
			}
		}
		return nil
	})

@dreson4
Copy link
Author

dreson4 commented Jul 26, 2022

Thanks for the help, closing this.
It was my bad. Didn't pay attention to Sign function it actually gets called multiple times so I should only call it once.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants