URI: 
       tMerge pull request #1280 from kyuupichan/BIP-LI01 - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit 00af3b394b10128ff1ca83102669cf28711ddf0f
   DIR parent ec9cdfaf48781356ad51db836c2336681d7c2caa
  HTML Author: ThomasV <electrumdev@gmail.com>
       Date:   Sun, 14 Jun 2015 08:08:55 +0200
       
       Merge pull request #1280 from kyuupichan/BIP-LI01
       
       Implement BIP-LI01.
       Diffstat:
         M lib/transaction.py                  |       4 ++++
         M lib/wallet.py                       |      15 +++++++--------
       
       2 files changed, 11 insertions(+), 8 deletions(-)
       ---
   DIR diff --git a/lib/transaction.py b/lib/transaction.py
       t@@ -627,6 +627,10 @@ class Transaction:
        
                return script
        
       +    def BIP_LI01_sort(self):
       +        # See https://github.com/kristovatlas/rfc/blob/master/bips/bip-li01.mediawiki
       +        self.inputs.sort(key = lambda i: (i['prevout_hash'], i['prevout_n']))
       +        self.outputs.sort(key = lambda o: (o[2], self.pay_script(o[0], o[1])))
        
            def serialize(self, for_sig=None):
                inputs = self.inputs
   DIR diff --git a/lib/wallet.py b/lib/wallet.py
       t@@ -898,27 +898,26 @@ class Abstract_Wallet(object):
                # if change is above dust threshold, add a change output.
                change_amount = total - ( amount + fee )
                if fixed_fee is not None and change_amount > 0:
       -            # Insert the change output at a random position in the outputs
       -            posn = random.randint(0, len(tx.outputs))
       -            tx.outputs[posn:posn] = [( 'address', change_addr,  change_amount)]
       +            tx.outputs.append(('address', change_addr, change_amount))
                elif change_amount > DUST_THRESHOLD:
       -            # Insert the change output at a random position in the outputs
       -            posn = random.randint(0, len(tx.outputs))
       -            tx.outputs[posn:posn] = [( 'address', change_addr,  change_amount)]
       +            tx.outputs.append(('address', change_addr, change_amount))
                    # recompute fee including change output
                    fee = self.estimated_fee(tx)
                    # remove change output
       -            tx.outputs.pop(posn)
       +            tx.outputs.pop()
                    # if change is still above dust threshold, re-add change output.
                    change_amount = total - ( amount + fee )
                    if change_amount > DUST_THRESHOLD:
       -                tx.outputs[posn:posn] = [( 'address', change_addr,  change_amount)]
       +                tx.outputs.append(('address', change_addr, change_amount))
                        print_error('change', change_amount)
                    else:
                        print_error('not keeping dust', change_amount)
                else:
                    print_error('not keeping dust', change_amount)
        
       +        # Sort the inputs and outputs deterministically
       +        tx.BIP_LI01_sort()
       +
                run_hook('make_unsigned_transaction', tx)
                return tx