txscript: Optimize ExtractPkScriptAddrs witness script hash

This continues the process of converting the ExtractPkScriptAddrs
function to use the optimized extraction functions recently introduced
as part of the typeOfScript conversion.

In particular, this converts the extract of witness-pay-to-script-hash
scripts.
This commit is contained in:
Conner Fromknecht 2019-04-19 19:22:52 -07:00 committed by Roy Lee
parent a3c39034b8
commit 37f9cdd115

View file

@ -899,35 +899,24 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
return WitnessV0PubKeyHashTy, addrs, 1, nil
}
if hash := extractWitnessScriptHash(pkScript); hash != nil {
var addrs []btcutil.Address
addr, err := btcutil.NewAddressWitnessScriptHash(hash, chainParams)
if err == nil {
addrs = append(addrs, addr)
}
return WitnessV0ScriptHashTy, addrs, 1, nil
}
// Fall back to slow path. Ultimately these are intended to be replaced by
// faster variants based on the unparsed raw scripts.
var addrs []btcutil.Address
var requiredSigs int
var err error
// No valid addresses or required signatures if the script doesn't
// parse.
pops, err := parseScript(pkScript)
if err != nil {
return NonStandardTy, nil, 0, err
}
scriptClass := typeOfScript(scriptVersion, pkScript)
switch scriptClass {
case WitnessV0ScriptHashTy:
// A pay-to-witness-script-hash script is of the form:
// OP_0 <32-byte hash>
// Therefore, the script hash is the second item on the stack.
// Skip the script hash if it's invalid for some reason.
requiredSigs = 1
addr, err := btcutil.NewAddressWitnessScriptHash(pops[1].data,
chainParams)
if err == nil {
addrs = append(addrs, addr)
}
case NonStandardTy:
// Don't attempt to extract addresses or required signatures for
// nonstandard transactions.