code: def btc_bitcoind_tx_serialize( tx ):
       
       tx_ins = []
       tx_outs = []
  
       try:
           for inp in tx['vin']:
               next_inp = {
                  "outpoint": {
                     "index": int(inp['vout']),
                     "hash": str(inp['txid'])
                  }
               }
               if 'sequence' in inp:
                   next_inp['sequence'] = int(inp['sequence'])
               else:
                   next_inp['sequence'] = UINT_MAX
  
               if 'scriptSig' in inp:
                   next_inp['script'] = str(inp['scriptSig']['hex'])
               else:
                   next_inp['script'] = ""
  
               if 'txinwitness' in inp:
                   next_inp['witness_script'] = btc_witness_script_serialize(inp['txinwitness'])
  
               tx_ins.append(next_inp)
  
           for out in tx['vout']:
           
               assert out['value'] < 1000, "High transaction value\n%s" % simplejson.dumps(tx, indent=4, sort_keys=True)
               next_out = {
                  'value': int(Decimal(out['value'] * 10**8)),
                  'script': str(out['scriptPubKey']['hex'])
               }
               tx_outs.append(next_out)
  
           tx_fields = {
              "locktime": int(tx['locktime']),
              "version": int(tx['version']),
              "ins": tx_ins,
              "outs": tx_outs
           }
  
           tx_serialized = btc_tx_serialize( tx_fields )
           return str(tx_serialized)
  
       except KeyError, ke:
           if btc_bitcoind_tx_is_coinbase(tx) and 'hex' in tx.keys():
               tx_serialized = tx['hex']
               return str(tx_serialized)
  
           log.error("Key error in:\n%s" % simplejson.dumps(tx, indent=4, sort_keys=True))
           traceback.print_exc()
           raise ke
docs: Convert a *Bitcoind*-given transaction into its hex string.
  
       tx format is {'vin': [...], 'vout': [...], 'locktime': ..., 'version': ...},
       with the same formatting rules as getrawtransaction.
       
       (in particular, each value in vout is a Decimal, in BTC)