Truncation is always a good idea to start with. Applying the optional stopping theorem to the bounded stopping time $\tau \wedge n$, we have

$$ \Bbb{E}[W_{\tau \wedge n}] = 0 \qquad \text{and} \qquad \Bbb{E}[W_{\tau \wedge n}^2 - (\tau \wedge n)] = 0. \tag{*} $$

  • Even at this point we can infer that $\tau$ is integrable. Indeed, let $c = \max\{a,b\}$ and note that $|W_{\tau \wedge n}| \leq c$. Then applying the monotone convergence theorem to the inequality $\Bbb{E}[\tau\wedge n]\leq c^2$ shows that $\tau$ is integrable.

  • Taking $n\to\infty$ to $\text{(*)}$ and applying both the bounded convergence theorem and the monotone convergence theorem gives

    $$ \Bbb{E}[W_{\tau}] = 0, \qquad \Bbb{E}[W_{\tau}^2] = \Bbb{E}[\tau]. $$

    Now the remaining computation is straightforward: Solving the system of equations

    $$ \Bbb{P}(W_{\tau} = -a) + \Bbb{P}(W_{\tau} = b) = 1, \qquad (-a)\cdot\Bbb{P}(W_{\tau} = -a) + b\cdot\Bbb{P}(W_{\tau} = b) = 0 $$

    gives $\Bbb{P}(W_{\tau} = -a) = \frac{b}{a+b}$ and $\Bbb{P}(W_{\tau} = b) = \frac{a}{a+b}$, and plugging this to $\Bbb{E}[W_{\tau}^2] = \Bbb{E}[\tau]$ gives

    $$ \Bbb{E}[\tau] = a^2 \cdot \Bbb{P}(W_{\tau} = -a) + b^2 \cdot \Bbb{P}(W_{\tau} = b) = ab. $$