Skip to content

Commit

Permalink
txscript: Optimize ExtractPkScriptAddrs witness script hash
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cfromknecht committed Feb 5, 2021
1 parent 57bef04 commit d17add5
Showing 1 changed file with 9 additions and 20 deletions.
29 changes: 9 additions & 20 deletions txscript/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,35 +891,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.
Expand Down

0 comments on commit d17add5

Please sign in to comment.