@@ -37,9 +37,10 @@ fn witness_keys_for_cert(cert_enum: &Certificate, keys: &mut BTreeSet<Ed25519Key
3737 }
3838}
3939
40- fn min_fee ( tx_builder : & TransactionBuilder ) -> Result < Coin , JsError > {
41- let body = tx_builder. build ( ) ?;
42-
40+ // tx_body must be the result of building from tx_builder
41+ // constructs the rest of the Transaction using fake witness data of the correct length
42+ // for use in calculating the size of the final Transaction
43+ fn fake_full_tx ( tx_builder : & TransactionBuilder , body : TransactionBody ) -> Result < Transaction , JsError > {
4344 let fake_key_root = Bip32PrivateKey :: from_bip39_entropy (
4445 // art forum devote street sure rather head chuckle guard poverty release quote oak craft enemy
4546 & [ 0x0c , 0xcb , 0x74 , 0xf3 , 0x6b , 0x7d , 0xa1 , 0x64 , 0x9a , 0x81 , 0x44 , 0x67 , 0x55 , 0x22 , 0xd4 , 0xd8 , 0x09 , 0x7c , 0x64 , 0x12 ] ,
@@ -92,11 +93,15 @@ fn min_fee(tx_builder: &TransactionBuilder) -> Result<Coin, JsError> {
9293 plutus_data : None ,
9394 redeemers : None ,
9495 } ;
95- let full_tx = Transaction {
96+ Ok ( Transaction {
9697 body,
9798 witness_set,
9899 auxiliary_data : tx_builder. auxiliary_data . clone ( ) ,
99- } ;
100+ } )
101+ }
102+
103+ fn min_fee ( tx_builder : & TransactionBuilder ) -> Result < Coin , JsError > {
104+ let full_tx = fake_full_tx ( tx_builder, tx_builder. build ( ) ?) ?;
100105 fees:: min_fee ( & full_tx, & tx_builder. fee_algo )
101106}
102107
@@ -540,15 +545,17 @@ impl TransactionBuilder {
540545 required_signers : None ,
541546 network_id : None ,
542547 } ;
543- let built_size = built. to_bytes ( ) . len ( ) ;
544- if built_size > self . max_tx_size as usize {
548+ // we must build a tx with fake data (of correct size) to check the final Transaction size
549+ let full_tx = fake_full_tx ( self , built) ?;
550+ let full_tx_size = full_tx. to_bytes ( ) . len ( ) ;
551+ if full_tx_size > self . max_tx_size as usize {
545552 Err ( JsError :: from_str ( & format ! (
546553 "Maximum transaction size of {} exceeded. Found: {}" ,
547554 self . max_tx_size,
548- built_size
555+ full_tx_size
549556 ) ) )
550557 } else {
551- Ok ( built )
558+ Ok ( full_tx . body )
552559 }
553560 }
554561
0 commit comments