From 2fd1dcbbff5ac0c47fdaf7538e167dc3ab659fef Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Wed, 13 Mar 2019 01:12:40 -0500 Subject: [PATCH] txscript: Optimize ExtractPkScriptAltSigType. This converts the ExtractPkScriptAltSigType function to use the optimized extraction functions recently introduced as part of the typeOfScript conversion. It is important to note that this new implementation intentionally has the same semantic differences from the existing implementation as discussed in the relevant commits that introduced the extraction functions. The following is a before and after comparison of analyzing a typical script: benchmark old ns/op new ns/op delta --------------------------------------------------------------- BenchmarkExtractAltSigType 497 12.8 -97.42% benchmark old allocs new allocs delta --------------------------------------------------------------- BenchmarkExtractAltSigType 1 0 -100.00% benchmark old bytes new bytes delta --------------------------------------------------------------- BenchmarkExtractAltSigType 896 0 -100.00% --- txscript/standard.go | 37 ++++++++++--------------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/txscript/standard.go b/txscript/standard.go index cdad8931..d594cecf 100644 --- a/txscript/standard.go +++ b/txscript/standard.go @@ -1572,38 +1572,21 @@ func extractOneBytePush(po parsedOpcode) int { // ExtractPkScriptAltSigType returns the signature scheme to use for an // alternative check signature script. +// +// NOTE: This function only attempts to identify version 0 scripts. Since the +// function does not accept a script version, the results are undefined for +// other script versions. func ExtractPkScriptAltSigType(pkScript []byte) (dcrec.SignatureType, error) { - pops, err := parseScript(pkScript) - if err != nil { - return 0, err + if pk, sigType := extractPubKeyAltDetails(pkScript); pk != nil { + return sigType, nil } - isPKA := isPubkeyAlt(pops) - isPKHA := isPubkeyHashAlt(pops) - if !(isPKA || isPKHA) { - return -1, fmt.Errorf("wrong script type") + if pk, sigType := extractPubKeyHashAltDetails(pkScript); pk != nil { + return sigType, nil } - sigTypeLoc := 1 - if isPKHA { - sigTypeLoc = 4 - } - - valInt := extractOneBytePush(pops[sigTypeLoc]) - if valInt < 0 { - return 0, fmt.Errorf("bad type push") - } - val := dcrec.SignatureType(valInt) - switch val { - case dcrec.STEd25519: - return val, nil - case dcrec.STSchnorrSecp256k1: - return val, nil - default: - break - } - - return -1, fmt.Errorf("bad signature scheme type") + return -1, fmt.Errorf("not a standard pay-to-alt-pubkey or " + + "pay-to-alt-pubkey-hash script") } // AtomicSwapDataPushes houses the data pushes found in atomic swap contracts.