/// Extracts the hash from the output script.
/// Determines type by the length prefix and validates format.
///
/// # Arguments
///
/// * `spk` - The script pubkey extracted from the output
///
/// # Errors
///
/// * Errors if spk is not a validly formatted standard hash-commitment output type
/// (i.e. PKH, SH, WPKH, or WSH).
Extracts the hash from the output script.
Determines type by the length prefix and validates format.
Extracts the hash from the output script . Determines type by the length prefix and validates format .
pub fn extract_hash<'a>(spk: &'a ScriptPubkey<'a>) -> Result<PayloadType, SPVError> {
    let tag = &spk[..3];
    if (tag[0]) as usize + 1 != spk.len() {
        return Err(SPVError::OutputLengthMismatch);
    }
    /* Witness */
    if spk[1] == 0 {
        let script_len = spk[0];
        let payload_len = spk[2];
        if script_len < 2 {
            return Err(SPVError::MalformattedWitnessOutput);
        }
        if payload_len + 2 == script_len {
            let payload = &spk[3..3 + payload_len as usize];
            match payload_len {
                0x20 => return Ok(PayloadType::WSH(payload)),
                0x14 => return Ok(PayloadType::WPKH(payload)),
                _ => {} // fall through to error
            }
        }
        return Err(SPVError::MalformattedWitnessOutput);
    }
    /* P2PKH */
    if tag == [0x19, 0x76, 0xa9] {
        let last_two: &[u8] = &spk[spk.len() - 2..];
        if spk[3] != 0x14 || last_two != [0x88, 0xac] {
            return Err(SPVError::MalformattedP2PKHOutput);
        }
        return Ok(PayloadType::PKH(&spk[4..24]));
    }
    /* P2SH */
    if tag == [0x17, 0xa9, 0x14] {
        if spk.last().cloned() != Some(0x87) {
            return Err(SPVError::MalformattedP2SHOutput);
        }
        return Ok(PayloadType::SH(&spk[3..23]));
    }
    Err(SPVError::MalformattedOutput)
}
pub fn extract_hash < ' a > ( spk : & ' a ScriptPubkey < ' a > ) -> Result < PayloadType , SPVError > { let tag = & spk [ .. 3 ] ; if ( tag [ 0 ] ) as usize + 1 != spk . len ( ) { return Err ( SPVError :: OutputLengthMismatch ) ; } /* Witness */ if spk [ 1 ] == 0 { let script_len = spk [ 0 ] ; let payload_len = spk [ 2 ] ; if script_len < 2 { return Err ( SPVError :: MalformattedWitnessOutput ) ; } if payload_len + 2 == script_len { let payload = & spk [ 3 .. 3 + payload_len as usize ] ; match payload_len { 0x20 => return Ok ( PayloadType :: WSH ( payload ) ) , 0x14 => return Ok ( PayloadType :: WPKH ( payload ) ) , _ => { } // fall through to error } } return Err ( SPVError :: MalformattedWitnessOutput ) ; } /* P2PKH */ if tag == [ 0x19 , 0x76 , 0xa9 ] { let last_two : & [ u8 ] = & spk [ spk . len ( ) - 2 .. ] ; if spk [ 3 ] != 0x14 || last_two != [ 0x88 , 0xac ] { return Err ( SPVError :: MalformattedP2PKHOutput ) ; } return Ok ( PayloadType :: PKH ( & spk [ 4 .. 24 ] ) ) ; } /* P2SH */ if tag == [ 0x17 , 0xa9 , 0x14 ] { if spk . last ( ) . cloned ( ) != Some ( 0x87 ) { return Err ( SPVError :: MalformattedP2SHOutput ) ; } return Ok ( PayloadType :: SH ( & spk [ 3 .. 23 ] ) ) ; } Err ( SPVError :: MalformattedOutput ) }
Extracts the hash from the output script.
Extracts the hash from the output script .
spk
&'a ScriptPubkey<'a>
errors
Errors if spk is not a validly formatted standard hash-commitment output type
.
Errors if spk is not a validly formatted standard hash - commitment output type .