code: func SignTxOutput(chainParams *chaincfg.Params, tx *wire.MsgTx, idx int,
  	pkScript []byte, hashType SigHashType, kdb KeyDB, sdb ScriptDB,
  	previousScript []byte) ([]byte, error) {
  
  	sigScript, class, addresses, nrequired, err := sign(chainParams, tx,
  		idx, pkScript, hashType, kdb, sdb)
  	if err != nil {
  		return nil, err
  	}
  
  	if class == ScriptHashTy {
  		// TODO keep the sub addressed and pass down to merge.
  		realSigScript, _, _, _, err := sign(chainParams, tx, idx,
  			sigScript, hashType, kdb, sdb)
  		if err != nil {
  			return nil, err
  		}
  
  		// Append the p2sh script as the last push in the script.
  		builder := NewScriptBuilder()
  		builder.AddOps(realSigScript)
  		builder.AddData(sigScript)
  
  		sigScript, _ = builder.Script()
  		// TODO keep a copy of the script for merging.
  	}
  
  	// Merge scripts. with any previous data, if any.
  	mergedScript := mergeScripts(chainParams, tx, idx, pkScript, class,
  		addresses, nrequired, sigScript, previousScript)
  	return mergedScript, nil
  }
docs: // SignTxOutput signs output idx of the given tx to resolve the script given in
  // pkScript with a signature type of hashType. Any keys required will be
  // looked up by calling getKey() with the string of the given address.
  // Any pay-to-script-hash signatures will be similarly looked up by calling
  // getScript. If previousScript is provided then the results in previousScript
  // will be merged in a type-dependent manner with the newly generated.
  // signature script.