txscript: Optimize ExtractPkScriptAddrs witness pubkey 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 extraction for witness-pubkey-hash
scripts.
This commit is contained in:
Conner Fromknecht 2019-04-19 19:18:51 -07:00 committed by Roy Lee
parent 45bdd26ac3
commit a3c39034b8

View file

@ -890,6 +890,15 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
return NullDataTy, nil, 0, nil return NullDataTy, nil, 0, nil
} }
if hash := extractWitnessPubKeyHash(pkScript); hash != nil {
var addrs []btcutil.Address
addr, err := btcutil.NewAddressWitnessPubKeyHash(hash, chainParams)
if err == nil {
addrs = append(addrs, addr)
}
return WitnessV0PubKeyHashTy, addrs, 1, nil
}
// Fall back to slow path. Ultimately these are intended to be replaced by // Fall back to slow path. Ultimately these are intended to be replaced by
// faster variants based on the unparsed raw scripts. // faster variants based on the unparsed raw scripts.
@ -907,18 +916,6 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
scriptClass := typeOfScript(scriptVersion, pkScript) scriptClass := typeOfScript(scriptVersion, pkScript)
switch scriptClass { switch scriptClass {
case WitnessV0PubKeyHashTy:
// A pay-to-witness-pubkey-hash script is of thw form:
// OP_0 <20-byte hash>
// Therefore, the pubkey hash is the second item on the stack.
// Skip the pubkey hash if it's invalid for some reason.
requiredSigs = 1
addr, err := btcutil.NewAddressWitnessPubKeyHash(pops[1].data,
chainParams)
if err == nil {
addrs = append(addrs, addr)
}
case WitnessV0ScriptHashTy: case WitnessV0ScriptHashTy:
// A pay-to-witness-script-hash script is of the form: // A pay-to-witness-script-hash script is of the form:
// OP_0 <32-byte hash> // OP_0 <32-byte hash>