From a83152214ccf3fd4972a011e065e37a982343778 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Fri, 19 Apr 2019 19:22:52 -0700 Subject: [PATCH] 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. --- txscript/standard.go | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/txscript/standard.go b/txscript/standard.go index c0178250..d9f8a9a3 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -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.