Skip to content

Commit

Permalink
txscript: Optimize ExtractPkScriptAddrs scripthash.
Browse files Browse the repository at this point in the history
This begins the process of converting the ExtractPkScriptAddrs function
to use the optimized extraction functions recently introduced as part of
the typeOfScript conversion.

In order to ease the review process, the detection of each script type
will be converted in a separate commit such that the script is only
parsed as a fallback for the cases that are not already converted to
more efficient variants.

In particular, this converts the detection for pay-to-script-hash
scripts.
  • Loading branch information
davecgh authored and cfromknecht committed Feb 5, 2021
1 parent 3b8712d commit 1adafa3
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions txscript/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -805,11 +805,36 @@ func PushedData(script []byte) ([][]byte, error) {
return data, nil
}

// scriptHashToAddrs is a convenience function to attempt to convert the passed
// hash to a pay-to-script-hash address housed within an address slice. It is
// used to consolidate common code.
func scriptHashToAddrs(hash []byte, params *chaincfg.Params) []btcutil.Address {
// Skip the hash if it's invalid for some reason.
var addrs []btcutil.Address
addr, err := btcutil.NewAddressScriptHashFromHash(hash, params)
if err == nil {
addrs = append(addrs, addr)
}
return addrs
}

// ExtractPkScriptAddrs returns the type of script, addresses and required
// signatures associated with the passed PkScript. Note that it only works for
// 'standard' transaction script types. Any data such as public keys which are
// invalid are omitted from the results.
func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (ScriptClass, []btcutil.Address, int, error) {

// Avoid parsing the script for the cases that already have the able to
// work with raw scripts.

// Check for pay-to-script-hash.
if hash := extractScriptHash(pkScript); hash != nil {
return ScriptHashTy, scriptHashToAddrs(hash, chainParams), 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

Expand Down Expand Up @@ -859,18 +884,6 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script
addrs = append(addrs, addr)
}

case ScriptHashTy:
// A pay-to-script-hash script is of the form:
// OP_HASH160 <scripthash> OP_EQUAL
// Therefore the script hash is the 2nd item on the stack.
// Skip the script hash if it's invalid for some reason.
requiredSigs = 1
addr, err := btcutil.NewAddressScriptHashFromHash(pops[1].data,
chainParams)
if err == nil {
addrs = append(addrs, addr)
}

case WitnessV0ScriptHashTy:
// A pay-to-witness-script-hash script is of the form:
// OP_0 <32-byte hash>
Expand Down

0 comments on commit 1adafa3

Please sign in to comment.