Can someone explain how an HTLC is redeemed? I'm looking at Bolt 3 in the Lightning rfc. My question is about how a HTLC is redeemed. Example HTLC script for reference:

# To remote node with revocation key
OP_DUP OP_HASH160 &lt;RIPEMD160(SHA256(revocationpubkey))&gt; OP_EQUAL
OP_IF
    OP_CHECKSIG
OP_ELSE
    &lt;remote_htlcpubkey&gt; OP_SWAP OP_SIZE 32 OP_EQUAL
    OP_NOTIF
        # To local node via HTLC-timeout transaction (timelocked).
        OP_DROP 2 OP_SWAP &lt;local_htlcpubkey&gt; 2 OP_CHECKMULTISIG
    OP_ELSE
        # To remote node with preimage.
        OP_HASH160 &lt;RIPEMD160(payment_hash)&gt; OP_EQUALVERIFY
        OP_CHECKSIG
    OP_ENDIF
OP_ENDIF


My understanding is that it can be redeemed in two ways. The first way can be done if the revocation secret is known.

&lt;revocation_sig&gt; &lt;revocationpubkey&gt;


revocationpubkey will produce the correct hash, OP_EQUAL will return "1", the script goes into OP_IF, and the revocation_sig will be valid for OP_CHECKSIG.

The second way involves 

&lt;remotehtlcsig&gt; &lt;payment_preimage&gt;


Can someone explain how this satisfies the script? For example, what OP_SWAP does or when it would go through OP_NOTIF/OP_ELSE?
There are three ways in which that offered HTLC output can be redeemed: Revocation using the revocation public key (the example you mentioned), redeemed using a valid payment preimage or back to the local node after the timeout.

Redeemed using a valid pre-image by remote node:  


The stack starts at &lt;remotehtlcsig&gt; &lt;payment_preimage&gt;
Since the HASH160 of &lt;payment_preimage&gt; is not equal to the HASH160 of revocationpubkey, the outer ELSE statement is triggered
&lt;remote_htlcpubkey&gt; is pushed to the stack. Stack is now &lt;remotehtlcsig&gt; &lt;payment_preimage&gt; &lt;remote_htlcpubkey&gt;.
OP_SWAP is pushed to the stack, which swaps the top two items on the stack.
Stack is now &lt;remotehtlcsig&gt; &lt;remote_htlcpubkey&gt; &lt;payment_preimage&gt;
OP_SIZE 32 OP_EQUAL verifies that the payment_preimage is 32 bytes.
Since the payment_preimage is 32 bytes it will output 1. As a result OP_NOTIF will not be triggered, and hence OP_ELSE statement will be triggered.


OP_NOTIF is triggered is the value on the stack is 0
OP_ELSE is triggered if the preceding OP_IF, OP_NOTIF and OP_ELSE are not executed.

The stack is still &lt;remotehtlcsig&gt; &lt;remote_htlcpubkey&gt; &lt;payment_preimage&gt;
The OP_HASH160 will hash the paymentpreimage. Since payment hash is SHA256 of the payment preimage, we need to hash it with RIPEMD160 so that it is equal to HASH160 of the paymentpreimage.
If the hash matches, then the stack will be &lt;remotehtlcsig&gt; &lt;remote_htlcpubkey&gt; OP_CHECKSIG
If the signature matches the remote_htlcpubkey then 1 will be returned to the stack and this offered HTLC output is successfully spent by the remote node.


Redeemed after time-out by the local node:  


To trigger the HTLC timeout condition, the local node will use the witness: 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; 0
The stack starts at 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; 0
Since the HASH160 of 0 is not equal to the HASH160 of revocationpubkey, the outer ELSE statement is triggered
&lt;remote_htlcpubkey&gt; is pushed to the stack. Stack is now: 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; 0 &lt;remote_htlcpubkey&gt;
OP_SWAP swaps the top two items on the stack. Stack is now: 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; &lt;remote_htlcpubkey&gt; 0
Since size of 0 is not 32 bytes it will return 0 on top of stack. This triggers the OP_NOTIF statement. Stack is still: 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; &lt;remote_htlcpubkey&gt; 0
OP_DROP drops the top item on the stack which is 0. Stack is now : 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; &lt;remote_htlcpubkey&gt;
2 is pushed to the stack. Stack is 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; &lt;remote_htlcpubkey&gt; 2
OP_SWAP swaps the top two items on the stac. Stack is: 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; 2 &lt;remote_htlcpubkey&gt;
&lt;local_htlcpubkey&gt; 2 OP_CHECKMULTISIG is pushed to the stack. The stack is thus 0 &lt;remotehtlcsig&gt; &lt;localhtlcsig&gt; 2 &lt;remote_htlcpubkey&gt; &lt;local_htlcpubkey&gt; 2 OP_CHECKMULTISIG.
The above is a multisig output which can be spent only with valid signatures from the local and remote keys. These keys are exchanged when adding the HTLC. The output of the above transaction is sent to the local node using a to_self_delay using the time-out transactions.