MCPcopy Create free account
hub / github.com/btcsuite/btcd / computeWitnessPkScript

Function computeWitnessPkScript

txscript/pkscript.go:254–286  ·  view source on GitHub ↗

computeWitnessPkScript computes the script of an output by looking at the spending input's witness.

(witness wire.TxWitness)

Source from the content-addressed store, hash-verified

252// computeWitnessPkScript computes the script of an output by looking at the
253// spending input's witness.
254func computeWitnessPkScript(witness wire.TxWitness) (PkScript, error) {
255 // We'll use the last item of the witness stack to determine the proper
256 // witness type.
257 lastWitnessItem := witness[len(witness)-1]
258
259 var pkScript PkScript
260 switch {
261 // If the witness stack has a size of 2 and its last item is a
262 // compressed public key, then this is a P2WPKH witness.
263 case len(witness) == 2 && len(lastWitnessItem) == compressedPubKeyLen:
264 pubKeyHash := hash160(lastWitnessItem)
265 script, err := payToWitnessPubKeyHashScript(pubKeyHash)
266 if err != nil {
267 return pkScript, err
268 }
269
270 pkScript.class = WitnessV0PubKeyHashTy
271 copy(pkScript.script[:], script)
272
273 // For any other witnesses, we'll assume it's a P2WSH witness.
274 default:
275 scriptHash := sha256.Sum256(lastWitnessItem)
276 script, err := payToWitnessScriptHashScript(scriptHash[:])
277 if err != nil {
278 return pkScript, err
279 }
280
281 pkScript.class = WitnessV0ScriptHashTy
282 copy(pkScript.script[:], script)
283 }
284
285 return pkScript, nil
286}
287
288// hash160 returns the RIPEMD160 hash of the SHA-256 HASH of the given data.
289func hash160(data []byte) []byte {

Callers 1

ComputePkScriptFunction · 0.85

Calls 3

hash160Function · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…