Skip to content

Only sign with the keypairs that need to sign #25

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

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions program_admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
PRICE_ACCOUNT_SIZE,
PRODUCT_ACCOUNT_SIZE,
compute_transaction_size,
get_actual_signers,
recent_blockhash,
sort_mapping_account_keys,
)
Expand Down Expand Up @@ -166,7 +167,7 @@ async def send_transaction(
transaction = Transaction(recent_blockhash=blockhash)

transaction.add(instructions[0])
transaction.sign(*signers)
transaction.sign(*get_actual_signers(signers, transaction))

ix_index = 1

Expand Down Expand Up @@ -207,7 +208,7 @@ async def send_transaction(
and instructions[ix_index:]
):
transaction.add(instructions[ix_index])
transaction.sign(*signers)
transaction.sign(*get_actual_signers(signers, transaction))
ix_index += 1

if not dump_instructions:
Expand Down
23 changes: 23 additions & 0 deletions program_admin/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict, List

from solana.blockhash import Blockhash
from solana.keypair import Keypair
from solana.publickey import PublicKey
from solana.rpc.async_api import AsyncClient
from solana.rpc.commitment import Commitment
Expand Down Expand Up @@ -107,3 +108,25 @@ def apply_overrides(
else:
overridden_permissions[key] = value
return overridden_permissions


def get_actual_signers(
signers: List[Keypair], transaction: Transaction
) -> List[Keypair]:
"""
Given a list of keypairs and a transaction, returns the keypairs that actually need to sign the transaction,
i.e. those whose pubkey appears in at least one of the instructions as a signer.
"""
actual_signers = []
for signer in signers:
instruction_has_signer = [
any(
signer.public_key == account.pubkey and account.is_signer
for account in instruction.keys
)
for instruction in transaction.instructions
]
if any(instruction_has_signer):
actual_signers.append(signer)

return actual_signers