From 8a4da7690d5cbe9a35918cc685ec747b86a9cfb8 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:12:33 -0500 Subject: [PATCH] txscript: Optimize ExtractPkScriptAddrs multisig. 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 detection for multisig scripts. Also, since the remaining slow path cases are all recursive calls, the parsed opcodes are no longer used, so parsing is removed. --- txscript/standard.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/txscript/standard.go b/txscript/standard.go index 6377598c..b187ac01 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -868,11 +868,27 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script return PubKeyTy, addrs, 1, nil } + // Check for multi-signature script. + const scriptVersion = 0 + details := extractMultisigScriptDetails(scriptVersion, pkScript, true) + if details.valid { + // Convert the public keys while skipping any that are invalid. + addrs := make([]btcutil.Address, 0, len(details.pubKeys)) + for _, pubkey := range details.pubKeys { + addr, err := btcutil.NewAddressPubKey(pubkey, chainParams) + if err == nil { + addrs = append(addrs, addr) + } + } + return MultiSigTy, addrs, details.requiredSigs, 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. @@ -881,7 +897,6 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script return NonStandardTy, nil, 0, err } - const scriptVersion = 0 scriptClass := typeOfScript(scriptVersion, pkScript) switch scriptClass { @@ -909,25 +924,6 @@ func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (Script addrs = append(addrs, addr) } - case MultiSigTy: - // A multi-signature script is of the form: - // ... OP_CHECKMULTISIG - // Therefore the number of required signatures is the 1st item - // on the stack and the number of public keys is the 2nd to last - // item on the stack. - requiredSigs = asSmallInt(pops[0].opcode.value) - numPubKeys := asSmallInt(pops[len(pops)-2].opcode.value) - - // Extract the public keys while skipping any that are invalid. - addrs = make([]btcutil.Address, 0, numPubKeys) - for i := 0; i < numPubKeys; i++ { - addr, err := btcutil.NewAddressPubKey(pops[i+1].data, - chainParams) - if err == nil { - addrs = append(addrs, addr) - } - } - case NullDataTy: // Null data transactions have no addresses or required // signatures.