Skip to content

Commit b85966b

Browse files
authored
Only sign with the keypairs that need to sign (#25)
* Fix signature logic * Fix comment * Refactor get_actual_signers to utils
1 parent b689c44 commit b85966b

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

program_admin/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
PRICE_ACCOUNT_SIZE,
3838
PRODUCT_ACCOUNT_SIZE,
3939
compute_transaction_size,
40+
get_actual_signers,
4041
recent_blockhash,
4142
sort_mapping_account_keys,
4243
)
@@ -166,7 +167,7 @@ async def send_transaction(
166167
transaction = Transaction(recent_blockhash=blockhash)
167168

168169
transaction.add(instructions[0])
169-
transaction.sign(*signers)
170+
transaction.sign(*get_actual_signers(signers, transaction))
170171

171172
ix_index = 1
172173

@@ -207,7 +208,7 @@ async def send_transaction(
207208
and instructions[ix_index:]
208209
):
209210
transaction.add(instructions[ix_index])
210-
transaction.sign(*signers)
211+
transaction.sign(*get_actual_signers(signers, transaction))
211212
ix_index += 1
212213

213214
if not dump_instructions:

program_admin/util.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Dict, List
22

33
from solana.blockhash import Blockhash
4+
from solana.keypair import Keypair
45
from solana.publickey import PublicKey
56
from solana.rpc.async_api import AsyncClient
67
from solana.rpc.commitment import Commitment
@@ -107,3 +108,25 @@ def apply_overrides(
107108
else:
108109
overridden_permissions[key] = value
109110
return overridden_permissions
111+
112+
113+
def get_actual_signers(
114+
signers: List[Keypair], transaction: Transaction
115+
) -> List[Keypair]:
116+
"""
117+
Given a list of keypairs and a transaction, returns the keypairs that actually need to sign the transaction,
118+
i.e. those whose pubkey appears in at least one of the instructions as a signer.
119+
"""
120+
actual_signers = []
121+
for signer in signers:
122+
instruction_has_signer = [
123+
any(
124+
signer.public_key == account.pubkey and account.is_signer
125+
for account in instruction.keys
126+
)
127+
for instruction in transaction.instructions
128+
]
129+
if any(instruction_has_signer):
130+
actual_signers.append(signer)
131+
132+
return actual_signers

0 commit comments

Comments
 (0)