From 16dbb2602a6097c370fac43761dcff9963f3c70a Mon Sep 17 00:00:00 2001 From: Marko Bencun Date: Sat, 18 Nov 2017 22:43:45 +0100 Subject: [PATCH] txscript: export calcSignatureHash This is a useful function for users of this library, and deserves to be public. --- txscript/script.go | 11 +++++++++++ txscript/sign.go | 5 ++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/txscript/script.go b/txscript/script.go index 1c3939b0..4977e961 100644 --- a/txscript/script.go +++ b/txscript/script.go @@ -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. diff --git a/txscript/sign.go b/txscript/sign.go index 09ba8d1f..42af9686 100644 --- a/txscript/sign.go +++ b/txscript/sign.go @@ -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)