@@ -31,9 +31,9 @@ def __init__(self, **config):
3131 self .fee_payer : PublicKey = config .get ("fee_payer" )
3232 self .nonce_info = config .get ("nonce_info" )
3333 self .recent_blockhash = config .get ("recent_blockhash" )
34- self .signers : List [PublicKey ] | List [Keypair ] = config .get ("signers" )
35- self .instructions : List [Instruction ] = []
36- self .signatures : List [PKSigPair ] = []
34+ self .signers : list [PublicKey ] | list [Keypair ] = config .get ("signers" )
35+ self .instructions : list [Instruction ] = []
36+ self .signatures : list [PKSigPair ] = config . get ( "signatures" , [])
3737 if "instructions" in config :
3838 instructions : Instruction = config .get ("instructions" )
3939 if (
@@ -60,9 +60,29 @@ def to_public_key(signer: PublicKey | Keypair) -> PublicKey:
6060 public_key = to_public_key (signer )
6161 ) for signer in self .signers ]
6262
63- self .signatures = pk_sig_pairs
64-
63+ if not self .signatures :
64+ self .signatures = pk_sig_pairs
65+
66+ self ._message : Message = None
67+ self .json = None
68+
69+ def _to_json (self ):
70+ return {
71+ "recentBlockhash" : self .recent_blockhash if hasattr (self , 'recent_blockhash' ) else None ,
72+ "feePayer" : self .fee_payer .base58_encode () if hasattr (self , 'fee_payer' ) else None ,
73+ "nonceInfo" : {
74+ "nonce" : self .nonce_info .nonce ,
75+ "nonceInstruction" : self .nonce_info .nonce_instruction ._to_json ()
76+ } if self .nonce_info else None ,
77+ "instructions" : [instruction ._to_json () for instruction in self .instructions ],
78+ "signers" : [signature .public_key .base58_encode () for signature in self .signatures ]
79+ }
80+
6581 def compile_transaction (self ) -> bytes :
82+ # Reference: https://github.com/solana-labs/solana-web3.js/blob/a1fafee/packages/library-legacy/src/transaction/legacy.ts#L367
83+ if self ._message and self ._to_json () == self .json :
84+ return self ._message .serialize ()
85+
6686 if self .nonce_info :
6787 self .recent_blockhash = self .nonce_info .nonce
6888
@@ -282,13 +302,16 @@ def populate(self, message: Message, signatures: List[bytes], signers: List[Keyp
282302 ))
283303
284304 fee_payer = message .account_keys [0 ] if message .header .num_required_signatures > 0 else None
285- return Transaction (
305+ transaction = Transaction (
286306 fee_payer = fee_payer ,
287307 recent_blockhash = message .recent_blockhash ,
288308 signatures = decoded_signatures ,
289309 instructions = instructions ,
290310 signers = signers
291311 )
312+ transaction ._message = message
313+ transaction .json = transaction ._to_json ()
314+ return transaction
292315
293316 @classmethod
294317 def from_buffer (self , buffer : bytes , signers : List [Keypair ]) -> Transaction :
0 commit comments