txscript: export calcSignatureHash

This is a useful function for users of this library, and deserves to
be public.
This commit is contained in:
Marko Bencun 2017-11-18 22:43:45 +01:00 committed by Dave Collins
parent 78d12c33f0
commit 16dbb2602a
2 changed files with 13 additions and 3 deletions

View file

@ -582,6 +582,17 @@ func shallowCopyTx(tx *wire.MsgTx) wire.MsgTx {
return txCopy
}
// CalcSignatureHash will, given a script and hash type for the current script
// engine instance, calculate the signature hash to be used for signing and
// verification.
func CalcSignatureHash(script []byte, hashType SigHashType, tx *wire.MsgTx, idx int) ([]byte, error) {
parsedScript, err := parseScript(script)
if err != nil {
return nil, fmt.Errorf("cannot parse output script: %v", err)
}
return calcSignatureHash(parsedScript, hashType, tx, idx), nil
}
// calcSignatureHash will, given a script and hash type for the current script
// engine instance, calculate the signature hash to be used for signing and
// verification.

View file

@ -74,11 +74,10 @@ func WitnessSignature(tx *wire.MsgTx, sigHashes *TxSigHashes, idx int, amt int64
func RawTxInSignature(tx *wire.MsgTx, idx int, subScript []byte,
hashType SigHashType, key *btcec.PrivateKey) ([]byte, error) {
parsedScript, err := parseScript(subScript)
hash, err := CalcSignatureHash(subScript, hashType, tx, idx)
if err != nil {
return nil, fmt.Errorf("cannot parse output script: %v", err)
return nil, err
}
hash := calcSignatureHash(parsedScript, hashType, tx, idx)
signature, err := key.Sign(hash)
if err != nil {
return nil, fmt.Errorf("cannot sign tx input: %s", err)