2020//! Extension for PSET is based on PSET defined in BIP370.
2121//! <https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki>
2222
23- use std:: collections:: HashMap ;
23+ use std:: collections:: { BTreeMap , HashMap } ;
2424use std:: { cmp, io} ;
2525
2626mod error;
@@ -46,7 +46,10 @@ use crate::{
4646 confidential:: { AssetBlindingFactor , ValueBlindingFactor } ,
4747 TxOutSecrets ,
4848} ;
49- use crate :: { OutPoint , LockTime , Sequence , SurjectionInput , Transaction , TxIn , TxInWitness , TxOut , TxOutWitness , Txid } ;
49+ use crate :: {
50+ LockTime , OutPoint , Sequence , SurjectionInput , Transaction , TxIn , TxInType ,
51+ TxInWitness , TxOut , TxOutWitness , Txid ,
52+ } ;
5053use secp256k1_zkp:: rand:: { CryptoRng , RngCore } ;
5154use secp256k1_zkp:: { self , RangeProof , SecretKey , SurjectionProof } ;
5255
@@ -478,17 +481,17 @@ impl PartiallySignedTransaction {
478481 rng : & mut R ,
479482 secp : & secp256k1_zkp:: Secp256k1 < C > ,
480483 inp_txout_sec : & HashMap < usize , TxOutSecrets > ,
481- ) -> Result < Vec < ( AssetBlindingFactor , ValueBlindingFactor , SecretKey ) > , PsetBlindError > {
484+ ) -> Result < BTreeMap < TxInType , ( AssetBlindingFactor , ValueBlindingFactor , SecretKey ) > , PsetBlindError > {
482485 let ( inp_secrets, outs_to_blind) = self . blind_checks ( inp_txout_sec) ?;
483486
487+ let mut ret = BTreeMap :: new ( ) ; // return all the random values used
484488 if outs_to_blind. is_empty ( ) {
485489 // Return empty values if no outputs are marked for blinding
486- return Ok ( Vec :: new ( ) ) ;
490+ return Ok ( ret ) ;
487491 }
488492 // Blind each output as non-last and save the secrets
489493 let surject_inputs = self . surjection_inputs ( inp_txout_sec) ?;
490494 let mut out_secrets = vec ! [ ] ;
491- let mut ret = vec ! [ ] ; // return all the random values used
492495 for i in outs_to_blind {
493496 let txout = self . outputs [ i] . to_txout ( ) ;
494497 let ( txout, abf, vbf, ephemeral_sk) = txout
@@ -538,7 +541,7 @@ impl PartiallySignedTransaction {
538541 ) ) ;
539542 }
540543 // return blinding factors used
541- ret. push ( ( abf, vbf, ephemeral_sk) ) ;
544+ ret. insert ( TxInType :: Input ( i ) , ( abf, vbf, ephemeral_sk) ) ;
542545 }
543546
544547 // safe to unwrap because we have checked that there is atleast one output to blind
@@ -579,10 +582,10 @@ impl PartiallySignedTransaction {
579582 rng : & mut R ,
580583 secp : & secp256k1_zkp:: Secp256k1 < C > ,
581584 inp_txout_sec : & HashMap < usize , TxOutSecrets > ,
582- ) -> Result < Vec < ( AssetBlindingFactor , ValueBlindingFactor , SecretKey ) > , PsetBlindError > {
585+ ) -> Result < BTreeMap < TxInType , ( AssetBlindingFactor , ValueBlindingFactor , SecretKey ) > , PsetBlindError > {
583586 let ( mut inp_secrets, mut outs_to_blind) = self . blind_checks ( inp_txout_sec) ?;
584587
585- let mut ret = vec ! [ ] ;
588+ let mut ret = BTreeMap :: new ( ) ;
586589 if outs_to_blind. is_empty ( ) {
587590 // Atleast one output must be marked for blinding for pset blind_last
588591 return Err ( PsetBlindError :: AtleastOneOutputBlind ) ;
@@ -658,7 +661,7 @@ impl PartiallySignedTransaction {
658661 ) ;
659662 let ( value_commitment, nonce, rangeproof) =
660663 blind_res. map_err ( |e| PsetBlindError :: ConfidentialTxOutError ( last_out_index, e) ) ?;
661- ret. push ( ( out_abf, final_vbf, ephemeral_sk) ) ;
664+ ret. insert ( TxInType :: Input ( last_out_index ) , ( out_abf, final_vbf, ephemeral_sk) ) ;
662665
663666 // mutate the pset
664667 {
0 commit comments