computeWitnessPkScript computes the script of an output by looking at the spending input's witness.
(witness wire.TxWitness)
| 252 | // computeWitnessPkScript computes the script of an output by looking at the |
| 253 | // spending input's witness. |
| 254 | func 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. |
| 289 | func hash160(data []byte) []byte { |
no test coverage detected
searching dependent graphs…